我有一张名为发票的表:
`si_invoices` (
`id` int(10) ,
`biller_id` int(10) ,
`customer_id` int(10) ,
`type_id` int(10) ,
`inv_tax_id` int(10) ,
`date` date ,
`unreg_customer` tinyint(1) ,
`discount` decimal(10,2) ,
`discount_type` tinyint(1)
)
每张发票都包含存储在invoice_items表中的项目:
`si_invoice_items` (
`id` int(10) ,
`invoice_id` int(10) ,
`quantity` int(10) ,
`product_id` int(10) ,
`warehouse_id` int(10) ,
`unit_price` decimal(25,2) ,
`total` decimal(25,2) ,
`description` text
) ;
和税表
`si_tax` (
`tax_id` int(11),
`tax_description` varchar(50) ,
`tax_percentage` decimal(25,6) ,
`type` varchar(1),
`tax_enabled` varchar(1)
) ;
这是我想做的事
第1步:获取发票的sum_total项目的发票项目
第2步:计算折扣,在发票表中我有一个discount_type字段:
如果它等于0,那么就没有折扣了
如果它等于1,折扣价值将存储在折扣栏中
如果它等于2,折扣是sum_total的百分比
第3步:根据inv_tax_id计算税金
根据税号,我会查看税表,获取tax_percentage并乘以(sum_total - discount)
简而言之就是等式
$ gross_total = $ sum_total - $ disount + tax
答案 0 :(得分:0)
不确定为什么要在MySQL中执行此操作,但这是查询:
SELECT (SELECT `tax_percentage` FROM `si_tax` WHERE `tax_id` = `si_invoices`.`inv_tax_id`) * (`sum_total` - CASE `discount_type` WHEN 1 THEN `discount` WHEN 2 THEN `sum_total` * `discount` / 100 ELSE 0 END) AS `gross`
FROM `si_invoices`
JOIN (SELECT SUM(`total`) AS `sum_total` FROM `si_invoice_items` WHERE `invoice_id` = `si_invoices`.`id`) AS `t`
WHERE `id` = ?
答案 1 :(得分:0)
<强>最后!!!!! 强> 我得到了它的工作
这是查询:
SELECT sum( iv.inv_total - iv.inv_discount + iv.taxes ) FROM
(
SELECT
(SELECT sum(ii.total) FROM si_invoice_items ii
where ii.invoice_id = v.id) as inv_total ,
(
SELECT
CASE v.discount_type
WHEN 1 THEN v.discount
WHEN 2 THEN (v.discount / 100) * ( SELECT inv_total )
ELSE 0
END
) AS inv_discount,
(SELECT
CASE t.type
WHEN '$' THEN t.tax_percentage
WHEN '%' THEN ( t.tax_percentage / 100 ) * (SELECT inv_total - inv_discount)
ELSE 0
END
FROM si_tax as t
WHERE t.tax_id = inv_tax_id
) AS taxes
FROM
si_invoices v
) iv
优雅,它的美丽不是她......:D
感谢 Arnie 感谢您的帮助, Mike Pelley 如果您给予积极的评价或一些帮助,而不是扮演“男人的角色”会更好法律“