会计年度更改时自动重设发票编号

时间:2019-06-10 06:02:45

标签: php mysql

我有一个生成发票的脚本。我在数据库表发票中有一个字段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 = date(“ 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');

1 个答案:

答案 0 :(得分:1)

请考虑以下内容:

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    |
+------------+-----------------+---------------+