|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Order\Invoice; |
|
6
|
|
|
use App\Model\Payment\Plan; |
|
7
|
|
|
use App\Model\Payment\PlanPrice; |
|
8
|
|
|
use App\Model\Payment\Promotion; |
|
9
|
|
|
use App\Model\Product\Product; |
|
10
|
|
|
use Bugsnag; |
|
11
|
|
|
use Illuminate\Http\Request; |
|
12
|
|
|
|
|
13
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
14
|
|
|
// ADVANCE SEARCH FOR INVOICE AND COUPON CODE CALCULATION |
|
15
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
16
|
|
|
|
|
17
|
|
|
trait CoupCodeAndInvoiceSearch |
|
18
|
|
|
{ |
|
19
|
|
|
public function checkCode($code, $productid, $currency) |
|
20
|
|
|
{ |
|
21
|
|
|
try { |
|
22
|
|
|
if ($code != '') { |
|
23
|
|
|
$promo = $this->promotion->where('code', $code)->first(); |
|
24
|
|
|
//check promotion code is valid |
|
25
|
|
|
if (!$promo) { |
|
26
|
|
|
throw new \Exception(\Lang::get('message.no-such-code')); |
|
27
|
|
|
} |
|
28
|
|
|
$relation = $promo->relation()->get(); |
|
29
|
|
|
//check the relation between code and product |
|
30
|
|
|
if (count($relation) == 0) { |
|
31
|
|
|
throw new \Exception(\Lang::get('message.no-product-related-to-this-code')); |
|
32
|
|
|
} |
|
33
|
|
|
//check the usess |
|
34
|
|
|
$cont = new \App\Http\Controllers\Payment\PromotionController(); |
|
35
|
|
|
$uses = $cont->checkNumberOfUses($code); |
|
36
|
|
|
if ($uses != 'success') { |
|
37
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-completed')); |
|
38
|
|
|
} |
|
39
|
|
|
//check for the expiry date |
|
40
|
|
|
$expiry = $this->checkExpiry($code); |
|
|
|
|
|
|
41
|
|
|
if ($expiry != 'success') { |
|
42
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-expired')); |
|
43
|
|
|
} |
|
44
|
|
|
$value = $this->findCostAfterDiscount($promo->id, $productid, $currency); |
|
45
|
|
|
|
|
46
|
|
|
return $value; |
|
47
|
|
|
} else { |
|
48
|
|
|
$product = $this->product->find($productid); |
|
49
|
|
|
$plans = Plan::where('product', $product)->pluck('id')->first(); |
|
50
|
|
|
$price = PlanPrice::where('currency', $currency)->where('plan_id', $plans)->pluck('add_price')->first(); |
|
51
|
|
|
|
|
52
|
|
|
return $price; |
|
53
|
|
|
} |
|
54
|
|
|
} catch (\Exception $ex) { |
|
55
|
|
|
throw new \Exception($ex->getMessage()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function findCostAfterDiscount($promoid, $productid, $currency) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$promotion = Promotion::findOrFail($promoid); |
|
63
|
|
|
$product = Product::findOrFail($productid); |
|
64
|
|
|
$promotion_type = $promotion->type; |
|
65
|
|
|
$promotion_value = $promotion->value; |
|
66
|
|
|
$planId = Plan::where('product', $productid)->pluck('id')->first(); |
|
67
|
|
|
// dd($planId); |
|
68
|
|
|
$product_price = PlanPrice::where('plan_id', $planId) |
|
69
|
|
|
->where('currency', $currency)->pluck('add_price')->first(); |
|
70
|
|
|
$updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
|
71
|
|
|
|
|
72
|
|
|
return $updated_price; |
|
73
|
|
|
} catch (\Exception $ex) { |
|
74
|
|
|
Bugsnag::notifyException($ex); |
|
75
|
|
|
|
|
76
|
|
|
throw new \Exception(\Lang::get('message.find-discount-error')); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function findCost($type, $value, $price, $productid) |
|
81
|
|
|
{ |
|
82
|
|
|
$price = intval($price); |
|
83
|
|
|
switch ($type) { |
|
84
|
|
|
case 1: |
|
85
|
|
|
$percentage = $price * ($value / 100); |
|
86
|
|
|
|
|
87
|
|
|
return $price - $percentage; |
|
88
|
|
|
case 2: |
|
89
|
|
|
return $price - $value; |
|
90
|
|
|
case 3: |
|
91
|
|
|
return $value; |
|
92
|
|
|
case 4: |
|
93
|
|
|
return 0; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function advanceSearch($name = '', $invoice_no = '', $currency = '', $status = '', $from = '', $till = '') |
|
98
|
|
|
{ |
|
99
|
|
|
$join = Invoice::leftJoin('users', 'invoices.user_id', '=', 'users.id'); |
|
100
|
|
|
$this->name($name, $join); |
|
101
|
|
|
$this->invoice_no($invoice_no, $join); |
|
102
|
|
|
$this->status($status, $join); |
|
103
|
|
|
$this->searchcurrency($currency, $join); |
|
104
|
|
|
|
|
105
|
|
|
$this->invoice_from($from, $till, $join); |
|
106
|
|
|
$this->till_date($till, $from, $join); |
|
107
|
|
|
|
|
108
|
|
|
$join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at'); |
|
109
|
|
|
|
|
110
|
|
|
$join = $join->orderBy('created_at', 'desc') |
|
111
|
|
|
->select( |
|
112
|
|
|
'invoices.id', |
|
113
|
|
|
'first_name', |
|
114
|
|
|
'invoices.created_at', |
|
115
|
|
|
'invoices.currency', |
|
116
|
|
|
'user_id', |
|
117
|
|
|
'invoices.grand_total', |
|
118
|
|
|
'number', |
|
119
|
|
|
'status' |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
return $join; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function name($name, $join) |
|
126
|
|
|
{ |
|
127
|
|
|
if ($name) { |
|
128
|
|
|
$join = $join->where('first_name', $name); |
|
129
|
|
|
|
|
130
|
|
|
return $join; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function invoice_no($invoice_no, $join) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
if ($invoice_no) { |
|
137
|
|
|
$join = $join->where('number', $invoice_no); |
|
138
|
|
|
|
|
139
|
|
|
return $join; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function status($status, $join) |
|
144
|
|
|
{ |
|
145
|
|
|
if ($status) { |
|
146
|
|
|
$join = $join->where('status', $status); |
|
147
|
|
|
|
|
148
|
|
|
return $join; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function searchcurrency($currency, $join) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($currency) { |
|
155
|
|
|
$join = $join->where('invoices.currency', $currency); |
|
156
|
|
|
|
|
157
|
|
|
return $join; |
|
158
|
|
|
} |
|
159
|
|
|
$return; |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function invoice_from($from, $till, $join) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
if ($from) { |
|
165
|
|
|
$fromdate = date_create($from); |
|
166
|
|
|
$from = date_format($fromdate, 'Y-m-d H:m:i'); |
|
167
|
|
|
$tills = date('Y-m-d H:m:i'); |
|
168
|
|
|
$tillDate = $this->getTillDate($from, $till, $tills); |
|
169
|
|
|
$join = $join->whereBetween('invoices.created_at', [$from, $tillDate]); |
|
170
|
|
|
|
|
171
|
|
|
return $join; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $join; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function till_date($till, $from, $join) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
if ($till) { |
|
180
|
|
|
$tilldate = date_create($till); |
|
181
|
|
|
$till = date_format($tilldate, 'Y-m-d H:m:i'); |
|
182
|
|
|
$froms = Invoice::first()->created_at; |
|
183
|
|
|
$fromDate = $this->getFromDate($from, $froms); |
|
184
|
|
|
$join = $join->whereBetween('invoices.created_at', [$fromDate, $till]); |
|
185
|
|
|
|
|
186
|
|
|
return $join; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function getExpiryStatus($start, $end, $now) |
|
191
|
|
|
{ |
|
192
|
|
|
$whenDateNotSet = $this->whenDateNotSet($start, $end); |
|
|
|
|
|
|
193
|
|
|
if ($whenDateNotSet) { |
|
194
|
|
|
return $whenDateNotSet; |
|
195
|
|
|
} |
|
196
|
|
|
$whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
|
|
|
|
|
|
197
|
|
|
if ($whenStartDateSet) { |
|
198
|
|
|
return $whenStartDateSet; |
|
199
|
|
|
} |
|
200
|
|
|
$whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
|
|
|
|
|
|
201
|
|
|
if ($whenEndDateSet) { |
|
202
|
|
|
return $whenEndDateSet; |
|
203
|
|
|
} |
|
204
|
|
|
$whenBothAreSet = $this->whenBothSet($start, $end, $now); |
|
|
|
|
|
|
205
|
|
|
if ($whenBothAreSet) { |
|
206
|
|
|
return $whenBothAreSet; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getTillDate($from, $till, $tills) |
|
211
|
|
|
{ |
|
212
|
|
|
if ($till) { |
|
213
|
|
|
$todate = date_create($till); |
|
214
|
|
|
$tills = date_format($todate, 'Y-m-d H:m:i'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $tills; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function getFromDate($from, $froms) |
|
221
|
|
|
{ |
|
222
|
|
|
if ($from) { |
|
223
|
|
|
$fromdate = date_create($from); |
|
224
|
|
|
$froms = date_format($fromdate, 'Y-m-d H:m:i'); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $froms; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
|
231
|
|
|
{ |
|
232
|
|
|
try { |
|
233
|
|
|
$invoice = Invoice::find($invoiceid); |
|
234
|
|
|
$invoice_status = 'pending'; |
|
235
|
|
|
|
|
236
|
|
|
$payment = $this->payment->create([ |
|
237
|
|
|
'invoice_id' => $invoiceid, |
|
238
|
|
|
'user_id' => $invoice->user_id, |
|
239
|
|
|
'amount' => $amount, |
|
240
|
|
|
'payment_method' => $payment_method, |
|
241
|
|
|
'payment_status' => $payment_status, |
|
242
|
|
|
'created_at' => $payment_date, |
|
243
|
|
|
]); |
|
244
|
|
|
$all_payments = $this->payment |
|
245
|
|
|
->where('invoice_id', $invoiceid) |
|
246
|
|
|
->where('payment_status', 'success') |
|
247
|
|
|
->pluck('amount')->toArray(); |
|
248
|
|
|
$total_paid = array_sum($all_payments); |
|
249
|
|
|
if ($total_paid >= $invoice->grand_total) { |
|
250
|
|
|
$invoice_status = 'success'; |
|
251
|
|
|
} |
|
252
|
|
|
if ($invoice) { |
|
253
|
|
|
$invoice->status = $invoice_status; |
|
254
|
|
|
$invoice->save(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $payment; |
|
258
|
|
|
} catch (\Exception $ex) { |
|
259
|
|
|
Bugsnag::notifyException($ex); |
|
260
|
|
|
|
|
261
|
|
|
throw new \Exception($ex->getMessage()); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Remove the specified resource from storage. |
|
267
|
|
|
* |
|
268
|
|
|
* @param int $id |
|
269
|
|
|
* |
|
270
|
|
|
* @return \Response |
|
271
|
|
|
*/ |
|
272
|
|
|
public function destroy(Request $request) |
|
273
|
|
|
{ |
|
274
|
|
|
try { |
|
275
|
|
|
$ids = $request->input('select'); |
|
276
|
|
|
if (!empty($ids)) { |
|
277
|
|
|
foreach ($ids as $id) { |
|
278
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
279
|
|
|
if ($invoice) { |
|
280
|
|
|
$invoice->delete(); |
|
281
|
|
|
} else { |
|
282
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
283
|
|
|
<i class='fa fa-ban'></i> |
|
284
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
285
|
|
|
/* @scrutinizer ignore-type */ |
|
286
|
|
|
\Lang::get('message.failed').' |
|
287
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
288
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
289
|
|
|
</div>'; |
|
290
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
294
|
|
|
<i class='fa fa-ban'></i> |
|
295
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
296
|
|
|
/* @scrutinizer ignore-type */ |
|
297
|
|
|
\Lang::get('message.success').' |
|
298
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
299
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
300
|
|
|
</div>'; |
|
301
|
|
|
} else { |
|
302
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
303
|
|
|
<i class='fa fa-ban'></i> |
|
304
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
305
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
306
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
307
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
308
|
|
|
</div>'; |
|
309
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
310
|
|
|
} |
|
311
|
|
|
} catch (\Exception $e) { |
|
312
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
313
|
|
|
<i class='fa fa-ban'></i> |
|
314
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
315
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
316
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
317
|
|
|
'.$e->getMessage().' |
|
318
|
|
|
</div>'; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function deletePayment(Request $request) |
|
323
|
|
|
{ |
|
324
|
|
|
try { |
|
325
|
|
|
$ids = $request->input('select'); |
|
326
|
|
|
if (!empty($ids)) { |
|
327
|
|
|
foreach ($ids as $id) { |
|
328
|
|
|
$payment = $this->payment->where('id', $id)->first(); |
|
329
|
|
|
if ($payment) { |
|
330
|
|
|
$invoice = $this->invoice->find($payment->invoice_id); |
|
331
|
|
|
if ($invoice) { |
|
332
|
|
|
$invoice->status = 'pending'; |
|
333
|
|
|
$invoice->save(); |
|
334
|
|
|
} |
|
335
|
|
|
$payment->delete(); |
|
336
|
|
|
} else { |
|
337
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
338
|
|
|
<i class='fa fa-ban'></i> |
|
339
|
|
|
<b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> |
|
340
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
341
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
342
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
343
|
|
|
</div>'; |
|
344
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
348
|
|
|
<i class='fa fa-ban'></i> |
|
349
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
350
|
|
|
/* @scrutinizer ignore-type */ |
|
351
|
|
|
\Lang::get('message.success').' |
|
352
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
353
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
354
|
|
|
</div>'; |
|
355
|
|
|
} else { |
|
356
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
357
|
|
|
<i class='fa fa-ban'></i> |
|
358
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
359
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
360
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
361
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
362
|
|
|
</div>'; |
|
363
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
364
|
|
|
} |
|
365
|
|
|
} catch (\Exception $e) { |
|
366
|
|
|
Bugsnag::notifyException($e); |
|
367
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
368
|
|
|
<i class='fa fa-ban'></i> |
|
369
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
370
|
|
|
/* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
|
371
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
372
|
|
|
'.$e->getMessage().' |
|
373
|
|
|
</div>'; |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
public function getCodeValue($promo,$code) |
|
378
|
|
|
{ |
|
379
|
|
|
if($promo && $code) { |
|
380
|
|
|
return $promo->value; |
|
381
|
|
|
} |
|
382
|
|
|
return ; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|