简体   繁体   English

如何使用PHP和MySQL对循环计费/发票系统进行编程

[英]How to program a recurring billing/invoice system using PHP and MySQL

I have already programmed a basic invoicing system using PHP/MySQL which has a table structure as follows; 我已经使用PHP / MySQL编写了一个基本的发票系统,该系统的表结构如下:

Invoice table; 发票表; invoice_id, invoice_date, customer_id, etc Invoice line table; invoice_id,invoice_date,customer_id等发票行表; invoice_line_id, invoice_id, quantity, price, description, etc invoice_line_id,invoice_id,数量,价格,说明等

I need the system to automatically generate future invoices at set intervals (for example every 1 week or every 2 months, etc). 我需要系统按设定的时间间隔(例如每1周或每2个月等)自动生成以后的发票。 I was thinking of creating a new table as follows; 我当时想创建一个新表,如下所示;

Invoice schedule table; 发票时间表表; invoice_schedule_id, invoice_id, interval (eg 1), interval_unit (months), start date, next_processing_date invoice_schedule_id,invoice_id,间隔(例如1),interval_unit(月),开始日期,next_processing_date

My idea was to then setup a cron job which would execute a PHP file once a day. 我的想法是设置一个cron作业,该作业每天执行一次PHP文件。 The PHP file would then generate an invoice when the next_processing_date matched today's date and update the next_processing_date in the database. 然后,当next_processing_date与今天的日期匹配时,PHP文件将生成发票,并更新数据库中的next_processing_date。 I'm comfortable on how to achieve this but what I'm stuck with is how to actually insert the new invoice into the table/database. 我对如何实现此目标很满意,但我坚持的是如何将新发票实际插入表/数据库中。 Does MySQL have any type of 'copy row' feature as the new invoice would be identical to the original one except for the invoice_date having to be updated. MySQL是否具有任何类型的“复制行”功能,因为除了需要更新invoice_date之外,新发票与原始发票相同。

Cron sounds good. Cron听起来不错。 (Also it is worth to mention the MySQL Event Scheduler, but again I would go for a Cronjob) (也值得一提的是MySQL Event Scheduler,但我还是会去参加Cronjob)

A copy would be something like this SQLFIDDLE : 复制将类似于以下SQLFIDDLE

create table t ( id int, d date );

insert into t values( 0, CURDATE() );
insert into t values( 2, CURDATE() );
insert into t values( 1, CURDATE() );

insert into t ( select id+1,curdate() from t order by id desc limit 1 );

Above example is to copy the latest order as a copy, of course you could put a where clause where id=1 or what id your reference order is. 上面的示例是复制最新的订单作为副本,当然您可以在where = 1或您的参考订单的id处放置where子句。

BigScar's reference of "How to copy a row and insert in same table with a autoincrement field in MySQL?" BigScar的参考资料:“如何在MySQL中使用自动增量字段复制一行并插入同一表中?” seems to solve your copy-insert problem. 似乎可以解决您的复制插入问题。

However, since you are mostly doing a specific group of DB queries, instead of cronjobs, you may use MySQL events . 但是,由于您主要是在执行一组特定的数据库查询,而不是cronjobs,因此可以使用MySQL events If your MySQL version supports them (check in phpmyadmin: select a DB and look to the top menu bar, you can create them there without even have to know the syntax), it's a good practical alternative. 如果您的MySQL版本支持它们(请检查phpmyadmin:选择一个数据库并查看顶部菜单栏,您甚至可以在不知道语法的情况下在此处创建它们),这是一个很好的实用选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM