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

时间:2018-12-09 16:19:39

标签: php mysql invoice

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

  1. 我从表格发票中下载所有已付款的发票,付款状态为OK。
  2. 我从invoice_order下载最高编号,我将其加+1,然后创建下一个编号,这表示付款= OK的那些人的发票编号
  3. 将新的发票编号保存在invoices / invoice_order表中
  4. 每次有新的已付款发票时都这样,所以给定月份具有连续性

您对如何进行这项工作有任何想法吗?

    $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>';  
        }          



    } 

1 个答案:

答案 0 :(得分:0)

这个问题有点难以理解,但是我想您希望发票一旦被支付要么连续order_number要么a)不管何时支付 OR b)重置数字每个月。

如果是A(每个发票的订单都是唯一的)

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;

如果是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;

在这里拨弄:https://www.db-fiddle.com/f/ccS723rK7vCjdJBdDihjj6/0