Ejemplo n.º 1
0
 public function calculateGrandTotal()
 {
     // calulate promo codes and group discounts
     //we need to tally up the cart, apply discounts, TAX that TOTAL somehow (different tax clases come into play), then add shipping
     //grab our discounts
     $cartDiscounts = $this->getOrderDiscounts();
     //reset totals
     $this->total_discounts = 0;
     $this->shipping_total = 0;
     $this->shipping_total_before_discounts = 0;
     $this->shippingDiscount = 0;
     $this->surcharge_total = 0;
     $this->subtotal = 0;
     $this->total = 0;
     $this->grand_total = 0;
     $this->tax = 0;
     $validateDiscountMessage = '';
     //eDebug($this->surcharge_total);
     //hate doing double loops, but we need to have the subtotal figured out already for
     //doing the straight dollar disoount calculations below
     for ($i = 0; $i < count($this->orderitem); $i++) {
         // figure out the amount of the discount
         /*if (!empty($this->product_discounts)) {
               $discount_amount = ($this->orderitem[$i]->products_price * ($this->product_discounts * .01));
               // change the price of the orderitem..this is needed for when we calculate tax below.
               $this->orderitem[$i]->products_price = $this->orderitem[$i]->products_price - $discount_amount;
               // keep a tally  of the total amount being subtracted by this discount.
               $this->total_discounts += $discount_amount;                
           }*/
         //$this->orderitem[$i]->products_price = $this->orderitem[$i]->getPriceWithOptions(); // * $this->orderitem[$i]->quantity;
         $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price;
         //$this->orderitem[$i]->products_price_original = $this->orderitem[$i]->product->getPrice();
         $this->subtotal += $this->orderitem[$i]->products_price * $this->orderitem[$i]->quantity;
         $this->surcharge_total += $this->orderitem[$i]->product->getSurcharge() * $this->orderitem[$i]->quantity;
     }
     for ($i = 0; $i < count($this->orderitem); $i++) {
         //only allowing one discount for now, but in future we'll need to process
         //multiple and accomdate the "weight" and 'allow other discounts' type settings
         //this foreach will only fire once as of now, and will only hit on one or the other
         //TODO: We need to use produce_price_adjusted in the loops to accomodate for more than one disocunt
         //otherwise it's just resetting them now instead of adding them
         foreach ($cartDiscounts as $od) {
             //do not calculate invalid discounts, but don't remove either
             $discount = new discounts($od->discounts_id);
             /*$validateDiscountMessage = $discount->validateDiscount();
               if($validateDiscountMessage != '') break;*/
             //percentage discount
             if ($discount->action_type == 3) {
                 $discount_amount = round($this->orderitem[$i]->products_price * ($discount->discount_percent / 100), 2);
                 // change the price of the orderitem..this is needed for when we calculate tax below.
                 $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price - $discount_amount;
                 // keep a tally  of the total amount being subtracted by this discount.
                 $this->total_discounts += $discount_amount * $this->orderitem[$i]->quantity;
             }
             //straight $$ discount
             if ($discount->action_type == 4) {
                 $this->total_discounts = $discount->discount_amount;
                 //what % of the order is this product with all it's quantity
                 $percentOfTotalOrder = $this->orderitem[$i]->products_price * $this->orderitem[$i]->quantity / $this->subtotal;
                 //figoure out how much that'll be and what each quanityt piece will bare
                 $discountAmountPerItem = round($percentOfTotalOrder * $discount->discount_amount / $this->orderitem[$i]->quantity, 2);
                 //$discount_amount = $this->orderitem[$i]->products_price * ($discount->discount_percent / 100);
                 // change the price of the orderitem..this is needed for when we calculate tax below.
                 $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price - $discountAmountPerItem;
                 // keep a tally  of the total amount being subtracted by this discount.
                 //$this->total_discounts += $discountAmountPerItem * $this->orderitem[$i]->quantity;                    //eDebug($discountAmountPerItem);
             }
         }
         // calculate the tax for this product
         $taxclass = new taxclass($this->orderitem[$i]->product->tax_class_id);
         $this->orderitem[$i]->products_tax = $taxclass->getProductTax($this->orderitem[$i]);
         $this->tax += $this->orderitem[$i]->products_tax * $this->orderitem[$i]->quantity;
         //save out the order item
         $this->orderitem[$i]->save();
     }
     // add the "cart discounts" - percentage for sure, but straight can work also should be added after the final total is calculated,
     //including tax but not shipping
     // $this->updateOrderDiscounts();
     /*foreach ($cartDiscounts as $od)
       {
           $discount = new discounts($od->discounts_id); 
           if ($discount->action_type == 4)
           {                       
                $this->total_discounts += $discount->discount_amount;                           
           }                 
       }   */
     // calculate the shipping costs - need to check shipping discounts here in the future
     $estimate_shipping = false;
     if ($this->shipping_required) {
         $shippingmethods = $this->getShippingMethods();
         if (count($shippingmethods) > 0) {
             foreach ($shippingmethods as $sm_id) {
                 $method = new shippingmethod($sm_id, true);
                 if ($method->requiresShipping($this)) {
                     /*
                                             //need to implement handling
                                             $shippingCalc = new shippingcalculator($method->shippingcalculator_id);
                                             $calc = new $shippingCalc->calculator_name($method->shippingcalculator_id);
                                             eDebug($calc,true);*/
                     $this->shipping_total += $method->shipping_cost;
                     // + $method->calculator->getHandling();
                 }
             }
         } else {
             $estimate_shipping = true;
         }
     }
     $this->shipping_total_before_discounts = $this->shipping_total;
     if (isset($cartDiscounts)) {
         foreach ($cartDiscounts as $od) {
             $discount = new discounts($od->discounts_id);
             $this->shipping_total = $discount->calculateShippingTotal($this->shipping_total);
         }
     }
     $this->shippingDiscount = $this->shipping_total_before_discounts - $this->shipping_total;
     //check here to make sure we don't discount ourselves into oblivion
     $orderTotalPreDiscounts = $this->subtotal + $this->tax + $this->shipping_total;
     if ($this->total_discounts > $orderTotalPreDiscounts) {
         $this->total_discounts = $orderTotalPreDiscounts;
     }
     $this->total = $this->subtotal - $this->total_discounts;
     if ($estimate_shipping) {
         $this->shipping_total = shipping::estimateShipping($this);
     }
     // figure out which tax zones apply to this order.
     $this->taxzones = taxclass::getCartTaxZones($this);
     $this->grand_total = $this->subtotal - $this->total_discounts + $this->tax + $this->shipping_total + $this->surcharge_total;
     //if($validateDiscountMessage != '') flash('message',$validateDiscountMessage);
     //eDebug($this, true);
 }