生成发票
我想每两周创建一张发票。一张发票可以包含 1 到 15 和 16 到 31 order_date
之间的 1 个或多个订单。有哪些方法可以做到这一点?
假设表设计为:
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
第一个解决方案:
每两周通过 Crob Job 运行一次。它扫描订单表(查找 1 到 15 或 16 到 31 之间的 order_date 且状态 = 1),然后添加到发票表中,然后更新 order.invoice_id
如果今天日期是 2010 年 6 月 11 日,那么它将在发票表和发票日期将为 2010 年 6 月 1 日。PHP 将在添加行之前检查发票日期,如果它已经存在,则会更新order.invoice_id 改为。
第二个解决方案:
从后端手动将 order.status 更改为 1,然后它将执行与“第一个解决方案”类似的功能(除了 Cron Job)
伪代码:
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)
哪个会更好或者您还有其他解决方案更好的解决方案?
I want to create an invoice of every 2 week. An invoice can have 1 or more orders between 1 to 15 and 16 to 31 order_date
. 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. 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
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.
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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的很喜欢 cron 来进行调度。它只做一件事,而且做得很好。
对于计划任务来说非常理想的另一个设计方面是幂等,这意味着您可以运行再次进行操作,不会造成任何损坏。
就您而言,这意味着您的更新/插入必须处理已经在该时间段内运行的情况。您想要这样做的原因首先是防御性的(以防意外重新运行)和稳健性(以防您需要重新运行它)
I really like cron for scheduling. 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)