1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin\Orders; |
4
|
|
|
|
5
|
|
|
use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface; |
6
|
|
|
use App\Shop\Addresses\Transformations\AddressTransformable; |
7
|
|
|
use App\Shop\Couriers\Courier; |
8
|
|
|
use App\Shop\Couriers\Repositories\CourierRepository; |
9
|
|
|
use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface; |
10
|
|
|
use App\Shop\Customers\Customer; |
11
|
|
|
use App\Shop\Customers\Repositories\CustomerRepository; |
12
|
|
|
use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface; |
13
|
|
|
use App\Shop\Orders\Order; |
14
|
|
|
use App\Shop\Orders\Repositories\Interfaces\OrderRepositoryInterface; |
15
|
|
|
use App\Shop\Orders\Repositories\OrderRepository; |
16
|
|
|
use App\Shop\OrderStatuses\OrderStatus; |
17
|
|
|
use App\Shop\OrderStatuses\Repositories\Interfaces\OrderStatusRepositoryInterface; |
18
|
|
|
use App\Shop\OrderStatuses\Repositories\OrderStatusRepository; |
19
|
|
|
use App\Http\Controllers\Controller; |
20
|
|
|
use Illuminate\Http\Request; |
21
|
|
|
use Illuminate\Support\Collection; |
22
|
|
|
|
23
|
|
|
class OrderController extends Controller |
24
|
|
|
{ |
25
|
|
|
use AddressTransformable; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var OrderRepositoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $orderRepo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CourierRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $courierRepo; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AddressRepositoryInterface |
39
|
|
|
*/ |
40
|
|
|
private $addressRepo; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CustomerRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
private $customerRepo; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var OrderStatusRepositoryInterface |
49
|
|
|
*/ |
50
|
|
|
private $orderStatusRepo; |
51
|
|
|
|
52
|
|
|
public function __construct( |
53
|
|
|
OrderRepositoryInterface $orderRepository, |
54
|
|
|
CourierRepositoryInterface $courierRepository, |
55
|
|
|
AddressRepositoryInterface $addressRepository, |
56
|
|
|
CustomerRepositoryInterface $customerRepository, |
57
|
|
|
OrderStatusRepositoryInterface $orderStatusRepository |
58
|
|
|
) { |
59
|
|
|
$this->orderRepo = $orderRepository; |
60
|
|
|
$this->courierRepo = $courierRepository; |
61
|
|
|
$this->addressRepo = $addressRepository; |
62
|
|
|
$this->customerRepo = $customerRepository; |
63
|
|
|
$this->orderStatusRepo = $orderStatusRepository; |
64
|
|
|
|
65
|
|
|
$this->middleware(['permission:update-order, guard:employee'], ['only' => ['edit', 'update']]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Display a listing of the resource. |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
|
|
public function index() |
74
|
|
|
{ |
75
|
|
|
$list = $this->orderRepo->listOrders('created_at', 'desc'); |
76
|
|
|
|
77
|
|
|
if (request()->has('q')) { |
78
|
|
|
$list = $this->orderRepo->searchOrder(request()->input('q') ?? ''); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$orders = $this->orderRepo->paginateArrayResults($this->transFormOrder($list), 10); |
82
|
|
|
|
83
|
|
|
return view('admin.orders.list', ['orders' => $orders]); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Display the specified resource. |
88
|
|
|
* |
89
|
|
|
* @param int $orderId |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
public function show($orderId) |
93
|
|
|
{ |
94
|
|
|
$order = $this->orderRepo->findOrderById($orderId); |
95
|
|
|
$order->courier = $this->courierRepo->findCourierById($order->courier_id); |
96
|
|
|
$order->address = $this->addressRepo->findAddressById($order->address_id); |
97
|
|
|
|
98
|
|
|
$orderRepo = new OrderRepository($order); |
99
|
|
|
|
100
|
|
|
$items = $orderRepo->listOrderedProducts(); |
101
|
|
|
|
102
|
|
|
return view('admin.orders.show', [ |
|
|
|
|
103
|
|
|
'order' => $order, |
104
|
|
|
'items' => $items, |
105
|
|
|
'customer' => $this->customerRepo->findCustomerById($order->customer_id), |
106
|
|
|
'currentStatus' => $this->orderStatusRepo->findOrderStatusById($order->order_status_id), |
107
|
|
|
'payment' => $order->payment, |
108
|
|
|
'user' => auth()->guard('employee')->user() |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $orderId |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
116
|
|
|
*/ |
117
|
|
|
public function edit($orderId) |
118
|
|
|
{ |
119
|
|
|
$order = $this->orderRepo->findOrderById($orderId); |
120
|
|
|
$order->courier = $this->courierRepo->findCourierById($order->courier_id); |
121
|
|
|
$order->address = $this->addressRepo->findAddressById($order->address_id); |
122
|
|
|
|
123
|
|
|
$orderRepo = new OrderRepository($order); |
124
|
|
|
|
125
|
|
|
$items = $orderRepo->listOrderedProducts(); |
126
|
|
|
|
127
|
|
|
return view('admin.orders.edit', [ |
128
|
|
|
'statuses' => $this->orderStatusRepo->listOrderStatuses(), |
129
|
|
|
'order' => $order, |
130
|
|
|
'items' => $items, |
131
|
|
|
'customer' => $this->customerRepo->findCustomerById($order->customer_id), |
132
|
|
|
'currentStatus' => $this->orderStatusRepo->findOrderStatusById($order->order_status_id), |
133
|
|
|
'payment' => $order->payment, |
134
|
|
|
'user' => auth()->guard('employee')->user() |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Request $request |
140
|
|
|
* @param $orderId |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\Http\RedirectResponse |
143
|
|
|
*/ |
144
|
|
|
public function update(Request $request, $orderId) |
145
|
|
|
{ |
146
|
|
|
$order = $this->orderRepo->findOrderById($orderId); |
147
|
|
|
$orderRepo = new OrderRepository($order); |
148
|
|
|
|
149
|
|
|
if ($request->has('total_paid') && $request->input('total_paid') != null) { |
150
|
|
|
$orderData = $request->except('_method', '_token'); |
151
|
|
|
} else { |
152
|
|
|
$orderData = $request->except('_method', '_token', 'total_paid'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$orderRepo->updateOrder($orderData); |
156
|
|
|
|
157
|
|
|
return redirect()->route('admin.orders.edit', $orderId); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Generate order invoice |
162
|
|
|
* |
163
|
|
|
* @param int $id |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function generateInvoice(int $id) |
167
|
|
|
{ |
168
|
|
|
$order = $this->orderRepo->findOrderById($id); |
169
|
|
|
|
170
|
|
|
$data = [ |
171
|
|
|
'order' => $order, |
172
|
|
|
'products' => $order->products, |
173
|
|
|
'customer' => $order->customer, |
174
|
|
|
'courier' => $order->courier, |
175
|
|
|
'address' => $this->transformAddress($order->address), |
176
|
|
|
'status' => $order->orderStatus, |
177
|
|
|
'payment' => $order->paymentMethod |
|
|
|
|
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
$pdf = app()->make('dompdf.wrapper'); |
181
|
|
|
$pdf->loadView('invoices.orders', $data)->stream(); |
182
|
|
|
return $pdf->stream(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param Collection $list |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
private function transFormOrder(Collection $list) |
190
|
|
|
{ |
191
|
|
|
$courierRepo = new CourierRepository(new Courier()); |
192
|
|
|
$customerRepo = new CustomerRepository(new Customer()); |
193
|
|
|
$orderStatusRepo = new OrderStatusRepository(new OrderStatus()); |
194
|
|
|
|
195
|
|
|
return $list->transform(function (Order $order) use ($courierRepo, $customerRepo, $orderStatusRepo) { |
196
|
|
|
$order->courier = $courierRepo->findCourierById($order->courier_id); |
197
|
|
|
$order->customer = $customerRepo->findCustomerById($order->customer_id); |
198
|
|
|
$order->status = $orderStatusRepo->findOrderStatusById($order->order_status_id); |
|
|
|
|
199
|
|
|
return $order; |
200
|
|
|
})->all(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|