【问题标题】:How to create cron for partial invoice of an order如何为订单的部分发票创建 cron
【发布时间】:2013-01-10 20:28:41
【问题描述】:

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

【问题讨论】:

    标签: magento magento-1.7 invoice


    【解决方案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;
            }
        }
    
    }
    

    【讨论】:

    • 谢谢,根据我的要求进行一些修改后,它的工作完美:D
    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 2012-08-11
    • 1970-01-01
    • 2013-08-18
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多