Frage

I want to create partial invoice for downloadable product of an order based on release date of that product, I have got so many solutions to create partial invoice but it's create the invoice for all product, not for selected one.

War es hilfreich?

Lösung

Here is the solution but don't forget to change condition according your requirement.

<?php

require_once '../app/Mage.php';
Mage::app('admin');

CapturePayment::createInvoice();

class CapturePayment {

    public static function createInvoice() {


        $orders = Mage::getModel('sales/order')->getCollection()
                        ->addFieldToFilter('status', array('processing'));

        foreach ($orders as $order) {
            $orderId = $order->getIncrementId();

            try {

                if (!$order->getId() || !$order->canInvoice()) {
                    echo 'Invoice is not allowed for order=>.' . $orderId . "\n";
                    continue;
                }
                $items = $order->getAllItems();
                $partial = false;
                $qtys = array();

                foreach ($items as $item) {
                    /* Check wheter we can create invoice for this item or not */
                    if (!CapturePayment::canInvoiceItem($item, array())) {
                        continue;
                    }
                    /* Get product id on the basis of order item id */
                    $productId = $item->getProductId();
                    /* If product is tablet then don't create invoice for the same */
                    /* Put your condition here for items you want to create the invoice e.g.*/
                    /* Load the product to get the release date */
                    $productDetails = Mage::getModel('catalog/product')->load($productId);

                    /* create your $qtys array here like */
                    $qtys['orderItemId'] = 'quantityOrdered'; 
                    /* NOTE: But if you don't want to craete invoice for any particular item then pass the '0' quantity for that item and set the value for $partial = true if some product remain in any order*/
                }
                /* Prepare the invoice to capture/create the invoice */
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($qtys);

                if (!$invoice->getTotalQty()) {
                    continue;
                }

                $amount = $invoice->getGrandTotal();
                if ($partial) {
                    $amount = $invoice->getGrandTotal();
                    $invoice->register()->pay();
                    $invoice->getOrder()->setIsInProcess(true);
                    $history = $invoice->getOrder()->addStatusHistoryComment('Partial amount of $' . $amount . ' captured automatically.', false);
                    $history->setIsCustomerNotified(true);
                    $invoice->sendEmail(true, '');
                    $order->save();
                    Mage::getModel('core/resource_transaction')
                            ->addObject($invoice)
                            ->addObject($invoice->getOrder())
                            ->save();
                    $invoice->save();
                } else {
                    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                    $invoice->register();
                    $invoice->getOrder()->setCustomerNoteNotify(true);
                    $invoice->getOrder()->setIsInProcess(false);
                    $invoice->getOrder()->setData('state', "complete");
                    $invoice->getOrder()->setStatus("complete");
                    $invoice->sendEmail(true, '');
                    $order->save();
                    Mage::getModel('core/resource_transaction')
                            ->addObject($invoice)
                            ->addObject($invoice->getOrder())
                            ->save();
                    $invoice->save();
                }
            } catch (Exception $e) {
                echo 'Exception in Order => ' . $orderId . '=>' . $e . "\n";
            }
        }
    }

    /* Check the invoice status for an item of an order */
    public static function canInvoiceItem($item, $qtys=array()) {
        if ($item->getLockedDoInvoice()) {
            return false;
        }
        if ($item->isDummy()) {
            if ($item->getHasChildren()) {
                foreach ($item->getChildrenItems() as $child) {
                    if (empty($qtys)) {
                        if ($child->getQtyToInvoice() > 0) {
                            return true;
                        }
                    } else {
                        if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
                            return true;
                        }
                    }
                }
                return false;
            } else if ($item->getParentItem()) {
                $parent = $item->getParentItem();
                if (empty($qtys)) {
                    return $parent->getQtyToInvoice() > 0;
                } else {
                    return isset($qtys[$parent->getId()]) && $qtys[$parent->getId()] > 0;
                }
            }
        } else {
            return $item->getQtyToInvoice() > 0;
        }
    }

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top