Completed
Push — development ( 821a8b...d3568e )
by Ashutosh
09:43
created

ExtendedBaseInvoiceController::multiplePayment()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 50
rs 8.7057
c 0
b 0
f 0
cc 6
nc 13
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Exception;
10
use Log;
11
use Bugsnag;
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', '=', 'pending')->orderBy('created_at', 'desc')->get();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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
            $orders = $order->where('client', $clientid)->get();
36
37
            return view('themes.default1.invoice.newpayment', compact('clientid', 'client', 'invoices',  'orders',
38
                  'invoiceSum', 'amountReceived', 'pendingAmount', 'currency'));
39
        } catch (Exception $ex) {
40
            return redirect()->back()->with('fails', $ex->getMessage());
41
        }
42
    }
43
44
    public function postNewPayment($clientid, Request $request)
45
    {
46
        $this->validate($request, [
47
           'payment_date'  => 'required',
48
           'payment_method'=> 'required',
49
           'amount'        => 'required',
50
        ]);
51
52
        try {
53
            $payment = new Payment();
54
            $payment->payment_status = 'success';
55
            $payment->user_id = $clientid;
56
            $payment->invoice_id = '--';
57
            $paymentReceived = $payment->fill($request->all())->save();
58
59
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
60
        } catch (Exception $ex) {
61
            return redirect()->back()->with('fails', $ex->getMessage());
62
        }
63
    }
64
65
    public function edit($invoiceid, Request $request)
66
    {
67
        $totalSum = '0';
68
        $invoice = Invoice::where('id',$invoiceid)->first();
69
        $payment = Payment::where('invoice_id',$invoiceid)->pluck('amount')->toArray();
70
        if($payment){
71
        $totalSum = array_sum($payment);
72
    }
73
        return view('themes.default1.invoice.editInvoice',compact('userid','invoiceid','invoice','totalSum'));
74
75
    }
76
77
    public function postEdit($invoiceid, Request $request)
78
    {
79
        $this->validate($request, [
80
        'total' => 'required',
81
        'status'=> 'required',
82
        'paid'  => 'required',
83
        ]);
84
85
        try {
86
            $total = $request->input('total');
87
            $status = $request->input('status');
88
            $paid = $request->input('paid');
89
            $invoice = Invoice::where('id', $invoiceid)->update(['grand_total'=>$total, 'status'=>$status]);
90
91
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
92
        } catch (\Exception $ex) {
93
            return redirect()->back()->with('fails', $ex->getMessage());
94
        }
95
    }
96
97
    public function postNewMultiplePayment($clientid , Request $request)
98
    {
99
        $this->validate($request, [
100
        'payment_date' => 'required',
101
        'payment_method'=> 'required',
102
        'totalAmt'  => 'required|numeric',
103
        ]);
104
        try {
105
            $payment_date = $request->payment_date;
106
            $payment_method = $request->payment_method;
107
            $totalAmt=$request->totalAmt;
108
            $invoiceChecked = $request->invoiceChecked;
109
            $invoicAmount = $request->invoiceAmount;
110
            $amtToCredit = $request->amtToCredit;
111
            $payment_status= "success";
112
            $payment = $this->multiplePayment($clientid,$invoiceChecked, $payment_method,
113
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status);
114
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully', ];
115
            return response()->json($response);
116
        } catch (\Exception $ex) {
117
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
118
            app('log')->error($ex->getMessage());
119
            Bugsnag::notifyException($ex);
120
121
            $result = [$ex->getMessage()];
122
123
            return response()->json(compact('result'), 500);
124
125
            // return redirect()->back()->with('fails', $ex->getMessage());
126
        }
127
128
    }
129
130
    public function multiplePayment($clientid,$invoiceChecked, $payment_method,
131
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status)
132
    {
133
       try {
134
        foreach ($invoiceChecked as $key => $value) {
135
            if($key != 0){//If Payment is linked to Invoice
136
        $invoice = Invoice::find($value);
137
        $invoice_status = 'pending';
138
        $payment = Payment::where('invoice_id',$value)->create([
139
                'invoice_id'     => $value,
140
                'user_id'       => $clientid,
141
                'amount'         =>$invoicAmount[$key],
142
                'amt_to_credit'  =>$amtToCredit,
143
                'payment_method' => $payment_method,
144
                'payment_status' => $payment_status,
145
                'created_at'     => $payment_date,
146
            ]);
147
            $totalPayments = $this->payment
148
            ->where('invoice_id', $value)
149
            ->where('payment_status', 'success')
150
            ->pluck('amount')->toArray();
151
            $total_paid = array_sum($totalPayments);
152
            if ($total_paid >= $invoice->grand_total) {
153
                $invoice_status = 'success';
154
            }
155
            if ($invoice) {
156
                $invoice->status = $invoice_status;
157
                $invoice->save();
158
            }
159
        }
160
        else{//If Payment is not linked to any invoice and is to be credited to User Accunt
161
            $payment = Payment::create([
162
                'invoice_id'     => $value,
163
                'user_id'       => $clientid,
164
                'amt_to_credit'  =>$amtToCredit,
165
                'payment_method' => $payment_method,
166
                'payment_status' => $payment_status,
167
                'created_at'     => $payment_date,
168
            ]);
169
        }
170
171
    }
172
    return $payment;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $payment seems to be defined by a foreach iteration on line 134. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
173
           
174
       } catch (Exception $e) {
175
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
176
            app('log')->error($ex->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ex seems to be never defined.
Loading history...
177
            Bugsnag::notifyException($ex);
178
179
            return redirect()->back()->with('fails', $ex->getMessage());
180
       }
181
    }
182
    
183
     /*
184
    * Editing  and Updating the Payment By linking with Invoice
185
    */
186
    public function updateNewMultiplePayment($clientid , Request $request)
187
    {
188
        try {
189
            $payment_date = $request->payment_date;
190
            $payment_method = $request->payment_method;
191
            $totalAmt=$request->totalAmt;
192
            $invoiceChecked = $request->invoiceChecked;
193
            $invoicAmount = $request->invoiceAmount;
194
            $amtToCredit = $request->amtToCredit;
195
            $payment_status= "success";
196
            $payment = $this->updatePaymentByInvoice($clientid,$invoiceChecked, $payment_method,
197
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status);
198
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully', ];
199
               return response()->json($response);
200
        } catch (\Exception $ex) {
201
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
202
            app('log')->error($ex->getMessage());
203
            Bugsnag::notifyException($ex);
204
205
            return redirect()->back()->with('fails', $ex->getMessage());
206
        }
207
208
    }
209
    
210
    
211
    public function updatePaymentByInvoice($clientid,$invoiceChecked, $payment_method,
212
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status)
213
    {
214
       try {
215
        $sum = 0 ;
216
        foreach ($invoiceChecked as $key => $value) {
217
            if($key != 0){//If Payment is linked to Invoice
218
        $invoice = Invoice::find($value);
219
        Payment::where('user_id', $clientid)->where('invoice_id',0)->update(['amt_to_credit'=>$amtToCredit]);
220
        $invoice_status = 'pending';
221
        $payment = Payment::create([
222
                'invoice_id'     => $value,
223
                'user_id'       => $clientid,
224
                'amount'         =>$invoicAmount[$key],
225
                'payment_method' => $payment_method,
226
                'payment_status' => $payment_status,
227
                'created_at'     => $payment_date,
228
            ]);
229
            $totalPayments = $this->payment
230
            ->where('invoice_id', $value)
231
            ->where('payment_status', 'success')
232
            ->pluck('amount')->toArray();
233
            $total_paid = array_sum($totalPayments);
234
            if ($total_paid >= $invoice->grand_total) {
235
                $invoice_status = 'success';
236
            }
237
            if ($invoice) {
238
                $invoice->status = $invoice_status;
239
                $invoice->save();
240
            }
241
        }
242
        // else{//If Payment is not linked to any invoice and is to be credited to User Accunt
243
        //     dd('as');
244
        //     $payment = Payment::create([
245
        //         'invoice_id'     => $value,
246
        //         'user_id'       => $clientid,
247
        //         'amount'         =>$invoicAmount[$key],
248
        //         'amt_to_credit'  =>$amtToCredit,
249
        //         'payment_method' => $payment_method,
250
        //         'payment_status' => $payment_status,
251
        //         'created_at'     => $payment_date,
252
        //     ]);
253
        // }
254
255
    }
256
    return $payment;
257
           
258
       } catch (Exception $e) {
259
        dd($e);
260
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
261
            app('log')->error($ex->getMessage());
262
            Bugsnag::notifyException($ex);
263
264
            return redirect()->back()->with('fails', $ex->getMessage());
265
       }
266
    }
267
}
268