Example #1
0
 public function onSalesOrderInvoicePay($observer)
 {
     Mage::log(" : onSalesOrderInvoicePay stap 1   ", null, 'custom.log');
     try {
         $couponDao = new CouponDao();
         $invoice = $observer->getEvent()->getInvoice();
         $order = $invoice->getOrder();
         // Method 1
         unset($this->usedDiscountCoupons);
         // Unset $array.
         $this->usedDiscountCoupons = array();
         // Reset $array to an empty array.
         foreach ($order->getAllItems() as $item) {
             // Do something with $item here...
             $name = $item->getName();
             $price = $item->getPrice();
             $_product = Mage::getModel('catalog/product')->load($item->getProductId());
             $manufacturerName = $_product->getAttributeText('manufacturer');
             $coupon = $couponDao->loadCouponByBrand($manufacturerName);
             if ($coupon != null) {
                 for ($i = 1; $i <= $item->getQtyOrdered(); $i++) {
                     $couponSaving = $couponDao->loadInInVoiceCouponSaving($coupon->getCouponId(), $order->getCustomerId());
                     if ($couponSaving != null) {
                         $couponDao->setCouponSavingToUsed($couponSaving);
                     } else {
                         if ($price >= $coupon->getMinPrice() && !$this->discountCouponUsed($order, $manufacturerName)) {
                             $couponSaving = $couponDao->loadCouponSaving($coupon->getCouponId(), $order->getCustomerId(), CouponState::OPEN);
                             if ($couponSaving == null) {
                                 $couponSavingNew = CouponSaving::startCouponSaving($coupon, $order->getCustomerId());
                                 $couponSaving = $couponDao->createCouponSaving($couponSavingNew);
                             }
                             $couponSavingId = $couponSaving->getCouponSavingId();
                             if (NOW() >= $couponSaving->getValidUntil()) {
                                 $couponDao->updateCouponSaving($couponSaving);
                                 $couponSavingNew = CouponSaving::startCouponSaving($coupon, $order->getCustomerId());
                                 $couponSavingNew = $couponDao->createCouponSaving($couponSavingNew);
                                 $couponSavingIdNew = $couponSavingNew->getCouponSavingId();
                                 $couponDao->createStamp($couponSavingIdNew, $name, $price, $order->getIncrementId());
                             } else {
                                 $couponDao->createStamp($couponSavingId, $name, $price, $order->getIncrementId());
                                 $couponDao->updateCouponSaving($couponSaving);
                             }
                         }
                     }
                 }
             }
         }
     } catch (Exception $e) {
         Mage::log("exception " + $e, null);
     }
     return $this;
 }