andreacanton
andreacanton

Reputation: 383

Magento + Phoenix Cash On Delivery + automatic invoices == Invoice totals wrong

I'm running Magento 1.8.1.0 (patched SUPEE-5344, SUPEE-1533, SUPEE-5994, SUPEE-6285) on a LAMP stack enviroment. I've installed the Phoenix COD extension (v. 1.0.8). In my development enviroment cache is disabled.

I've implemented a module for automatically create invoices according to the kind of payment method (COD, PayPal, Credit Card, etc). When a COD order is generated the order totals are correct, but in invoice's totals the COD fee doesn't appear.

Autoinvoice Module

this is the /etc/modules/ file of my module:

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_Autoinvoice>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Sales />
                <Phoenix_CashOnDelivery />
            </depends>
        </MyCompany_Autoinvoice>
    </modules>
</config>

I've created an observer function on the sales_order_save_after event

public function salesSaveAfter( $event ) {
    $order               = $event->getOrder();
    $payment_method_code = $order->getPayment()->getMethodInstance()->getCode();

    $is_Paypal = ( strpos( $payment_method_code, 'paypal' ) !== false ) ? true : false;
    $is_Xpay   = ( strpos( $payment_method_code, 'xpay' ) !== false ) ? true : false;
    $is_Cod    = ( strpos( $payment_method_code, 'cashondelivery' ) !== false ) ? true : false;

    if ( $order->canInvoice() && ( $is_Paypal || $is_Xpay || $is_Cod ) ) {
        $invoice = Mage::getModel( 'sales/service_order', $order )->prepareInvoice();
        if ( ! $invoice->getTotalQty() ) {
            Mage::throwException( $this->__( 'Cannot create an invoice without products.' ) );

            return;
        }

        $invoice->register();
        $transactionSave = Mage::getModel( 'core/resource_transaction' )
                               ->addObject( $invoice )
                               ->addObject( $invoice->getOrder() );
        $transactionSave->save();
    } else {
        return;
    }
}

Some more notes

Has you can see my module depends on Phoenix_CacheOnDelivery so this function should be triggered after any observer's function.

If I manually generate the invoice everything work fine.

If I programmatically launch a script to generate the second invoice (for one order with the first invoice without the COD fee), the invoice is generated only with the COD fee: so everything fine.

Upvotes: 1

Views: 569

Answers (1)

andreacanton
andreacanton

Reputation: 383

I've found the solution myself!

The problem: Phoenix_CashOnDelivery add the COD fees by attaching to the event sales_order_payment_place_end that came after the sales_order_save_after so in the order they was set to 0 at the invoice generation time.

The solution: Simply change the event to listen in sales_order_place_after.

Plus, the sales_order_save_after is called many more times needed, so the code it's optimized! Yay!

I hope this could be useful for someone out there.

Upvotes: 1

Related Questions