Skip to content

Commit

Permalink
Merge pull request #93 from javierav/feature/taxes
Browse files Browse the repository at this point in the history
Add taxes calculators
  • Loading branch information
javierav authored Jan 27, 2024
2 parents ef84150 + 7ec00da commit ee0ca2d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/lib/taxes/electricity.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions app/lib/taxes/value_add.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions test/lib/taxes/electricity_test.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions test/lib/taxes/value_add_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ee0ca2d

Please sign in to comment.