commit 6da3702b613a58c3811f5f09bed334b92005305f Author: Diego Narciso Hernandes Date: Sat Nov 22 01:19:45 2014 -0500 Added validation to the return form. Fix form submit avoid loosing information, allowing using anonymos as owner. Avoiding database garbage saving the commerce_return object in the database when creating the objecti and also avoid duplicate entries for commerce_rma. Allowing only one commerce_return refund per order. diff --git a/www/sites/all/modules/commerce_rma/modules/return/commerce_return.module b/www/sites/all/modules/commerce_rma/modules/return/commerce_return.module index 60e152b..6bd203f 100644 --- a/www/sites/all/modules/commerce_rma/modules/return/commerce_return.module +++ b/www/sites/all/modules/commerce_rma/modules/return/commerce_return.module @@ -776,7 +776,7 @@ function commerce_return_new(stdClass $order, $uid = 0, $status = NULL, $type = $status = $return_state['default_status']; } - return entity_get_controller('commerce_return')->create( + $return = entity_get_controller('commerce_return')->create( array( 'order' => $order, 'uid' => $uid, @@ -784,6 +784,10 @@ function commerce_return_new(stdClass $order, $uid = 0, $status = NULL, $type = 'type' => $type, ) ); + + commerce_return_save($return); + + return $return; } /** diff --git a/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.admin.inc b/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.admin.inc index a3c35c3..1664b16 100644 --- a/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.admin.inc +++ b/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.admin.inc @@ -84,7 +84,19 @@ class CommerceReturnUIController extends EntityDefaultUIController { * The new return form array. */ function commerce_return_new_order_wrapper(stdClass $order) { - return entity_ui_get_form('commerce_return', commerce_return_new($order), 'add'); + $query = new EntityFieldQuery(); + $query->entityCondition('entity_type', 'commerce_return') + ->entityCondition('bundle', 'commerce_return') + ->fieldCondition('commerce_order_reference', 'target_id', $order->order_id,'=' ); + $result = $query->execute(); + + if ( isset($result['commerce_return']) ) { + $return = entity_load_single('commerce_return', key($result['commerce_return'])); + } else { + $return = commerce_return_new($order); + } + + return entity_ui_get_form('commerce_return', $return, 'add'); } /** @@ -140,7 +152,8 @@ function commerce_return_overview_form() { * The updated form array. */ function commerce_return_form($form, &$form_state, CommerceReturn $return, $op = 'edit') { - global $user; + global $user; + // Ensure the owner name is accessible if the uid is set. if (!empty($return->uid) && $owner = user_load($return->uid)) { $return->name = $owner->name; @@ -148,15 +161,15 @@ function commerce_return_form($form, &$form_state, CommerceReturn $return, $op = if (empty($return->mail)) { $return->mail = $owner->mail; } - } else { - $return->name = $user->name; - $return->mail = $user->mail; + } - + + if (empty($return->created)) { $return->date = format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O'); } + // Add the field related form elements. $form_state['commerce_return'] = $return; field_attach_form('commerce_return', $return, $form, $form_state); @@ -297,15 +310,115 @@ function commerce_return_form($form, &$form_state, CommerceReturn $return, $op = * Form API validation callback for the type form. */ function commerce_return_form_validate($form, &$form_state) { - field_attach_form_validate('commerce_return', $form_state['commerce_return'], $form, $form_state); + $return = $form_state['commerce_return']; + + // Validate the "owned by" field. + if (!empty($form_state['values']['name']) && !($account = user_load_by_name($form_state['values']['name']))) { + // The use of empty() is mandatory in the context of usernames as the empty + // string denotes an anonymous user. + form_set_error('name', t('The username %name does not exist.', array('%name' => $form_state['values']['name']))); + } + + // Validate the "created on" field. + if (!empty($form_state['values']['date']) && strtotime($form_state['values']['date']) === FALSE) { + form_set_error('date', t('You have to specify a valid date.')); + } + + // Validate the e-mail address entered. + if (!empty($form_state['values']['mail']) && !valid_email_address($form_state['values']['mail'])) { + form_set_error('mail', t('You have specified an invalid e-mail address.')); + } } /** * Form API submit callback for the type form. */ function commerce_return_form_submit(&$form, &$form_state) { - $commerce_return = entity_ui_form_submit_build_entity($form, $form_state); - $commerce_return->save(); + global $user; + + // If the user is editing an return, load a fresh copy to merge changes to. + if ($form_state['commerce_return']->return_id) { + $form_state['commerce_return'] = commerce_return_load($form_state['commerce_return']->return_id); + } + + // Merge changes into the return object in the form state so it is accessible + // by field handlers. + $return = $form_state['commerce_return']; + + + if ($form_state['values']['revision'] || !empty($form_state['values']['log'])) { + $return->revision = TRUE; + $return->log = $form_state['values']['log']; + } + + // Set the return's owner uid based on the supplied name. + $converted = FALSE; + + if (!empty($form_state['values']['name']) && $account = user_load_by_name($form_state['values']['name'])) { + // If the return is being converted to an authenticated return from an + // anonymous return... + if ($return->uid == 0) { + // Set the converted boolean for later processing. + $converted = TRUE; + } + + $return->uid = $account->uid; + + if (empty($form_state['values']['mail'])) { + $return->mail = $account->mail; + } + } + else { + $return->uid = 0; + } + + if (!empty($form_state['values']['mail'])) { + $return->mail = $form_state['values']['mail']; + } + + $return->created = !empty($form_state['values']['date']) ? strtotime($form_state['values']['date']) : REQUEST_TIME; + + // Notify field widgets. + field_attach_submit('commerce_return', $return, $form, $form_state); + + // Ensure the attached customer profiles are associated with the return owner + // if they do not have a uid yet and the return does. + if ($converted) { + $wrapper = entity_metadata_wrapper('commerce_return', $return); + + foreach (field_info_instances('commerce_return', $return->type) as $field_name => $instance) { + $field_info = field_info_field($field_name); + + if ($field_info['type'] == 'commerce_customer_profile_reference') { + if (!is_null($wrapper->{$field_name}->value()) && $wrapper->{$field_name}->uid->value() == 0) { + $wrapper->{$field_name}->uid = $return->uid; + $wrapper->{$field_name}->save(); + break; + } + } + } + } + + // Update the return status if specified. + if ($form_state['values']['status'] != $form_state['values']['status_original']) { + // We skip commece_return saving in the update since we do it below once for the + // entire form submission. + commerce_return_status_update($return, $form_state['values']['status'], TRUE); + } + + // Save the return + commerce_return_save($return); + + + // Ensure the attached line items are associated with the return if they do not + // have an return_id set yet. + foreach (entity_metadata_wrapper('commerce_return', $return)->commerce_return_line_items as $delta => $line_item_wrapper) { + if ($line_item_wrapper->order_id->value() == 0) { + $line_item_wrapper->order_id = $return->return_id; + $line_item_wrapper->save(); + } + } + $form_state['redirect'] = 'admin/commerce/store/returns'; } diff --git a/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.controller.inc b/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.controller.inc index b0ea367..4193bbc 100644 --- a/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.controller.inc +++ b/www/sites/all/modules/commerce_rma/modules/return/includes/commerce_return.controller.inc @@ -112,7 +112,8 @@ class CommerceReturnEntityController extends EntityAPIController { * @return int * SAVED_NEW or SAVED_UPDATED depending on the operation performed. */ - public function save($return, DatabaseTransaction $transaction = NULL) { + public function save($return, DatabaseTransaction $transaction = NULL) { + if (!isset($transaction)) { $transaction = db_transaction(); $started_transaction = TRUE; @@ -125,9 +126,14 @@ class CommerceReturnEntityController extends EntityAPIController { $return->is_new = empty($return->return_id); // Set the timestamp fields. - if ($return->is_new && empty($return->created)) { - $return->created = REQUEST_TIME; - $return->hostname = ip_address(); + + if ($return->is_new) { + if (empty($return->created)) { + $return->created = REQUEST_TIME; + } + if (empty($return->hostname)) { + $return->hostname = ip_address(); + } } else { // Otherwise if the return is not new but comes from an entity_create() @@ -140,7 +146,7 @@ class CommerceReturnEntityController extends EntityAPIController { if ($return->uid === '') { unset($return->uid); } - if (property_exists($return, 'hostname') && $return->hostname === '') { + if ($return->hostname === '') { unset($return->hostname); } }