以编程方式创建发票后,Magento订单总数未更新

时间:2017-01-19 19:25:51

标签: magento

我正在以编程方式创建发票。由于我们正在使用Fishbowl库存,因此我们直接生成magento订单的装运。所以我根据货件自动创建发票。现在发票已成功创建,但订单总数未根据发票金额进行更新。

以下是我从发货项目创建发票的代码。我在 sales_order_shipment_save_after 事件中使用此功能,并使用「采购订单」作为订单的付款方式,因此无法捕获发票。

    public function autoInvoiceGenerate(Varien_Event_Observer $observer){

    $shipment = $observer->getEvent()->getShipment();
    $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment->getIncrementId());

    $shippedItems = $shipment->getAllItems();
    $order = $shipment->getOrder();
    $order = Mage::getModel('sales/order')->load($order->getId());


    try{ 

     if(!$order->canInvoice()) {
            //$order->addStatusHistoryComment('Order cannot be invoiced.', false);
            //$order->save();
            return $this;
     }

     $invoiceQtys = array();
     foreach($shippedItems as $item){
         $invoiceQtys[$item->getOrderItemId()] = $item->getQty();
     }

     if(empty($invoiceQtys)){
        return $this;
     } 



    //START Handle Invoice
    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($invoiceQtys);
    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
    $invoice->register();
    $invoice->getOrder()->setCustomerNoteNotify(false);
    $invoice->getOrder()->setIsInProcess(true);
    $invoice->sendEmail(true, '');



    $order->addStatusHistoryComment('Invoice created for shipment #'.$shipment->getData('increment_id').'.', false);
    $transactionSave = Mage::getModel('core/resource_transaction')
        ->addObject($invoice)
        ->addObject($invoice->getOrder());
    $transactionSave->save();

}

1 个答案:

答案 0 :(得分:0)

有点晚了,但如果你在这里仍有这个问题我的建议:

  • 保存与发票相关联的订单。
  • 最后保存发票。

这对我有用:

$order = Mage::getModel('sales/order')->load($orderId);
$invoice = Mage::getModel('sales/service_order', $order)
    ->prepareInvoice($itemsarray);

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(true);
$order->save();
Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder())
    ->save();
$invoice->save();