66class Croatia ::Invoice ::LineItem
77 include Croatia ::Enum
88
9- attr_accessor :description , :unit , :taxes
9+ attr_accessor :description , :unit , :taxes , :surcharges
1010 attr_reader :quantity , :unit_price , :discount_rate
1111
1212 def initialize ( **options )
@@ -15,6 +15,7 @@ def initialize(**options)
1515 self . unit = options [ :unit ]
1616 self . unit_price = options . fetch ( :unit_price , 0.0 )
1717 self . taxes = options . fetch ( :taxes , { } )
18+ self . surcharges = options . fetch ( :surcharges , { } )
1819 end
1920
2021 def quantity = ( value )
@@ -102,8 +103,12 @@ def tax
102103 tax_breakdown . sum { |breakdown | breakdown [ :tax ] }
103104 end
104105
106+ def surcharge
107+ surcharges . values . sum ( &:amount ) . round ( 2 , BigDecimal ::ROUND_HALF_UP )
108+ end
109+
105110 def total
106- ( subtotal + tax ) . round ( 2 , BigDecimal ::ROUND_HALF_UP )
111+ ( subtotal + tax + surcharge ) . round ( 2 , BigDecimal ::ROUND_HALF_UP )
107112 end
108113
109114 def add_tax ( tax = nil , **options , &block )
@@ -132,4 +137,23 @@ def vat_exempt?
132137 def outside_vat_scope?
133138 !!taxes [ :value_added_tax ] &.outside_scope?
134139 end
140+
141+ def add_surcharge ( surcharge = nil , **options , &block )
142+ if surcharge . nil?
143+ surcharge = Croatia ::Invoice ::Surcharge . new ( **options )
144+ end
145+
146+ surcharge . tap ( &block ) if block_given?
147+
148+ surcharges [ surcharge . name ] = surcharge
149+ surcharge
150+ end
151+
152+ def remove_surcharge ( name )
153+ surcharges . delete ( name )
154+ end
155+
156+ def clear_surcharges
157+ surcharges . clear
158+ end
135159end
0 commit comments