我想创建每 2 周的发票。一张发票可以有 1 到 15 和 16 到 31 之间的 1 个或多个订单order_date
。这样做的方法是什么?
假设表格设计为:
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
第一个解决方案:
每 2 周通过 Crob Job 运行。它扫描订单表(找到介于 1 到 15 或 16 到 31 之间且状态 = 1 的 order_date),然后添加到发票表中,然后更新 order.invoice_id
如果今天的日期是 2010 年 6 月 11 日,那么它将在发票表中插入一行,并且 invoice_date 将是 2010 年 6 月 1 日。PHP 将在添加行之前检查 invoice_date,如果它已经存在,那么它将更新 order.invoice_id。
第二种解决方案:
从后端手动将 order.status 更改为 1,然后它将执行与“第一个解决方案”类似的功能(除了 Cron 作业)
伪代码:
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)
哪个会更好,或者您有其他更好的解决方案?