|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Order\Invoice; |
|
6
|
|
|
use App\Model\Product\Product; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
10
|
|
|
// PAYMENTS AND EXTRA FUNCTIONALITIES FOR INVOICES |
|
11
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
12
|
|
|
|
|
13
|
|
|
trait PaymentsAndInvoices |
|
14
|
|
|
{ |
|
15
|
|
|
/* |
|
16
|
|
|
*Edit payment Total. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function paymentTotalChange(Request $request) |
|
19
|
|
|
{ |
|
20
|
|
|
try { |
|
21
|
|
|
$invoice = new Invoice(); |
|
22
|
|
|
$total = $request->input('total'); |
|
23
|
|
|
if ($total == '') { |
|
24
|
|
|
$total = 0; |
|
25
|
|
|
} |
|
26
|
|
|
$paymentid = $request->input('id'); |
|
27
|
|
|
$creditAmtUserId = $this->payment->where('id', $paymentid)->value('user_id'); |
|
28
|
|
|
$creditAmt = $this->payment->where('user_id', $creditAmtUserId) |
|
29
|
|
|
->where('invoice_id', '=', 0)->value('amt_to_credit'); |
|
30
|
|
|
$invoices = $invoice->where('user_id', $creditAmtUserId)->orderBy('created_at', 'desc')->get(); |
|
31
|
|
|
$cltCont = new \App\Http\Controllers\User\ClientController(); |
|
32
|
|
|
$invoiceSum = $cltCont->getTotalInvoice($invoices); |
|
33
|
|
|
if ($total > $invoiceSum) { |
|
34
|
|
|
$diff = $total - $invoiceSum; |
|
35
|
|
|
$creditAmt = $creditAmt + $diff; |
|
36
|
|
|
$total = $invoiceSum; |
|
37
|
|
|
} |
|
38
|
|
|
$payment = $this->payment->where('id', $paymentid)->update(['amount'=>$total]); |
|
39
|
|
|
|
|
40
|
|
|
$creditAmtInvoiceId = $this->payment->where('user_id', $creditAmtUserId) |
|
41
|
|
|
->where('invoice_id', '!=', 0)->first(); |
|
42
|
|
|
$invoiceId = $creditAmtInvoiceId->invoice_id; |
|
43
|
|
|
$invoice = $invoice->where('id', $invoiceId)->first(); |
|
44
|
|
|
$grand_total = $invoice->grand_total; |
|
45
|
|
|
$diffSum = $grand_total - $total; |
|
46
|
|
|
|
|
47
|
|
|
$finalAmt = $creditAmt + $diffSum; |
|
48
|
|
|
$updatedAmt = $this->payment->where('user_id', $creditAmtUserId) |
|
49
|
|
|
->where('invoice_id', '=', 0)->update(['amt_to_credit'=>$creditAmt]); |
|
50
|
|
|
} catch (\Exception $ex) { |
|
51
|
|
|
app('log')->info($ex->getMessage()); |
|
52
|
|
|
Bugsnag::notifyException($ex); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function doPayment( |
|
59
|
|
|
$payment_method, |
|
60
|
|
|
$invoiceid, |
|
61
|
|
|
$amount, |
|
62
|
|
|
$parent_id = '', |
|
63
|
|
|
$userid = '', |
|
64
|
|
|
$payment_status = 'pending' |
|
65
|
|
|
) { |
|
66
|
|
|
try { |
|
67
|
|
|
if ($amount > 0) { |
|
68
|
|
|
if ($userid == '') { |
|
69
|
|
|
$userid = \Auth::user()->id; |
|
70
|
|
|
} |
|
71
|
|
|
if ($amount == 0) { |
|
72
|
|
|
$payment_status = 'success'; |
|
73
|
|
|
} |
|
74
|
|
|
$this->payment->create([ |
|
75
|
|
|
'parent_id' => $parent_id, |
|
76
|
|
|
'invoice_id' => $invoiceid, |
|
77
|
|
|
'user_id' => $userid, |
|
78
|
|
|
'amount' => $amount, |
|
79
|
|
|
'payment_method' => $payment_method, |
|
80
|
|
|
'payment_status' => $payment_status, |
|
81
|
|
|
]); |
|
82
|
|
|
$this->updateInvoice($invoiceid); |
|
83
|
|
|
} |
|
84
|
|
|
} catch (\Exception $ex) { |
|
85
|
|
|
Bugsnag::notifyException($ex); |
|
86
|
|
|
|
|
87
|
|
|
throw new \Exception($ex->getMessage()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getAgents($agents, $productid, $plan) |
|
92
|
|
|
{ |
|
93
|
|
|
if (!$agents) {//If agents is not received in the request in the case when |
|
94
|
|
|
// 'modify agent' is not allowed for the Product,get the no of Agents from the Plan Table. |
|
95
|
|
|
$planForAgent = Product::find($productid)->planRelation->find($plan); |
|
|
|
|
|
|
96
|
|
|
if ($planForAgent) {//If Plan Exists For the Product ie not a Product without Plan |
|
97
|
|
|
$noOfAgents = $planForAgent->planPrice->first()->no_of_agents; |
|
98
|
|
|
$agents = $noOfAgents ? $noOfAgents : 0; //If no. of Agents is specified then that,else 0(Unlimited Agents) |
|
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
$agents = 0; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $agents; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getQuantity($qty, $productid, $plan) |
|
108
|
|
|
{ |
|
109
|
|
|
if (!$qty) {//If quantity is not received in the request in the case when 'modify quantity' is not allowed for the Product,get the Product qUANTITY from the Plan Table. |
|
|
|
|
|
|
110
|
|
|
$planForQty = Product::find($productid)->planRelation->find($plan); |
|
|
|
|
|
|
111
|
|
|
if ($planForQty) { |
|
112
|
|
|
$quantity = Product::find($productid)->planRelation->find($plan)->planPrice->first()->product_quantity; |
|
|
|
|
|
|
113
|
|
|
$qty = $quantity ? $quantity : 1; //If no. of Agents is specified then that,else 0(Unlimited Agents) |
|
114
|
|
|
} else { |
|
115
|
|
|
$qty = 1; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $qty; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function updateInvoice($invoiceid) |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
|
|
$invoice = $this->invoice->findOrFail($invoiceid); |
|
126
|
|
|
$payment = $this->payment->where('invoice_id', $invoiceid) |
|
127
|
|
|
->where('payment_status', 'success')->pluck('amount')->toArray(); |
|
128
|
|
|
$total = array_sum($payment); |
|
129
|
|
|
if ($total < $invoice->grand_total) { |
|
130
|
|
|
$invoice->status = 'pending'; |
|
131
|
|
|
} |
|
132
|
|
|
if ($total >= $invoice->grand_total) { |
|
133
|
|
|
$invoice->status = 'success'; |
|
134
|
|
|
} |
|
135
|
|
|
if ($total > $invoice->grand_total) { |
|
136
|
|
|
$user = $invoice->user()->first(); |
|
137
|
|
|
$balance = $total - $invoice->grand_total; |
|
138
|
|
|
$user->debit = $balance; |
|
139
|
|
|
$user->save(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$invoice->save(); |
|
143
|
|
|
} catch (\Exception $ex) { |
|
144
|
|
|
Bugsnag::notifyException($ex); |
|
145
|
|
|
|
|
146
|
|
|
throw new \Exception($ex->getMessage()); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function postRazorpayPayment($invoiceid, $grand_total) |
|
151
|
|
|
{ |
|
152
|
|
|
try { |
|
153
|
|
|
$payment_method = \Session::get('payment_method'); |
|
154
|
|
|
$payment_status = 'success'; |
|
155
|
|
|
$payment_date = \Carbon\Carbon::now()->toDateTimeString(); |
|
156
|
|
|
$amount = $grand_total; |
|
157
|
|
|
$paymentRenewal = $this->updateInvoicePayment( |
|
|
|
|
|
|
158
|
|
|
$invoiceid, |
|
159
|
|
|
$payment_method, |
|
160
|
|
|
$payment_status, |
|
161
|
|
|
$payment_date, |
|
162
|
|
|
$amount |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
|
166
|
|
|
} catch (\Exception $ex) { |
|
167
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function sendmailClientAgent($userid, $invoiceid) |
|
172
|
|
|
{ |
|
173
|
|
|
try { |
|
174
|
|
|
$agent = \Input::get('agent'); |
|
175
|
|
|
$client = \Input::get('client'); |
|
176
|
|
|
if ($agent == 1) { |
|
177
|
|
|
$id = \Auth::user()->id; |
|
178
|
|
|
$this->sendMail($id, $invoiceid); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
if ($client == 1) { |
|
181
|
|
|
$this->sendMail($userid, $invoiceid); |
|
182
|
|
|
} |
|
183
|
|
|
} catch (\Exception $ex) { |
|
184
|
|
|
app('log')->info($ex->getMessage()); |
|
185
|
|
|
Bugsnag::notifyException($ex); |
|
186
|
|
|
|
|
187
|
|
|
throw new \Exception($ex->getMessage()); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function payment(Request $request) |
|
192
|
|
|
{ |
|
193
|
|
|
try { |
|
194
|
|
|
if ($request->has('invoiceid')) { |
|
195
|
|
|
$invoice_id = $request->input('invoiceid'); |
|
196
|
|
|
$invoice = $this->invoice->find($invoice_id); |
|
197
|
|
|
$userid = $invoice->user_id; |
|
198
|
|
|
//dd($invoice); |
|
199
|
|
|
$invoice_status = ''; |
|
200
|
|
|
$payment_status = ''; |
|
201
|
|
|
$payment_method = ''; |
|
202
|
|
|
$domain = ''; |
|
203
|
|
|
if ($invoice) { |
|
204
|
|
|
$invoice_status = $invoice->status; |
|
205
|
|
|
$items = $invoice->invoiceItem()->first(); |
|
206
|
|
|
if ($items) { |
|
207
|
|
|
$domain = $items->domain; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
$payment = $this->payment->where('invoice_id', $invoice_id)->first(); |
|
211
|
|
|
if ($payment) { |
|
212
|
|
|
$payment_status = $payment->payment_status; |
|
213
|
|
|
$payment_method = $payment->payment_method; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return view( |
|
217
|
|
|
'themes.default1.invoice.payment', |
|
218
|
|
|
compact( |
|
219
|
|
|
'invoice_status', |
|
220
|
|
|
'payment_status', |
|
221
|
|
|
'payment_method', |
|
222
|
|
|
'invoice_id', |
|
223
|
|
|
'domain', |
|
224
|
|
|
'invoice', |
|
225
|
|
|
'userid' |
|
226
|
|
|
) |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return redirect()->back(); |
|
231
|
|
|
} catch (\Exception $ex) { |
|
232
|
|
|
Bugsnag::notifyException($ex); |
|
233
|
|
|
|
|
234
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function deletePayment(Request $request) |
|
239
|
|
|
{ |
|
240
|
|
|
try { |
|
241
|
|
|
$ids = $request->input('select'); |
|
242
|
|
|
if (!empty($ids)) { |
|
243
|
|
|
foreach ($ids as $id) { |
|
244
|
|
|
$payment = $this->payment->where('id', $id)->first(); |
|
245
|
|
|
if ($payment) { |
|
246
|
|
|
$invoice = $this->invoice->find($payment->invoice_id); |
|
247
|
|
|
if ($invoice) { |
|
248
|
|
|
$invoice->status = 'pending'; |
|
249
|
|
|
$invoice->save(); |
|
250
|
|
|
} |
|
251
|
|
|
$payment->delete(); |
|
252
|
|
|
} else { |
|
253
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
254
|
|
|
<i class='fa fa-ban'></i> |
|
255
|
|
|
<b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> |
|
256
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
257
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
258
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
|
259
|
|
|
</div>'; |
|
260
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
264
|
|
|
<i class='fa fa-ban'></i> |
|
265
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
266
|
|
|
/* @scrutinizer ignore-type */ |
|
267
|
|
|
\Lang::get('message.success').' |
|
268
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
269
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
270
|
|
|
</div>'; |
|
271
|
|
|
} else { |
|
272
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
273
|
|
|
<i class='fa fa-ban'></i> |
|
274
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
275
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
276
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
277
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
278
|
|
|
</div>'; |
|
279
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
280
|
|
|
} |
|
281
|
|
|
} catch (\Exception $e) { |
|
282
|
|
|
Bugsnag::notifyException($e); |
|
283
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
284
|
|
|
<i class='fa fa-ban'></i> |
|
285
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
286
|
|
|
/* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
|
287
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
288
|
|
|
'.$e->getMessage().' |
|
289
|
|
|
</div>'; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function pdf(Request $request) |
|
294
|
|
|
{ |
|
295
|
|
|
try { |
|
296
|
|
|
$id = $request->input('invoiceid'); |
|
297
|
|
|
if (!$id) { |
|
298
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.no-invoice-id')); |
|
299
|
|
|
} |
|
300
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
301
|
|
|
if (!$invoice) { |
|
302
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id')); |
|
303
|
|
|
} |
|
304
|
|
|
$invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get(); |
|
305
|
|
|
if ($invoiceItems->count() == 0) { |
|
306
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id')); |
|
307
|
|
|
} |
|
308
|
|
|
$user = $this->user->find($invoice->user_id); |
|
309
|
|
|
if (!$user) { |
|
310
|
|
|
return redirect()->back()->with('fails', 'No User'); |
|
311
|
|
|
} |
|
312
|
|
|
$cont = new \App\Http\Controllers\Front\CartController(); |
|
313
|
|
|
$currency = $cont->currency($user->id); |
|
314
|
|
|
$symbol = $currency['currency']; |
|
315
|
|
|
$pdf = \PDF::loadView('themes.default1.invoice.newpdf', compact('invoiceItems', 'invoice', 'user', 'currency', 'symbol')); |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
return $pdf->download($user->first_name.'-invoice.pdf'); |
|
318
|
|
|
} catch (\Exception $ex) { |
|
319
|
|
|
Bugsnag::notifyException($ex); |
|
320
|
|
|
|
|
321
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|