/** * Presets flat data from subscription * @param AW_Sarp_Model_Subscription $Subscription * @return AW_Sarp_Model_Subscription_Flat */ public function setSubscription(AW_Sarp_Model_Subscription $Subscription) { Zend_Date::setOptions(array('extend_month' => true)); // Fix Zend_Date::addMonth unexpected result if (!$Subscription->isInfinite()) { $expireDate = $Subscription->getDateExpire()->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); } else { $expireDate = null; } if ($Subscription->getIsNew()) { $lastOrderAmount = $Subscription->getQuote()->getGrandTotal(); $virtual = $Subscription->getQuote()->getIsVirtual(); } else { $lastOrderAmount = $Subscription->getLastOrder()->getGrandTotal(); $virtual = $Subscription->getLastOrder()->getIsVirtual(); } if ($Subscription->isActive()) { $paymentOffset = $Subscription->getPeriod()->getPaymentOffset(); // Get next payment date if (!$Subscription->getLastPaidDate()) { $nextPaymentDate = $Subscription->getLastOrder()->getCreatedAtStoreDate(); $nextPaymentDate = $Subscription->getNextSubscriptionEventDate($Subscription->getDateStart()); $nextDeliveryDate = clone $Subscription->getDateStart(); $nextDeliveryDate->addDayOfYear(0 + floatval($paymentOffset)); } else { $nextPaymentDate = $Subscription->getNextSubscriptionEventDate(); } if ($paymentOffset) { if (!$Subscription->getLastPaidDate()) { // No payments made yet $lastOrderDate = clone $Subscription->getDateStart(); $lastOrderDate->addDayOfYear(0 - floatval($paymentOffset)); } else { $lastOrderDate = $Subscription->getLastOrder()->getCreatedAtStoreDate(); } $probablyDeliveryDate = clone $lastOrderDate; $probablyDeliveryDate = $probablyDeliveryDate->addDayOfYear(floatval($paymentOffset)); if ($probablyDeliveryDate->compare(new Zend_Date(), Zend_Date::DATE_SHORT) > 0) { $nextDeliveryDate = clone $lastOrderDate; } $nextPaymentDate->addDayOfYear(0 - floatval($paymentOffset)); } if (!isset($nextDeliveryDate)) { $nextDeliveryDate = clone $nextPaymentDate; } $nextDeliveryDate = $nextDeliveryDate->addDayOfYear(floatval($paymentOffset))->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); $nextPaymentDate = $nextPaymentDate->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); } else { // Drop next payment date if subscription is suspended $nextDeliveryDate = $nextPaymentDate = null; } $this->setSubscriptionId($Subscription->getId())->setCustomerName($Subscription->getCustomer()->getName())->setCustomerEmail($Subscription->getCustomer()->getEmail())->setFlatLastOrderStatus($Subscription->getLastOrder()->getStatus())->setFlatLastOrderAmount($lastOrderAmount)->setFlatLastOrderCurrencyCode($Subscription->getLastOrder()->getOrderCurrencyCode())->setFlatDateExpire($expireDate)->setHasShipping(1 - $virtual)->setFlatNextPaymentDate($nextPaymentDate)->setFlatNextDeliveryDate($nextDeliveryDate)->setProductsText($this->_convertProductsText($Subscription))->setProductsSku($this->_convertProductsSku($Subscription)); $Subscription->setCustomerName($Subscription->getCustomer()->getName())->setCustomerEmail($Subscription->getCustomer()->getEmail())->setFlatLastOrderStatus($Subscription->getLastOrder()->getStatus())->setFlatLastOrderAmount($lastOrderAmount)->setFlatLastOrderCurrencyCode($Subscription->getLastOrder()->getOrderCurrencyCode())->setFlatDateExpire($expireDate)->setHasShipping(1 - $virtual)->setFlatNextPaymentDate($nextPaymentDate)->setFlatNextDeliveryDate($nextDeliveryDate)->setProductsText($this->_convertProductsText($Subscription))->setProductsSku($this->_convertProductsSku($Subscription)); return $this; }
/** * Generates events for one subscription * @param AW_Sarp_Model_Subscription $Subscription * @param object $woDelete [optional] * @return AW_Sarp_Model_Alert */ public function generateSubscriptionEvents(AW_Sarp_Model_Subscription $Subscription, $woDelete = null) { // Purge events before if (!$Subscription->getId()) { return $this; } if (!$woDelete) { if ($Subscription->getIsNew() && $this->getType() == self::TYPE_NEW_SUBSCRIPTION) { $this->getResource()->getWriteAdapter()->delete($this->getResource()->getTable('sarp/alert_event'), 'subscription_id=' . $Subscription->getId() . " AND alert_id=" . $this->getId()); } elseif ($this->getType() == self::TYPE_DATE_EXPIRE) { $_alertCollection = Mage::getModel('sarp/alert_event')->getCollection(); $_alertCollection->getSelect()->where('subscription_id = ?', $Subscription->getId())->where('alert_id = ?', $this->getId()); $_alertCollection->load(); if (sizeof($_alertCollection) > 0) { return $this; } } elseif ($this->getType() != self::TYPE_NEW_SUBSCRIPTION) { $this->getResource()->getWriteAdapter()->delete($this->getResource()->getTable('sarp/alert_event'), 'subscription_id=' . $Subscription->getId() . " AND alert_id=" . $this->getId()); } } // Don't generate events for disabled alerts if ($this->getStatus() == self::STATUS_DISABLED) { return $this; } // Generate dates $suffix = $this->getTimeIsAfter() ? 1 : -1; $timeAmount = $suffix * $this->getTimeMultiplier() * $this->getTimeAmount(); // hours switch ($this->getType()) { case self::TYPE_DATE_START: if ($Subscription->isActive()) { $date = $Subscription->getDateStart(); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_DATE_EXPIRE: if (($Subscription->getIsExpiring() || $Subscription->isActive()) && !$Subscription->isInfinite()) { $date = $Subscription->getDateExpire(); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_DELIVERY: if ($Subscription->isActive()) { $date = $Subscription->getFlatNextDeliveryDate(); $date = new Zend_Date($date, AW_Sarp_Model_Subscription::DB_DATE_FORMAT); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_NEW_SUBSCRIPTION: if ($Subscription->getIsNew()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_SUSPENDED: if ($Subscription->getIsSuspending()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_ACTIVATION: if ($Subscription->getIsReactivated()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_CANCEL_SUBSCRIPTION: if ($Subscription->getIsCancelling()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; default: throw new AW_Sarp_Exception("Alert for events type '{$this->getType()}' are not supported"); } if (isset($date) && $date instanceof Zend_Date && $date->compare(new Zend_Date()) > 0) { $Event = Mage::getModel('sarp/alert_event')->setSubscriptionId($Subscription->getId())->setRecipient($this->getRecipientAddress($Subscription))->setAlertId($this->getId())->setDate($date->toString(AW_Sarp_Model_Subscription::DB_DATETIME_FORMAT))->save(); } return $this; }