public function getProductTax($product_price, $product_id) { $amount = 0; $taxprofile_id = J2StorePrices::getTaxProfileId($product_id); $rates = $this->getRates($taxprofile_id); $tax_rates = $this->getTaxRates($product_price, $rates); //print_r($tax_rates); foreach ($tax_rates as $tax_rate) { $amount += $tax_rate['amount']; } return $amount; }
/** * Calculates the product total (aka subtotal) * using the array of items in the order object * * @return unknown_type */ function calculateProductTotals() { $app = JFactory::getApplication(); $session = JFactory::getSession(); $tax = new J2StoreTax(); $subtotal = 0.0; $this->_taxes = J2StoreHelperCart::getTaxes(); // TODO Must decide what we want these methods to return; for now, null $items = $this->getItems(); if (!is_array($items)) { $this->order_subtotal = $subtotal; return; } // calculate product subtotal foreach ($items as $item) { //$item->orderitem_final_price; // track the subtotal $subtotal += $item->orderitem_final_price; } // set subtotal $this->order_subtotal = $subtotal; //coupon if ($session->has('coupon', 'j2store')) { $coupon_info = J2StoreHelperCart::getCoupon($session->get('coupon', '', 'j2store')); if ($coupon_info) { $discount_total = 0; if (!$coupon_info->product) { $sub_total = $this->order_subtotal; } else { $sub_total = 0; foreach ($items as $item) { if (in_array($item->product_id, $coupon_info->product)) { $sub_total += $item->orderitem_final_price; } } } if ($coupon_info->value_type == 'F') { $coupon_info->value = min($coupon_info->value, $sub_total); } //maximum value restriction. If set then we need to check if ($coupon_info->value_type == 'P' && !empty($coupon_info->max_value) && (double) $coupon_info->max_value > 0) { //calculate the actual discount require_once JPATH_SITE . '/components/com_j2store/models/mycart.php'; $cart_model = new J2StoreModelMyCart(); $actual_discount = $cart_model->getDiscountTotal(); //is the actual discount greater than the max value if ($actual_discount > 0 && $actual_discount > (double) $coupon_info->max_value) { //set the coupon to be of fixed value $coupon_info->value = (double) $coupon_info->max_value; $coupon_info->value_type = 'F'; } } foreach ($items as $item) { $discount = 0; if (!$coupon_info->product) { $status = true; } else { if (in_array($item->product_id, $coupon_info->product)) { $status = true; } else { $status = false; } } if ($status) { if ($coupon_info->value_type == 'F') { $discount = $coupon_info->value * ($item->orderitem_final_price / $sub_total); } elseif ($coupon_info->value_type == 'P') { $discount = $item->orderitem_final_price / 100 * $coupon_info->value; } //get tax profile id. We need to adjust tax against coupons $tax_profile_id = J2StorePrices::getTaxProfileId($item->product_id); if ($tax_profile_id) { $this->_product_taxrates = $tax->getRateArray($item->orderitem_final_price, $tax_profile_id); $tax_rates = $tax->getRateArray($item->orderitem_final_price - ($item->orderitem_final_price - $discount), $tax_profile_id); foreach ($tax_rates as $tax_rate) { // if ($tax_rate['value_type'] == 'P') { $this->_taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount']; $this->_product_taxrates[$tax_rate['taxrate_id']]['amount'] -= $tax_rate['amount']; // } } } } $item->orderitem_discount = $discount; $discount_total += $discount; //adjust the tax $product_tax_totals = 0; foreach ($this->_product_taxrates as $product_tax_rate) { $product_tax_totals += $product_tax_rate['amount']; } if ($tax_profile_id) { $item->orderitem_tax = $product_tax_totals; } } $shipping_cost = 0; //free shipping if ($coupon_info->free_shipping && $this->shipping) { //we have a shipping method $shipping_cost = $this->shipping->shipping_price + $this->shipping->shipping_extra + $this->shipping->shipping_tax; $discount_total += $shipping_cost; } $this->order_discount = $discount_total > $this->order_subtotal + $this->order_tax + $shipping_cost ? $this->order_subtotal + $this->order_tax + $shipping_cost : $discount_total; } } //tax override. If there is a coupon. we need to do this $tax_total = 0; foreach ($this->_taxes as $key => $value) { if ($value > 0) { $tax_total += $value; } } $this->order_tax = $tax_total; }