-
Notifications
You must be signed in to change notification settings - Fork 34
/
EloquentOrderRepository.php
294 lines (256 loc) · 11.6 KB
/
EloquentOrderRepository.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
namespace DuncanMcClean\SimpleCommerce\Orders;
use Doctrine\DBAL\Schema\Column;
use DuncanMcClean\SimpleCommerce\Contracts\Coupon as CouponContract;
use DuncanMcClean\SimpleCommerce\Contracts\Customer as CustomerContract;
use DuncanMcClean\SimpleCommerce\Contracts\Order;
use DuncanMcClean\SimpleCommerce\Contracts\OrderRepository as RepositoryContract;
use DuncanMcClean\SimpleCommerce\Exceptions\OrderNotFound;
use DuncanMcClean\SimpleCommerce\Facades\Coupon;
use DuncanMcClean\SimpleCommerce\Facades\Customer;
use DuncanMcClean\SimpleCommerce\SimpleCommerce;
use Illuminate\Support\Facades\Schema;
class EloquentOrderRepository implements RepositoryContract
{
protected $model;
protected $knownColumns = [
'id', 'order_number', 'order_status', 'payment_status', 'items', 'grand_total', 'items_total', 'tax_total',
'shipping_total', 'coupon_total', 'shipping_name', 'shipping_address', 'shipping_address_line2', 'shipping_city',
'shipping_postal_code', 'shipping_region', 'shipping_country', 'billing_name', 'billing_address', 'billing_address_line2',
'billing_city', 'billing_postal_code', 'billing_region', 'billing_country', 'use_shipping_address_for_billing', 'customer_id',
'coupon', 'gateway', 'data', 'created_at', 'updated_at',
];
public function __construct()
{
$this->model = SimpleCommerce::orderDriver()['model'];
}
public function all()
{
return $this->query()->get();
}
public function query()
{
return app(EloquentQueryBuilder::class, [
'builder' => (new $this->model)->query(),
]);
}
public function find($id): ?Order
{
$model = (new $this->model)->find($id);
if (! $model) {
return null;
}
return $this->fromModel($model);
}
public function findOrFail($id): Order
{
$order = $this->find($id);
if (! $order) {
throw new OrderNotFound("Order [{$id}] could not be found.");
}
return $order;
}
public function fromModel(OrderModel $model)
{
$order = app(Order::class)
->resource($model)
->id($model->id)
->orderNumber($model->order_number)
->status($model->order_status ?? 'cart')
->paymentStatus($model->payment_status ?? 'unpaid')
->lineItems($model->items)
->grandTotal($model->grand_total)
->itemsTotal($model->items_total)
->taxTotal($model->tax_total)
->shippingTotal($model->shipping_total)
->couponTotal($model->coupon_total)
->customer($model->customer_id)
->coupon($model->coupon)
->data(
collect($model->data)
->merge([
'shipping_name' => $model->shipping_name,
'shipping_address' => $model->shipping_address,
'shipping_address_line2' => $model->shipping_address_line2,
'shipping_city' => $model->shipping_city,
'shipping_postal_code' => $model->shipping_postal_code,
'shipping_region' => $model->shipping_region,
'shipping_country' => $model->shipping_country,
'billing_name' => $model->billing_name,
'billing_address' => $model->billing_address,
'billing_address_line2' => $model->billing_address_line2,
'billing_city' => $model->billing_city,
'billing_postal_code' => $model->billing_postal_code,
'billing_region' => $model->billing_region,
'billing_country' => $model->billing_country,
'use_shipping_address_for_billing' => $model->use_shipping_address_for_billing,
])
->merge(
collect($this->getCustomColumns())
->mapWithKeys(function ($columnName) use ($model) {
return [$columnName => $model->{$columnName}];
})
->toArray()
)
->merge([
'status_log' => $model->statusLog()->get()->map(fn ($statusLog) => [
'status' => $statusLog->status,
'timestamp' => $statusLog->timestamp->timestamp,
'data' => $statusLog->data ?? [],
]),
])
);
if ($model->gateway) {
$order->gatewayData(
gateway: $model->gateway['use'] ?? null,
data: $model->gateway['data'] ?? null,
refund: $model->gateway['refund'] ?? null
);
}
return $order;
}
public function make(): Order
{
return app(Order::class);
}
public function save($order): void
{
$model = $order->resource();
if (! $model) {
$model = new $this->model();
}
$model->order_number = $order->orderNumber() ?? $this->generateOrderNumber();
$model->order_status = $order->status()->value;
$model->payment_status = $order->paymentStatus()->value;
$model->items = $order->lineItems()->map->toArray();
$model->grand_total = $order->grandTotal();
$model->items_total = $order->itemsTotal();
$model->tax_total = $order->taxTotal();
$model->shipping_total = $order->shippingTotal();
$model->coupon_total = $order->couponTotal();
$model->customer_id = $order->customer() instanceof CustomerContract ? $order->customer()->id() : $order->customer();
$model->coupon = $order->coupon() instanceof CouponContract ? $order->coupon()->id() : $order->coupon();
$model->gateway = $order->gatewayData()?->toArray();
$model->shipping_name = $order->get('shipping_name');
$model->shipping_address = $order->get('shipping_address');
$model->shipping_address_line2 = $order->get('shipping_address_line2');
$model->shipping_city = $order->get('shipping_city');
$model->shipping_postal_code = $order->get('shipping_postal_code');
$model->shipping_region = $order->get('shipping_region');
$model->shipping_country = $order->get('shipping_country');
$model->billing_name = $order->get('billing_name');
$model->billing_address = $order->get('billing_address');
$model->billing_address_line2 = $order->get('billing_address_line2');
$model->billing_city = $order->get('billing_city');
$model->billing_postal_code = $order->get('billing_postal_code');
$model->billing_region = $order->get('billing_region');
$model->billing_country = $order->get('billing_country');
$model->use_shipping_address_for_billing = $order->get('use_shipping_address_for_billing') == 'true';
// If anything in the order data has it's own column, save it
// there, rather than in the data column.
collect($this->getCustomColumns())
->filter(function ($columnName) use ($order) {
return $order->has($columnName);
})
->each(function ($columnName) use (&$model, $order) {
$model->{$columnName} = $order->get($columnName);
});
// Set the value of the data column - we take out any 'known' columns,
// along with any custom columns.
$model->data = $order->data()
->except($this->knownColumns)
->except($this->getCustomColumns());
$model->save();
// Loop through status log events & create/update them in the database.
$order->statusLog()->map(function (StatusLogEvent $statusLogEvent) use ($model) {
StatusLogModel::updateOrCreate(
['order_id' => $model->id, 'status' => $statusLogEvent->status, 'timestamp' => $statusLogEvent->date()],
['data' => $statusLogEvent->data ?? []]
);
});
$order->id = $model->id;
$order->orderNumber = $model->order_number;
$order->status = OrderStatus::from($model->order_status);
$order->paymentStatus = PaymentStatus::from($model->payment_status);
// $order->lineItems = collect($model->items);
$order->grandTotal = $model->grand_total;
$order->itemsTotal = $model->items_total;
$order->taxTotal = $model->tax_total;
$order->shippingTotal = $model->shipping_total;
$order->couponTotal = $model->coupon_total;
$order->customer = $model->customer_id ? Customer::find($model->customer_id) : null;
$order->coupon = $model->coupon ? Coupon::find($model->coupon) : null;
$order->gateway = $model->gateway;
$order->data = collect($model->data)
->merge([
'shipping_name' => $model->shipping_name,
'shipping_address' => $model->shipping_address,
'shipping_address_line2' => $model->shipping_address_line2,
'shipping_city' => $model->shipping_city,
'shipping_postal_code' => $model->shipping_postal_code,
'shipping_region' => $model->shipping_region,
'shipping_country' => $model->shipping_country,
'billing_name' => $model->billing_name,
'billing_address' => $model->billing_address,
'billing_address_line2' => $model->billing_address_line2,
'billing_city' => $model->billing_city,
'billing_postal_code' => $model->billing_postal_code,
'billing_region' => $model->billing_region,
'billing_country' => $model->billing_country,
'use_shipping_address_for_billing' => $model->use_shipping_address_for_billing,
])
->merge(
collect($this->getCustomColumns())
->mapWithKeys(function ($columnName) use ($model) {
return [$columnName => $model->{$columnName}];
})
->toArray()
)
->merge([
'status_log' => $model->statusLog()->get()->map(fn ($statusLog) => [
'status' => $statusLog->status,
'timestamp' => $statusLog->timestamp->timestamp,
'data' => $statusLog->data ?? [],
]),
]);
$order->resource = $model;
}
public function delete($order): void
{
$order->resource()->delete();
}
/**
* Returns an array of custom columns the developer
* has added to the 'orders' table.
*/
protected function getCustomColumns(): array
{
$tableColumns = Schema::getConnection()
->getDoctrineSchemaManager()
->listTableColumns((new $this->model)->getTable());
return collect($tableColumns)
->reject(function (Column $column) {
return in_array($column->getName(), $this->knownColumns);
})
->map->getName()
->toArray();
}
/**
* Returns the next order number, based on the highest order number.
*/
protected function generateOrderNumber(): int
{
$model = (new $this->model);
$lastOrderNumber = $model->query()
->orderBy('order_number', 'DESC')
->value('order_number');
if (! $lastOrderNumber) {
return config('simple-commerce.minimum_order_number', 1000);
}
return $lastOrderNumber + 1;
}
public static function bindings(): array
{
return [];
}
}