gpt4 book ai didi

php - 会计年度更改时自动重置发票编号

转载 作者:行者123 更新时间:2023-11-29 02:39:09 25 4
gpt4 key购买 nike

我有一个生成发票的脚本。我在 db 表 invoice 中有一个 invoice_no 字段(格式:INV-yyyymm-0001,这里 0001 应该为每张发票递增直到下一个财政年度),它应该在财政年度更改后重置为 001。例如:如果发票编号为 INV-201903-2111 ,则在 2019 年 3 月 31 日之后,它应该自动更改为 INV-201904-0001 而不是 INV-201904-2112

我已将会计年度的开始和结束存储在包含 Fyear、fsYear、feYear 的表中。每当我生成新发票时,它都必须使用 Fyear 检查当前年份,然后检查开始日期和结束日期,如果当前日期包含 fsYear 和 feYear,则它必须重置为 001,然后递增到该财政年度结束。我究竟该如何实现?

这是表格

INSERT INTO `fyear` (`id`, `fyYear`, `fyStart`, `fyEnd`) VALUES
(1, 2019, '2019-04-01', '2020-03-31'),
(2, 2020, '2020-04-01', '2021-03-31');

我尝试创建一个函数来获取开始和结束日期$cdate = date('Y-m-d');$cyear = 日期("Y");

function GetFdate($Fyear, $Fstdate, $feddate)
{
$m1 = mysqli_query($con, "SELECT * FROM fyear WHERE fyYear='".$cyear."'");
$m2 = mysqli_fetch_array($m1);

$sYear = $m2['fyStart'];
$eYear = $m2['fyEnd'];
}

但不知道如何递增发票编号然后重置。

这是我的架构:

CREATE TABLE `sales_invoice` (
`invoice_id` int(10) NOT NULL,
`invoice_no` varchar(50) NOT NULL,
`order_id` varchar(50) NOT NULL,
`customer` int(10) NOT NULL,
`contact_person` int(10) NOT NULL,
`validity` int(10) NOT NULL,
`payment` varchar(100) NOT NULL,
`date_invoiced` date NOT NULL,
`status` varchar(20) NOT NULL,
`user_id` int(20) NOT NULL,
`shipping_address` varchar(100) NOT NULL,
`billing_address` varchar(100) NOT NULL,
`reference` varchar(255) NOT NULL,
`is_complete` varchar(10) NOT NULL DEFAULT 'No'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `sales_invoice`
ADD PRIMARY KEY (`invoice_id`);
ALTER TABLE `sales_invoice`
MODIFY `invoice_id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

INSERT INTO `sales_invoice` (`invoice_id`, `invoice_no`, `order_id`, `customer`, `contact_person`, `validity`, `payment`, `date_invoiced`, `status`, `user_id`, `shipping_address`, `billing_address`, `reference`, `is_complete`) VALUES
(1, 'INV-201903-2110', 'SO006', 91, 6, 5, 'Immediate', '2019-03-19', 'Paid', 1, '1', 'Company123', 'Webmail', 'No'),
(2, 'INV-201903-2111', 'SO007', 136, 4, 0, 'Immediate', '2019-03-30', 'Open', 1, '1', 'Company456', 'Google mail', 'No');

最佳答案

考虑以下几点:

DROP TABLE IF EXISTS sales_invoice;

CREATE TABLE sales_invoice
(invoice_id SERIAL PRIMARY KEY
,invoice_no VARCHAR(50) NOT NULL UNIQUE
,date_invoiced date NOT NULL
);

SET @dt = CURDATE();
-- SET @dt = '2019-03-15';


INSERT INTO sales_invoice (invoice_no,date_invoiced)
SELECT CONCAT_WS( '-'
, 'INV'
, DATE_FORMAT(@dt,'%Y%m')
, COALESCE(LPAD(
CASE WHEN @dt > DATE_FORMAT(@dt,'%Y-04-01')
THEN SUM(date_invoiced > DATE_FORMAT(@dt,'%Y-04-01'))
ELSE SUM(date_invoiced BETWEEN DATE_FORMAT(@dt,'%Y-04-01')-INTERVAL 1 YEAR AND DATE_FORMAT(@dt,'%Y-04-01'))
END +1,4,0
),LPAD(1,4,0))
)
, @dt FROM sales_invoice;

SELECT * FROM sales_invoice;
+------------+-----------------+---------------+
| invoice_id | invoice_no | date_invoiced |
+------------+-----------------+---------------+
| 1 | INV-201906-0001 | 2019-06-10 |
+------------+-----------------+---------------+

关于php - 会计年度更改时自动重置发票编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com