Мне нужно поддерживать накопительные налоги в моем заявлении.Я разрешаю мои товары поддерживать несколько налогов (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
ВЫХОД:
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+
| 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 |
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+