Skip to content

Commit caacc44

Browse files
Merged OrderItemInvoicingProcess into InvoiceGeneration process
Removed InvoiceItemCalculated event. Removed the choreography with VAT determination via commands and events. Instead now, we access to the VatRateCatalog - not perfect either. Next time we work in this area, it would be nice to just subscribe in process for VAT events. Or have a read model for the process. Many tests had to be updated to set proper vat rate.
1 parent bc84d0d commit caacc44

22 files changed

+439
-598
lines changed

ecommerce/taxes/lib/taxes.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Taxes
99
class Configuration
1010
def call(event_store, command_bus)
1111
command_bus.register(SetVatRate, SetVatRateHandler.new(event_store))
12-
command_bus.register(DetermineVatRate, DetermineVatRateHandler.new(event_store))
1312
command_bus.register(AddAvailableVatRate, AddAvailableVatRateHandler.new(event_store))
1413
command_bus.register(RemoveAvailableVatRate, RemoveAvailableVatRateHandler.new(event_store))
1514
end

ecommerce/taxes/lib/taxes/commands.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ class SetVatRate < Infra::Command
44
attribute :vat_rate_code, Infra::Types::String
55
end
66

7-
class DetermineVatRate < Infra::Command
8-
attribute :product_id, Infra::Types::UUID
9-
attribute :order_id, Infra::Types::UUID
10-
end
11-
127
class AddAvailableVatRate < Infra::Command
138
attribute :available_vat_rate_id, Infra::Types::UUID
149
attribute :vat_rate, Infra::Types::VatRate

ecommerce/taxes/lib/taxes/events.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ class VatRateSet < Infra::Event
44
attribute :vat_rate, Infra::Types::VatRate
55
end
66

7-
class VatRateDetermined < Infra::Event
8-
attribute :order_id, Infra::Types::UUID
9-
attribute :product_id, Infra::Types::UUID
10-
attribute :vat_rate, Infra::Types::VatRate
11-
end
12-
137
class AvailableVatRateAdded < Infra::Event
148
attribute :available_vat_rate_id, Infra::Types::UUID
159
attribute :vat_rate, Infra::Types::VatRate

ecommerce/taxes/lib/taxes/services.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,6 @@ def call(cmd)
1717
end
1818
end
1919

20-
class DetermineVatRateHandler
21-
def initialize(event_store)
22-
@catalog = VatRateCatalog.new(event_store)
23-
@event_store = event_store
24-
end
25-
26-
def call(cmd)
27-
order_id = cmd.order_id
28-
product_id = cmd.product_id
29-
vat_rate = catalog.vat_rate_for(product_id)
30-
return unless vat_rate
31-
event = VatRateDetermined.new(data: { order_id: order_id, product_id: product_id, vat_rate: vat_rate })
32-
event_store.publish(event, stream_name: stream_name(order_id))
33-
end
34-
35-
private
36-
37-
attr_reader :catalog, :event_store
38-
39-
def stream_name(order_id)
40-
"Taxes::Order$#{order_id}"
41-
end
42-
end
43-
4420
class AddAvailableVatRateHandler
4521
include Infra::Retry
4622

rails_application/.mutant.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ matcher:
4545
- Processes::Test*
4646
- Processes::ReleasePaymentProcess*
4747
- Processes::MoneySplitter*
48-
- Processes::OrderItemInvoicingProcess#fetch_id
4948
- Processes::SyncShipmentFromPricing*
5049
- Processes::SyncInventoryFromOrdering*
5150
- Processes::NotifyPaymentsAboutOrderValue*

rails_application/app/processes/processes/configuration.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def call(event_store, command_bus)
1616

1717
enable_release_payment_process(event_store, command_bus)
1818
enable_shipment_process(event_store, command_bus)
19-
enable_order_item_invoicing_process(event_store, command_bus)
2019
enable_reservation_process(event_store, command_bus)
2120
enable_welcome_message_process(event_store, command_bus)
2221
enable_total_order_value_process(event_store, command_bus)
@@ -60,17 +59,6 @@ def enable_release_payment_process(event_store, command_bus)
6059
)
6160
end
6261

63-
def enable_order_item_invoicing_process(event_store, command_bus)
64-
event_store.subscribe(
65-
OrderItemInvoicingProcess.new(event_store, command_bus),
66-
to: [
67-
Processes::InvoiceItemValueCalculated,
68-
Taxes::VatRateDetermined
69-
]
70-
)
71-
end
72-
73-
7462
def enable_product_name_sync(event_store, command_bus)
7563
Infra::Process.new(event_store, command_bus)
7664
.call(ProductCatalog::ProductNamed, [:product_id, :name],

rails_application/app/processes/processes/invoice_generation.rb

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ module Processes
22
class InvoiceGeneration
33
include Infra::ProcessManager.with_state { Invoice }
44

5+
def initialize(event_store, command_bus)
6+
super(event_store, command_bus)
7+
@vat_rate_catalog = Taxes::VatRateCatalog.new(event_store)
8+
end
9+
510
subscribes_to(
611
Pricing::PriceItemAdded,
712
Pricing::PriceItemRemoved,
@@ -13,7 +18,6 @@ class InvoiceGeneration
1318

1419
def act
1520
calculate_sub_amounts
16-
determine_vat_rates if state.placed?
1721
end
1822

1923
private
@@ -37,37 +41,36 @@ def apply(event)
3741
apply_percentage_discount_removed(event)
3842
when Fulfillment::OrderRegistered
3943
apply_order_registered
40-
else
41-
state
4244
end
4345
end
4446

4547
def calculate_sub_amounts
48+
return unless state.placed?
49+
4650
sub_amounts_total = state.sub_amounts_total
47-
4851
sub_amounts_total.each_pair do |product_id, h|
49-
publish_invoice_item_value_calculated(
52+
create_invoice_items_for_product(
5053
product_id: product_id,
5154
quantity: h.fetch(:quantity),
52-
amount: h.fetch(:base_amount),
5355
discounted_amount: h.fetch(:amount)
5456
)
5557
end
5658
end
5759

58-
def publish_invoice_item_value_calculated(product_id:, quantity:, amount:, discounted_amount:)
59-
event_store.publish(
60-
InvoiceItemValueCalculated.new(
61-
data: {
62-
order_id: @order_id,
60+
def create_invoice_items_for_product(product_id:, quantity:, discounted_amount:)
61+
vat_rate = @vat_rate_catalog.vat_rate_for(product_id)
62+
unit_prices = MoneySplitter.new(discounted_amount, quantity).call
63+
unit_prices.tally.each do |unit_price, quantity|
64+
command_bus.call(
65+
Invoicing::AddInvoiceItem.new(
66+
invoice_id: @order_id,
6367
product_id: product_id,
68+
vat_rate: vat_rate,
6469
quantity: quantity,
65-
amount: amount,
66-
discounted_amount: discounted_amount
67-
}
68-
),
69-
stream_name: "Processes::InvoiceGeneration$#{@order_id}"
70-
)
70+
unit_price: unit_price
71+
)
72+
)
73+
end
7174
end
7275

7376
def apply_price_item_added(event)
@@ -125,13 +128,6 @@ def apply_discounts_to_existing_lines(new_state)
125128
new_state.with(lines: updated_lines)
126129
end
127130

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-
135131
def apply_order_registered
136132
state.with(order_placed: true)
137133
end
@@ -174,4 +170,30 @@ def placed?
174170
end
175171
end
176172

173+
class MoneySplitter
174+
def initialize(amount, quantity)
175+
@amount = amount
176+
@weights = Array.new(quantity, 1)
177+
end
178+
179+
def call
180+
distributed_amounts = []
181+
total_weight = @weights.sum.to_d
182+
@weights.each do |weight|
183+
if total_weight.eql?(0)
184+
distributed_amounts << 0
185+
next
186+
end
187+
188+
p = weight / total_weight
189+
distributed_amount = (p * @amount).round(2)
190+
distributed_amounts << distributed_amount
191+
total_weight -= weight
192+
@amount -= distributed_amount
193+
end
194+
195+
distributed_amounts
196+
end
197+
end
198+
177199
end

rails_application/app/processes/processes/invoice_item_value_calculated.rb

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

rails_application/app/processes/processes/order_item_invoicing_process.rb

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

rails_application/test/client_orders/order_cancelled_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def test_order_confirmed
3838
private
3939

4040
def create_product(product_id, name, price)
41+
vat_rate = Infra::Types::VatRate.new(rate: 20, code: "20")
4142
run_command(ProductCatalog::RegisterProduct.new(product_id: product_id))
4243
run_command(ProductCatalog::NameProduct.new(product_id: product_id, name: name))
4344
run_command(Pricing::SetPrice.new(product_id: product_id, price: price))
45+
Rails.configuration.event_store.publish(Taxes::VatRateSet.new(data: { product_id: product_id, vat_rate: vat_rate }))
4446
end
4547
end
4648
end

0 commit comments

Comments
 (0)