Skip to content

Commit bc84d0d

Browse files
Merged VatRateDetermination into InvoiceGeneration process
It's the first step of simplifying (hopefully) the invoice generation ceremony and keeping it in one place. Next step is to merge ItemInvoicingProcess here too.
1 parent 1edd07a commit bc84d0d

File tree

4 files changed

+37
-75
lines changed

4 files changed

+37
-75
lines changed

rails_application/app/processes/processes/configuration.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def call(event_store, command_bus)
1010
enable_coupon_discount_process(event_store, command_bus)
1111
notify_payments_about_order_total_value(event_store, command_bus)
1212
enable_shipment_sync(event_store, command_bus)
13-
determine_vat_rates_on_order_placed(event_store, command_bus)
1413
set_invoice_payment_date_when_order_confirmed(event_store, command_bus)
1514
enable_product_name_sync(event_store, command_bus)
1615
confirm_order_on_payment_captured(event_store, command_bus)
@@ -71,15 +70,6 @@ def enable_order_item_invoicing_process(event_store, command_bus)
7170
)
7271
end
7372

74-
def determine_vat_rates_on_order_placed(event_store, command_bus)
75-
event_store.subscribe(
76-
DetermineVatRatesOnOrderPlaced.new(event_store, command_bus),
77-
to: [
78-
Pricing::OfferAccepted,
79-
Fulfillment::OrderRegistered
80-
]
81-
)
82-
end
8373

8474
def enable_product_name_sync(event_store, command_bus)
8575
Infra::Process.new(event_store, command_bus)

rails_application/app/processes/processes/determine_vat_rates_on_order_placed.rb

Lines changed: 0 additions & 49 deletions
This file was deleted.

rails_application/app/processes/processes/invoice_generation.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ class InvoiceGeneration
77
Pricing::PriceItemRemoved,
88
Pricing::PercentageDiscountSet,
99
Pricing::PercentageDiscountChanged,
10-
Pricing::PercentageDiscountRemoved
10+
Pricing::PercentageDiscountRemoved,
11+
Fulfillment::OrderRegistered
1112
)
1213

1314
def act
1415
calculate_sub_amounts
16+
determine_vat_rates if state.placed?
1517
end
1618

1719
private
@@ -33,6 +35,8 @@ def apply(event)
3335
apply_percentage_discount_changed(event)
3436
when Pricing::PercentageDiscountRemoved
3537
apply_percentage_discount_removed(event)
38+
when Fulfillment::OrderRegistered
39+
apply_order_registered
3640
else
3741
state
3842
end
@@ -121,11 +125,22 @@ def apply_discounts_to_existing_lines(new_state)
121125
new_state.with(lines: updated_lines)
122126
end
123127

128+
def determine_vat_rates
129+
state.sub_amounts_total.each_key do |product_id|
130+
command = Taxes::DetermineVatRate.new(order_id: @order_id, product_id: product_id)
131+
command_bus.call(command)
132+
end
133+
end
134+
135+
def apply_order_registered
136+
state.with(order_placed: true)
137+
end
138+
124139
end
125140

126-
Invoice = Data.define(:lines, :discounts) do
127-
def initialize(lines: [], discounts: [])
128-
super(lines: lines.freeze, discounts: discounts.freeze)
141+
Invoice = Data.define(:lines, :discounts, :order_placed) do
142+
def initialize(lines: [], discounts: [], order_placed: false)
143+
super(lines: lines.freeze, discounts: discounts.freeze, order_placed: order_placed)
129144
end
130145

131146
def sub_amounts_total
@@ -153,6 +168,10 @@ def final_discount_percentage
153168
def discounted_value
154169
subtotal * (1 - final_discount_percentage / 100.0)
155170
end
171+
172+
def placed?
173+
order_placed
174+
end
156175
end
157176

158177
end

rails_application/test/processes/determine_vat_rate_test.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@
22

33
module Processes
44
class DetermineVatRateTest < ProcessTest
5-
cover "Processes::DetermineVatRatesOnOrderPlaced*"
5+
cover "Processes::InvoiceGeneration*"
66

77
def test_happy_path
88
product_id = SecureRandom.uuid
9-
process = DetermineVatRatesOnOrderPlaced.new(event_store, command_bus)
10-
given([offer_accepted(order_id, product_id), order_placed]).each do |event|
9+
process = InvoiceGeneration.new(event_store, command_bus)
10+
given([price_item_added(order_id, product_id), order_placed]).each do |event|
1111
process.call(event)
1212
end
1313
assert_command(Taxes::DetermineVatRate.new(order_id: order_id, product_id: product_id))
1414
end
1515

16-
def test_accepted_but_not_placed
17-
process = DetermineVatRatesOnOrderPlaced.new(event_store, command_bus)
18-
given([offer_accepted(order_id, SecureRandom.uuid)]).each do |event|
16+
def test_price_added_but_not_placed
17+
process = InvoiceGeneration.new(event_store, command_bus)
18+
given([price_item_added(order_id, SecureRandom.uuid)]).each do |event|
1919
process.call(event)
2020
end
2121
assert_no_command
2222
end
2323

2424
def test_placed_but_not_accepted
25-
process = DetermineVatRatesOnOrderPlaced.new(event_store, command_bus)
25+
process = InvoiceGeneration.new(event_store, command_bus)
2626
given([order_placed]).each do |event|
2727
process.call(event)
2828
end
2929
assert_no_command
3030
end
3131

3232
def test_stream_name
33-
process = DetermineVatRatesOnOrderPlaced.new(event_store, command_bus)
33+
process = InvoiceGeneration.new(event_store, command_bus)
3434
given([order_placed]).each do |event|
3535
process.call(event)
3636
end
37-
assert_equal "Processes::DetermineVatRatesOnOrderPlaced$#{order_id}", process.send(:stream_name)
37+
assert_equal "Processes::InvoiceGeneration$#{order_id}", process.send(:stream_name)
3838
end
3939

4040
private
@@ -43,11 +43,13 @@ def command_bus
4343
@command_bus
4444
end
4545

46-
def offer_accepted(order_id, product_id)
47-
Pricing::OfferAccepted.new(
46+
def price_item_added(order_id, product_id)
47+
Pricing::PriceItemAdded.new(
4848
data: {
4949
order_id: order_id,
50-
order_lines: [{ product_id: product_id, quantity: 1 }]
50+
product_id: product_id,
51+
base_price: 100,
52+
price: 100
5153
}
5254
)
5355
end

0 commit comments

Comments
 (0)