如何为订单的部分发票创建cron

时间:2013-01-10 20:28:41

标签: magento magento-1.7 invoice

我想根据该产品的发布日期为订单的可下载产品创建部分发票,我有很多解决方案来创建部分发票但是它为所有产品创建发票,而不是为所选产品创建发票。

1 个答案:

答案 0 :(得分:1)

这是解决方案,但不要忘记根据您的要求改变条件。

<?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;
        }
    }

}