Exemple #1
0
 function getTotals()
 {
     $app = JFactory::getApplication();
     $session = JFactory::getSession();
     $tax = new K2StoreTax();
     $products = $this->getDataNew();
     $total_data = array();
     $total = 0;
     //products
     $total_data['products'] = $products;
     //sub total
     $total_data['subtotal'] = K2StoreHelperCart::getSubtotal();
     $total += $total_data['subtotal'];
     //taxes
     $tax_data = array();
     $taxes = K2StoreHelperCart::getTaxes();
     //coupon
     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 = $this->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;
             }
             $total_data['coupon'] = array('title' => JText::sprintf('K2STORE_COUPON_TITLE', $session->get('coupon', '', 'k2store')), 'value' => -$discount_total);
             //$total_data['coupon'] = $coupon_data;
             //less the coupon discount in the total
             $total -= $discount_total;
         }
     }
     if ($session->has('shipping_values', 'k2store')) {
         $shipping = $session->get('shipping_values', array(), 'k2store');
         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' => $this->tax->getRateName($key), 'percent' => $tax->getPercent($key), 'value' => $value);
             $total += $value;
         }
     }
     $total_data['taxes'] = $tax_data;
     $total_data['total'] = $total;
     return $total_data;
 }