From 8ae36cffd3bbab8e61852810c21a3c3a558378b4 Mon Sep 17 00:00:00 2001 From: Laurie Barker Date: Mon, 2 Mar 2015 15:33:53 +0000 Subject: [PATCH] Add Create Invoice Option Add ability to configure that a cash on delivery order automatically create an invoice. --- .../Phoenix/CashOnDelivery/Model/Observer.php | 34 ++++++++++++++++++- .../Phoenix/CashOnDelivery/etc/config.xml | 9 +++++ .../Phoenix/CashOnDelivery/etc/system.xml | 9 +++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/code/community/Phoenix/CashOnDelivery/Model/Observer.php b/app/code/community/Phoenix/CashOnDelivery/Model/Observer.php index 773fded..b0e3cbf 100644 --- a/app/code/community/Phoenix/CashOnDelivery/Model/Observer.php +++ b/app/code/community/Phoenix/CashOnDelivery/Model/Observer.php @@ -55,7 +55,7 @@ public function sales_order_payment_place_end(Varien_Event_Observer $observer) { $payment = $observer->getPayment(); if ($payment->getMethodInstance()->getCode() != 'phoenix_cashondelivery') { - return $this;; + return $this; } $order = $payment->getOrder(); @@ -139,4 +139,36 @@ public function order_cancel_after(Varien_Event_Observer $observer) return $this; } + + + /** + * Invoice and set status to Cash On Delivery if order payment type is Cash on Delivery + * + * @param Varien_Event_Observer $observer + * @return void + */ + public function invoice_cashondelivery($observer) + { + // Get order + $order = $observer->getEvent()->getOrder(); + + // If order is instance of Phoenix_CashOnDelivery_Model_CashOnDelivery + if ($order->getPayment()->getMethodInstance()->getCode() == 'phoenix_cashondelivery' && + // Can be invoiced + $order->canInvoice() && + // order_invoice is true + Mage::getStoreConfig('payment/phoenix_cashondelivery/order_invoice')) { + // Prepare invoice + $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice(); + // Set capture case - OFFLINE + $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE); + $invoice->register(); + // + // Save transaction + $transactionSave = Mage::getModel('core/resource_transaction') + ->addObject($invoice) + ->addObject($invoice->getOrder()); + $transactionSave->save(); + } + } } \ No newline at end of file diff --git a/app/code/community/Phoenix/CashOnDelivery/etc/config.xml b/app/code/community/Phoenix/CashOnDelivery/etc/config.xml index 65d71f4..868f23f 100644 --- a/app/code/community/Phoenix/CashOnDelivery/etc/config.xml +++ b/app/code/community/Phoenix/CashOnDelivery/etc/config.xml @@ -136,6 +136,15 @@ + + + + singleton + phoenix_cashondelivery/observer + invoice_cashondelivery + + + diff --git a/app/code/community/Phoenix/CashOnDelivery/etc/system.xml b/app/code/community/Phoenix/CashOnDelivery/etc/system.xml index 5798e18..c813e63 100644 --- a/app/code/community/Phoenix/CashOnDelivery/etc/system.xml +++ b/app/code/community/Phoenix/CashOnDelivery/etc/system.xml @@ -83,6 +83,15 @@ 1 1 + + + select + adminhtml/system_config_source_yesno + 45 + 1 + 1 + 1 + text