|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Orders\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Carts\Repositories\CartRepository; |
|
6
|
|
|
use App\Shop\Carts\ShoppingCart; |
|
7
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart; |
|
8
|
|
|
use Jsdecena\Baserepo\BaseRepository; |
|
9
|
|
|
use App\Shop\Employees\Employee; |
|
10
|
|
|
use App\Shop\Employees\Repositories\EmployeeRepository; |
|
11
|
|
|
use App\Events\OrderCreateEvent; |
|
12
|
|
|
use App\Mail\sendEmailNotificationToAdminMailable; |
|
13
|
|
|
use App\Mail\SendOrderToCustomerMailable; |
|
14
|
|
|
use App\Shop\Orders\Exceptions\OrderInvalidArgumentException; |
|
15
|
|
|
use App\Shop\Orders\Exceptions\OrderNotFoundException; |
|
16
|
|
|
use App\Shop\Addresses\Address; |
|
17
|
|
|
use App\Shop\Couriers\Courier; |
|
18
|
|
|
use App\Shop\Orders\Order; |
|
19
|
|
|
use App\Shop\Orders\Repositories\Interfaces\OrderRepositoryInterface; |
|
20
|
|
|
use App\Shop\Orders\Transformers\OrderTransformable; |
|
21
|
|
|
use App\Shop\Products\Product; |
|
22
|
|
|
use App\Shop\Products\Repositories\ProductRepository; |
|
23
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
24
|
|
|
use Illuminate\Database\QueryException; |
|
25
|
|
|
use Illuminate\Support\Collection; |
|
26
|
|
|
use Illuminate\Support\Facades\Mail; |
|
27
|
|
|
|
|
28
|
|
|
class OrderRepository extends BaseRepository implements OrderRepositoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use OrderTransformable; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* OrderRepository constructor. |
|
34
|
|
|
* @param Order $order |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Order $order) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($order); |
|
39
|
|
|
$this->model = $order; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create the order |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $params |
|
46
|
|
|
* @return Order |
|
47
|
|
|
* @throws OrderInvalidArgumentException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function createOrder(array $params) : Order |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
|
|
53
|
|
|
$order = $this->create($params); |
|
54
|
|
|
|
|
55
|
|
|
$orderRepo = new OrderRepository($order); |
|
56
|
|
|
$orderRepo->buildOrderDetails(Cart::content()); |
|
57
|
|
|
|
|
58
|
|
|
event(new OrderCreateEvent($order)); |
|
59
|
|
|
|
|
60
|
|
|
return $order; |
|
|
|
|
|
|
61
|
|
|
} catch (QueryException $e) { |
|
62
|
|
|
throw new OrderInvalidArgumentException($e->getMessage(), 500, $e); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $params |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
* @throws OrderInvalidArgumentException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function updateOrder(array $params) : bool |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
return $this->update($params); |
|
76
|
|
|
} catch (QueryException $e) { |
|
77
|
|
|
throw new OrderInvalidArgumentException($e->getMessage()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param int $id |
|
83
|
|
|
* @return Order |
|
84
|
|
|
* @throws OrderNotFoundException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function findOrderById(int $id) : Order |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
90
|
|
|
} catch (ModelNotFoundException $e) { |
|
91
|
|
|
throw new OrderNotFoundException($e); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return all the orders |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $order |
|
100
|
|
|
* @param string $sort |
|
101
|
|
|
* @param array $columns |
|
102
|
|
|
* @return Collection |
|
103
|
|
|
*/ |
|
104
|
|
|
public function listOrders(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Collection |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->all($columns, $order, $sort); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Order $order |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
public function findProducts(Order $order) : Collection |
|
114
|
|
|
{ |
|
115
|
|
|
return $order->products; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param Product $product |
|
120
|
|
|
* @param int $quantity |
|
121
|
|
|
* @param array $data |
|
122
|
|
|
*/ |
|
123
|
|
|
public function associateProduct(Product $product, int $quantity = 1, array $data = []) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->model->products()->attach($product, [ |
|
126
|
|
|
'quantity' => $quantity, |
|
127
|
|
|
'product_name' => $product->name, |
|
128
|
|
|
'product_sku' => $product->sku, |
|
129
|
|
|
'product_description' => $product->description, |
|
130
|
|
|
'product_price' => $product->price, |
|
131
|
|
|
'product_attribute_id' => isset($data['product_attribute_id']) ? $data['product_attribute_id']: null, |
|
132
|
|
|
]); |
|
133
|
|
|
$product->quantity = ($product->quantity - $quantity); |
|
134
|
|
|
$product->save(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Send email to customer |
|
139
|
|
|
*/ |
|
140
|
|
|
public function sendEmailToCustomer() |
|
141
|
|
|
{ |
|
142
|
|
|
Mail::to($this->model->customer) |
|
143
|
|
|
->send(new SendOrderToCustomerMailable($this->findOrderById($this->model->id))); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Send email notification to the admin |
|
148
|
|
|
*/ |
|
149
|
|
|
public function sendEmailNotificationToAdmin() |
|
150
|
|
|
{ |
|
151
|
|
|
$employeeRepo = new EmployeeRepository(new Employee); |
|
152
|
|
|
$employee = $employeeRepo->findEmployeeById(1); |
|
153
|
|
|
|
|
154
|
|
|
Mail::to($employee) |
|
155
|
|
|
->send(new sendEmailNotificationToAdminMailable($this->findOrderById($this->model->id))); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $text |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
*/ |
|
162
|
|
|
public function searchOrder(string $text) : Collection |
|
163
|
|
|
{ |
|
164
|
|
|
if (!empty($text)) { |
|
165
|
|
|
return $this->model->searchForOrder($text)->get(); |
|
166
|
|
|
} else { |
|
167
|
|
|
return $this->listOrders(); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return Order |
|
173
|
|
|
*/ |
|
174
|
|
|
public function transform() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->transformOrder($this->model); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return Collection |
|
181
|
|
|
*/ |
|
182
|
|
|
public function listOrderedProducts() : Collection |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->model->products->map(function (Product $product) { |
|
|
|
|
|
|
185
|
|
|
$product->name = $product->pivot->product_name; |
|
|
|
|
|
|
186
|
|
|
$product->sku = $product->pivot->product_sku; |
|
187
|
|
|
$product->description = $product->pivot->product_description; |
|
188
|
|
|
$product->price = $product->pivot->product_price; |
|
189
|
|
|
$product->quantity = $product->pivot->quantity; |
|
190
|
|
|
$product->product_attribute_id = $product->pivot->product_attribute_id; |
|
|
|
|
|
|
191
|
|
|
return $product; |
|
192
|
|
|
}); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param Collection $items |
|
197
|
|
|
*/ |
|
198
|
|
|
public function buildOrderDetails(Collection $items) |
|
199
|
|
|
{ |
|
200
|
|
|
$items->each(function ($item) { |
|
201
|
|
|
$productRepo = new ProductRepository(new Product); |
|
202
|
|
|
$product = $productRepo->find($item->id); |
|
203
|
|
|
if ($item->options->has('product_attribute_id')) { |
|
204
|
|
|
$this->associateProduct($product, $item->qty, [ |
|
205
|
|
|
'product_attribute_id' => $item->options->product_attribute_id |
|
206
|
|
|
]); |
|
207
|
|
|
} else { |
|
208
|
|
|
$this->associateProduct($product, $item->qty); |
|
209
|
|
|
} |
|
210
|
|
|
}); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return Address $address |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getAddress() : Address |
|
217
|
|
|
{ |
|
218
|
|
|
$address = $this->model->first()->address()->first(); |
|
219
|
|
|
return $address; |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return Courier $courier |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getCourier() : Courier |
|
226
|
|
|
{ |
|
227
|
|
|
$courier = $this->model->first()->courier()->first(); |
|
228
|
|
|
return $courier; |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|