Skip to content

Commit 1b4fa5a

Browse files
Display total value with tax on the invoice
- added acceptance test - implemented as methods on ActiveRecord - Vat Rate was already stored and displayed Idea for the future: store product/vat matching in the read model here. Then we can remove the DetermineVatRate process manager which feels artificial.
1 parent 681ed68 commit 1b4fa5a

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

rails_application/app/read_models/invoices/configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ module Invoices
22
class Invoice < ApplicationRecord
33
self.table_name = "invoices"
44
has_many :invoice_items
5+
6+
def total_value_with_tax
7+
invoice_items.sum(&:value_with_tax)
8+
end
59
end
610

711
class InvoiceItem < ApplicationRecord
812
self.table_name = "invoice_items"
913
belongs_to :invoice
14+
15+
def value_with_tax
16+
value + (value * vat_rate / 100)
17+
end
1018
end
1119

1220
class Order < ApplicationRecord

rails_application/app/views/invoices/show.html.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
<th class="text-left py-2">VAT rate</th>
3535
<th class="text-left py-2">Quantity</th>
3636
<th class="text-left py-2">Unit Price</th>
37-
<th class="text-right py-2">Value</th>
37+
<th class="text-right py-2">Value (net)</th>
38+
<th class="text-right py-2">Value + VAT</th>
3839
</tr>
3940
</thead>
4041

@@ -46,13 +47,15 @@
4647
<td class="py-2"><%= item.quantity %></td>
4748
<td class="py-2"><%= number_to_currency(item.unit_price) %></td>
4849
<td class="py-2 text-right"><%= number_to_currency(item.value) %></td>
50+
<td class="py-2 text-right"><%= number_to_currency(item.value_with_tax) %></td>
4951
</tr>
5052
<% end %>
5153
</tbody>
5254
<tfoot>
5355
<tr class="border-t">
5456
<td class="py-2" colspan="4">Total</td>
55-
<td class="py-2 text-right font-bold"><%= number_to_currency(@invoice.total_value) %></td>
57+
<td class="py-2 text-right"><%= number_to_currency(@invoice.total_value) %></td>
58+
<td class="py-2 text-right font-bold"><%= number_to_currency(@invoice.total_value_with_tax) %></td>
5659
</tr>
5760
</tfoot>
5861
</table>

rails_application/test/invoices/invoices_test.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def test_create_draft_order_when_not_exists
1919
end
2020

2121
def test_product_name_change_affects_existing_invoices
22-
product_id = SecureRandom.uuid
2322
initial_product_name = "Initial Name"
2423
updated_product_name = "Updated Name"
2524

@@ -42,6 +41,18 @@ def test_product_name_change_affects_existing_invoices
4241
assert_invoice_product_name(new_order_id, updated_product_name)
4342
end
4443

44+
def test_invoice_value_with_taxes
45+
add_available_vat_rate(20)
46+
product_id = register_product("Test Product", 100, 20)
47+
customer_id = register_customer("Test Customer")
48+
49+
order_id = SecureRandom.uuid
50+
add_product_to_basket(order_id, product_id)
51+
submit_order(customer_id, order_id)
52+
53+
assert_invoice_value_with_taxes(order_id, 120)
54+
end
55+
4556
private
4657

4758
def update_product_name(product_id, new_name)
@@ -58,5 +69,11 @@ def assert_invoice_product_name(order_id, expected_name)
5869
assert_response :success
5970
assert_select ".py-2", text: expected_name
6071
end
72+
73+
def assert_invoice_value_with_taxes(order_id, expected_value)
74+
get "/invoices/#{order_id}"
75+
assert_response :success
76+
assert_select "td.font-bold", text: "$#{expected_value}.00"
77+
end
6178
end
6279
end

0 commit comments

Comments
 (0)