/
order.ex
193 lines (160 loc) · 5.42 KB
/
order.ex
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
defmodule Snitch.Domain.Order do
@moduledoc """
Order helpers.
"""
@editable_states ~w(cart address delivery payment)a
use Snitch.Domain
import Ecto.Changeset
import Ecto.Query
alias Snitch.Data.Schema.{Order, Package, Payment}
alias Snitch.Data.Model.{Product, Image}
alias Snitch.Data.Model.GeneralConfiguration, as: GCModel
@spec validate_change(Ecto.Changeset.t()) :: Ecto.Changeset.t()
def validate_change(%{valid?: false} = changeset), do: changeset
def validate_change(%{valid?: true} = changeset) do
prepare_changes(changeset, fn changeset ->
with {_, order_id} <- fetch_field(changeset, :order_id),
%Order{state: order_state} <- Repo.get(Order, order_id) do
if order_state in @editable_states do
changeset
else
add_error(changeset, :order, "has been frozen", validation: :state, state: order_state)
end
else
_ ->
changeset
end
end)
end
@doc """
Returns summation of all the `payments` for the order in the supplied `payment`
state.
"""
@spec payments_total(Order.t(), String.t()) :: Money.t()
def payments_total(order, payment_state) do
currency = GCModel.fetch_currency()
query =
from(
payment in Payment,
where: payment.state == ^payment_state
)
order = Repo.preload(order, payments: query)
order.payments
|> Enum.reduce(Money.new(currency, 0), fn payment, acc ->
Money.add!(acc, payment.amount)
end)
|> Money.round(currency_digits: :cash)
end
@doc """
Returns the total cost for an `order`.
The total for an `order` depends on the `state` in which the order is in.
If the order is in `cart` or the `address` state then total cost is summation
of individual costs of the lineitems.
In case the order is in advanced stages like `delivery` or `payment` then the
summation includes shipment cost, shipping taxes and other taxes on individual
lineitems as well.
"""
@spec total_amount(Order.t()) :: Money.t()
def total_amount(%Order{state: state} = order) when state in [:cart, :address] do
order = Repo.preload(order, :line_items)
total = line_item_total(order)
total
end
def total_amount(%Order{} = order) do
order = Repo.preload(order, [:line_items, packages: :items])
currency = GCModel.fetch_currency()
total =
Money.add!(
line_item_total(order),
packages_total_cost(order.packages, currency)
)
Money.round(total, currency_digits: :cash)
end
def line_item_total(order) do
currency = GCModel.fetch_currency()
case order do
nil ->
Money.new(currency, 0)
_ ->
order.line_items
|> Enum.reduce(Money.new(currency, 0), fn line_item, acc ->
{:ok, total} = Money.mult(line_item.unit_price, line_item.quantity)
{:ok, acc} = Money.add(acc, total)
acc
end)
|> Money.round(currency_digits: :cash)
end
end
defp packages_total_cost(packages, currency) do
zero_money = Money.new(currency, 0)
packages
|> Enum.reduce(zero_money, fn %{
items: items,
shipping_tax: shipping_tax,
cost: cost
},
acc ->
acc
|> Money.add!(shipping_tax || zero_money)
|> Money.add!(cost || zero_money)
|> Money.add!(package_items_total_cost(items, currency) || zero_money)
end)
|> Money.round(currency_digits: :cash)
end
# TODO: handle shipping tax for items
# At present updating taxes for package items on persisting shipping
# preferences(transition funtction) is not handled.
defp package_items_total_cost(package_items, currency) do
Enum.reduce(package_items, Money.new(currency, 0), fn %{shipping_tax: shipping_tax, tax: tax},
acc ->
shipping_tax = shipping_tax || Money.new!(currency, 0)
tax = tax || Money.new!(currency, 0)
acc
|> Money.add!(shipping_tax)
|> Money.add!(tax)
end)
end
def total_tax(packages) do
currency = GCModel.fetch_currency()
packages
|> Enum.reduce(Money.new(currency, 0), fn %{
items: items,
shipping_tax: shipping_tax
},
acc ->
acc
|> Money.add!(shipping_tax)
|> Money.add!(package_items_total_cost(items, currency))
end)
|> Money.round(currency_digits: :cash)
end
def shipping_total(packages) do
currency = GCModel.fetch_currency()
packages
|> Enum.reduce(Money.new(currency, 0), fn %{cost: cost}, acc ->
Money.add!(acc, cost)
end)
|> Money.round(currency_digits: :cash)
end
def fetch_image_url(line_item) do
image = line_item.product.images |> List.first()
Image.image_url(image.name, line_item.product)
end
def format_date(date) do
date
|> NaiveDateTime.to_erl()
|> form_date
end
defp form_date({{year, month, date}, _}) do
"#{date}/#{month}/#{year}"
end
def line_items_count(order) do
length(order.line_items)
end
def order_package_delivered?(order) do
order = Repo.preload(order, :packages)
Enum.all?(order.packages, fn package ->
package.state == :delivered
end)
end
end