2011-04-18 24 views
3

在我們的訂單過程中,可以發送部分訂單的發票。所以,當一些訂單項目發貨時,發票也必須發送。開具部分訂單;總計未更新

爲了使這成爲可能我使用此代碼:

$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和產品的量的數組開具發票。

創建的發票顯示要開票的正確產品,但不知何故合計未更新。總數仍然是完整訂單的總數,而不是部分發票。

我可能必須更新或重新計算總計,但無法找到正確的代碼來強制更新。

任何人都可以讓我在正確的方向嗎?

+0

您能否提供您的$ items數組的示例內容? – 2011-04-18 13:01:27

+0

已經發現問題了,謝謝你的幫助! – pderaaij 2011-04-18 13:05:25

回答

2

好吧,似乎我發現了這個問題。上述功能可以在管理員界面中手動執行。上面附上的代碼我只能通過更改Magento的核心文件來工作。

如果您將Mage_Sales_Model_Service_Order的第103行從continue;更改爲$qty = 0;,則該功能將起作用。

總之,這是發生了什麼事。繼續第二行項目不會添加到發票所認爲的現金項目是整個訂單的最後一個項目,因此需要開具全部未付金額的發票。在我的情況下,我確實希望發票的發票和我不想開具發票的行。

我已將它作爲Magento問題列表中的問題提交。

0

今天我遇到了這個問題,但我發現了一個更優雅的方式來解決它,而無需編輯核心。解決方法是將不需要發票的產品通過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 
      } 

行動完全像在溶膠因此,您不必編輯核心代碼。

希望它能幫助:)

+0

Magento團隊也在bug報告中提出了建議。感謝您的評論! – pderaaij 2011-05-25 07:57:22

+0

現在我搜索了它並找到http://www.magentocommerce.com/bug-tracking/issue?issue=11355。你根本沒有在這裏發佈。無論如何,驚人的:) – s3v3n 2011-05-25 11:16:35

0

OK - 我花了一點,但現在我明白如何正確地創建陣列。

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); 

這將創建一個適當的發票。