1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Events\OrderCreated; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Http\Requests\Order\StoreRequest; |
8
|
|
|
use App\Http\Resources\DataTables\OrderResource; |
9
|
|
|
use App\Models\Branch; |
10
|
|
|
use App\Models\Order; |
11
|
|
|
use App\Models\User; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
use Illuminate\Support\Facades\Event; |
16
|
|
|
use Yajra\DataTables\Facades\DataTables; |
17
|
|
|
|
18
|
|
|
class OrderController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Create a new instance class. |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct() |
26
|
|
|
{ |
27
|
4 |
|
$this->authorizeResource(Order::class, 'order'); |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Display a listing of the resource. |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Contracts\Support\Renderable |
34
|
|
|
*/ |
35
|
1 |
|
public function index() |
36
|
|
|
{ |
37
|
1 |
|
return view('admin.order.index'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return datatable server side response. |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\JsonResponse |
44
|
|
|
*/ |
45
|
1 |
|
public function datatable() |
46
|
|
|
{ |
47
|
1 |
|
$this->authorize('viewAny', Order::class); |
48
|
|
|
|
49
|
1 |
|
return DataTables::eloquent(Order::query()->with('user:id,fullname')) |
50
|
1 |
|
->setTransformer(fn ($model) => OrderResource::make($model)->resolve()) |
51
|
1 |
|
->orderColumn('customer_fullname', function ($query, $direction) { |
52
|
|
|
$query->join('customers', 'orders.customer_id', '=', 'customers.id') |
53
|
|
|
->select('orders.*', 'customers.id as customer_id', 'customers.fullname as customer_fullname') |
54
|
|
|
->orderBy('customers.fullname', $direction); |
55
|
1 |
|
}) |
56
|
1 |
|
->filterColumn('customer_fullname', function ($query, $keyword) { |
57
|
|
|
$query->whereHas('customer', function ($query) use ($keyword) { |
58
|
|
|
$query->where('fullname', 'like', '%' . $keyword . '%'); |
59
|
|
|
}); |
60
|
1 |
|
}) |
61
|
1 |
|
->orderColumn('status', function ($query, $direction) { |
62
|
|
|
$query->join('order_statuses', 'order_statuses.order_id', '=', 'orders.id') |
63
|
|
|
->select('orders.*', 'order_statuses.id as order_status_id', 'order_statuses.status as order_status') |
64
|
|
|
->orderBy('order_statuses.status', $direction); |
65
|
1 |
|
}) |
66
|
1 |
|
->filterColumn('status', function ($query, $keyword) { |
67
|
|
|
$query->whereHas('latestStatus', function ($query) use ($keyword) { |
68
|
|
|
$query->where('status', 'like', '%' . $keyword . '%'); |
69
|
|
|
}); |
70
|
1 |
|
}) |
71
|
1 |
|
->toJson(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return datatable row child data. |
76
|
|
|
* |
77
|
|
|
* @param \App\Models\Order $order |
78
|
|
|
* @return \Illuminate\Contracts\Support\Renderable |
79
|
|
|
*/ |
80
|
|
|
public function datatableRowChild(Order $order) |
81
|
|
|
{ |
82
|
|
|
$this->authorize('view', $order); |
83
|
|
|
|
84
|
|
|
return view('admin.order.datatable-row-child', compact('order')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Store a newly created resource in storage. |
89
|
|
|
* |
90
|
|
|
* Note: This method now accessed from customer page. |
91
|
|
|
* |
92
|
|
|
* @param \App\Http\Requests\Order\StoreRequest $request |
93
|
|
|
* @return \Illuminate\Http\RedirectResponse |
94
|
|
|
*/ |
95
|
|
|
public function store(StoreRequest $request) |
96
|
|
|
{ |
97
|
|
|
$alert = [ |
98
|
|
|
'alert' => [ |
99
|
|
|
'type' => 'alert-success', |
100
|
|
|
'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.order')]), |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$order = DB::transaction(function () use ($request) { |
106
|
|
|
$order = $request->getOrder(); |
107
|
|
|
|
108
|
|
|
$order->setCustomerRelationValue($request->getCustomer()); |
109
|
|
|
transform($request->getUser(), fn (User $user) => $order->setUserRelationValue($user)); |
110
|
|
|
transform($request->getBranch(), fn (Branch $branch) => $order->setBranchRelationValue($branch)); |
111
|
|
|
|
112
|
|
|
$order->save(); |
113
|
|
|
|
114
|
|
|
$order->statuses()->save($request->getOrderStatus()); |
115
|
|
|
|
116
|
|
|
return $order; |
117
|
|
|
}); |
118
|
|
|
} catch (\Throwable $th) { |
119
|
|
|
throw $th; |
120
|
|
|
|
121
|
|
|
$alert = [ |
|
|
|
|
122
|
|
|
'alert' => [ |
123
|
|
|
'type' => 'alert-danger', |
124
|
|
|
'message' => $th->getMessage(), |
125
|
|
|
], |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
Event::dispatch(new OrderCreated($order)); |
130
|
|
|
|
131
|
|
|
return redirect()->route('admin.order.show', $order)->with($alert); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Display the specified resource. |
136
|
|
|
* |
137
|
|
|
* @param \App\Models\Order $order |
138
|
|
|
* @return \Illuminate\Contracts\Support\Renderable |
139
|
|
|
*/ |
140
|
|
|
public function show(Order $order) |
141
|
|
|
{ |
142
|
|
|
$order->load([ |
143
|
|
|
'statuses', |
144
|
|
|
'items' => fn (Relation $query) => $query->with('denomination'), |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
return view('admin.order.show', compact('order')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Remove the specified resource from storage. |
152
|
|
|
* |
153
|
|
|
* @param \App\Models\Order $order |
154
|
|
|
* @return \Illuminate\Http\RedirectResponse |
155
|
|
|
*/ |
156
|
1 |
|
public function destroy(Order $order) |
157
|
|
|
{ |
158
|
1 |
|
DB::transaction(fn () => $order->delete()); |
159
|
|
|
|
160
|
1 |
|
return redirect()->route('admin.order.index')->with([ |
161
|
|
|
'alert' => [ |
162
|
1 |
|
'type' => 'alert-success', |
163
|
1 |
|
'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.order')]), |
164
|
|
|
], |
165
|
|
|
]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Remove the specified list of resource from storage. |
170
|
|
|
* |
171
|
|
|
* @param \Illuminate\Http\Request $request |
172
|
|
|
* @return \Illuminate\Http\RedirectResponse |
173
|
|
|
*/ |
174
|
1 |
|
public function destroyMultiple(Request $request) |
175
|
|
|
{ |
176
|
1 |
|
DB::transaction(function () use ($request) { |
177
|
1 |
|
foreach ($request->input('checkbox', []) as $id) { |
178
|
1 |
|
$order = Order::find($id, 'id'); |
179
|
|
|
|
180
|
1 |
|
$this->authorize('delete', $order); |
181
|
|
|
|
182
|
1 |
|
$order->delete(); |
183
|
|
|
} |
184
|
1 |
|
}); |
185
|
|
|
|
186
|
1 |
|
return redirect()->route('admin.order.index')->with([ |
187
|
|
|
'alert' => [ |
188
|
1 |
|
'type' => 'alert-success', |
189
|
1 |
|
'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.order')]), |
190
|
|
|
], |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.