ExtendedBaseInvoiceController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Order\Invoice;
7
use App\Model\Order\Order;
8
use App\Model\Order\Payment;
9
use App\Model\Payment\Currency;
10
use Bugsnag;
11
use Exception;
12
use Illuminate\Http\Request;
13
14
class ExtendedBaseInvoiceController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('admin', ['except' => ['pdf']]);
20
    }
21
22
    public function newPayment(Request $request)
23
    {
24
        try {
25
            $clientid = $request->input('clientid');
26
            $invoice = new Invoice();
27
            $order = new Order();
28
            $invoices = $invoice->where('user_id', $clientid)->where('status', '!=', 'Success')->orderBy('created_at', 'desc')->get();
29
            $cltCont = new \App\Http\Controllers\User\ClientController();
30
            $invoiceSum = $cltCont->getTotalInvoice($invoices);
31
            $amountReceived = $cltCont->getAmountPaid($clientid);
32
            $pendingAmount = $invoiceSum - $amountReceived;
33
            $client = $this->user->where('id', $clientid)->first();
34
            $currency = $client->currency;
35
            $symbol = Currency::where('code', $currency)->pluck('symbol')->first();
36
            $orders = $order->where('client', $clientid)->get();
37
38
            return view('themes.default1.invoice.newpayment', compact('clientid', 'client', 'invoices',  'orders',
39
                  'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'symbol'));
40
        } catch (Exception $ex) {
41
            return redirect()->back()->with('fails', $ex->getMessage());
42
        }
43
    }
44
45
    public function postNewPayment($clientid, Request $request)
46
    {
47
        $this->validate($request, [
48
            'payment_date'  => 'required',
49
            'payment_method'=> 'required',
50
            'amount'        => 'required',
51
        ]);
52
53
        try {
54
            $payment = new Payment();
55
            $payment->payment_status = 'success';
56
            $payment->user_id = $clientid;
57
            $payment->invoice_id = '--';
58
            $paymentReceived = $payment->fill($request->all())->save();
59
60
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
61
        } catch (Exception $ex) {
62
            return redirect()->back()->with('fails', $ex->getMessage());
63
        }
64
    }
65
66
    public function edit($invoiceid, Request $request)
67
    {
68
        $totalSum = '0';
69
        $invoice = Invoice::where('id', $invoiceid)->first();
70
        $date = date('m/d/Y', strtotime($invoice->date));
71
        $payment = Payment::where('invoice_id', $invoiceid)->pluck('amount')->toArray();
72
        if ($payment) {
73
            $totalSum = array_sum($payment);
74
        }
75
76
        return view('themes.default1.invoice.editInvoice', compact('date', 'invoiceid', 'invoice', 'totalSum'));
77
    }
78
79
    public function postEdit($invoiceid, Request $request)
80
    {
81
        $this->validate($request, [
82
            'date'  => 'required',
83
            'total' => 'required',
84
            'status'=> 'required',
85
        ]);
86
87
        try {
88
            $total = $request->input('total');
89
            $status = $request->input('status');
90
            $paid = $request->input('paid');
91
            $invoice = Invoice::where('id', $invoiceid)->update(['grand_total'=> $total, 'status'=>$status,
92
                'date'                                                        => \Carbon\Carbon::parse($request->input('date')), ]);
93
            $order = Order::where('invoice_id', $invoiceid)->update(['price_override'=>$total]);
94
95
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
96
        } catch (\Exception $ex) {
97
            return redirect()->back()->with('fails', $ex->getMessage());
98
        }
99
    }
100
101
    public function postNewMultiplePayment($clientid, Request $request)
102
    {
103
        $this->validate($request, [
104
            'payment_date'  => 'required',
105
            'payment_method'=> 'required',
106
            'totalAmt'      => 'required|numeric|not_in:0',
107
        ], [
108
            'totalAmt.required'=> 'The amount field is required',
109
        ]);
110
111
        try {
112
            $payment_date = \Carbon\Carbon::parse($request->payment_date);
113
            $payment_method = $request->payment_method;
114
            $totalAmt = $request->totalAmt;
115
            $invoiceChecked = $request->invoiceChecked;
116
            $invoicAmount = $request->invoiceAmount;
117
            $amtToCredit = $request->amtToCredit;
118
            $payment_status = 'success';
119
            $payment = $this->multiplePayment($clientid,$invoiceChecked, $payment_method,
120
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status);
121
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully'];
122
123
            return response()->json($response);
124
        } catch (\Exception $ex) {
125
            app('log')->error($ex->getMessage());
126
            Bugsnag::notifyException($ex);
127
128
            $result = [$ex->getMessage()];
129
130
            return response()->json(compact('result'), 500);
131
132
            // return redirect()->back()->with('fails', $ex->getMessage());
133
        }
134
    }
135
136
    public function multiplePayment($clientid,$invoiceChecked, $payment_method,
137
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status)
138
    {
139
        try {
140
            foreach ($invoiceChecked as $key => $value) {
141
                if ($key != 0) {//If Payment is linked to Invoice
142
                    $invoice = Invoice::find($value);
143
                    $invoice_status = 'pending';
144
                    $invoicAmount[$key] = $invoicAmount[$key] == '' ? 0 : $invoicAmount[$key];
145
                    $payment = Payment::where('invoice_id', $value)->create([
146
                        'invoice_id'     => $value,
147
                        'user_id'        => $clientid,
148
                        'amount'         => $invoicAmount[$key],
149
                        'amt_to_credit'  => $amtToCredit,
150
                        'payment_method' => $payment_method,
151
                        'payment_status' => $payment_status,
152
                        'created_at'     => $payment_date,
153
                    ]);
154
                    $totalPayments = $this->payment
155
            ->where('invoice_id', $value)
156
            ->where('payment_status', 'success')
157
            ->pluck('amount')->toArray();
158
                    $total_paid = array_sum($totalPayments);
159
160
                    if ($invoice) {
161
                        if ($total_paid >= $invoice->grand_total) {
162
                            $invoice_status = 'success';
163
                        } elseif ($total_paid > 0) {
164
                            $invoice_status = 'partially paid';
165
                        }
166
                        $invoice->status = $invoice_status;
167
                        $invoice->save();
168
                    }
169
                } elseif (count($invoiceChecked) == 1 || $amtToCredit > 0) {//If Payment is not linked to any invoice and is to be credited to User Accunt
170
                    $totalExtraSum = Payment::where('user_id', $clientid)->where('invoice_id', 0)
171
                    ->pluck('amt_to_credit')->first(); //Get the total Extra Amt Paid
172
                    if ($totalExtraSum) {
173
                        $amtToCredit = $totalExtraSum + $amtToCredit; //Add the total extra amt to the existing extra amt paid before deleting
174
                        Payment::where('user_id', $clientid)->delete();
175
                    }
176
                    $payment = Payment::updateOrCreate([
177
                        'invoice_id'     => $value,
178
                        'user_id'        => $clientid,
179
                        'amt_to_credit'  => $amtToCredit,
180
                        'payment_method' => $payment_method,
181
                        'payment_status' => $payment_status,
182
                        'created_at'     => $payment_date,
183
                    ]);
184
                }
185
            }
186
187
            // return $payment;
188
        } catch (Exception $ex) {
189
            app('log')->error($ex->getMessage());
190
            Bugsnag::notifyException($ex);
191
192
            return redirect()->back()->with('fails', $ex->getMessage());
193
        }
194
    }
195
196
    /*
197
    * Editing  and Updating the Payment By linking with Invoice
198
    */
199
    public function updateNewMultiplePayment($clientid, Request $request)
200
    {
201
        $this->validate($request, [
202
            'payment_date'  => 'required',
203
            'payment_method'=> 'required',
204
            'totalAmt'      => 'required|numeric',
205
            'invoiceChecked'=> 'required',
206
        ],
207
        [
208
            'invoiceChecked.required' => 'Please link the amount with at least one Invoice',
209
        ]
210
        );
211
212
        try {
213
            $payment_date = \Carbon\Carbon::parse($request->payment_date);
214
            $payment_method = $request->payment_method;
215
            $totalAmt = $request->totalAmt;
216
            $invoiceChecked = $request->invoiceChecked;
217
            $invoicAmount = $request->invoiceAmount;
218
            $amtToCredit = $request->amtToCredit;
219
            $payment_status = 'success';
220
            $payment = $this->updatePaymentByInvoice($clientid,$invoiceChecked, $payment_method,
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $payment is correct as $this->updatePaymentByIn...redit, $payment_status) targeting App\Http\Controllers\Ord...pdatePaymentByInvoice() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
221
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status);
222
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully'];
223
224
            return response()->json($response);
225
        } catch (\Exception $ex) {
226
            app('log')->error($ex->getMessage());
227
            Bugsnag::notifyException($ex);
228
            $result = [$ex->getMessage()];
229
230
            return response()->json(compact('result'), 500);
231
        }
232
    }
233
234
    public function updatePaymentByInvoice($clientid,$invoiceChecked, $payment_method,
235
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status)
236
    {
237
        try {
238
            $sum = 0;
239
            foreach ($invoiceChecked as $key => $value) {
240
                if ($key != 0) {//If Payment is linked to Invoice
241
                    $invoice = Invoice::find($value);
242
                    Payment::where('user_id', $clientid)->where('invoice_id', 0)
243
                     ->update(['amt_to_credit'=>$amtToCredit]);
244
                    $invoice_status = 'pending';
245
                    $payment = Payment::create([
246
                        'invoice_id'     => $value,
247
                        'user_id'        => $clientid,
248
                        'amount'         => $invoicAmount[$key],
249
                        'payment_method' => $payment_method,
250
                        'payment_status' => $payment_status,
251
                        'created_at'     => $payment_date,
252
                    ]);
253
                    $totalPayments = $this->payment
254
            ->where('invoice_id', $value)
255
            ->where('payment_status', 'success')
256
            ->pluck('amount')->toArray();
257
                    $total_paid = array_sum($totalPayments);
258
                    if ($total_paid >= $invoice->grand_total) {
259
                        $invoice_status = 'success';
260
                    }
261
                    if ($invoice) {
262
                        $invoice->status = $invoice_status;
263
                        $invoice->save();
264
                    }
265
                }
266
                // else{//If Payment is not linked to any invoice and is to be credited to User Accunt
267
        //     dd('as');
268
        //     $payment = Payment::create([
269
        //         'invoice_id'     => $value,
270
        //         'user_id'       => $clientid,
271
        //         'amount'         =>$invoicAmount[$key],
272
        //         'amt_to_credit'  =>$amtToCredit,
273
        //         'payment_method' => $payment_method,
274
        //         'payment_status' => $payment_status,
275
        //         'created_at'     => $payment_date,
276
        //     ]);
277
        // }
278
            }
279
280
            // return $payment;
281
        } catch (Exception $ex) {
282
            dd($ex);
283
            app('log')->error($ex->getMessage());
284
            Bugsnag::notifyException($ex);
285
286
            return redirect()->back()->with('fails', $ex->getMessage());
287
        }
288
    }
289
}
290