diff --git a/app/lib/taxes/electricity.rb b/app/lib/taxes/electricity.rb new file mode 100644 index 0000000..1f45fdc --- /dev/null +++ b/app/lib/taxes/electricity.rb @@ -0,0 +1,24 @@ +module Taxes + class Electricity + def self.for(time_or_date) + new(time_or_date).for + end + + def initialize(time_or_date) + @date = time_or_date.respond_to?(:to_date) ? time_or_date.to_date : time_or_date + end + + def for + case @date + when Date.new(2023, 1, 1)..Date.new(2023, 12, 31) + 0.005 + when Date.new(2024, 1, 1)..Date.new(2024, 3, 31) + 0.025 + when Date.new(2024, 4, 1)..Date.new(2024, 6, 30) + 0.038 + else + 0.0 + end + end + end +end diff --git a/app/lib/taxes/value_add.rb b/app/lib/taxes/value_add.rb new file mode 100644 index 0000000..03712f8 --- /dev/null +++ b/app/lib/taxes/value_add.rb @@ -0,0 +1,41 @@ +module Taxes + class ValueAdd + def self.for(time_or_date) + new(time_or_date).for + end + + def initialize(time_or_date) + @date = time_or_date.respond_to?(:to_date) ? time_or_date.to_date : time_or_date + end + + def for + case Region.current.key.to_sym + when :pen, :bal + for_pb + when :can + for_can + when :ceu, :mel + for_cm + end + end + + private + + def for_pb + case @date + when Date.new(2023, 1, 1)..Date.new(2023, 12, 31) + 0.05 + else + 0.1 + end + end + + def for_can + 0.0 + end + + def for_cm + 0.01 + end + end +end diff --git a/test/lib/taxes/electricity_test.rb b/test/lib/taxes/electricity_test.rb new file mode 100644 index 0000000..422f1b7 --- /dev/null +++ b/test/lib/taxes/electricity_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +module Taxes + class ElectricityTest < ActiveSupport::TestCase + test "from 2023-01-01 to 2023-12-31" do + assert_in_delta 0.005, Electricity.for(Date.new(2023, 1, 1)) + assert_in_delta 0.005, Electricity.for(Date.new(2023, 12, 31)) + end + + test "from 2024-01-01 to 2024-03-31" do + assert_in_delta 0.025, Electricity.for(Date.new(2024, 1, 1)) + assert_in_delta 0.025, Electricity.for(Date.new(2024, 3, 31)) + end + + test "from 2024-04-01 to 2024-06-30" do + assert_in_delta 0.038, Electricity.for(Date.new(2024, 4, 1)) + assert_in_delta 0.038, Electricity.for(Date.new(2024, 6, 30)) + end + end +end diff --git a/test/lib/taxes/value_add_test.rb b/test/lib/taxes/value_add_test.rb new file mode 100644 index 0000000..7f944e6 --- /dev/null +++ b/test/lib/taxes/value_add_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +module Taxes + class ValueAddTest < ActiveSupport::TestCase + test "when region is pen or bal" do + %w[1 3].each do |region| + Region.stub(:current, Region.new(region)) do + assert_in_delta 0.05, ValueAdd.for(Date.new(2023, 1, 1)) + assert_in_delta 0.05, ValueAdd.for(Date.new(2023, 12, 31)) + assert_in_delta 0.1, ValueAdd.for(Date.new(2024, 1, 1)) + end + end + end + + test "when region is can" do + Region.stub(:current, Region.new("2")) do + assert_in_delta 0.0, ValueAdd.for(Date.new(2023, 1, 1)) + assert_in_delta 0.0, ValueAdd.for(Date.new(2023, 12, 31)) + assert_in_delta 0.0, ValueAdd.for(Date.new(2024, 1, 1)) + end + end + + test "when region is ceu or mel" do + %w[4 5].each do |region| + Region.stub(:current, Region.new(region)) do + assert_in_delta 0.01, ValueAdd.for(Date.new(2023, 1, 1)) + assert_in_delta 0.01, ValueAdd.for(Date.new(2023, 12, 31)) + assert_in_delta 0.01, ValueAdd.for(Date.new(2024, 1, 1)) + end + end + end + end +end