function getTotals() { $app = JFactory::getApplication(); $session = JFactory::getSession(); $products = $this->getDataNew(); $tax = new J2StoreTax(); $total_data = array(); $total = 0; //products $total_data['products'] = $products; //sub total $total_data['subtotal'] = J2StoreHelperCart::getSubtotal(); $total += $total_data['subtotal']; //taxes $tax_data = array(); $taxes = J2StoreHelperCart::getTaxes(); //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 = J2StoreHelperCart::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); } //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 $actual_discount = $this->getDiscountTotal(); //is the actual discount greater than the max value if ($actual_discount > 0 && $actual_discount > (double) $coupon_info->max_value) { $coupon_info->value = (double) $coupon_info->max_value; $coupon_info->value_type = 'F'; } } $product_array2 = array(); 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') { $taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount']; // } } } } $discount_total += $discount; } if ($coupon_info->free_shipping && $session->has('shipping_values', 'j2store')) { $shipping = $session->get('shipping_values', array(), 'j2store'); $shipping_cost = $shipping['shipping_price'] + $shipping['shipping_extra'] + $shipping['shipping_tax']; $discount_total += $shipping_cost; } $total_data['coupon'] = array('title' => JText::sprintf('J2STORE_COUPON_TITLE', $session->get('coupon', '', 'j2store')), 'value' => -$discount_total); //$total_data['coupon'] = $coupon_data; //less the coupon discount in the total $total -= $discount_total; } } if ($session->has('shipping_values', 'j2store')) { $shipping = $session->get('shipping_values', array(), 'j2store'); if (count($shipping) && isset($shipping['shipping_name'])) { $total_data['shipping_amount'] = $shipping['shipping_price'] + $shipping['shipping_extra']; $total_data['shipping_tax'] = $shipping['shipping_tax']; $total_data['shipping_total'] = $shipping['shipping_price'] + $shipping['shipping_extra'] + $shipping['shipping_tax']; $total_data['shipping_name'] = $shipping['shipping_name']; $total += $total_data['shipping_total']; } } $total_data['total_without_tax'] = $total; //taxes foreach ($taxes as $key => $value) { if ($value > 0) { $tax_data[] = array('title' => $tax->getRateName($key), 'percent' => $tax->getPercent($key), 'value' => $value); $total += $value; } } $total_data['taxes'] = $tax_data; $total_data['total'] = $total; return $total_data; }
/** * 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 = J2StoreHelperCart::getTaxes(); $session = JFactory::getSession(); $tax = new J2StoreTax(); JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_j2store/models'); $model = JModelLegacy::getInstance('MyCart', 'J2StoreModel'); $products = $model->getDataNew(); 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 = J2StoreHelperCart::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); } //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 ($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; }