-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #369 from dmitry-sinina/1.7.8
fix cdr price rouding
- Loading branch information
Showing
3 changed files
with
207 additions
and
29 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
db/secondbase/migrate/20180911180345_fix_price_rounding.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
class FixPriceRounding < ActiveRecord::Migration[5.1] | ||
def up | ||
execute %q{ | ||
CREATE or replace FUNCTION switch.vendor_price_round(i_config sys.config, i_amount numeric) RETURNS numeric | ||
LANGUAGE plpgsql COST 10 | ||
AS $$ | ||
DECLARE | ||
BEGIN | ||
case i_config.vendor_amount_round_mode_id | ||
when 1 then -- disable rounding | ||
return i_amount; | ||
when 2 then --always up | ||
return trunc(i_amount, i_config.vendor_amount_round_precision) + | ||
(mod(i_amount::numeric, power(10,-i_config.vendor_amount_round_precision)::numeric)>0)::int*power(10,-i_config.vendor_amount_round_precision); | ||
when 3 then --always down | ||
return trunc(i_amount, i_config.vendor_amount_round_precision); | ||
when 4 then -- math | ||
return round(i_amount, i_config.vendor_amount_round_precision); | ||
else -- fallback to math rules | ||
return round(i_amount, i_config.vendor_amount_round_precision); | ||
end case; | ||
END; | ||
$$; | ||
CREATE or replace FUNCTION switch.customer_price_round(i_config sys.config, i_amount numeric) RETURNS numeric | ||
LANGUAGE plpgsql COST 10 | ||
AS $$ | ||
DECLARE | ||
BEGIN | ||
case i_config.customer_amount_round_mode_id | ||
when 1 then -- disable rounding | ||
return i_amount; | ||
when 2 then --always up | ||
return trunc(i_amount, i_config.customer_amount_round_precision) + | ||
(mod(i_amount::numeric, power(10,-i_config.customer_amount_round_precision)::numeric)>0)::int*power(10,-i_config.customer_amount_round_precision); | ||
when 3 then --always down | ||
return trunc(i_amount, i_config.customer_amount_round_precision); | ||
when 4 then -- math | ||
return round(i_amount, i_config.customer_amount_round_precision); | ||
else -- fallback to math rules | ||
return round(i_amount, i_config.customer_amount_round_precision); | ||
end case; | ||
END; | ||
$$; | ||
} | ||
end | ||
|
||
def down | ||
execute %q{ | ||
CREATE or replace FUNCTION switch.vendor_price_round(i_config sys.config, i_amount numeric) RETURNS numeric | ||
LANGUAGE plpgsql COST 10 | ||
AS $$ | ||
DECLARE | ||
BEGIN | ||
case i_config.vendor_amount_round_mode_id | ||
when 1 then -- disable rounding | ||
return i_amount; | ||
when 2 then --always up | ||
return trunc(i_amount, i_config.vendor_amount_round_precision) + power(10 , - i_config.vendor_amount_round_precision); | ||
when 3 then --always down | ||
return trunc(i_amount, i_config.vendor_amount_round_precision); | ||
when 4 then -- math | ||
return round(i_amount, i_config.vendor_amount_round_precision); | ||
else -- fallback to math rules | ||
return round(i_amount, i_config.vendor_amount_round_precision); | ||
end case; | ||
END; | ||
$$; | ||
CREATE or replace FUNCTION switch.customer_price_round(i_config sys.config, i_amount numeric) RETURNS numeric | ||
LANGUAGE plpgsql COST 10 | ||
AS $$ | ||
DECLARE | ||
BEGIN | ||
case i_config.customer_amount_round_mode_id | ||
when 1 then -- disable rounding | ||
return i_amount; | ||
when 2 then --always up | ||
return trunc(i_amount, i_config.customer_amount_round_precision) + power(10 , - i_config.customer_amount_round_precision); | ||
when 3 then --always down | ||
return trunc(i_amount, i_config.customer_amount_round_precision); | ||
when 4 then -- math | ||
return round(i_amount, i_config.customer_amount_round_precision); | ||
else -- fallback to math rules | ||
return round(i_amount, i_config.customer_amount_round_precision); | ||
end case; | ||
END; | ||
$$; | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters