Issues (129)

app/Traits/PaymentsAndInvoices.php (4 issues)

1
<?php
2
3
namespace App\Traits;
4
5
use App\Model\Order\Invoice;
6
use App\Model\Order\Payment;
7
use App\Model\Product\Product;
8
use Bugsnag;
9
use Illuminate\Http\Request;
10
11
//////////////////////////////////////////////////////////////////////////////
12
// PAYMENTS AND EXTRA FUNCTIONALITIES FOR INVOICES
13
//////////////////////////////////////////////////////////////////////////////
14
15
    trait PaymentsAndInvoices
16
    {
17
        /*
18
    *Edit payment Total.
19
    */
20
        public function paymentTotalChange(Request $request)
21
        {
22
            try {
23
                $invoice = new Invoice();
24
                $total = $request->input('total');
25
                if ($total == '') {
26
                    $total = 0;
27
                }
28
                $paymentid = $request->input('id');
29
                $creditAmtUserId = $this->payment->where('id', $paymentid)->value('user_id');
30
                $creditAmt = $this->payment->where('user_id', $creditAmtUserId)
31
                  ->where('invoice_id', '=', 0)->value('amt_to_credit');
32
                $invoices = $invoice->where('user_id', $creditAmtUserId)->orderBy('created_at', 'desc')->get();
33
                $cltCont = new \App\Http\Controllers\User\ClientController();
34
                $invoiceSum = $cltCont->getTotalInvoice($invoices);
35
                if ($total > $invoiceSum) {
36
                    $diff = $total - $invoiceSum;
37
                    $creditAmt = $creditAmt + $diff;
38
                    $total = $invoiceSum;
39
                }
40
                $payment = $this->payment->where('id', $paymentid)->update(['amount'=>$total]);
41
42
                $creditAmtInvoiceId = $this->payment->where('user_id', $creditAmtUserId)
43
        ->where('invoice_id', '!=', 0)->first();
44
                $invoiceId = $creditAmtInvoiceId->invoice_id;
45
                $invoice = $invoice->where('id', $invoiceId)->first();
46
                $grand_total = $invoice->grand_total;
47
                $diffSum = $grand_total - $total;
48
49
                $finalAmt = $creditAmt + $diffSum;
50
                $updatedAmt = $this->payment->where('user_id', $creditAmtUserId)
51
        ->where('invoice_id', '=', 0)->update(['amt_to_credit'=>$creditAmt]);
52
            } catch (\Exception $ex) {
53
                app('log')->info($ex->getMessage());
54
                Bugsnag::notifyException($ex);
55
56
                return redirect()->back()->with('fails', $ex->getMessage());
57
            }
58
        }
59
60
        public function doPayment(
61
        $payment_method,
62
        $invoiceid,
63
        $amount,
64
       $parent_id = '',
65
        $userid = '',
66
        $payment_status = 'pending'
67
    ) {
68
            try {
69
                if ($amount > 0) {
70
                    if ($userid == '') {
71
                        $userid = \Auth::user()->id;
72
                    }
73
                    if ($amount == 0) {
74
                        $payment_status = 'success';
75
                    }
76
                    $this->payment->create([
77
                        'parent_id'      => $parent_id,
78
                        'invoice_id'     => $invoiceid,
79
                        'user_id'        => $userid,
80
                        'amount'         => $amount,
81
                        'payment_method' => $payment_method,
82
                        'payment_status' => $payment_status,
83
                    ]);
84
                    $this->updateInvoice($invoiceid);
85
                }
86
            } catch (\Exception $ex) {
87
                Bugsnag::notifyException($ex);
88
89
                throw new \Exception($ex->getMessage());
90
            }
91
        }
92
93
        public function getAgents($agents, $productid, $plan)
94
        {
95
            if (! $agents) {//If agents is not received in the request in the case when
96
                // 'modify agent' is not allowed for the Product,get the no of Agents from the Plan Table.
97
                $planForAgent = Product::find($productid)->planRelation->find($plan);
0 ignored issues
show
The property planRelation does not seem to exist on App\Model\Product\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
98
                if ($planForAgent) {//If Plan Exists For the Product ie not a Product without Plan
99
                    $noOfAgents = $planForAgent->planPrice->first()->no_of_agents;
100
                    $agents = $noOfAgents ? $noOfAgents : 0; //If no. of Agents is specified then that,else 0(Unlimited Agents)
101
                } else {
102
                    $agents = 0;
103
                }
104
            }
105
106
            return $agents;
107
        }
108
109
        public function getQuantity($qty, $productid, $plan)
110
        {
111
            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.
112
                $planForQty = Product::find($productid)->planRelation->find($plan);
0 ignored issues
show
The property planRelation does not seem to exist on App\Model\Product\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
113
                if ($planForQty) {
114
                    $quantity = Product::find($productid)->planRelation->find($plan)->planPrice->first()->product_quantity;
115
                    $qty = $quantity ? $quantity : 1; //If no. of Agents is specified then that,else 0(Unlimited Agents)
116
                } else {
117
                    $qty = 1;
118
                }
119
            }
120
121
            return $qty;
122
        }
123
124
        public function updateInvoice($invoiceid)
125
        {
126
            try {
127
                $invoice = $this->invoice->findOrFail($invoiceid);
128
                foreach (\Cart::getConditionsByType('fee') as $value) {
129
                    $invoice->processing_fee = $value->getValue();
130
                }
131
                $payment = $this->payment->where('invoice_id', $invoiceid)
132
            ->where('payment_status', 'success')->pluck('amount')->toArray();
133
                $total = array_sum($payment);
134
                if ($total < $invoice->grand_total) {
135
                    $invoice->status = 'pending';
136
                }
137
                if ($total >= $invoice->grand_total) {
138
                    $invoice->status = 'success';
139
                }
140
                if ($total > $invoice->grand_total) {
141
                    $user = $invoice->user()->first();
142
                    $balance = $total - $invoice->grand_total;
143
                    $user->debit = $balance;
144
                    $user->save();
145
                }
146
147
                $invoice->save();
148
            } catch (\Exception $ex) {
149
                Bugsnag::notifyException($ex);
150
151
                throw new \Exception($ex->getMessage());
152
            }
153
        }
154
155
        public function postRazorpayPayment($invoice)
156
        {
157
            try {
158
                $totalPayment = $invoice->grand_total;
159
                if (count($invoice->payment()->get())) {//If partial payment is made
160
                    $paid = array_sum($invoice->payment()->pluck('amount')->toArray());
161
                    $totalPayment = $invoice->grand_total - $paid;
162
                }
163
                $payment_method = \Session::get('payment_method');
164
                $payment_status = 'success';
165
                $payment_date = \Carbon\Carbon::now()->toDateTimeString();
166
                $paymentRenewal = $this->updateInvoicePayment(
0 ignored issues
show
The method updateInvoicePayment() does not exist on App\Traits\PaymentsAndInvoices. Did you maybe mean updateInvoice()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
                /** @scrutinizer ignore-call */ 
167
                $paymentRenewal = $this->updateInvoicePayment(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
                $invoice->id,
168
                $payment_method,
169
                $payment_status,
170
                $payment_date,
171
                $totalPayment
172
            );
173
174
                return redirect()->back()->with('success', 'Payment Accepted Successfully');
175
            } catch (\Exception $ex) {
176
                return redirect()->back()->with('fails', $ex->getMessage());
177
            }
178
        }
179
180
        public function sendmailClientAgent($userid, $invoiceid)
181
        {
182
            try {
183
                $agent = \Input::get('agent');
184
                $client = \Input::get('client');
185
                if ($agent == 1) {
186
                    $id = \Auth::user()->id;
187
                    $this->sendMail($id, $invoiceid);
0 ignored issues
show
It seems like sendMail() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
                    $this->/** @scrutinizer ignore-call */ 
188
                           sendMail($id, $invoiceid);
Loading history...
188
                }
189
                if ($client == 1) {
190
                    $this->sendMail($userid, $invoiceid);
191
                }
192
            } catch (\Exception $ex) {
193
                app('log')->info($ex->getMessage());
194
                Bugsnag::notifyException($ex);
195
196
                throw new \Exception($ex->getMessage());
197
            }
198
        }
199
200
        public function payment(Request $request)
201
        {
202
            try {
203
                if ($request->has('invoiceid')) {
204
                    $invoice_id = $request->input('invoiceid');
205
                    $invoice = $this->invoice->find($invoice_id);
206
                    $userid = $invoice->user_id;
207
                    //dd($invoice);
208
                    $invoice_status = '';
209
                    $payment_status = '';
210
                    $payment_method = '';
211
                    $domain = '';
212
                    if ($invoice) {
213
                        $invoice_status = $invoice->status;
214
                        $items = $invoice->invoiceItem()->first();
215
                        if ($items) {
216
                            $domain = $items->domain;
217
                        }
218
                    }
219
                    $payment = $this->payment->where('invoice_id', $invoice_id)->first();
220
                    if ($payment) {
221
                        $payment_status = $payment->payment_status;
222
                        $payment_method = $payment->payment_method;
223
                    }
224
225
                    return view(
226
                    'themes.default1.invoice.payment',
227
                 compact(
228
                     'invoice_status',
229
                     'payment_status',
230
                  'payment_method',
231
                     'invoice_id',
232
                     'domain',
233
                     'invoice',
234
                     'userid'
235
                 )
236
                );
237
                }
238
239
                return redirect()->back();
240
            } catch (\Exception $ex) {
241
                Bugsnag::notifyException($ex);
242
243
                return redirect()->back()->with('fails', $ex->getMessage());
244
            }
245
        }
246
247
        public function getExtraAmtPaid($userId)
248
        {
249
            try {
250
                $amounts = Payment::where('user_id', $userId)->where('invoice_id', 0)->select('amt_to_credit')->get();
251
                $balance = 0;
252
                foreach ($amounts as $amount) {
253
                    if ($amount) {
254
                        $balance = $balance + $amount->amt_to_credit;
255
                    }
256
                }
257
258
                return $balance;
259
            } catch (\Exception $ex) {
260
                app('log')->info($ex->getMessage());
261
                Bugsnag::notifyException($ex);
262
263
                return redirect()->back()->with('fails', $ex->getMessage());
264
            }
265
        }
266
267
        /**
268
         * Get total of the Invoices for a User.
269
         */
270
        public function getTotalInvoice($invoices)
271
        {
272
            $sum = 0;
273
            foreach ($invoices as $invoice) {
274
                $sum = $sum + $invoice->grand_total;
275
            }
276
277
            return $sum;
278
        }
279
280
        public function getAmountPaid($userId)
281
        {
282
            try {
283
                $amounts = Payment::where('user_id', $userId)->select('amount', 'amt_to_credit')->get();
284
                $paidSum = 0;
285
                foreach ($amounts as $amount) {
286
                    if ($amount) {
287
                        $paidSum = $paidSum + $amount->amount;
288
                        // $credit = $paidSum + $amount->amt_to_credit;
289
                    }
290
                }
291
292
                return $paidSum;
293
            } catch (\Exception $ex) {
294
                app('log')->info($ex->getMessage());
295
                Bugsnag::notifyException($ex);
296
297
                return redirect()->back()->with('fails', $ex->getMessage());
298
            }
299
        }
300
    }
301