开发者

Help calculating tax and discounts

I'm having problems correctly calculating tax and discounts per item, mainly because I'm not certain what the order should be. I can do this on a calculator without any problems but writing the SQL is a different story. The total column was done by a friend and works as expected. Based on his total column, I've added the taxtotal column which seems to give the correct result. I am still having an issue with the discounts though.

There are 2 discount columns. One is an i开发者_Go百科nitial discount that can be set in the inventory and a second discount that can be applied by the clerk. The columns for the discounts are op.discount and p.discount. There is no separate table for discounts and are just columns within the two joins.

I'd like to make sure that my taxtotal column is correct as well so if someone sees an issue or a better way of writing it, I'd be appreciative of any suggestions.

SELECT
    p.tax_state as tax_state,
    p.tax_fed as tax_fed,
    p.price AS price,
    op.quantity AS quantity,
    op.discount AS discount,
    (p.tax_state + p.tax_fed) AS tax,
    ( ( p.tax_state + p.tax_fed ) / 100 ) * ( p.price * op.quantity - ( p.price * op.quantity * op.discount + p.discount /100 ) ) AS taxtotal,
    (p.price * (1 + ((p.tax_state + p.tax_fed) / 100)) * (1.0 - op.discount) * op.quantity) AS total
FROM pos_order o
JOIN pos_item_order op ON op.order_id = o.order_id
JOIN inv_item p ON p.item_id = op.item_id


if you can create function do this


SELECT
    p.tax_state as tax_state,
    p.tax_fed as tax_fed,
    p.price AS price,
    op.quantity AS quantity,
    op.discount AS discount,
    (p.tax_state + p.tax_fed) AS tax,
    taxtotal(p.tax_state , p.tax_fed, p.price ,op.quantity, op.discount ) AS taxtotal
    valueTotal(p.tax_state , p.tax_fed, p.price ,op.quantity, op.discount ) AS total
FROM pos_item_order op 
JOIN inv_item p ON p.item_id = op.item_id


create this function


DROP FUNCTION IF EXISTS `taxtotal`;
CREATE DEFINER=`root`@`localhost` FUNCTION `taxtotal`(`tax_state` double,`tax_fed` double,`price` double,`quantity` INT,`discount` double) RETURNS double
BEGIN
    RETURN ( ( `tax_state` + `tax_fed`) / 100 ) * ( `price` * `quantity` - ( `price` * `quantity` * `discount`/100 ) );
END
;;
DELIMITER ;
DROP FUNCTION IF EXISTS `valueTotal`;
CREATE DEFINER=`root`@`localhost` FUNCTION `valueTotal`(`tax_state` double,`tax_fed` double,`price` double,`quantity` INT,`discount` double) RETURNS double
BEGIN
    RETURN (p.price * (1 + ((p.tax_state + p.tax_fed) / 100)) * (1.0 - op.discount) * op.quantity);
END
;;
DELIMITER ;


I think you mean

( p.price * op.quantity * ((op.discount + p.discount) / 100 ))

with extra braces because multiply has a higher precedence than add - assuming these are both percentage discounts?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜