自动递增发票编号,每月重置为零

Automatically incrementing invoice numbers, resetting to zero each month

我有这个问题,我不知道如何创建发票编号。我希望它像这样工作:

  1. 我从table张发票中下载所有已支付的发票,付款状态为OK的。
  2. 我从 invoice_order 下载了最大的数字,我给它加上 +1 并创建下一个数字,这意味着付款=OK
  3. 的人的发票编号
  4. 在 invoices/invoice_order table
  5. 中保存新的发票编号
  6. 每次有新的已付发票时都这样,所以在给定的月份有连续性

你有什么想法让这个工作发挥作用吗?

    $year = date('Y'); 
    $month = date('m');
    $curent_date = date('Y-m-d');           

    $ask = $conn->createCommand("SELECT * FROM invoices WHERE payment = 'OK' "
            . "AND invoice_month=$month AND invoice_year=$year");
    $data = $ask->query();  

    while (($row = $data->read()) !== FALSE) { 
        if($row['invoice_suffix'] != 'KOR') {

                echo $row['ID'].'<br>';  
        }          



    } 

这个问题有点难以理解,但我认为您希望发票在付款后收到连续的 order_number 或者 a) 无论何时付款 b ) 每个月都会重置数字。

如果 A(每个 invoice_order 都是唯一的)

UPDATE invoices t1,
       (SELECT MAX(invoice_order)+1 as x FROM invoices WHERE payment = 'OK') t2
SET t1.payment = 'OK', t1.invoice_order = t2.x
WHERE t1.id = INVOICE_TO_BE_UPDATED;

if B(每个月都有一组新的重复订单号)

UPDATE invoices t1,
       (SELECT month, year, MAX(invoice_order)+1 as x FROM invoices WHERE payment = 'OK' GROUP BY month, year) t2
SET t1.payment = 'OK', t1.invoice_order = t2.x
WHERE t1.id = INVOICE_TO_BE_UPDATED AND t1.month = t2.month AND t1.year = t2.year;

Fiddle 这里:https://www.db-fiddle.com/f/ccS723rK7vCjdJBdDihjj6/0