2012-08-15 206 views
3

我使用这个代码,以创建在Magento发票:Magento:创建发票号码=订单号的发票?

$invoiceId = Mage::getModel('sales/order_invoice_api')->create($order->getIncrementId(), array()); 

这会自动分配一个编号(increment_id)到发票,例如100016050.我想创建发票其中的发票increment_id =订单的increment_id。

如何做到这一点?

谢谢!

回答

3

这需要编写一个完整的自定义模块,所以我将解释一些基础知识。

在Magento的,像orderinvoicecreditmemoshipping每个实体都有每store_id自己的独立的和数组。

这些号码组可以在表eav_entity_store定义:

entity_store_id entity_type_id store_id increment_prefix increment_last_id 
       1    5   1     1   100000000 
       2    6   1     2   200000000 
       3    7   1     3   300000000 
       4    8   1     4   400000000 

要知道哪些entity_type_id是指实体,请检查您eav_entity_type表:

entity_type_id entity_type_code entity_model 
      5 order    sales/order 
      6 invoice   sales/order_invoice 
      7 creditmemo  sales/order_creditmemo 
      8 shipment   sales/order_shipment 

请注意,您的entity_type_id的可能(或可能不)与此不同。

Magento通常会将每个实体增加1,参见eav_entity_type.increment_per_store

这种情况发生在创建此实体的时间。但是,order的创建并不总是意味着创建invoice也是如此。例如,用户可以在下订单时取消付款,或者付款不会被付款提供商授权,因此不会创建invoice

这可能导致间隙,例如, order已在100000005,而invoice仍在200000002

您的代码需要以保持orderinvoice同步的方式管理此间隙。例如,您可以为sales_order_invoice_save_before事件创建一个观察者。

app/code/local/Mycompany/Mymodule/etc/config.xml

<config> 
    <modules> 
     <Mycompany_Mymodule> 
      <version>0.1.0</version> 
     </Mycompany_Mymodule> 
    </modules> 
    <global> 
     <models> 
      <mymodule> 
       <class>Mycompany_Mymodule_Model</class> 
      </mymodule> 
     </models> 
     <events> 
      <sales_order_invoice_save_before> 
       <observers> 
        <myobserver> 
         <type>singleton</type> 
         <class>mymodule/observer</class> 
         <method>salesOrderInvoiceSaveBefore</method> 
        </myobserver> 
       </observers> 
      </sales_order_invoice_save_before> 
     </events> 
    </global> 
</config> 

app/code/local/Mycompany/Mymodule/Model/Observer.php

class Mycompany_Mymodule_Model_Observer 
{ 

    /** 
    * Hook to observe `sales_order_invoice_save_before` event 
    * 
    * @param Varien_Event_Observer $oObserver 
    */ 

    public function salesOrderInvoiceSaveBefore($oObserver) 
    { 
     $oInvoice = $oObserver->getInvoice(); 
    } 

} 

Magento的传递invoice对象到此观测器,该invoice对象被保存之前。这将允许您使用此invoice对象检索相关的order对象(并因此检索orderincrement_id)。

检索了order.increment_id后,您可以搜索invoice以确定是否存在具有该order.increment_idinvoice

如果它尚不存在,则可以在离开观察者之前将order.increment_id的值指定为invoice.increment_id并完成。

请注意,这些只是基础知识。它还有一些缺陷。

例如,每个订单案例的多个和/或重复发票尚未处理。

例如,在一些国家,财政/税务部门要求发票号码不断增加。它必须是1, 2, 3, 4, 5,但1, 2, 3, 4 is missing, 5是不可接受的。使用上述技术,这种差距仍然可能发生,因为用户付款取消等。

但是,这应该让你走上正轨。