我试图以编程方式创建一个magento贷项凭证,但是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中找到了这段代码,我正在尝试使用它,但效果不太好。
为什么总数没有更新?哪种方法是正确的?
谢谢

最佳答案

你可以很容易地从已付的发票中生成贷项凭单。因此,首先,您需要从订单中获得所有已付款的发票:

$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();
}

希望对你有用。

关于php - 如何以编程方式在magento中创建贷项凭证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17302909/

10-11 01:36