gpt4 book ai didi

magento-1.4 - Magento : Create an invoice with invoice number = order number?

转载 作者:行者123 更新时间:2023-12-01 01:16:13 26 4
gpt4 key购买 nike

我正在使用此代码在 Magento 中创建发票:

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

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

怎么可能呢?

谢谢!

最佳答案

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

在 Magento 中,order 等实体, invoice , creditmemoshipping每个 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 table :
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 通常将每个实体加一,见 eav_entity_type.increment_per_store .

这发生在创建此类实体时。但是,创建 order并不总是意味着 invoice因为它也会被创造。例如,用户可以在下订单时取消支付,否则支付将不会被支付提供商授权,所以没有 invoice将被创建。

这可能会导致差距,例如 order已经在 100000005 , 而 invoice还在 200000002 .

您的代码需要以保持 order 的方式管理此差距。和 invoice同步中。

为此,您可以为 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 通过 invoiceinvoice 之前反对该观察者对象被保存。这将允许您检索相关的 order对象(因此 orderincrement_id )使用此 invoice目的。

检索到 order.increment_id你可以搜索 invoice s 找出是否 invoice用那个 order.increment_id已经存在。

如果它还不存在,您可以分配 order.increment_id 的值至 invoice.increment_id在离开观察者之前完成。

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

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

例如,在某些国家/地区,财政/税务当局要求发票数量不断增加。必须是 1, 2, 3, 4, 5 , 但是 1, 2, 3, 4 is missing, 5是不能接受的。使用上述技术,由于用户取消付款等原因,仍然可能发生这种差距。

但是,这应该让您走上正确的轨道。

关于magento-1.4 - Magento : Create an invoice with invoice number = order number?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11965721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com