|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Payment; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\Payment\PromotionRequest; |
|
6
|
|
|
use App\Model\Order\Invoice; |
|
7
|
|
|
use App\Model\Payment\PromoProductRelation; |
|
8
|
|
|
use App\Model\Payment\Promotion; |
|
9
|
|
|
use App\Model\Payment\PromotionType; |
|
10
|
|
|
use App\Model\Product\Product; |
|
11
|
|
|
use Darryldecode\Cart\CartCondition; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
class PromotionController extends BasePromotionController |
|
15
|
|
|
{ |
|
16
|
|
|
public $promotion; |
|
17
|
|
|
public $product; |
|
18
|
|
|
public $promoRelation; |
|
19
|
|
|
public $type; |
|
20
|
|
|
public $invoice; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->middleware('auth'); |
|
25
|
|
|
$this->middleware('admin'); |
|
26
|
|
|
|
|
27
|
|
|
$promotion = new Promotion(); |
|
28
|
|
|
$this->promotion = $promotion; |
|
29
|
|
|
|
|
30
|
|
|
$product = new Product(); |
|
31
|
|
|
$this->product = $product; |
|
32
|
|
|
|
|
33
|
|
|
$promoRelation = new PromoProductRelation(); |
|
34
|
|
|
$this->promoRelation = $promoRelation; |
|
35
|
|
|
|
|
36
|
|
|
$type = new PromotionType(); |
|
37
|
|
|
$this->type = $type; |
|
38
|
|
|
|
|
39
|
|
|
$invoice = new Invoice(); |
|
40
|
|
|
$this->invoice = $invoice; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Display a listing of the resource. |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Response |
|
47
|
|
|
*/ |
|
48
|
|
|
public function index() |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
|
|
return view('themes.default1.payment.promotion.index'); |
|
52
|
|
|
} catch (\Exception $ex) { |
|
53
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getPromotion() |
|
58
|
|
|
{ |
|
59
|
|
|
$new_promotion = $this->promotion->select('code', 'type', 'id')->get(); |
|
60
|
|
|
|
|
61
|
|
|
return\ DataTables::of($new_promotion) |
|
62
|
|
|
->addColumn('checkbox', function ($model) { |
|
63
|
|
|
return "<input type='checkbox' class='promotion_checkbox' |
|
64
|
|
|
value=".$model->id.' name=select[] id=check>'; |
|
65
|
|
|
}) |
|
66
|
|
|
->addColumn('code', function ($model) { |
|
67
|
|
|
return ucfirst($model->code); |
|
68
|
|
|
}) |
|
69
|
|
|
->addColumn('type', function ($model) { |
|
70
|
|
|
return $this->type->where('id', $model->type)->first()->name; |
|
71
|
|
|
}) |
|
72
|
|
|
->addColumn('products', function ($model) { |
|
73
|
|
|
$selected = $this->promoRelation->select('product_id') |
|
74
|
|
|
->where('promotion_id', $model->id)->get(); |
|
75
|
|
|
$result = []; |
|
76
|
|
|
foreach ($selected as $key => $select) { |
|
77
|
|
|
$result[$key] = $this->product->where('id', $select->product_id)->first()->name; |
|
78
|
|
|
} |
|
79
|
|
|
if (!empty($result)) { |
|
80
|
|
|
return implode(',', $result); |
|
81
|
|
|
} else { |
|
82
|
|
|
return 'None'; |
|
83
|
|
|
} |
|
84
|
|
|
}) |
|
85
|
|
|
->addColumn('action', function ($model) { |
|
86
|
|
|
return '<a href='.url('promotions/'.$model->id.'/edit') |
|
87
|
|
|
." class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' |
|
88
|
|
|
style='color:white;'> </i> Edit</a>"; |
|
89
|
|
|
}) |
|
90
|
|
|
->rawColumns(['checkbox', 'code', 'products', 'action']) |
|
91
|
|
|
|
|
92
|
|
|
->make(true); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Show the form for creating a new resource. |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Response |
|
99
|
|
|
*/ |
|
100
|
|
|
public function create() |
|
101
|
|
|
{ |
|
102
|
|
|
try { |
|
103
|
|
|
$product = $this->product->pluck('name', 'id')->toArray(); |
|
104
|
|
|
$type = $this->type->pluck('name', 'id')->toArray(); |
|
105
|
|
|
|
|
106
|
|
|
return view('themes.default1.payment.promotion.create', compact('product', 'type')); |
|
107
|
|
|
} catch (\Exception $ex) { |
|
108
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Store a newly created resource in storage. |
|
114
|
|
|
* |
|
115
|
|
|
* @return \Response |
|
116
|
|
|
*/ |
|
117
|
|
|
public function store(PromotionRequest $request) |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
|
|
$startdate = date_create($request->input('start')); |
|
121
|
|
|
$start = date_format($startdate, 'Y-m-d H:m:i'); |
|
122
|
|
|
$enddate = date_create($request->input('expiry')); |
|
123
|
|
|
$expiry = date_format($enddate, 'Y-m-d H:m:i'); |
|
124
|
|
|
$this->promotion->code = $request->input('code'); |
|
125
|
|
|
$this->promotion->type = $request->input('type'); |
|
126
|
|
|
$this->promotion->value = $request->input('value'); |
|
127
|
|
|
$this->promotion->uses = $request->input('uses'); |
|
128
|
|
|
$this->promotion->start = $start; |
|
129
|
|
|
$this->promotion->expiry = $expiry; |
|
130
|
|
|
$this->promotion->save(); |
|
131
|
|
|
//dd($this->promotion); |
|
132
|
|
|
$products = $request->input('applied'); |
|
133
|
|
|
|
|
134
|
|
|
foreach ($products as $product) { |
|
135
|
|
|
$this->promoRelation->create(['product_id' => $product, 'promotion_id' => $this->promotion->id]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
|
|
|
|
|
|
139
|
|
|
} catch (\Exception $ex) { |
|
140
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Show the form for editing the specified resource. |
|
146
|
|
|
* |
|
147
|
|
|
* @param int $id |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Response |
|
150
|
|
|
*/ |
|
151
|
|
|
public function edit($id) |
|
152
|
|
|
{ |
|
153
|
|
|
try { |
|
154
|
|
|
$promotion = $this->promotion->where('id', $id)->first(); |
|
155
|
|
|
$product = $this->product->pluck('name', 'id')->toArray(); |
|
156
|
|
|
$type = $this->type->pluck('name', 'id')->toArray(); |
|
157
|
|
|
$selectedProduct = $this->promoRelation |
|
158
|
|
|
->where('promotion_id', $id) |
|
159
|
|
|
->pluck('product_id', 'product_id')->toArray(); |
|
160
|
|
|
|
|
161
|
|
|
return view( |
|
162
|
|
|
'themes.default1.payment.promotion.edit', |
|
163
|
|
|
compact('product', 'promotion', 'selectedProduct', 'type') |
|
164
|
|
|
); |
|
165
|
|
|
} catch (\Exception $ex) { |
|
166
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Update the specified resource in storage. |
|
172
|
|
|
* |
|
173
|
|
|
* @param int $id |
|
174
|
|
|
* |
|
175
|
|
|
* @return \Response |
|
176
|
|
|
*/ |
|
177
|
|
|
public function update($id, PromotionRequest $request) |
|
178
|
|
|
{ |
|
179
|
|
|
try { |
|
180
|
|
|
$startdate = date_create($request->input('start')); |
|
181
|
|
|
$start = date_format($startdate, 'Y-m-d H:m:i'); |
|
182
|
|
|
$enddate = date_create($request->input('expiry')); |
|
183
|
|
|
$expiry = date_format($enddate, 'Y-m-d H:m:i'); |
|
184
|
|
|
|
|
185
|
|
|
$promotion = $this->promotion->where('id', $id)->update([ |
|
186
|
|
|
'code' => $request->input('code'), |
|
187
|
|
|
'type' => $request->input('type'), |
|
188
|
|
|
'value' => $request->input('value'), |
|
189
|
|
|
'uses' => $request->input('uses'), |
|
190
|
|
|
'start' => $start, |
|
191
|
|
|
'expiry' => $expiry, |
|
192
|
|
|
]); |
|
193
|
|
|
/* Delete the products has this id */ |
|
194
|
|
|
$deletes = $this->promoRelation->where('promotion_id', $id)->get(); |
|
195
|
|
|
foreach ($deletes as $delete) { |
|
196
|
|
|
$delete->delete(); |
|
197
|
|
|
} |
|
198
|
|
|
/* Update the realtion details */ |
|
199
|
|
|
$products = $request->input('applied'); |
|
200
|
|
|
foreach ($products as $product) { |
|
201
|
|
|
$this->promoRelation->create(['product_id' => $product, 'promotion_id' => $id]); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
|
|
|
|
|
|
205
|
|
|
} catch (\Exception $ex) { |
|
206
|
|
|
dd($ex); |
|
207
|
|
|
|
|
208
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Remove the specified resource from storage. |
|
214
|
|
|
* |
|
215
|
|
|
* @param int $id |
|
216
|
|
|
* |
|
217
|
|
|
* @return \Response |
|
218
|
|
|
*/ |
|
219
|
|
|
public function destroy(Request $request) |
|
220
|
|
|
{ |
|
221
|
|
|
try { |
|
222
|
|
|
$ids = $request->input('select'); |
|
223
|
|
|
if (!empty($ids)) { |
|
224
|
|
|
foreach ($ids as $id) { |
|
225
|
|
|
$promotion = $this->promotion->where('id', $id)->first(); |
|
226
|
|
|
if ($promotion) { |
|
227
|
|
|
$promotion->delete(); |
|
228
|
|
|
} else { |
|
229
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
230
|
|
|
<i class='fa fa-ban'></i> |
|
231
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
232
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
233
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
234
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
235
|
|
|
</div>'; |
|
236
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
240
|
|
|
<i class='fa fa-ban'></i> |
|
241
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
242
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.success').' |
|
243
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
244
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
245
|
|
|
</div>'; |
|
246
|
|
|
} else { |
|
247
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
248
|
|
|
<i class='fa fa-ban'></i> |
|
249
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
250
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
251
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
252
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
253
|
|
|
</div>'; |
|
254
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
255
|
|
|
} |
|
256
|
|
|
} catch (\Exception $e) { |
|
257
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
258
|
|
|
<i class='fa fa-ban'></i> |
|
259
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
260
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
261
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
262
|
|
|
'.$e->getMessage().' |
|
263
|
|
|
</div>'; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function checkCode($code, $productid) |
|
268
|
|
|
{ |
|
269
|
|
|
try { |
|
270
|
|
|
$inv_cont = new \App\Http\Controllers\Order\InvoiceController(); |
|
271
|
|
|
$promo = $inv_cont->getPromotionDetails($code); |
|
272
|
|
|
$value = $this->findCostAfterDiscount($promo->id, $productid); |
|
273
|
|
|
$coupon = new CartCondition([ |
|
274
|
|
|
'name' => $promo->code, |
|
275
|
|
|
'type' => 'coupon', |
|
276
|
|
|
'target' => 'item', |
|
277
|
|
|
'value' => $value, |
|
278
|
|
|
]); |
|
279
|
|
|
if (\Session::get('usage') == null || \Session::get('usage') != 1) { |
|
280
|
|
|
$userId = \Auth::user()->id; |
|
281
|
|
|
\Cart::update($productid, [ |
|
282
|
|
|
'id' => $productid, |
|
283
|
|
|
'price' => $value, |
|
284
|
|
|
'conditions' => $coupon, |
|
285
|
|
|
|
|
286
|
|
|
// new item price, price can also be a string format like so: '98.67' |
|
287
|
|
|
]); |
|
288
|
|
|
\Session::put('usage', 1); |
|
289
|
|
|
\Session::put('code', $promo->code); |
|
290
|
|
|
\Session::put('codevalue', $promo->value); |
|
291
|
|
|
} |
|
292
|
|
|
$items = \Cart::getContent(); |
|
293
|
|
|
\Session::put('items', $items); |
|
294
|
|
|
|
|
295
|
|
|
foreach ($items as $item) { |
|
296
|
|
|
if ($item->conditions) { |
|
297
|
|
|
// if (count($item->conditions) == 2 || count($item->conditions) == 1) { |
|
298
|
|
|
\Cart::addItemCondition($productid, $coupon); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return 'success'; |
|
303
|
|
|
} catch (\Exception $ex) { |
|
304
|
|
|
if (!\Auth::user()) { |
|
305
|
|
|
throw new \Exception('Please Login'); |
|
306
|
|
|
} else { |
|
307
|
|
|
throw new \Exception($ex->getMessage()); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
public function checkNumberOfUses($code) |
|
313
|
|
|
{ |
|
314
|
|
|
try { |
|
315
|
|
|
$promotion = $this->promotion->where('code', $code)->first(); |
|
316
|
|
|
$uses = $promotion->uses; |
|
317
|
|
|
if ($uses == 0) { |
|
318
|
|
|
return 'success'; |
|
319
|
|
|
} |
|
320
|
|
|
$used_number = $this->invoice->where('coupon_code', $code)->count(); |
|
321
|
|
|
if ($uses >= $used_number) { |
|
322
|
|
|
return 'success'; |
|
323
|
|
|
} else { |
|
324
|
|
|
return 'fails'; |
|
325
|
|
|
} |
|
326
|
|
|
} catch (\Exception $ex) { |
|
327
|
|
|
throw new \Exception(\Lang::get('message.find-cost-error')); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
public function checkExpiry($code) |
|
332
|
|
|
{ |
|
333
|
|
|
try { |
|
334
|
|
|
$promotion = $this->promotion->where('code', $code)->first(); |
|
335
|
|
|
$start = $promotion->start; |
|
336
|
|
|
$end = $promotion->expiry; |
|
337
|
|
|
$now = \Carbon\Carbon::now(); |
|
338
|
|
|
$inv_cont = new \App\Http\Controllers\Order\InvoiceController(); |
|
339
|
|
|
$getExpiryStatus = $inv_cont->getExpiryStatus($start, $end, $now); |
|
340
|
|
|
|
|
341
|
|
|
return $getExpiryStatus; |
|
342
|
|
|
} catch (\Exception $ex) { |
|
343
|
|
|
throw new \Exception(\Lang::get('message.check-expiry')); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|