You are here

function uc_taxes_order in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_taxes/uc_taxes.module \uc_taxes_order()

Implements hook_order().

Updates and saves tax line items to the order.

File

uc_taxes/uc_taxes.module, line 159

Code

function uc_taxes_order($op, $arg1, $arg2) {
  switch ($op) {
    case 'save':
      $changes = array();
      $callback = _line_item_data('tax', 'callback');
      $line_items = $callback('load', $arg1);
      $context = array(
        'revision' => 'formatted',
        'type' => 'line_item',
        'subject' => array(
          'order' => $arg1,
        ),
      );
      if (is_array($arg1->line_items)) {
        foreach ($arg1->line_items as $i => $line) {
          if ($line['type'] == 'tax') {
            $delete = TRUE;
            foreach ($line_items as $id => $new_line) {
              if ($new_line['title'] == $line['title']) {
                if ($new_line['amount'] != $line['amount']) {
                  $context['subject']['line_item'] = $new_line;
                  uc_order_update_line_item($line['line_item_id'], $new_line['title'], $new_line['amount'], $new_line['data']);
                  $arg1->line_items[$i]['amount'] = $new_line['amount'];
                  $changes[] = t('Changed %title to %amount.', array(
                    '%amount' => uc_price($new_line['amount'], $context),
                    '%title' => $new_line['title'],
                  ));
                }
                unset($line_items[$id]);
                $delete = FALSE;
                break;
              }
            }
            if ($delete) {
              uc_order_delete_line_item($line['line_item_id']);
              unset($arg1->line_items[$i]);
              $changes[] = t('Removed %title.', array(
                '%title' => $line['title'],
              ));
            }
          }
        }
      }
      if (is_array($line_items)) {
        foreach ($line_items as $line) {
          uc_order_line_item_add($arg1->order_id, $line['id'], $line['title'], $line['amount'], $line['weight'], $line['data']);
          $line['type'] = 'tax';
          $arg1->line_items[] = $line;
          $context['subject']['line_item'] = $line;
          $changes[] = t('Added %amount for %title.', array(
            '%amount' => uc_price($line['amount'], $context),
            '%title' => $line['title'],
          ));
        }
      }
      if (count($changes)) {
        uc_order_log_changes($arg1->order_id, $changes);
      }
      break;
  }
}