3

我正在尝试以编程方式创建一个 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 信用票据页面 谢谢

4

1 回答 1

2

You can easily make credit memo from paid invoices. So first you need to get all paid invoices from your order:

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

Then go throughout the invoices and make credit memo:

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

Hope it works for you.

于 2013-10-18T09:54:59.570 回答