/** * Calculates the per_order coupon discount for the order * and the total post-tax/shipping discount * and sets order->order_discount * * @return unknown_type */ function calculateDiscountTotals() { $this->_taxes = K2StoreHelperCart::getTaxes(); $session = JFactory::getSession(); $tax = new K2StoreTax(); JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_k2store/models'); $model = JModelLegacy::getInstance('MyCart', 'K2StoreModel'); $products = $model->getDataNew(); if ($session->has('coupon', 'k2store')) { $coupon_info = K2StoreHelperCart::getCoupon($session->get('coupon', '', 'k2store')); if ($coupon_info) { $discount_total = 0; if (!$coupon_info->product) { $sub_total = K2StoreHelperCart::getSubTotal(); } else { $sub_total = 0; foreach ($products as $product) { if (in_array($product['product_id'], $coupon_info->product)) { $sub_total += $product['total']; } } } if ($coupon_info->value_type == 'F') { $coupon_info->value = min($coupon_info->value, $sub_total); } foreach ($products as $product) { $discount = 0; if (!$coupon_info->product) { $status = true; } else { if (in_array($product['product_id'], $coupon_info->product)) { $status = true; } else { $status = false; } } if ($status) { if ($coupon_info->value_type == 'F') { $discount = $coupon_info->value * ($product['total'] / $sub_total); } elseif ($coupon_info->value_type == 'P') { $discount = $product['total'] / 100 * $coupon_info->value; } if ($product['tax_profile_id']) { $tax_rates = $tax->getRateArray($product['total'] - ($product['total'] - $discount), $product['tax_profile_id']); foreach ($tax_rates as $tax_rate) { // if ($tax_rate['value_type'] == 'P') { $this->_taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount']; // } } } } $discount_total += $discount; } } } // store the total amount of the discount //set the total as equal to the order_subtotal + order_tax if its greater than the sum of the two $this->order_discount = $discount_total > $this->order_subtotal + $this->order_tax ? $this->order_subtotal + $this->order_tax : $discount_total; }
public static function getTaxes() { $tax_data = array(); JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_k2store/models'); $model = JModelLegacy::getInstance('Mycart', 'K2StoreModel'); $products = $model->getDataNew(); $t = new K2StoreTax(); foreach ($products as $product) { if ($product['tax_profile_id']) { $tax_rates = $t->getRateArray($product['price'], $product['tax_profile_id']); foreach ($tax_rates as $tax_rate) { if (!isset($tax_data[$tax_rate['taxrate_id']])) { $tax_data[$tax_rate['taxrate_id']] = $tax_rate['amount'] * $product['quantity']; } else { $tax_data[$tax_rate['taxrate_id']] += $tax_rate['amount'] * $product['quantity']; } } } } return $tax_data; }