Esempio n. 1
0
 public function setDiscount($observer)
 {
     try {
         $array = array();
         Mage::log(" we zijn in  setDiscount ", null, 'custom.log');
         $quote = $observer->getEvent()->getQuote();
         $quoteId = $quote->getId();
         $cartItems = $quote->getAllVisibleItems();
         $obj = new Excellence_Fee_Model_Fee();
         $customerId = Mage::getSingleton('customer/session')->getId();
         $discountAmount = 0;
         $discountDescription = "Voor spaarkaart voor";
         foreach ($cartItems as $item) {
             $productPrice = $item->getProduct()->getPrice();
             $store = Mage::app()->getStore('default');
             $request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store);
             $taxClassId = $item->getData('tax_class_id');
             $percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId));
             $tax = $percent * 0.01 * $productPrice;
             $totalAmount = $productPrice + $tax;
             Mage::log("mogelijk korting  {$totalAmount} , product prijs {$productPrice} en btw is {$tax} ", null, 'custom.log');
             $this->productId = $item->getProductId();
             $_product = Mage::getModel('catalog/product')->load($item->getProductId());
             $brand = $_product->getAttributeText('manufacturer');
             $couponDao = new CouponDao();
             $couponSaving = $couponDao->loadCouponSavingByOrderId($brand, $quoteId, $customerId);
             if ($couponSaving != null) {
                 $discountAmount = $couponSaving->getDiscountAmount();
                 Mage::log("discountAmount is {$discountAmount}", null, 'custom.log');
             } else {
                 Mage::log("discountAmount is 0", null, 'custom.log');
             }
             // if (!in_array($brand, $array)) {
             if ($couponSaving != null) {
                 Mage::log("setDiscount : stap voor 1   ", null, 'custom.log');
                 if ($couponSaving->getDiscountAmount() > 0) {
                     $discountDescription = "{$discountDescription} en  {$brand}";
                 } else {
                     $discountDescription = "{$discountDescription} voor {$brand}";
                 }
                 //$exist_amount = $couponSaving->getDiscountAmount();
                 //$discountAmount = $exist_amount;
                 array_push($array, $brand);
             } else {
                 if ($obj->canApply($item, $quoteId, $totalAmount)) {
                     Mage::log("setDiscount : stap voor 2   ", null, 'custom.log');
                     $brand = $obj->getBrand();
                     array_push($array, $brand);
                     Mage::log(" in canApply check for setDiscount  {$totalAmount}", null, 'custom.log');
                     $discountAmount = $discountAmount + $totalAmount;
                     if ($discountAmount > 0) {
                         $discountDescription = "{$discountDescription} en  {$brand}";
                     } else {
                         $discountDescription = "{$discountDescription} voor {$brand}";
                     }
                 }
             }
             //}
         }
         if ($quoteId) {
             Mage::log("setDiscount : stap 1   ", null, 'custom.log');
             if ($discountAmount > 0) {
                 Mage::log("setDiscount : stap 2   ", null, 'custom.log');
                 //we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how much we need to remove.
                 $quote->setGrandTotal($quote->getGrandTotal() - $discountAmount)->setBaseGrandTotal($quote->getBaseGrandTotal() - $discountAmount)->setSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)->setBaseGrandTotal((double) $quote->getBaseGrandTotal())->setDiscountAmount($discountAmount)->setDiscountDescription($discountDescription)->setBaseDiscountAmount($discountAmount)->save();
                 $canAddItems = $quote->isVirtual() ? 'billing' : 'shipping';
                 foreach ($quote->getAllAddresses() as $address) {
                     $address->setSubtotal(0);
                     $address->setBaseSubtotal(0);
                     $address->setGrandTotal(0);
                     $address->setBaseGrandTotal(0);
                     $address->collectTotals();
                     if ($address->getAddressType() == $canAddItems) {
                         $address->setSubtotal((double) $quote->getSubtotal());
                         $address->setBaseSubtotal((double) $quote->getBaseSubtotal());
                         $address->setSubtotalWithDiscount((double) $quote->getSubtotalWithDiscount());
                         $address->setBaseSubtotalWithDiscount((double) $quote->getBaseSubtotalWithDiscount());
                         $address->setGrandTotal((double) $quote->getGrandTotal());
                         $address->setBaseGrandTotal((double) $quote->getBaseGrandTotal());
                         $address->setDiscountAmount(-$discountAmount);
                         $address->setDiscountDescription($discountDescription);
                         $address->setBaseDiscountAmount(-$discountAmount);
                         $address->setTaxAmount($tax);
                         $address->setBaseTaxAmount($tax);
                         $address->save();
                         Mage::log("setDiscount : stap 6   ", null, 'custom.log');
                     }
                     //end: if
                 }
                 //end: foreach
             }
         }
         Mage::log("setDiscount : stap 8   ", null, 'custom.log');
     } catch (Exception $e) {
         Mage::log("Set discount  exception " . $e, null);
     }
 }