Skip to content

Commit e227c5d

Browse files
committed
Implement surcharges
1 parent 86af73b commit e227c5d

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ Croatia is a gem that contains various utilities for performing Croatia-specific
99
- [x] Parsing
1010
- [x] Generation
1111
- [ ] Invoices
12+
- [ ] Line items
13+
- [x] Discounts
14+
- [x] Percentage
15+
- [x] Total
16+
- [x] Taxes
17+
- [x] Types
18+
- [x] Categories
19+
- [ ] Surcharges _(Naknade)_
20+
- [ ] Margins _(Marza)_
1221
- [x] Payment barcodes
1322
- [x] HUB3 standard 2D barcode generation
1423
- [ ] Fiscalization v2.3 _(Fiskalizacija)_
1524
- [x] Issuer protection code generation _(ZKI)_
16-
- [ ] Invoices _(racuni)_
1725
- [x] Reverse _(storno)_
26+
- [ ] Invoices _(racuni)_
1827
- [ ] Supporting documents _(prateci dokumenti - otpremnice, radni nalozi, ponude, ugovori, ...)_
1928
- [ ] Payment method change
2029
- [x] Verification QR code generation

lib/croatia/invoice/line_item.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class 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
135159
end

lib/croatia/invoice/surcharge.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "bigdecimal"
4+
require "bigdecimal/util"
5+
6+
class Croatia::Invoice::Surcharge
7+
include Croatia::Enum
8+
9+
attr_reader :name, :amount
10+
11+
def initialize(name:, amount:)
12+
self.name = name
13+
self.amount = amount
14+
end
15+
16+
def name=(value)
17+
if value.nil?
18+
raise ArgumentError, "Name cannot be nil"
19+
end
20+
21+
@name = value.to_s.strip
22+
end
23+
24+
def amount=(value)
25+
if value.nil?
26+
raise ArgumentError, "Amount cannot be nil"
27+
end
28+
29+
@amount = value.to_d
30+
end
31+
end

0 commit comments

Comments
 (0)