|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Order\Invoice; |
|
6
|
|
|
use Bugsnag; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
10
|
|
|
// ADVANCE SEARCH FOR INVOICE AND COUPON CODE CALCULATION |
|
11
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
12
|
|
|
|
|
13
|
|
|
trait CoupCodeAndInvoiceSearch |
|
14
|
|
|
{ |
|
15
|
|
|
public function advanceSearch($name = '', $invoice_no = '', $currency = '', $status = '', $from = '', $till = '') |
|
16
|
|
|
{ |
|
17
|
|
|
$join = Invoice::leftJoin('users', 'invoices.user_id', '=', 'users.id'); |
|
18
|
|
|
$this->name($name, $join); |
|
19
|
|
|
$this->invoice_no($invoice_no, $join); |
|
20
|
|
|
$this->status($status, $join); |
|
21
|
|
|
$this->searchcurrency($currency, $join); |
|
22
|
|
|
|
|
23
|
|
|
$this->invoice_from($from, $till, $join); |
|
24
|
|
|
$this->till_date($till, $from, $join); |
|
25
|
|
|
|
|
26
|
|
|
$join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at'); |
|
27
|
|
|
|
|
28
|
|
|
$join = $join->orderBy('created_at', 'desc') |
|
29
|
|
|
->select( |
|
30
|
|
|
'invoices.id', |
|
31
|
|
|
'first_name', |
|
32
|
|
|
'invoices.created_at', |
|
33
|
|
|
'invoices.currency', |
|
34
|
|
|
'user_id', |
|
35
|
|
|
'invoices.grand_total', |
|
36
|
|
|
'number', |
|
37
|
|
|
'status' |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
return $join; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function name($name, $join) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($name) { |
|
46
|
|
|
$join = $join->where('first_name', $name); |
|
47
|
|
|
|
|
48
|
|
|
return $join; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function invoice_no($invoice_no, $join) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($invoice_no) { |
|
55
|
|
|
$join = $join->where('number', $invoice_no); |
|
56
|
|
|
|
|
57
|
|
|
return $join; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function status($status, $join) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($status) { |
|
64
|
|
|
$join = $join->where('status', $status); |
|
65
|
|
|
|
|
66
|
|
|
return $join; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function searchcurrency($currency, $join) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($currency) { |
|
73
|
|
|
$join = $join->where('invoices.currency', $currency); |
|
74
|
|
|
|
|
75
|
|
|
return $join; |
|
76
|
|
|
} |
|
77
|
|
|
$return; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function invoice_from($from, $till, $join) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($from) { |
|
83
|
|
|
$fromdate = date_create($from); |
|
84
|
|
|
$from = date_format($fromdate, 'Y-m-d H:m:i'); |
|
85
|
|
|
$tills = date('Y-m-d H:m:i'); |
|
86
|
|
|
$tillDate = $this->getTillDate($from, $till, $tills); |
|
87
|
|
|
$join = $join->whereBetween('invoices.created_at', [$from, $tillDate]); |
|
88
|
|
|
|
|
89
|
|
|
return $join; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $join; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function till_date($till, $from, $join) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($till) { |
|
98
|
|
|
$tilldate = date_create($till); |
|
99
|
|
|
$till = date_format($tilldate, 'Y-m-d H:m:i'); |
|
100
|
|
|
$froms = Invoice::first()->created_at; |
|
101
|
|
|
$fromDate = $this->getFromDate($from, $froms); |
|
102
|
|
|
$join = $join->whereBetween('invoices.created_at', [$fromDate, $till]); |
|
103
|
|
|
|
|
104
|
|
|
return $join; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getTillDate($from, $till, $tills) |
|
109
|
|
|
{ |
|
110
|
|
|
if ($till) { |
|
111
|
|
|
$todate = date_create($till); |
|
112
|
|
|
$tills = date_format($todate, 'Y-m-d H:m:i'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $tills; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getFromDate($from, $froms) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($from) { |
|
121
|
|
|
$fromdate = date_create($from); |
|
122
|
|
|
$froms = date_format($fromdate, 'Y-m-d H:m:i'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $froms; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
|
129
|
|
|
{ |
|
130
|
|
|
try { |
|
131
|
|
|
$invoice = Invoice::find($invoiceid); |
|
132
|
|
|
$processingFee = ''; |
|
133
|
|
|
foreach (\Cart::getConditionsByType('fee') as $processFee) { |
|
134
|
|
|
$processingFee = $processFee->getValue(); |
|
135
|
|
|
} |
|
136
|
|
|
$invoice_status = 'pending'; |
|
137
|
|
|
|
|
138
|
|
|
$payment = $this->payment->create([ |
|
139
|
|
|
'invoice_id' => $invoiceid, |
|
140
|
|
|
'user_id' => $invoice->user_id, |
|
141
|
|
|
'amount' => $amount, |
|
142
|
|
|
'payment_method' => $payment_method, |
|
143
|
|
|
'payment_status' => $payment_status, |
|
144
|
|
|
'created_at' => $payment_date, |
|
145
|
|
|
]); |
|
146
|
|
|
$all_payments = $this->payment |
|
147
|
|
|
->where('invoice_id', $invoiceid) |
|
148
|
|
|
->where('payment_status', 'success') |
|
149
|
|
|
->pluck('amount')->toArray(); |
|
150
|
|
|
$total_paid = array_sum($all_payments); |
|
151
|
|
|
if ($total_paid >= $invoice->grand_total) { |
|
152
|
|
|
$invoice_status = 'success'; |
|
153
|
|
|
} |
|
154
|
|
|
if ($invoice) { |
|
155
|
|
|
$sessionValue = $this->getCodeFromSession(); |
|
|
|
|
|
|
156
|
|
|
$code = $sessionValue['code']; |
|
157
|
|
|
$codevalue = $sessionValue['codevalue']; |
|
158
|
|
|
$invoice->discount = $codevalue; |
|
159
|
|
|
$invoice->coupon_code = $code; |
|
160
|
|
|
$invoice->processing_fee = $processingFee; |
|
161
|
|
|
$invoice->status = $invoice_status; |
|
162
|
|
|
$invoice->save(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $payment; |
|
166
|
|
|
} catch (\Exception $ex) { |
|
167
|
|
|
Bugsnag::notifyException($ex); |
|
168
|
|
|
|
|
169
|
|
|
throw new \Exception($ex->getMessage()); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Remove the specified resource from storage. |
|
175
|
|
|
* |
|
176
|
|
|
* @param int $id |
|
177
|
|
|
* |
|
178
|
|
|
* @return \Response |
|
179
|
|
|
*/ |
|
180
|
|
|
public function destroy(Request $request) |
|
181
|
|
|
{ |
|
182
|
|
|
try { |
|
183
|
|
|
$ids = $request->input('select'); |
|
184
|
|
|
if (! empty($ids)) { |
|
185
|
|
|
foreach ($ids as $id) { |
|
186
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
187
|
|
|
if ($invoice) { |
|
188
|
|
|
$invoice->delete(); |
|
189
|
|
|
} else { |
|
190
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
191
|
|
|
<i class='fa fa-ban'></i> |
|
192
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
193
|
|
|
/* @scrutinizer ignore-type */ |
|
194
|
|
|
\Lang::get('message.failed').' |
|
195
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
196
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
197
|
|
|
</div>'; |
|
198
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
202
|
|
|
<i class='fa fa-check'></i> |
|
203
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
204
|
|
|
/* @scrutinizer ignore-type */ |
|
205
|
|
|
\Lang::get('message.success').' |
|
206
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
207
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
208
|
|
|
</div>'; |
|
209
|
|
|
} else { |
|
210
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
211
|
|
|
<i class='fa fa-ban'></i> |
|
212
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
213
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
214
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
215
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
216
|
|
|
</div>'; |
|
217
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
218
|
|
|
} |
|
219
|
|
|
} catch (\Exception $e) { |
|
220
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
221
|
|
|
<i class='fa fa-ban'></i> |
|
222
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
223
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
224
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
225
|
|
|
'.$e->getMessage().' |
|
226
|
|
|
</div>'; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function deletePayment(Request $request) |
|
231
|
|
|
{ |
|
232
|
|
|
try { |
|
233
|
|
|
$ids = $request->input('select'); |
|
234
|
|
|
if (! empty($ids)) { |
|
235
|
|
|
foreach ($ids as $id) { |
|
236
|
|
|
$payment = $this->payment->where('id', $id)->first(); |
|
237
|
|
|
if ($payment) { |
|
238
|
|
|
$invoice = $this->invoice->find($payment->invoice_id); |
|
239
|
|
|
if ($invoice) { |
|
240
|
|
|
$invoice->status = 'pending'; |
|
241
|
|
|
$invoice->save(); |
|
242
|
|
|
} |
|
243
|
|
|
$payment->delete(); |
|
244
|
|
|
} else { |
|
245
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
246
|
|
|
<i class='fa fa-ban'></i> |
|
247
|
|
|
<b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> |
|
248
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
249
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
250
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
251
|
|
|
</div>'; |
|
252
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
256
|
|
|
<i class='fa fa-ban'></i> |
|
257
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
258
|
|
|
/* @scrutinizer ignore-type */ |
|
259
|
|
|
\Lang::get('message.success').' |
|
260
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
261
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
262
|
|
|
</div>'; |
|
263
|
|
|
} else { |
|
264
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
265
|
|
|
<i class='fa fa-ban'></i> |
|
266
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
267
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
268
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
269
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
270
|
|
|
</div>'; |
|
271
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
272
|
|
|
} |
|
273
|
|
|
} catch (\Exception $e) { |
|
274
|
|
|
Bugsnag::notifyException($e); |
|
275
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
276
|
|
|
<i class='fa fa-ban'></i> |
|
277
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
278
|
|
|
/* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
|
279
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
280
|
|
|
'.$e->getMessage().' |
|
281
|
|
|
</div>'; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|