Ejemplo n.º 1
0
 private function _calculate($set_shipping = true, $generate_id = true)
 {
     global $lC_Currencies, $lC_Tax, $lC_Weight, $lC_Shipping, $lC_Database, $lC_OrderTotal, $lC_Services, $lC_Coupons, $lC_Vqmod, $lC_Customer;
     $this->_sub_total = 0;
     $this->_total = 0;
     $this->_weight = 0;
     $this->_tax = 0;
     $this->_tax_groups = array();
     $this->_shipping_boxes_weight = 0;
     $this->_shipping_boxes = 0;
     $this->_shipping_quotes = array();
     $this->_order_totals = array();
     if ($generate_id) {
         $_SESSION['cartID'] = $this->generateCartID();
     }
     if ($this->hasContents()) {
         foreach ($this->_contents as $data) {
             $products_weight = $lC_Weight->convert($data['weight'], $data['weight_class_id'], SHIPPING_WEIGHT_UNIT);
             $this->_weight += $products_weight * $data['quantity'];
             if ($lC_Customer->isLoggedOn()) {
                 $tax = $lC_Tax->getTaxRate($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id'));
                 $tax_description = $lC_Tax->getTaxRateDescription($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id'));
             }
             $shown_price = $lC_Currencies->addTaxRateToPrice($data['price'], $tax, $data['quantity']);
             $this->_sub_total += $shown_price;
             $this->_total += $shown_price;
             if (DISPLAY_PRICE_WITH_TAX == '1' || $_SESSION['localization']['show_tax'] == 1) {
                 $tax_amount = $shown_price - $shown_price / ($tax < 10 ? '1.0' . str_replace('.', '', $tax) : '1.' . str_replace('.', '', $tax));
             } else {
                 $tax_amount = $tax / 100 * $shown_price;
                 $this->_total += $tax_amount;
             }
             $this->_tax += $tax_amount;
             if (isset($this->_tax_groups[$tax_description])) {
                 $this->_tax_groups[$tax_description] += $tax_amount;
             } else {
                 $this->_tax_groups[$tax_description] = $tax_amount;
             }
         }
         // coupons
         if (defined('MODULE_SERVICES_INSTALLED') && in_array('coupons', explode(';', MODULE_SERVICES_INSTALLED)) && isset($lC_Coupons)) {
             if ($lC_Coupons->hasContents()) {
                 $discount = $lC_Coupons->getTotalDiscount();
                 $this->_total -= $discount;
             }
         }
         $this->_shipping_boxes_weight = $this->_weight;
         $this->_shipping_boxes = 1;
         if (SHIPPING_BOX_WEIGHT >= $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100) {
             $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + SHIPPING_BOX_WEIGHT;
         } else {
             $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100;
         }
         if ($this->_shipping_boxes_weight > SHIPPING_MAX_WEIGHT) {
             // Split into many boxes
             $this->_shipping_boxes = ceil($this->_shipping_boxes_weight / SHIPPING_MAX_WEIGHT);
             $this->_shipping_boxes_weight = $this->_shipping_boxes_weight / $this->_shipping_boxes;
         }
         if ($set_shipping === true) {
             if (!class_exists('lC_Shipping')) {
                 include $lC_Vqmod->modCheck('includes/classes/shipping.php');
             }
             if (!$this->hasShippingMethod() || $this->getShippingMethod('is_cheapest') === true) {
                 $lC_Shipping = new lC_Shipping();
                 $this->setShippingMethod($lC_Shipping->getCheapestQuote(), false);
             } else {
                 $lC_Shipping = new lC_Shipping($this->getShippingMethod('id'));
                 $this->setShippingMethod($lC_Shipping->getQuote(), false);
             }
         }
         if (!class_exists('lC_OrderTotal')) {
             include $lC_Vqmod->modCheck('includes/classes/order_total.php');
         }
         $lC_OrderTotal = new lC_OrderTotal();
         $this->_order_totals = $lC_OrderTotal->getResult();
         // sanity recalc total
         $tkey = '';
         $ot_total = 0;
         foreach ($this->_order_totals as $key => $ot) {
             if ($ot['code'] != 'total') {
                 $ot_total = $ot_total + $ot['value'];
                 $_SESSION['lC_ShoppingCart_data']['order_totals'][$key]['text'] = $lC_Currencies->format($ot['value']);
             } else {
                 $tkey = $key;
             }
         }
         $_SESSION['lC_ShoppingCart_data']['order_totals'][$tkey]['text'] = $lC_Currencies->format($ot_total);
         $_SESSION['lC_ShoppingCart_data']['order_totals'][$tkey]['value'] = $ot_total;
         // coupons
         if (defined('MODULE_SERVICES_INSTALLED') && in_array('coupons', explode(';', MODULE_SERVICES_INSTALLED)) && isset($lC_Coupons)) {
             $lC_Coupons->displayCouponInOrderTotal();
         }
     }
 }