如何以编程方式在magento中创建贷项通知单

时间:2013-06-25 16:40:37

标签: php magento

我正在尝试以编程方式创建Magento Credit Memo,但Magento向我显示了一个零总计的贷项通知单。

$incrementID = "100000016";
$orderMagento = Mage::getModel('sales/order')->loadByIncrementId($incrementID);

$convertOrder = new Mage_Sales_Model_Convert_Order ();
$creditmemo = $convertOrder->toCreditmemo ( $orderMagento );

$items = $orderMagento->getAllItems();  

foreach ( $items as $item ) {
    $_eachCreditMemoItem = $convertOrder->itemToCreditmemoItem ( $item );
    $_eachCreditMemoItem->setQty ($item->getQtyInvoiced());
    $_eachCreditMemoItem->register ();
    $creditmemo->addItem ( $_eachCreditMemoItem );
    $totalQty += $item->getQtyInvoiced ();
} 

$creditmemo->refund();
$creditmemo->setTotalQty ( $totalQty );
$creditmemo->collectTotals();

$orderCreditMemoStatusCode = Mage_Sales_Model_Order::STATE_CLOSED;
$orderCreditMemoStatusComment = $comment;
$saveTransaction = Mage::getModel('core/resource_transaction')->addObject ($creditmemo )->addObject ( $orderMagento )->save ();
$orderMagento->addStatusToHistory ( $orderCreditMemoStatusCode, $orderCreditMemoStatusComment, false );
$orderMagento->save ();

我在StackOverflow中找到了这段代码,我正在尝试使用它,但它也不起作用。

为什么总数不会更新?哪种方法是正确的?

Magento Credit Note Page 感谢

1 个答案:

答案 0 :(得分:1)

您可以轻松地从付款发票中提​​取贷项通知单。首先,您需要从订单中获取所有付款发票:

$invoices = array();
foreach ($order->getInvoiceCollection() as $invoice) {
    if ($invoice->canRefund()) {
        $invoices[] = $invoice;
    }
}

然后查看发票并制作贷项通知单:

$service = Mage::getModel('sales/service_order', $order);
foreach ($invoices as $invoice) {
    $creditmemo = $service->prepareInvoiceCreditmemo($invoice);
    $creditmemo->refund();
}

希望它适合你。