繁体   English   中英

累积税MYSQL总和

[英]Cumulative Taxes MYSQL Sum

我需要在我的申请中支持累积税。 我允许我的商品支持多种税项(1或2)。 目前,我计算总税额的方法是将每个税百分比(例如4和8)相加,并收取12%的税。 但是对于累积税,您需要以(小计+税额1)*税额2%计算第二个税额。 如何在mysql中使用以下表结构在一个查询中执行此操作。 (我需要计算的是总税)

mysql> describe phppos_sales_items;
+--------------------+---------------+------+-----+---------+-------+
| Field              | Type          | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+-------+
| sale_id            | int(10)       | NO   | PRI | 0       |       |
| item_id            | int(10)       | NO   | PRI | 0       |       |
| description        | varchar(255)  | YES  |     | NULL    |       |
| serialnumber       | varchar(255)  | YES  |     | NULL    |       |
| line               | int(3)        | NO   | PRI | 0       |       |
| quantity_purchased | double(15,2)  | NO   |     | 0.00    |       |
| item_cost_price    | decimal(15,2) | NO   |     | NULL    |       |
| item_unit_price    | double(15,2)  | NO   |     | NULL    |       |
| discount_percent   | int(11)       | NO   |     | 0       |       |
+--------------------+---------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

mysql> describe phppos_sales_items_taxes;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| sale_id    | int(10)      | NO   | PRI | NULL    |       |
| item_id    | int(10)      | NO   | PRI | NULL    |       |
| line       | int(3)       | NO   | PRI | 0       |       |
| name       | varchar(255) | NO   | PRI | NULL    |       |
| percent    | double(15,2) | NO   | PRI | NULL    |       |
| cumulative | int(1)       | NO   |     | 0       |       |
+------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

这是我当前的查询,它没有使累积标志生效:

CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_items.sale_id, comment,payment_type, customer_id, employee_id, phppos_items.item_id, NULL as item_kit_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, category, SUM(percent) as item_tax_percent, discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_items.line as line, serialnumber, phppos_sales_items.description as description, ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total, ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit FROM phppos_sales_items INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id LEFT OUTER JOIN phppos_suppliers ON phppos_items.supplier_id=phppos_suppliers.person_id LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" GROUP BY sale_id, item_id, line) UNION ALL (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_item_kits.sale_id, comment,payment_type, customer_id, employee_id, NULL as item_id, phppos_item_kits.item_kit_id, '' as supplier_id, quantity_purchased, item_kit_cost_price, item_kit_unit_price, category, SUM(percent) as item_tax_percent, discount_percent, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_item_kits.line as line, '' as serialnumber, phppos_sales_item_kits.description as description, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) - (item_kit_cost_price*quantity_purchased) as profit FROM phppos_sales_item_kits INNER JOIN phppos_sales ON phppos_sales_item_kits.sale_id=phppos_sales.sale_id INNER JOIN phppos_item_kits ON phppos_sales_item_kits.item_kit_id=phppos_item_kits.item_kit_id LEFT OUTER JOIN phppos_sales_item_kits_taxes ON phppos_sales_item_kits.sale_id=phppos_sales_item_kits_taxes.sale_id and phppos_sales_item_kits.item_kit_id=phppos_sales_item_kits_taxes.item_kit_id and phppos_sales_item_kits.line=phppos_sales_item_kits_taxes.line WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" GROUP BY sale_id, item_kit_id, line) ORDER BY sale_id, line

OUTPUT:

+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+
| deleted | sale_date  | sale_id | comment | payment_type          | customer_id | employee_id | item_id | item_kit_id | supplier_id | quantity_purchased | item_cost_price | item_unit_price | category  | item_tax_percent | discount_percent | subtotal    | line | serialnumber | description        | total   | tax    | profit     |
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+
|       0 | 2011-07-28 |       1 |         | Cash: $1,416.00<br /> |        NULL |           1 |       1 |        NULL | NULL        |               1.00 |          800.00 |         1200.00 | Computers |            18.00 |                0 | 1200.000000 |    1 |              | Best Computer ever | 1416.00 | 216.00 | 400.000000 |
|       0 | 2011-07-28 |       2 |         | Cash: $2,832.00<br /> |        NULL |           1 |    NULL |           1 |             |               1.00 |         1600.00 |         2400.00 | Computers |            18.00 |                0 | 2400.000000 |    1 |              |                    | 2832.00 | 432.00 | 800.000000 |
|       0 | 2011-07-28 |       3 |         | Cash: $14.52<br />    |        NULL |           1 |       4 |        NULL | NULL        |               1.00 |           10.00 |           12.00 | Test      |            20.00 |                0 |   12.000000 |    1 |              |                    |   14.40 |   2.40 |   2.000000 |
|       0 | 2011-07-28 |       4 |         | Cash: $2,904.00<br /> |        NULL |           1 |    NULL |           1 |             |               1.00 |         1600.00 |         2400.00 | Computers |            20.00 |                0 | 2400.000000 |    1 |              |                    | 2880.00 | 480.00 | 800.000000 |
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+

您可以使用数据透视技术在2个不同的列中获取累积百分比和正常百分比

SELECT a.sale_id, a.item_id, a.line, 
IFNULL(MAX(CASE WHEN b.cumulative = 1 THEN b.percent END),0) as cum_pcnt,
IFNULL(MAX(CASE WHEN b.cumulative != 1 THEN b.percent END),0) as norm_pcnt
FROM phppos_sales_items a
INNER JOIN phppos_sales_items_taxes b ON 
(b.sale_id = a.sale_id AND b.item_id = a.item_id AND b.line = a.line)
GROUP BY a.sale_id, a.item_id, a.line

现在,您可以将上面的查询用作派生表,并将其phppos_sales_items_taxes ,而不是phppos_sales_items_taxes表(以下查询只是为了演示此方法,我肯定错失了如何计算成本并将百分比转换为乘数的要点):

SELECT ..., SUM([your cost calculation]*sales_taxes.norm_pcnt) as tax1,
SUM([your cost calculation]*sales_taxes.norm_pcnt*cum_pcnt) as tax2
FROM  ....
INNER[or LEFT, whatever makes sense] JOIN 
([the query above goes here]) sales_taxes 

另外,我强烈建议重构您的表结构-这些复合PK在您的情况下没有任何意义; 如果您需要字段的组合是唯一的,则不必使它们成为主键的一部分,而应使用UNIQUE索引/约束。

暂无
暂无

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

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