You are here

function uc_coupon_tax_adjustment in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 6 uc_coupon.module \uc_coupon_tax_adjustment()
  2. 7.2 uc_coupon.module \uc_coupon_tax_adjustment()

Handle tax on coupons by calculating tax for individual discounted prices.

1 string reference to 'uc_coupon_tax_adjustment'
uc_coupon_uc_line_item in ./uc_coupon.module
Implements hook_uc_line_item().

File

./uc_coupon.module, line 1757
Provides discount codes and gift certificates for Ubercart.

Code

function uc_coupon_tax_adjustment($price, $order, $tax) {
  $amount = 0;
  if (isset($order->data['coupons'])) {
    foreach ($order->data['coupons'] as $discounts) {
      foreach ($discounts as $id => $item) {
        if (is_numeric($id) && $id > 0) {

          // This is a product discount, so see if the product is taxable.
          $node = node_load($id);
          $adjust = in_array($node->type, $tax->taxed_product_types) && ($tax->shippable == 0 || $node->shippable == 1);
        }
        else {

          // This is a line-item discount, so find the corresponding line item.
          $lid = is_numeric($id) ? -$id : $id;

          // Convert id to a line item id.
          foreach ($order->line_items as $line_item) {
            if ($line_item['line_item_id'] == $lid) {
              $adjust = in_array($line_item['type'], $tax->taxed_line_items);
              break;
            }
          }
        }
        if ($adjust) {
          $amount += (isset($item->pretax_discount) ? $item->pretax_discount : $item->discount) * ($price > 0 ? 1 : -1);
        }
      }
    }
  }
  return $amount;
}