@@ -6,31 +6,32 @@ class EditOrder < Arbre::Component
66 include ActionView ::Helpers ::UrlHelper
77
88 def self . build ( view_context , order_id )
9+ order = ClientOrders ::Order . find_or_initialize_by ( order_uid : order_id ) do |order |
10+ order . total_value = 0
11+ order . discounted_value = 0
12+ end
913 order_lines = ClientOrders ::OrderLine . where ( order_uid : order_id )
1014 products = ClientOrders ::Product . all
11- new ( Arbre ::Context . new ( nil , view_context ) ) . build ( order_id , order_lines , products )
15+ time_promotions = TimePromotions ::TimePromotion . current
16+ new ( Arbre ::Context . new ( nil , view_context ) ) . build ( order , order_lines , products , time_promotions )
1217 end
1318
14- def build ( order_id , order_lines , products , attributes = { } )
19+ def build ( order , order_lines , products , time_promotions , attributes = { } )
1520 super ( attributes )
1621 div do
17- products_table ( order_id , products , order_lines )
18- coupon_form ( order_id )
19- submit_form ( order_id )
22+ products_table ( order , products , order_lines , time_promotions )
23+ coupon_form ( order )
24+ submit_form ( order )
2025 end
2126 end
2227
2328 private
2429
25- def products_table ( order_id , products , order_lines )
30+ def products_table ( order , products , order_lines , time_promotions )
2631 table class : "w-full" do
2732 headers_row
28- tbody do
29- text_node turbo_stream_from "client_orders_#{ order_id } "
30- products . each do |product |
31- product_row ( order_id , product , order_lines )
32- end
33- end
33+ products_rows ( order , products , order_lines )
34+ footer_rows ( order , time_promotions )
3435 end
3536 end
3637
@@ -46,16 +47,34 @@ def headers_row
4647 end
4748 end
4849
49- def product_row ( order_id , product , order_lines )
50+ def products_rows ( order , products , order_lines )
51+ tbody do
52+ text_node turbo_stream_from "client_orders_#{ order . order_uid } "
53+ products . each do |product |
54+ product_row ( order , product , order_lines )
55+ end
56+ end
57+ end
58+
59+ def product_row ( order , product , order_lines )
5060 order_line = order_lines &.find { |order_line | order_line . product_id == product . uid }
5161 tr class : "border-b" do
5262 td ( class : "py-2" ) { product . name }
5363 td ( class : "py-2" ) { out_of_stock_badge unless product . available? }
5464 td ( class : "py-2" , id : "client_orders_#{ product . uid } _product_quantity" ) { order_line . try ( &:product_quantity ) || 0 }
5565 td ( class : "py-2" ) { number_to_currency ( product . price ) }
56- td ( class : "py-2" , id : "client_orders_#{ product . uid } _value" ) { number_to_currency ( order_line . try ( &:value ) ) }
57- td ( class : "py-2 text-right" ) { add_item_button ( order_id , product . uid ) }
58- td ( class : "py-2 text-right" , id : "client_orders_#{ product . uid } _remove_item_button" ) { remove_item_button ( order_id , product . uid ) if order_line }
66+ td ( class : "py-2" , id : "client_orders_#{ product . uid } _value" ) { number_to_currency ( order_line . try ( &:value ) ) || "$0.00" }
67+ td ( class : "py-2 text-right" ) { add_item_button ( order . order_uid , product . uid ) }
68+ td ( class : "py-2 text-right" , id : "client_orders_#{ product . uid } _remove_item_button" ) { remove_item_button ( order , product . uid ) if order_line }
69+ end
70+ end
71+
72+ def footer_rows ( order , time_promotions )
73+ tfoot class :"border-t-4" do
74+ before_discounts_row ( order ) if order . percentage_discount || time_promotions . any?
75+ coupon_discount_row ( order ) if order . percentage_discount
76+ time_promotions_rows ( time_promotions )
77+ total_row ( order )
5978 end
6079 end
6180
@@ -67,12 +86,42 @@ def add_item_button(order_id, product_id)
6786 button_to "Add" , add_item_client_order_path ( id : order_id , product_id : product_id ) , class : "hover:underline text-blue-500"
6887 end
6988
70- def remove_item_button ( order_id , product_id )
71- button_to "Remove" , remove_item_client_order_path ( id : order_id , product_id : product_id ) , class : "hover:underline text-blue-500"
89+ def remove_item_button ( order , product_id )
90+ button_to "Remove" , remove_item_client_order_path ( id : order . order_uid , product_id : product_id ) , class : "hover:underline text-blue-500"
91+ end
92+
93+ def before_discounts_row ( order )
94+ tr ( class : "border-t" ) do
95+ td ( class : "py-2" , colspan : 4 ) { "Before discounts" }
96+ td ( class : "py-2" , id : "client_orders_#{ order . order_uid } _total_value" ) { number_to_currency ( order . total_value ) }
97+ end
98+ end
99+
100+ def coupon_discount_row ( order )
101+ tr ( class : "border-t" ) do
102+ td ( class : "py-2" , colspan : 4 ) { "Coupon discount" }
103+ td ( class : "py-2" , id : "client_orders_#{ order . order_uid } _percentage_discount" ) { "#{ order . percentage_discount } %" }
104+ end
105+ end
106+
107+ def time_promotions_rows ( time_promotions )
108+ time_promotions . each do |time_promotion |
109+ tr ( class : "border-t" ) do
110+ td ( class : "py-2" , colspan : 4 ) { "Promotion: #{ time_promotion . label } (if you buy before #{ time_promotion . end_time } )" }
111+ td ( class : "py-2" ) { "#{ time_promotion . discount } %" }
112+ end
113+ end
114+ end
115+
116+ def total_row ( order )
117+ tr ( class : "border-t" ) do
118+ td ( class : "py-2" , colspan : 4 ) { "Total" }
119+ td ( class : "py-2 font-bold" , id : "client_orders_#{ order . order_uid } _discounted_value" ) { number_to_currency ( order . discounted_value ) }
120+ end
72121 end
73122
74- def coupon_form ( order_id )
75- form ( action : use_coupon_client_order_path ( id : order_id ) , method : :post , class : "inline-flex gap-4 mt-8" ) do
123+ def coupon_form ( order )
124+ form ( action : use_coupon_client_order_path ( id : order . order_uid ) , method : :post , class : "inline-flex gap-4 mt-8" ) do
76125 input (
77126 id : "coupon_code" ,
78127 type : :text ,
@@ -84,9 +133,9 @@ def coupon_form(order_id)
84133 end
85134 end
86135
87- def submit_form ( order_id )
136+ def submit_form ( order )
88137 form ( id : "form" , action : client_orders_path , method : :post ) do
89- input ( type : :hidden , name : :order_id , value : order_id )
138+ input ( type : :hidden , name : :order_id , value : order . order_uid )
90139 div ( class : "mt-8" ) do
91140 input type : :submit , value : "Create Order" , class : "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
92141 end
0 commit comments