简体   繁体   English

生成发票

[英]Generating Invoice

I want to create an invoice of every 2 week.我想创建每 2 周的发票。 An invoice can have 1 or more orders between 1 to 15 and 16 to 31 order_date .一张发票可以有 1 到 15 和 16 到 31 order_date之间的 1 个或多个订单。 What are the way doing that?这样做的方法是什么?

Let assume the table design is:假设表格设计为:

order table
- order_id (PK)
- user_id (FK)
- total
- status
- order_date
- invoice_id (FK, default is 0)

invoice table
- invoice_id (PK)
- invoice_date
- total (total of all orders that is linked to order.invoice_id)
- status (Paid, Unpaid, etc)

//order.invoice_id can have multiple same invoice id

First Solution:第一个解决方案:

Run via Crob Job every 2 week.每 2 周通过 Crob Job 运行。 It scan through the order table (find order_date between 1 to 15 or 16 to 31 and status = 1) and then add into invoice table and then update the order.invoice_id它扫描订单表(找到介于 1 到 15 或 16 到 31 之间且状态 = 1 的 order_date),然后添加到发票表中,然后更新 order.invoice_id

If today date is 11th June 2010 then it will insert a row in the invoice table and the invoice_date would be 01 June 2010. PHP will check the invoice_date before adding a row, if it already exist then it will update order.invoice_id instead.如果今天的日期是 2010 年 6 月 11 日,那么它将在发票表中插入一行,并且 invoice_date 将是 2010 年 6 月 1 日。PHP 将在添加行之前检查 invoice_date,如果它已经存在,那么它将更新 order.invoice_id。

Second Solution:第二种解决方案:

Change the order.status to 1 manually from the backend then it will do similar functionality as "First Solution" (apart from Cron Job)从后端手动将 order.status 更改为 1,然后它将执行与“第一个解决方案”类似的功能(除了 Cron 作业)

Pseudo code:伪代码:

NowDate = Date();

//Invoice Date can be 01 or 16 date current month
InvoiceDate = InvoiceDate(NowDate)

if (there is invoice for InvoiceDate) {
  invoiceNumber = getNumber(invoice)
} else {
  invoiceNumber = new Invoice(InvoiceDate)
}
new Order(invoiceNumber, orderitems)

Which will be better or do you have other better solution?哪个会更好,或者您有其他更好的解决方案?

I really like cron for scheduling.我真的很喜欢 cron 来安排时间。 It does one thing and it does it well.它只做一件事,而且做得很好。

The other design aspect that is very desirable for scheduled tasks is to be idempotent , which means you can run it again without doing any damage.计划任务非常需要的另一个设计方面是幂等的,这意味着您可以再次运行它而不会造成任何损坏。

In your case, this means your updates/inserts must handle having already been run for the time period.在您的情况下,这意味着您的更新/插入必须处理已经运行一段时间。 The reason you want this is firstly defensive (in case it's accidentally re-run) and robust (in case you need to re-run it)你想要这个的原因首先是防御性的(以防它意外地重新运行)和强大的(以防你需要重新运行它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM