diff --git a/commerce_flat_rate.install b/commerce_flat_rate.install index b084772..b0ffc68 100644 --- a/commerce_flat_rate.install +++ b/commerce_flat_rate.install @@ -5,6 +5,19 @@ * Defines the database schema for flat rate shipping services. */ +/** + * Implements hook_install(). + */ +function commerce_flat_rate_install() { + $rules_config = rules_config_load('commerce_shipping_flat_rate'); + + if (!empty($rules_config) && $rules_config->active) { + $rules_config->active = 0; + $rules_config->save(); + + drupal_set_message(t('The rule created by Commerce Flat Rate Shipping for Commerce Shipping 1.0 has been disabled. You can migrate this rule to this module.', array('!migrate_url' => url('admin/commerce/config/shipping/methods/flat-rate/migrate')))); + } +} /** * Implements hook_schema(). diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 1dd8f6a..4ee5f14 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -37,6 +37,21 @@ function commerce_flat_rate_menu() { 'file' => 'includes/commerce_flat_rate.admin.inc', ); + if (module_exists('commerce_shipping_flat_rate')) { + $items['admin/commerce/config/shipping/methods/flat-rate/migrate'] = array( + 'title' => 'Migrate flat rate services', + 'description' => 'Migrate flat rate shipping services from Commerce Shipping 1.0.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('commerce_flat_rate_migrate_form'), + 'access callback' => 'commerce_flat_rate_service_access', + 'access arguments' => array('create'), + 'type' => MENU_LOCAL_TASK, + 'context' => MENU_CONTEXT_INLINE, + 'weight' => 100, + 'file' => 'includes/commerce_flat_rate.migrate.inc', + ); + } + foreach (commerce_shipping_services('flat_rate') as $name => $shipping_service) { // Convert underscores to hyphens for the menu item argument. $service_name_arg = 'flat-rate-' . strtr($name, '_', '-'); diff --git a/includes/commerce_flat_rate.migrate.inc b/includes/commerce_flat_rate.migrate.inc new file mode 100644 index 0000000..3196da2 --- /dev/null +++ b/includes/commerce_flat_rate.migrate.inc @@ -0,0 +1,223 @@ +actions() as $action) { + if (!empty($action->settings['shipping_method']) && ($action->settings['shipping_method']['method_id'] == 'flat_rate')) { + return $action->settings; + } + } + + return FALSE; +} + +function commerce_flat_rate_migrate_form($form, &$form_state) { + module_load_include('inc', 'commerce_flat_rate', 'commerce_flat_rate.admin'); + + $form_state['rules_config'] = rules_config_load('commerce_shipping_flat_rate'); + $rules_config_settings = commerce_flat_rate_migrate($form_state['rules_config']); + + if (empty($rules_config_settings['shipping_method']['settings']['shipping_rates'])) { + $options = commerce_currency_get_code(TRUE); + $currency_code = key($options); + + $shipping_rates = array($currency_code => array( + $currency_code => $rules_config_settings['shipping_method']['settings']['shipping_price'], + 'include_tax' => empty($rules_config_settings['shipping_method']['settings']['include_tax']) ? '_none' : $rules_config_settings['shipping_method']['settings']['include_tax'], + )); + } + else { + $shipping_rates = $rules_config_settings['shipping_method']['settings']['shipping_rates']; + } + + $form['help'] = array( + '#markup' => t('Below you can find the existing shipping service from Commerce Shipping Flat Rate and for each configured currency the option to convert the service to this module.', array('@commerce_shipping_flat_rate' => 'http://www.drupal.org/project/commerce_shipping_flat_rate')), + ); + + $form['flat_rate'] = array( + '#tree' => TRUE, + ); + + foreach ($shipping_rates as $currency_code => $shipping_rate) { + $form['flat_rate']['migrate_' . $currency_code] = array( + '#type' => 'checkbox', + '#title' => t('Migrate the shipping service for %currency_code', array('%currency_code' => $currency_code)), + '#default_value' => TRUE, + ); + + $form['flat_rate'][$currency_code] = array( + '#type' => 'fieldset', + '#title' => t('Shipping service (@currency_code)', array('@currency_code' => $currency_code)), + '#tree' => TRUE, + '#states' => array( + 'invisible' => array( + ':input[name="flat_rate[' . 'migrate_' . $currency_code . ']"]' => array('checked' => FALSE), + ), + ), + ); + + $form['flat_rate'][$currency_code]['title'] = array( + '#type' => 'value', + '#value' => $rules_config_settings['shipping_method']['settings']['label'] . ' (' . $currency_code . ')', + ); + + $form['flat_rate'][$currency_code]['display_title'] = array( + '#type' => 'value', + '#value' => $rules_config_settings['shipping_method']['shipping_label'], + ); + + $form['flat_rate'][$currency_code]['amount'] = array( + '#type' => 'value', + '#value' => $shipping_rate[$currency_code] * 100, + ); + + $form['flat_rate'][$currency_code]['currency_code'] = array( + '#type' => 'value', + '#default_value' => $currency_code, + ); + + if ($shipping_rate['include_tax'] != '_none') { + $form['flat_rate'][$currency_code]['include_tax'] = array( + '#type' => 'value', + '#value' => $shipping_rate['include_tax'], + ); + } + + $form['flat_rate'][$currency_code]['name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#default_value' => 'commerce_flat_rate_migrate_' . strtolower($currency_code), + '#maxlength' => 32, + '#required' => TRUE, + '#machine_name' => array( + 'exists' => 'commerce_shipping_service_load', + 'source' => array('flat_rate', 'title'), + ), + '#description' => t('The machine-name of this flat rate. This name must contain only lowercase letters, numbers, and underscores. It must be unique.'), + ); + + $form['flat_rate'][$currency_code]['description'] = array( + '#type' => 'textarea', + '#title' => t('Description'), + '#description' => t('Describe this flat rate if necessary. The text will be displayed in the flat rate services overview table.'), + '#default_value' => '', + '#rows' => 3, + ); + } + + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Migrate'), + ); + + return $form; +} + +function commerce_flat_rate_migrate_form_validate($form, &$form_state) { + foreach ($form_state['values']['flat_rate'] as $currency_code => $shipping_service) { + if (is_array($shipping_service) && $form_state['values']['flat_rate']['migrate_' . $currency_code]) { + $rules_config = rules_config_load('commerce_shipping_service_' . $shipping_service['name']); + + if (!empty($rules_config)) { + form_set_error('flat_rate][' . $currency_code . '][name', t('A rule with the specified name already exists.')); + } + } + } +} + +function commerce_flat_rate_migrate_form_submit($form, &$form_state) { + $shipping_services = array(); + $success = TRUE; + + foreach ($form_state['values']['flat_rate'] as $currency_code => $shipping_service) { + if (is_array($shipping_service) && $form_state['values']['flat_rate']['migrate_' . $currency_code]) { + $shipping_service += commerce_flat_rate_service_new(); + + // Save the shipping service. + unset($shipping_service['base_rate']); + if (!commerce_flat_rate_service_save($shipping_service)) { + $success = FALSE; + } else { + $shipping_services[$shipping_service['currency_code']] = $shipping_service; + } + } + } + + if (!$success) { + drupal_set_message(t('The flat rate service failed to save properly. Please review the form and try again.'), 'error'); + $form_state['rebuild'] = TRUE; + } + else { + drupal_set_message(t('Flat rate service saved.')); + + $batch = array( + 'operations' => array( + array('commerce_flat_rate_migrate_batch', array($shipping_services)), + ), + 'title' => t('Flat Rate Shipping Migrate'), + 'init_message' => t('Flat rate shipping service migration is starting.'), + 'progress_message' => t('Processed @current out of @total.'), + 'error_message' => t('Flat rate shipping service migration has encountered an error.'), + 'file' => drupal_get_path('module', 'commerce_flat_rate') . '/includes/commerce_flat_rate.migrate.inc', + ); + + batch_set($batch); + + $form_state['redirect'] = 'admin/commerce/config/shipping/services/flat-rate'; + } +} + +function commerce_flat_rate_migrate_batch($shipping_services, &$context) { + if (empty($context['sandbox'])) { + $context['sandbox']['progress'] = 0; + $context['sandbox']['max'] = db_query("SELECT COUNT(line_item_id) FROM {commerce_line_item} WHERE type = 'shipping'")->fetchColumn(); + $context['sandbox']['current'] = 0; + } + + $line_item_ids = array_keys(db_query("SELECT line_item_id FROM {commerce_line_item} WHERE type = 'shipping' AND line_item_id > :current ORDER BY line_item_id ASC LIMIT 10", array(':current' => $context['sandbox']['current']))->fetchAllAssoc('line_item_id', PDO::FETCH_ASSOC)); + + foreach (commerce_line_item_load_multiple($line_item_ids, array('type' => 'shipping')) as $line_item) { + $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item); + + $commerce_unit_price = $line_item_wrapper->commerce_unit_price->value(); + $currency_code = $commerce_unit_price['currency_code']; + + foreach ($commerce_unit_price['data']['components'] as &$component) { + if (($component['name'] == 'shipping') && !empty($component['data']['original'])) { + $original = $component['data']['original']; + + if (!empty($shipping_services[$currency_code])) { + $component['name'] = 'flat_rate_' . $shipping_services[$currency_code]['name']; + } + + break; + } + } + + if (($line_item_wrapper->commerce_shipping_service->value() == 'flat_rate') && isset($original)) { + if (!empty($shipping_services[$currency_code])) { + $line_item_wrapper->commerce_shipping_service = $shipping_services[$currency_code]['name']; + $line_item_wrapper->commerce_unit_price = $commerce_unit_price; + + $line_item_wrapper->save(); + + $order = commerce_order_load($line_item_wrapper->order_id->value()); + commerce_order_save($order); + } + } + + $context['sandbox']['progress'] ++; + $context['sandbox']['current'] = $line_item->line_item_id; + } + + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; +} +