You are here

function commerce_avatax_ui_add_erp_rules in Drupal Commerce Connector for AvaTax 7.4

Add/Delete ERP related rules.

1 string reference to 'commerce_avatax_ui_add_erp_rules'
commerce_avatax_ui_admin_settings in includes/commerce_avatax_ui.admin.inc
Admin settings menu callback.

File

includes/commerce_avatax_ui.admin.inc, line 529
Admin settings for commerce_avatax.

Code

function commerce_avatax_ui_add_erp_rules($form, &$form_state) {
  if (isset($form_state['values']['commerce_avatax_erp_status']) && $form_state['values']['commerce_avatax_erp_status']) {
    $rules_exist = FALSE;
    $rules = rules_config_load_multiple(array(
      'commerce_avatax_sop_commit',
      'commerce_avatax_sop_cancel',
      'commerce_avatax_sop_cancel_on_delete',
    ));
    if (count($rules) == 3) {
      $rules_exist = TRUE;
    }

    // Create rules if they don't already exist or the overwrite option has been
    // selected.
    if (!$rules_exist || isset($form_state['values']['overwrite_rules']) && $form_state['values']['overwrite_rules']) {

      // Delete rules if the delete option has been selected.
      $rules = rules_config_load_multiple(array(
        'commerce_avatax_sop_commit',
        'commerce_avatax_sop_cancel',
        'commerce_avatax_sop_cancel_on_delete',
      ));
      rules_config_delete(array_keys($rules));
      $commit_rule = new RulesReactionRule();
      $commit_rule->label = 'COMMIT order sales tax';
      $commit_rule->active = TRUE;
      $commit_rule
        ->event('commerce_order_update');
      $commit_rule
        ->condition(rules_condition('data_is', array(
        'data:select' => 'commerce-order:status',
        'op' => '==',
        'value:select' => 'commerce-order-unchanged:status',
      ))
        ->negate());
      $commit_rule
        ->condition(rules_condition('data_is', array(
        'data:select' => 'commerce-order:status',
        'op' => '==',
        'value' => 'completed',
      )));
      $commit_rule
        ->action(rules_action('commerce_avatax_commit_transaction', array(
        'order:select' => 'commerce-order',
      )));
      $commit_rule
        ->save('commerce_avatax_sop_commit');
      $cancel_rule = new RulesReactionRule();
      $cancel_rule->label = 'CANCEL order sales tax';
      $cancel_rule->active = TRUE;
      $cancel_rule
        ->event('commerce_order_update');
      $cancel_rule
        ->condition(rules_condition('data_is', array(
        'data:select' => 'commerce-order:status',
        'op' => '==',
        'value:select' => 'commerce-order-unchanged:status',
      ))
        ->negate());
      $cancel_rule
        ->condition(rules_condition('data_is', array(
        'data:select' => 'commerce-order:status',
        'op' => '==',
        'value' => 'canceled',
      )));
      $cancel_rule
        ->action(rules_action('commerce_avatax_cancel_transaction', array(
        'order:select' => 'commerce-order',
      )));
      $cancel_rule
        ->save('commerce_avatax_sop_cancel');
      $cancel_delete_rule = new RulesReactionRule();
      $cancel_delete_rule->label = 'CANCEL order sales tax on order delete';
      $cancel_delete_rule->active = TRUE;
      $cancel_delete_rule
        ->event('commerce_order_delete');
      $cancel_delete_rule
        ->action(rules_action('commerce_avatax_cancel_transaction', array(
        'order:select' => 'commerce-order',
      )));
      $cancel_delete_rule
        ->save('commerce_avatax_sop_cancel_on_delete');
    }
  }
  else {

    // Delete rules if the delete option has been selected.
    if (isset($form_state['values']['delete_rules']) && $form_state['values']['delete_rules']) {
      $rules = rules_config_load_multiple(array(
        'commerce_avatax_sop_commit',
        'commerce_avatax_sop_cancel',
        'commerce_avatax_sop_cancel_on_delete',
      ));
      rules_config_delete(array_keys($rules));
    }
  }
}