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; }
/** * @param CouponSaving $couponSaving * @return array */ public function getNumberOfProductsOrderdForSavingCoupon(CouponSaving $couponSaving, $customerId) { // couponId=$couponSaving->getCouponSavingId(); $query = "SELECT COUNT(*) number_of_products_ordered, co_sa.required_number_of_products , max(st.prices) maxPaid\n FROM coupon_saving co_sa, stamp st\n WHERE co_sa.coupon_saving_id=st.coupon_saving_id\n AND customer ={$customerId} AND\n co_sa.coupon_saving_id=" . $couponSaving->getCouponSavingId(); Mage::log("query voor max paid {$query} ", null, 'custom.log'); $results = $this->readConnection->fetchAll($query); $foundData; foreach ($results as $savingCouponData) { $requiredNumberOfProducts = $savingCouponData['required_number_of_products']; $numberOfProductsOrdered = $savingCouponData['number_of_products_ordered']; $maxPaid = $savingCouponData['maxPaid']; Mage::log("requiredNumberOfProducts {$requiredNumberOfProducts} ", null, 'custom.log'); Mage::log(" numberOfProductsOrdered {$numberOfProductsOrdered}", null, 'custom.log'); Mage::log(" numberOfProductsOrdered {$numberOfProductsOrdered}", null, 'custom.log'); $foundData = array($requiredNumberOfProducts, $numberOfProductsOrdered, $maxPaid); break; } return $foundData; }