/** * 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; }