-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathorders_controller.rb
More file actions
142 lines (118 loc) · 4.62 KB
/
orders_controller.rb
File metadata and controls
142 lines (118 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class OrdersController < ApplicationController
def index
@orders = Orders::Order.where(archived: false).order("id DESC").page(params[:page]).per(10)
end
def show
@order = Orders::Order.find_by_uid(params[:id])
return not_found unless @order
@order_lines = Orders::OrderLine.where(order_uid: @order.uid)
@shipment = Shipments::Shipment.find_by(order_uid: @order.uid)
@invoice = Invoices::Invoice.find_or_initialize_by(order_uid: @order.uid)
end
def new
redirect_to edit_order_path(SecureRandom.uuid)
end
def edit
@order_id = params[:id]
@order = Orders::Order.find_by_uid(params[:id])
@order_lines = Orders::OrderLine.where(order_uid: params[:id])
@products = Products::Product.where(archived: false)
@customers = Customers::Customer.all
@time_promotions = TimePromotions::TimePromotion.current
render :edit,
locals: {
discounted_value: @order&.discounted_value || 0,
total_value: @order&.total_value || 0,
percentage_discount: @order&.percentage_discount
}
end
def edit_discount
@order_id = params[:id]
end
def update_discount
@order_id = params[:id]
order = Orders::Order.find_or_create_by!(uid: params[:id])
if order.percentage_discount
command_bus.(Pricing::ChangePercentageDiscount.new(order_id: @order_id, amount: params[:amount]))
else
command_bus.(Pricing::SetPercentageDiscount.new(order_id: @order_id, amount: params[:amount]))
end
redirect_to edit_order_path(@order_id)
end
def remove_discount
@order_id = params[:id]
command_bus.(Pricing::RemovePercentageDiscount.new(order_id: @order_id))
redirect_to edit_order_path(@order_id)
end
def add_item
read_model = Orders::OrderLine.where(order_uid: params[:id], product_id: params[:product_id]).first
unless Availability.approximately_available?(params[:product_id], (read_model&.quantity || 0) + 1)
redirect_to edit_order_path(params[:id]),
alert: "Product not available in requested quantity!" and return
end
price = Products::Product.find(params[:product_id]).price
ActiveRecord::Base.transaction do
command_bus.(Pricing::AddPriceItem.new(order_id: params[:id], product_id: params[:product_id], price:))
end
head :ok
end
def remove_item
command_bus.(Pricing::RemovePriceItem.new(order_id: params[:id], product_id: params[:product_id]))
head :ok
end
def create
Orders::SubmitService.call(order_id: params[:order_id], customer_id: params[:customer_id])
rescue Orders::OrderHasUnavailableProducts => e
unavailable_products = e.unavailable_products.join(", ")
redirect_to edit_order_path(params[:order_id]), alert: "Order can not be submitted! #{unavailable_products} not available in requested quantity!"
rescue Pricing::Offer::IsEmpty
redirect_to edit_order_path(params[:order_id]), alert: "You can't submit an empty order"
rescue Crm::Customer::NotExists
redirect_to order_path(params[:order_id]), alert: "Order can not be submitted! Customer does not exist."
else
redirect_to order_path(params[:order_id]), notice: "Your order is being submitted"
end
def expire
Orders::Order
.where(state: "Draft")
.find_each { |order| command_bus.(Pricing::ExpireOffer.new(order_id: order.uid)) }
redirect_to root_path
end
def pay
ActiveRecord::Base.transaction do
authorize_payment(params[:id])
capture_payment(params[:id])
flash[:notice] = "Order paid successfully"
rescue Payments::Payment::AlreadyAuthorized
flash[:alert] = "Payment was already authorized"
rescue Payments::Payment::AlreadyCaptured
flash[:alert] = "Payment was already captured"
rescue Payments::Payment::NotAuthorized
flash[:alert] = "Payment wasn't yet authorized"
rescue Fulfillment::Order::InvalidState
flash[:alert] = "Order is not in a valid state for payment"
end
redirect_to orders_path
end
def cancel
command_bus.(Fulfillment::CancelOrder.new(order_id: params[:id]))
redirect_to root_path, notice: "Order cancelled"
end
def archive
command_bus.(Fulfillment::ArchiveOrder.new(order_id: params[:id]))
redirect_to orders_path, notice: "Order was archived"
end
private
def authorize_payment(order_id)
command_bus.call(authorize_payment_cmd(order_id))
end
def capture_payment(order_id)
command_bus.call(capture_payment_cmd(order_id))
end
def authorize_payment_cmd(order_id)
Payments::AuthorizePayment.new(order_id: order_id)
end
def capture_payment_cmd(order_id)
Payments::CapturePayment.new(order_id: order_id)
end
end