为部分订单开具发票;总数未更新

发布于 2024-11-01 16:55:18 字数 996 浏览 1 评论 0原文

在我们的订单流程中,可以发送部分订单的发票。因此,当运送几个订单行时,还必须发送发票。

为了实现这一点,我使用以下代码:

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

        if (!$invoice->getTotalQty()) {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
        }

        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
        $invoice->register();
        $transactionSave = Mage::getModel('core/resource_transaction')
                        ->addObject($invoice)
                        ->addObject($invoice->getOrder());

        $transactionSave->save();

        $invoice->sendEmail();
        $invoice->setEmailSent(true);
        $invoice->save();

其中 $items 变量是一个数组,其中包含订单 ID 和要开具发票的产品数量。

创建的发票显示要开具发票的正确产品,但不知何故总计未更新。总计仍然是完整订单的总计,而不是部分发票的总计。

我可能必须更新或重新计算总数,但找不到正确的代码来强制更新。

周围有谁可以引导我走向正确的方向吗?

In our order proces it is possible to send an invoice for a partial order. So when a couple of order lines are being shipped, an invoice have to be send also.

To make this possible I use this code:

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

        if (!$invoice->getTotalQty()) {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
        }

        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
        $invoice->register();
        $transactionSave = Mage::getModel('core/resource_transaction')
                        ->addObject($invoice)
                        ->addObject($invoice->getOrder());

        $transactionSave->save();

        $invoice->sendEmail();
        $invoice->setEmailSent(true);
        $invoice->save();

Where the $items variable is an array containing the order ids and the amount of products to be invoiced.

The created invoice shows the correct products to be invoiced, but somehow the totals aren't updated. The totals still are the totals of the complete order, instead of the partial invoice.

I probably have to update or recalculate the totals but can't find the right code to force the update.

Anyone around who can put me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨离汐 2024-11-08 16:55:18

嗯,看来我已经发现问题了。上述功能可在管理员界面中手动执行。上面所附的代码我只能通过更改 Magento 的核心文件来工作。

如果将 Mage_Sales_Model_Service_Order 的第 103 行从 continue; 更改为 $qty = 0;,该功能将起作用。

简而言之,这就是发生的事情。继续时,第二行项目不会添加到发票中,发票认为当前项目是整个订单的最后一个项目,因此需要对完整的未清金额开具发票。就我而言,我确实想要开具发票的发票和我不想开具发票的行。

我已将其作为 Magento 问题列表中的问题提交。

Well, it seems I have found the problem. The functionality as described above works manually executing it in the administrator interface. The code as enclosed above I only got to work by changing a core file of Magento.

If you change line 103 of Mage_Sales_Model_Service_Order from continue; to $qty = 0; the functionality works.

In short, this is what happens. With continue the second row item isn't added to the invoice which the invoice makes thinks the curren item is the last item of the whole order and therefore needs to invoice the complete outstanding amount. In my case the invoice I did want to invoice and the row I didn't want to invoice.

I've submitted it as issue on the Magento issue list.

秋凉 2024-11-08 16:55:18

今天我就遇到了这个问题,但我找到了一种更优雅的方法来解决它,而无需编辑核心。解决方案是通过我们不想开发票的产品,数量为 0。
通过这种方式,您在核心中更改的代码的行为将与您的解决方案中的行为完全相同:)

举个例子,如果我的订单中有 2 个产品:

array(
    1234 => 1,
    1235 => 2
)

传递此数组:

$qtys = array(
    1234 => 1,
    1235 => 0
)

将强制此代码:

            // Mage_Sales_Model_Service_Order: lines 97-103
            if (isset($qtys[$orderItem->getId()])) { // here's the magic
                $qty = (float) $qtys[$orderItem->getId()];
            } elseif (!count($qtys)) {
                $qty = $orderItem->getQtyToInvoice();
            } else {
                continue; // the line to edit according to previous solution
            }

与您的解决方案中的行为完全相同,因此您不必编辑核心代码。

希望它有帮助:)

Today I faced with exactly this problem, but I found a more elegant way to solve it without editing the core. The solution is to pass the products that we don't want to invoice, with 0 quantity.
In this way, the code you changed in core will act exactly like in your solution :)

As an example if I have 2 products in my order:

array(
    1234 => 1,
    1235 => 2
)

passing this array:

$qtys = array(
    1234 => 1,
    1235 => 0
)

will force this code:

            // Mage_Sales_Model_Service_Order: lines 97-103
            if (isset($qtys[$orderItem->getId()])) { // here's the magic
                $qty = (float) $qtys[$orderItem->getId()];
            } elseif (!count($qtys)) {
                $qty = $orderItem->getQtyToInvoice();
            } else {
                continue; // the line to edit according to previous solution
            }

to act exactly like in your solution, so you don't have to edit core code.

Hope it helps :)

错々过的事 2024-11-08 16:55:18

好的 - 花了我一点时间,但现在我看到了如何正确创建数组。

foreach ($items as $itemId => $item) {
    $itemQtyToShip = $item->getQtyToShip()*1;
    if ($itemQtyToShip>0) {
       $itemQtyOnHand = $stockItem->getQty()*1;
       if ($itemQtyOnHand>0) {
          //use the order item id as key 
          //set the amount to invoice for as the value
          $toShip[$item->getId()] = $itemQtyToShip;             
       } else {
          //if not shipping the item set the qty to 0
          $toShip[$item->getId()] = 0;
    }
}

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

这将创建正确的发票。

OK - took me a bit, but now I see how to correctly create the array.

foreach ($items as $itemId => $item) {
    $itemQtyToShip = $item->getQtyToShip()*1;
    if ($itemQtyToShip>0) {
       $itemQtyOnHand = $stockItem->getQty()*1;
       if ($itemQtyOnHand>0) {
          //use the order item id as key 
          //set the amount to invoice for as the value
          $toShip[$item->getId()] = $itemQtyToShip;             
       } else {
          //if not shipping the item set the qty to 0
          $toShip[$item->getId()] = 0;
    }
}

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

This creates a proper invoice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文