public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     $address->setData('zitec_dpd_cashondelivery_surcharge', 0);
     $address->setData('base_zitec_dpd_cashondelivery_surcharge', 0);
     $address->setData('zitec_dpd_cashondelivery_surcharge_tax', 0);
     $address->setData('base_zitec_dpd_cashondelivery_surcharge_tax', 0);
     $paymentMethod = $address->getQuote()->getPayment()->getMethod();
     if ($paymentMethod == Mage::helper('zitec_dpd')->getDpdPaymentCode() && $address->getAddressType() == 'shipping') {
         $quote = $address->getQuote();
         /* @var $quote Mage_Sales_Model_Quote */
         $shippingAddress = $quote->getShippingAddress();
         $request = new Varien_Object();
         $request->setWebsiteId(Mage::helper('zitec_dpd/payment')->getWebsiteId());
         $request->setDestCountryId($shippingAddress->getCountryId());
         $request->setDestRegionId($shippingAddress->getRegionId());
         $request->setDestPostcode($shippingAddress->getPostcode());
         $request->setPackageWeight($shippingAddress->getWeight());
         if ($this->_getTaxHelper()->shippingPriceIncludesTax($address->getQuote()->getStoreId())) {
             $request->setData('zitec_table_price', $shippingAddress->getBaseSubtotalInclTax());
         } else {
             $request->setData('zitec_table_price', $shippingAddress->getBaseSubtotal());
         }
         $request->setMethod(str_replace(Mage::helper('zitec_dpd')->getDPDCarrierCode() . '_', '', $shippingAddress->getShippingMethod()));
         $tablerateSurcharge = Mage::getResourceModel('zitec_dpd/carrier_tablerate')->getCashOnDeliverySurcharge($request);
         if (is_null($tablerateSurcharge)) {
             return $this;
         } elseif (!empty($tablerateSurcharge)) {
             $baseCashondeliverySurcharge = $this->_getHelper()->calculateQuoteBaseCashOnDeliverySurcharge($quote, $tablerateSurcharge);
         } else {
             $baseCashondeliverySurcharge = $this->_getHelper()->returnDefaultBaseCashOnDeliverySurcharge($quote);
         }
         if (!isset($baseCashondeliverySurcharge)) {
             return $this;
         }
         $baseCurrencyCode = $quote->getStore()->getBaseCurrencyCode();
         $currentCurrencyCode = $quote->getStore()->getCurrentCurrencyCode();
         $cashondeliverySurcharge = Mage::helper('directory')->currencyConvert($baseCashondeliverySurcharge, $baseCurrencyCode, $currentCurrencyCode);
         $address->setData('zitec_dpd_cashondelivery_surcharge', $cashondeliverySurcharge);
         $address->setData('base_zitec_dpd_cashondelivery_surcharge', $baseCashondeliverySurcharge);
         $this->_calculateSurchargeSalesTax($address);
         $quote->save();
     }
     $address->setGrandTotal($address->getGrandTotal() + $address->getData('zitec_dpd_cashondelivery_surcharge'));
     $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getData('base_zitec_dpd_cashondelivery_surcharge'));
     return $this;
 }
Example #2
0
 /**
  * Generate gift card accounts after order save
  *
  * @param Varien_Event_Observer $observer
  * @return Enterprise_GiftCard_Model_Observer
  */
 public function generateGiftCardAccounts(Varien_Event_Observer $observer)
 {
     // sales_order_save_after
     $order = $observer->getEvent()->getOrder();
     $requiredStatus = Mage::getStoreConfig(Enterprise_GiftCard_Model_Giftcard::XML_PATH_ORDER_ITEM_STATUS, $order->getStore());
     $loadedInvoices = array();
     foreach ($order->getAllItems() as $item) {
         if ($item->getProductType() == Enterprise_GiftCard_Model_Catalog_Product_Type_Giftcard::TYPE_GIFTCARD) {
             $qty = 0;
             $options = $item->getProductOptions();
             switch ($requiredStatus) {
                 case Mage_Sales_Model_Order_Item::STATUS_INVOICED:
                     $paidInvoiceItems = isset($options['giftcard_paid_invoice_items']) ? $options['giftcard_paid_invoice_items'] : array();
                     // find invoice for this order item
                     $invoiceItemCollection = Mage::getResourceModel('sales/order_invoice_item_collection')->addFieldToFilter('order_item_id', $item->getId());
                     foreach ($invoiceItemCollection as $invoiceItem) {
                         $invoiceId = $invoiceItem->getParentId();
                         if (isset($loadedInvoices[$invoiceId])) {
                             $invoice = $loadedInvoices[$invoiceId];
                         } else {
                             $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
                             $loadedInvoices[$invoiceId] = $invoice;
                         }
                         // check, if this order item has been paid
                         if ($invoice->getState() == Mage_Sales_Model_Order_Invoice::STATE_PAID && !in_array($invoiceItem->getId(), $paidInvoiceItems)) {
                             $qty += $invoiceItem->getQty();
                             $paidInvoiceItems[] = $invoiceItem->getId();
                         }
                     }
                     $options['giftcard_paid_invoice_items'] = $paidInvoiceItems;
                     break;
                 default:
                     $qty = $item->getQtyOrdered();
                     if (isset($options['giftcard_created_codes'])) {
                         $qty -= count($options['giftcard_created_codes']);
                     }
                     break;
             }
             $hasFailedCodes = false;
             if ($qty > 0) {
                 $isRedeemable = 0;
                 if ($option = $item->getProductOptionByCode('giftcard_is_redeemable')) {
                     $isRedeemable = $option;
                 }
                 $lifetime = 0;
                 if ($option = $item->getProductOptionByCode('giftcard_lifetime')) {
                     $lifetime = $option;
                 }
                 $amount = $item->getBasePrice();
                 $websiteId = Mage::app()->getStore($order->getStoreId())->getWebsiteId();
                 $data = new Varien_Object();
                 $data->setWebsiteId($websiteId)->setAmount($amount)->setLifetime($lifetime)->setIsRedeemable($isRedeemable)->setOrderItem($item);
                 $codes = isset($options['giftcard_created_codes']) ? $options['giftcard_created_codes'] : array();
                 $goodCodes = 0;
                 for ($i = 0; $i < $qty; $i++) {
                     try {
                         $code = new Varien_Object();
                         Mage::dispatchEvent('enterprise_giftcardaccount_create', array('request' => $data, 'code' => $code));
                         $codes[] = $code->getCode();
                         $goodCodes++;
                     } catch (Mage_Core_Exception $e) {
                         $hasFailedCodes = true;
                         $codes[] = null;
                     }
                 }
                 if ($goodCodes && $item->getProductOptionByCode('giftcard_recipient_email')) {
                     $sender = $item->getProductOptionByCode('giftcard_sender_name');
                     $senderName = $item->getProductOptionByCode('giftcard_sender_name');
                     if ($senderEmail = $item->getProductOptionByCode('giftcard_sender_email')) {
                         $sender = "{$sender} <{$senderEmail}>";
                     }
                     $codeList = Mage::helper('enterprise_giftcard')->getEmailGeneratedItemsBlock()->setCodes($codes)->setIsRedeemable($isRedeemable)->setStore(Mage::app()->getStore($order->getStoreId()));
                     $balance = Mage::app()->getLocale()->currency(Mage::app()->getStore($order->getStoreId())->getBaseCurrencyCode())->toCurrency($amount);
                     $templateData = array('name' => $item->getProductOptionByCode('giftcard_recipient_name'), 'email' => $item->getProductOptionByCode('giftcard_recipient_email'), 'mobile' => $item->getProductOptionByCode('giftcard_recipient_mobile'), 'sender_name_with_email' => $sender, 'sender_name' => $senderName, 'gift_message' => $item->getProductOptionByCode('giftcard_message'), 'giftcards' => $codeList->toHtml(), 'balance' => $balance, 'is_multiple_codes' => 1 < $goodCodes, 'store' => $order->getStore(), 'store_name' => $order->getStore()->getName(), 'is_redeemable' => $isRedeemable);
                     $email = Mage::getModel('core/email_template')->setDesignConfig(array('store' => $item->getOrder()->getStoreId()));
                     $email->sendTransactional($item->getProductOptionByCode('giftcard_email_template'), Mage::getStoreConfig(Enterprise_GiftCard_Model_Giftcard::XML_PATH_EMAIL_IDENTITY, $item->getOrder()->getStoreId()), $item->getProductOptionByCode('giftcard_recipient_email'), $item->getProductOptionByCode('giftcard_recipient_name'), $templateData);
                     if ($email->getSentSuccess()) {
                         $options['email_sent'] = 1;
                         // Start Sending SMS for Giftcard
                         $templateData['send_to'] = $templateData['mobile'];
                         $templateData['giftcards'] = strip_tags($templateData['giftcards']);
                         $helper = Mage::helper('nosql/joker');
                         $helper->sendNow($templateData, 'sms', 'EVOUCHER');
                         // End Sending SMS for Giftcart
                     }
                 }
                 $options['giftcard_created_codes'] = $codes;
                 $item->setProductOptions($options);
                 $item->save();
             }
             if ($hasFailedCodes) {
                 $url = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/giftcardaccount');
                 $message = Mage::helper('enterprise_giftcard')->__('Some of Gift Card Accounts were not generated properly. You can create Gift Card Accounts manually <a href="%s">here</a>.', $url);
                 Mage::getSingleton('adminhtml/session')->addError($message);
             }
         }
     }
     return $this;
 }
Example #3
0
 /**
  * Create giftcard accounts and update the giftcard_created_codes in product option
  * Returns whether there is an error in creating giftcard
  *
  * @param Mage_Sales_Model_Order_Item $item
  * @param int $numAccounts number of accounts to create
  * @return boolean whether there is failure when creating gift card accounts
  */
 protected function _createGiftCardAccounts(Mage_Sales_Model_Order_Item $item, $numAccounts)
 {
     $options = $item->getProductOptions();
     $isRedeemable = $this->_isGiftcardRedeemable($item);
     $lifetime = $this->_getGiftcardLifeTime($item);
     $amount = $item->getBasePrice();
     $websiteId = Mage::app()->getStore($item->getOrder()->getStoreId())->getWebsiteId();
     $createdCodes = isset($options['giftcard_created_codes']) ? $options['giftcard_created_codes'] : array();
     $data = new Varien_Object();
     $data->setWebsiteId($websiteId)->setAmount($amount)->setLifetime($lifetime)->setIsRedeemable($isRedeemable)->setOrderItem($item);
     $hasFailedCodes = false;
     for ($i = 0; $i < $numAccounts; $i++) {
         try {
             $code = new Varien_Object();
             Mage::dispatchEvent('enterprise_giftcardaccount_create', array('request' => $data, 'code' => $code));
             $createdCodes[] = $code->getCode();
         } catch (Mage_Core_Exception $e) {
             $hasFailedCodes = true;
         }
     }
     $options['giftcard_created_codes'] = $createdCodes;
     $item->setProductOptions($options);
     return $hasFailedCodes;
 }
 public function getSurchage()
 {
     if ($this->_surcharge === null) {
         $order = $this->fetchOrder();
         if ($order) {
             $this->_surcharge = $order->getData('base_zitec_dpd_cashondelivery_surcharge');
         } else {
             $quote = Mage::helper('zitec_dpd/payment')->getQuote();
             /* @var $quote Mage_Sales_Model_Quote */
             if ($quote) {
                 $shippingAddress = $quote->getShippingAddress();
                 $request = new Varien_Object();
                 $request->setWebsiteId(Mage::helper('zitec_dpd/payment')->getWebsiteId());
                 $request->setDestCountryId($shippingAddress->getCountryId());
                 $request->setDestRegionId($shippingAddress->getRegionId());
                 $request->setDestPostcode($shippingAddress->getPostcode());
                 $request->setPackageWeight($shippingAddress->getWeight());
                 if ($this->_getTaxHelper()->shippingPriceIncludesTax($quote->getStoreId())) {
                     $request->setData('zitec_table_price', $shippingAddress->getBaseSubtotalInclTax());
                 } else {
                     $request->setData('zitec_table_price', $shippingAddress->getBaseSubtotal());
                 }
                 $request->setMethod(str_replace(Mage::helper('zitec_dpd')->getDPDCarrierCode() . '_', '', $shippingAddress->getShippingMethod()));
                 $tablerateSurcharge = Mage::getResourceModel('zitec_dpd/carrier_tablerate')->getCashOnDeliverySurcharge($request);
                 if (is_null($tablerateSurcharge) || is_array($tablerateSurcharge) && is_null($tablerateSurcharge['cashondelivery_surcharge'])) {
                     return null;
                 } elseif (!empty($tablerateSurcharge)) {
                     $this->_surcharge = $this->_getHelper()->calculateQuoteBaseCashOnDeliverySurcharge($quote, $tablerateSurcharge);
                 } else {
                     $this->_surcharge = $this->_getHelper()->returnDefaultBaseCashOnDeliverySurcharge($quote);
                 }
             }
         }
     }
     return $this->_surcharge;
 }