Completed
Push — development ( 6f9f18...b1cef6 )
by Ashutosh
10:23
created

ExtendedBaseInvoiceController::newPayment()   A

Complexity

Conditions 2
Paths 11

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 19
rs 9.7333
c 0
b 0
f 0
cc 2
nc 11
nop 1
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 Bugsnag;
10
use Exception;
11
use Illuminate\Http\Request;
12
use Log;
13
14
class ExtendedBaseInvoiceController extends Controller
15
{
16
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Function name "__construct" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
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
        dd('sad');
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
        $invoice = Invoice::where('id', $invoiceid)->first();
69
70
        return view('themes.default1.invoice.editInvoice', compact('userid', 'invoiceid', 'invoice'));
71
    }
72
73
    public function postEdit($invoiceid, Request $request)
74
    {
75
        $this->validate($request, [
76
        'total' => 'required',
77
        'status'=> 'required',
78
        ]);
79
80
        try {
81
            $total = $request->input('total');
82
            $status = $request->input('status');
83
            $invoice = Invoice::where('id', $invoiceid)->update(['grand_total'=>$total, 'status'=>$status]);
84
85
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
86
        } catch (\Exception $ex) {
87
            return redirect()->back()->with('fails', $ex->getMessage());
88
        }
89
    }
90
91
    public function postNewMultiplePayment($clientid, Request $request)
92
    {
93
        try {
94
            $payment_date = $request->payment_date;
95
            $payment_method = $request->payment_method;
96
            $totalAmt = $request->totalAmt;
97
            $invoiceChecked = $request->invoiceChecked;
98
            $invoicAmount = $request->invoiceAmount;
99
            $amtToCredit = $request->amtToCredit;
100
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 100 at column 0
Loading history...
101
            $payment_status= "success";
102
            $payment = $this->multiplePayment($clientid,$invoiceChecked, $payment_method,
103
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status);
104
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully', ];
105
               return response()->json($response);
106
        } catch (\Exception $ex) {
107
=======
108
            $payment_status = 'success';
109
            $payment = $this->updateForMultiplePayment($clientid,$invoiceChecked, $payment_method,
110
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status);
111
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully'];
112
113
            return response()->json($response);
114
        } catch (\Exception $e) {
115
            dd($e);
116
>>>>>>> 6f9f185892ed37a20bc00c4de5aedea3dea955eb
117
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
118
            app('log')->error($ex->getMessage());
119
            Bugsnag::notifyException($ex);
120
121
            return redirect()->back()->with('fails', $ex->getMessage());
122
        }
123
    }
124
125
<<<<<<< HEAD
126
    public function multiplePayment($clientid,$invoiceChecked, $payment_method,
127
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status)
128
    {
129
       try {
130
        foreach ($invoiceChecked as $key => $value) {
131
            dd($key);
132
            if($key != 0){//If Payment is linked to Invoice
133
        $invoice = Invoice::find($value);
134
        $invoice_status = 'pending';
135
        $payment = Payment::where('invoice_id',$value)->create([
136
=======
137
    public function updateForMultiplePayment($clientid,$invoiceChecked, $payment_method,
138
             $payment_date, $totalAmt, $invoicAmount, $amtToCredit, $payment_status)
139
    {
140
        try {
141
            foreach ($invoiceChecked as $key => $value) {
142
                if ($key != 0) {
143
                    $invoice = Invoice::find($value);
144
                    $invoice_status = 'pending';
145
                    $payment = Payment::where('invoice_id', $value)->create([
146
>>>>>>> 6f9f185892ed37a20bc00c4de5aedea3dea955eb
147
                'invoice_id'     => $value,
148
                'user_id'        => $clientid,
149
                'amount'         => $invoicAmount[$key],
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
                    if ($total_paid >= $invoice->grand_total) {
160
                        $invoice_status = 'success';
161
                    }
162
                    if ($invoice) {
163
                        $invoice->status = $invoice_status;
164
                        $invoice->save();
165
                    }
166
                }
167
            }
168
<<<<<<< HEAD
169
        }
170
        else{//If Payment is not linked to any invoice and is to be credited to User Accunt
171
            $payment = Payment::create([
172
                'invoice_id'     => $value,
173
                'user_id'       => $clientid,
174
                'amount'         =>$totalAmt,
175
                'payment_method' => $payment_method,
176
                'payment_status' => $payment_status,
177
                'created_at'     => $payment_date,
178
            ]);
179
        }
180
181
    }
182
    return $payment;
183
           
184
       } catch (Exception $e) {
185
=======
186
187
            return $payment;
188
        } catch (Exception $e) {
189
            dd($e);
190
>>>>>>> 6f9f185892ed37a20bc00c4de5aedea3dea955eb
191
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
192
            app('log')->error($ex->getMessage());
193
            Bugsnag::notifyException($ex);
194
195
            return redirect()->back()->with('fails', $ex->getMessage());
196
        }
197
    }
198
199
    public function updateNewMultiplePayment($clientid , Request $request)
200
    {
201
        try {
202
            $payment_date = $request->payment_date;
203
            $payment_method = $request->payment_method;
204
            $totalAmt=$request->totalAmt;
205
            $invoiceChecked = $request->invoiceChecked;
206
            $invoicAmount = $request->invoiceAmount;
207
            $amtToCredit = $request->amtToCredit;
208
            $payment_status= "success";
209
            $payment = $this->multiplePayment($clientid,$invoiceChecked, $payment_method,
210
             $payment_date, $totalAmt,$invoicAmount,$amtToCredit,$payment_status);
211
            $response = ['type' => 'success', 'message' => 'Payment Updated Successfully', ];
212
               return response()->json($response);
213
        } catch (\Exception $ex) {
214
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
215
            app('log')->error($ex->getMessage());
216
            Bugsnag::notifyException($ex);
217
218
            return redirect()->back()->with('fails', $ex->getMessage());
219
        }
220
221
    }
222
}
223