生成发票&跟踪

时间:2012-02-16 10:07:36

标签: mysql database database-design billing invoice

公司将在每月的1日和16日收到发票。 (它将每两周通过Cron Job运行。它扫描订单表,然后添加到'invoice'表中。还有替代方案吗?)

orders表中有客户订单列表,它还指明了它所属的公司(orders.company_id

invoice表计算来自orders表的订单总成本。

我想弄清楚如何设计合理的发票跟踪。有时公司必须向我发送费用,或者有时我向他们发送费用(invoice.amount

我需要使用以下内容跟踪发票:

  • 当公司向我发送金额时
  • 我何时将金额发送给公司
  • 从公司收到多少金额
  • 我向公司发送了多少金额
  • 我收到了全部金额(如果没有,我需要在Db上更新什么?)
  • 发票状态(已发票,已取消,已收金额,已发送金额)

以下是我提出的数据库设计:

公司表

mysql> select * from company;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Company A |
|  2 | Company B |
+----+-----------+

客户可以从我的网站中选择一家公司。

订单表

mysql> select * from orders;
+----+---------+------------+------------+---------------------+-----------+
| id | user_id | company_id | total_cost | order_date          | status_id |
+----+---------+------------+------------+---------------------+-----------+
|  1 |       5 |          2 |      25.00 | 2012-02-03 23:30:24 |         1 |
|  2 |       7 |          2 |      30.00 | 2012-02-13 18:06:12 |         1 |
+----+---------+------------+------------+---------------------+-----------+

两位客户已从公司B orders.company_id = 2)订购了产品。我知道订单字段不够,只是简化了。

orders_products表

mysql> select * from orders_products;
+----+----------+------------+--------------+-------+
| id | order_id | product_id | product_name | cost  |
+----+----------+------------+--------------+-------+
|  1 |        1 |         34 | Chair        | 10.00 |
|  2 |        1 |         25 | TV           | 10.00 |
|  3 |        1 |         27 | Desk         |  2.50 |
|  4 |        1 |         36 | Laptop       |  2.50 |
|  5 |        2 |         75 | PHP Book     | 25.00 |
|  6 |        2 |         74 | MySQL Book   |  5.00 |
+----+----------+------------+--------------+-------+

客户订购的产品清单。

发票表

mysql> select * from invoice;
+----+------------+------------+---------------------+--------+-----------+
| id | company_id | invoice_no | invoice_date        | amount | status_id |
+----+------------+------------+---------------------+--------+-----------+
|  7 |          2 |        123 | 2012-02-16 23:59:59 |  55.00 |         1 |
+----+------------+------------+---------------------+--------+-----------+

这是我非常坚持发票表设计的地方。我不确定应该怎么做。发票将每两周生成一次。从结果示例invoice.amount为55.00,因为它是根据orders.company_id = 2

计算的

如果invoice.amount是-50.00(减号),则表示公司需要向我发送费用金额。

如果invoice.amount是50.00,则表示我需要向公司发送费用。

status_id可以是:(1)已发票,(2)已取消,(3)已完成

我是否需要在invoice_id表格中添加orders字段?将行插入“发票”表后,更新orders.invoice_id字段。

invoice_payment表

mysql> select * from invoice_payment;
+----+------------+-----------------+-------------+---------------------+---------------------+
| id | invoice_id | amount_received | amount_sent | date_received       | date_sent           |
+----+------------+-----------------+-------------+---------------------+---------------------+
|  1 |          1 |            0.00 |       55.00 | 0000-00-00 00:00:00 | 2012-02-18 22:20:53 |
+----+------------+-----------------+-------------+---------------------+---------------------+

这是我可以跟踪和更新交易的地方..付款将通过BACS进行。

这是好桌子设计还是我需要改进什么?我应该添加哪些字段和表格?

如果已生成发票,之后我需要在orders_productsorders表中进行更改 - 是否应重新计算invoice.amount字段? (我将使用PHP / MySQL)。

SQL转储

CREATE TABLE IF NOT EXISTS `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `company` (`id`, `name`) VALUES
(1, 'Company A'),
(2, 'Company B');

CREATE TABLE IF NOT EXISTS `invoice` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_id` int(11) NOT NULL,
  `invoice_no` int(11) NOT NULL,
  `invoice_date` datetime NOT NULL,
  `amount` decimal(6,2) NOT NULL,
  `status_id` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


INSERT INTO `invoice` (`id`, `company_id`, `invoice_no`, `invoice_date`, `amount`, `status_id`) VALUES
(7, 2, 123, '2012-02-16 23:59:59', '55.00', 1);


CREATE TABLE IF NOT EXISTS `invoice_payment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `invoice_id` int(11) NOT NULL,
  `amount_received` decimal(6,2) NOT NULL,
  `amount_sent` decimal(6,2) NOT NULL,
  `date_received` datetime NOT NULL,
  `date_sent` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `invoice_payment` (`id`, `invoice_id`, `amount_received`, `amount_sent`, `date_received`, `date_sent`) VALUES
(1, 1, '0.00', '55.00', '0000-00-00 00:00:00', '2012-02-18 22:20:53');


CREATE TABLE IF NOT EXISTS `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `company_id` int(11) NOT NULL,
  `total_cost` decimal(6,2) NOT NULL,
  `order_date` datetime NOT NULL,
  `status_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `orders` (`id`, `user_id`, `company_id`, `total_cost`, `order_date`, `status_id`) VALUES
(1, 5, 2, '25.00', '2012-02-03 23:30:24', 1),
(2, 7, 2, '30.00', '2012-02-13 18:06:12', 1);


CREATE TABLE IF NOT EXISTS `orders_products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `product_name` varchar(100) NOT NULL,
  `cost` decimal(6,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `orders_products` (`id`, `order_id`, `product_id`, `product_name`, `cost`) VALUES
(1, 1, 34, 'Chair', '10.00'),
(2, 1, 25, 'TV', '10.00'),
(3, 1, 27, 'Desk', '2.50'),
(4, 1, 36, 'Laptop', '2.50'),
(5, 2, 75, 'PHP Book', '25.00'),
(6, 2, 74, 'MySQL Book', '5.00');

您可以随意更新/添加表格以回答此处。

由于

1 个答案:

答案 0 :(得分:0)

查看我的Gemini加载项 - SimplyFi。它允许您相应地标记发票,可以在客户生成时自动通过电子邮件发送给客户,可以记录付款并发送未收到付款的提醒(报表),并且可以使用完整的基于REST的API集成到您的系统中。也可能能够从其功能的重复计费中受益。

如果您提到负发票金额,那么这些金额实际上是“信用票据”(根据我对您的帖子的理解)。一般情况下,您不应在发票发给客户后自行更改发票 - 如果您需要修改金额(即:加上或减去),那么您应该发出新发票(增加金额)或减去信用额度的减去金额。

此外,如果他们要在几周内收到新发票,只需跟踪他们的帐户余额,并且只在必要时发出发票或信用票据,我建议您不要发回客户的钱。转移资金需要花钱,如果没有必要,你不需要这么做。只差我2美分