You are here

function uc_taxes_order in Ubercart 5

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

Update and save tax line items to the order.

File

uc_taxes/uc_taxes.module, line 126

Code

function uc_taxes_order($op, $arg1, $arg2) {
  switch ($op) {
    case 'save':
      $changes = array();
      $line_items = uc_line_item_tax('load', $arg1);

      //$arg1->line_items = uc_order_load_line_items($arg1, true);
      if (is_array($arg1->line_items)) {

        //drupal_set_message('<pre>'. var_export($arg1->line_items, true) .'</pre>');
        foreach ($arg1->line_items as $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']) {
                  uc_order_update_line_item($line['line_item_id'], $new_line['title'], $new_line['amount'], $new_line['data']);
                  $changes[] = t('Changed %title to %amount.', array(
                    '%amount' => uc_currency_format($new_line['amount']),
                    '%title' => $new_line['title'],
                  ));
                }
                unset($line_items[$id]);
                $delete = false;
                break;
              }
            }
            if ($delete) {
              uc_order_delete_line_item($line['line_item_id']);
              $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']);
          $changes[] = t('Added %amount for %title.', array(
            '%amount' => uc_currency_format($line['amount']),
            '%title' => $line['title'],
          ));
        }
      }
      if (count($changes)) {
        uc_order_log_changes($arg1->order_id, $changes);
      }
      break;
  }
}