|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Http\Controllers; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
|
5
|
|
|
use Auth; |
|
6
|
|
|
use DB; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use App\Payment_detail; |
|
9
|
|
|
use App\Member; |
|
10
|
|
|
use App\Plan; |
|
11
|
|
|
use App\Subscription; |
|
12
|
|
|
use App\Invoice; |
|
13
|
|
|
use App\Invoice_detail; |
|
14
|
|
|
use App\Cheque_detail; |
|
15
|
|
|
use App\Sms_trigger; |
|
16
|
|
|
use App\Http\Requests; |
|
17
|
|
|
use App\Http\Controllers\Controller; |
|
18
|
|
|
|
|
19
|
|
|
class PaymentsController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->middleware('auth'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function index(Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
$payment_details = Payment_detail::indexQuery($request->sort_field,$request->sort_direction,$request->drp_start,$request->drp_end)->search('"'.$request->input('search').'"')->paginate(10); |
|
30
|
|
|
$paymentTotal = Payment_detail::indexQuery($request->sort_field,$request->sort_direction,$request->drp_start,$request->drp_end)->search('"'.$request->input('search').'"')->get(); |
|
31
|
|
|
$count = $paymentTotal->sum('payment_amount'); |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
if (!$request->has('drp_start') or !$request->has('drp_end')) |
|
35
|
|
|
{ |
|
36
|
|
|
$drp_placeholder = "Select daterange filter"; |
|
37
|
|
|
} |
|
38
|
|
|
else |
|
39
|
|
|
{ |
|
40
|
|
|
$drp_placeholder = $request->drp_start. ' - ' .$request->drp_end; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$request->flash(); |
|
44
|
|
|
|
|
45
|
|
|
return view('payments.index', compact('payment_details','count','drp_placeholder')); |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function create() |
|
50
|
|
|
{ |
|
51
|
|
|
return view('payments.create'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function store(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
DB::beginTransaction(); |
|
57
|
|
|
try |
|
58
|
|
|
{ |
|
59
|
|
|
// Storing Payment Details |
|
60
|
|
|
$payment_detail = new Payment_detail($request->all()); |
|
61
|
|
|
$payment_detail->createdBy()->associate(Auth::user()); |
|
62
|
|
|
$payment_detail->updatedBy()->associate(Auth::user()); |
|
63
|
|
|
$payment_detail->save(); |
|
64
|
|
|
|
|
65
|
|
|
if($request->mode == \constPaymentMode::Cheque) |
|
66
|
|
|
{ |
|
67
|
|
|
// Store Cheque Details |
|
68
|
|
|
$chequeData = array('payment_id'=> $payment_detail->id, |
|
69
|
|
|
'number'=> $request->number, |
|
70
|
|
|
'date'=> $request->date, |
|
71
|
|
|
'status'=> \constChequeStatus::Recieved); |
|
72
|
|
|
|
|
73
|
|
|
$cheque_details = new Cheque_detail($chequeData); |
|
74
|
|
|
$cheque_details->createdBy()->associate(Auth::user()); |
|
75
|
|
|
$cheque_details->updatedBy()->associate(Auth::user()); |
|
76
|
|
|
$cheque_details->save(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
elseif($request->mode == \constPaymentMode::Cash) |
|
80
|
|
|
{ |
|
81
|
|
|
// Updating Invoice Status and amounts |
|
82
|
|
|
$invoice_total = $payment_detail->invoice->total; |
|
|
|
|
|
|
83
|
|
|
$payment_total = Payment_detail::where('invoice_id',$payment_detail->invoice_id)->sum('payment_amount'); |
|
84
|
|
|
$amount_due = $invoice_total - $payment_total; |
|
85
|
|
|
|
|
86
|
|
|
$payment_detail->invoice->pending_amount = $amount_due; |
|
87
|
|
|
$payment_detail->invoice->status = \Utilities::setInvoiceStatus($amount_due,$invoice_total); |
|
88
|
|
|
$payment_detail->invoice->save(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//If cheque reissued , set the status of the previous cheque detail to Reissued |
|
92
|
|
|
if($request->has('previousPayment')) |
|
93
|
|
|
{ |
|
94
|
|
|
$cheque_detail = Cheque_detail::where('payment_id',$request->previousPayment)->first(); |
|
95
|
|
|
$cheque_detail->status = \constChequeStatus::Reissued; |
|
96
|
|
|
$cheque_detail->save(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// SMS Trigger |
|
100
|
|
|
$sender_id = \Utilities::getSetting('sms_sender_id'); |
|
101
|
|
|
$gym_name = \Utilities::getSetting('gym_name'); |
|
102
|
|
|
|
|
103
|
|
|
if($request->mode == \constPaymentMode::Cash) |
|
104
|
|
|
{ |
|
105
|
|
|
$sms_trigger = Sms_trigger::where('alias','=','payment_recieved')->first(); |
|
106
|
|
|
$message = $sms_trigger->message; |
|
107
|
|
|
$sms_text = sprintf($message,$payment_detail->invoice->member->name,$payment_detail->payment_amount,$payment_detail->invoice->invoice_number); |
|
108
|
|
|
$sms_status = $sms_trigger->status; |
|
109
|
|
|
$sender_id = \Utilities::getSetting('sms_sender_id'); |
|
110
|
|
|
|
|
111
|
|
|
\Utilities::Sms($sender_id,$payment_detail->invoice->member->contact,$sms_text,$sms_status); |
|
112
|
|
|
} |
|
113
|
|
|
elseif($request->mode == \constPaymentMode::Cheque) |
|
114
|
|
|
{ |
|
115
|
|
|
$sms_trigger = Sms_trigger::where('alias','=','payment_with_cheque')->first(); |
|
116
|
|
|
$message = $sms_trigger->message; |
|
117
|
|
|
$sms_text = sprintf($message,$payment_detail->invoice->member->name,$payment_detail->payment_amount,$cheque_details->number,$payment_detail->invoice->invoice_number,$gym_name); |
|
|
|
|
|
|
118
|
|
|
$sms_status = $sms_trigger->status; |
|
119
|
|
|
|
|
120
|
|
|
\Utilities::Sms($sender_id,$payment_detail->invoice->member->contact,$sms_text,$sms_status); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
DB::commit(); |
|
124
|
|
|
flash()->success('Payment Details were successfully stored'); |
|
125
|
|
|
return redirect (action('InvoicesController@show', ['id' => $payment_detail->invoice_id])); |
|
126
|
|
|
} |
|
127
|
|
|
catch (Exception $e) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
DB::rollback(); |
|
130
|
|
|
flash()->error('Payment Details weren\'t stored succesfully' ); |
|
131
|
|
|
return redirect('payments/all'); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function edit($id) |
|
136
|
|
|
{ |
|
137
|
|
|
$payment_detail = Payment_detail::findOrFail($id); |
|
138
|
|
|
$cheque_detail = Cheque_detail::where('payment_id',$id)->first(); |
|
139
|
|
|
|
|
140
|
|
|
return view('payments.edit', compact('payment_detail','cheque_detail')); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function update($id, Request $request) |
|
144
|
|
|
{ |
|
145
|
|
|
DB::beginTransaction(); |
|
146
|
|
|
|
|
147
|
|
|
try |
|
148
|
|
|
{ |
|
149
|
|
|
// Storing Payment Details |
|
150
|
|
|
$payment_detail=Payment_detail::findOrFail($id); |
|
151
|
|
|
$payment_detail->update($request->all()); |
|
152
|
|
|
$payment_detail->updatedBy()->associate(Auth::user()); |
|
153
|
|
|
$payment_detail->save(); |
|
154
|
|
|
|
|
155
|
|
|
if($request->mode == \constPaymentMode::Cheque) |
|
156
|
|
|
{ |
|
157
|
|
|
// Store Cheque Details |
|
158
|
|
|
$cheque_detail = Cheque_detail::where('payment_id',$id)->first(); |
|
159
|
|
|
$cheque_detail->update(['number' => $request->number, |
|
160
|
|
|
'date' => $request->date |
|
161
|
|
|
]); |
|
162
|
|
|
$cheque_detail->updatedBy()->associate(Auth::user()); |
|
163
|
|
|
$cheque_detail->save(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
elseif($request->mode == \constPaymentMode::Cash) |
|
167
|
|
|
{ |
|
168
|
|
|
// Updating Invoice Status and amounts |
|
169
|
|
|
$invoice_total = $payment_detail->invoice->total; |
|
170
|
|
|
$payment_total = Payment_detail::where('invoice_id',$payment_detail->invoice_id)->sum('payment_amount'); |
|
171
|
|
|
$amount_due = $invoice_total - $payment_total; |
|
172
|
|
|
|
|
173
|
|
|
$payment_detail->invoice->pending_amount = $amount_due; |
|
174
|
|
|
$payment_detail->invoice->status = \Utilities::setInvoiceStatus($amount_due,$invoice_total); |
|
175
|
|
|
$payment_detail->invoice->updatedBy()->associate(Auth::user()); |
|
176
|
|
|
$payment_detail->invoice->save(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
DB::commit(); |
|
180
|
|
|
flash()->success('Payment Details were successfully updated'); |
|
181
|
|
|
return redirect (action('InvoicesController@show', ['id' => $payment_detail->invoice_id])); |
|
182
|
|
|
} |
|
183
|
|
|
catch (Exception $e) |
|
184
|
|
|
{ |
|
185
|
|
|
DB::rollback(); |
|
186
|
|
|
flash()->error('Payment Details weren\'t updated succesfully' ); |
|
187
|
|
|
return redirect('payments'); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function delete($id) |
|
192
|
|
|
{ |
|
193
|
|
|
DB::beginTransaction(); |
|
194
|
|
|
|
|
195
|
|
|
try |
|
196
|
|
|
{ |
|
197
|
|
|
$payment_detail = Payment_detail::findOrFail($id); |
|
198
|
|
|
$invoice = Invoice::where('id',$payment_detail->invoice_id)->first(); |
|
199
|
|
|
$cheque_details = Cheque_detail::where('payment_id',$payment_detail->id)->get(); |
|
200
|
|
|
|
|
201
|
|
|
foreach($cheque_details as $cheque_detail) |
|
202
|
|
|
{ |
|
203
|
|
|
$cheque_detail->delete(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$payment_detail->delete(); |
|
207
|
|
|
|
|
208
|
|
|
$invoice_total = $invoice->total; |
|
209
|
|
|
$payment_total = Payment_detail::leftJoin('trn_cheque_details','trn_payment_details.id','=','trn_cheque_details.payment_id') |
|
210
|
|
|
->whereRaw("trn_payment_details.invoice_id = $invoice->id AND (trn_cheque_details.`status` = 2 or trn_cheque_details.`status` IS NULL)") |
|
211
|
|
|
->sum('trn_payment_details.payment_amount'); |
|
212
|
|
|
|
|
213
|
|
|
$amount_due = $invoice_total - $payment_total; |
|
214
|
|
|
|
|
215
|
|
|
$invoice->pending_amount = $amount_due; |
|
216
|
|
|
$invoice->status = \Utilities::setInvoiceStatus($amount_due,$invoice_total); |
|
217
|
|
|
$invoice->updatedBy()->associate(Auth::user()); |
|
218
|
|
|
$invoice->save(); |
|
219
|
|
|
|
|
220
|
|
|
DB::commit(); |
|
221
|
|
|
return redirect('payments/all'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
catch(\Exception $e) |
|
225
|
|
|
{ |
|
226
|
|
|
DB::rollback(); |
|
227
|
|
|
return redirect('payments/all'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function depositCheque($id) |
|
233
|
|
|
{ |
|
234
|
|
|
Cheque_detail::where('payment_id',$id)->update(['status' => \constChequeStatus::Deposited]); |
|
235
|
|
|
|
|
236
|
|
|
flash()->success('Cheque has been marked as deposited'); |
|
237
|
|
|
return back(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function clearCheque($id) |
|
241
|
|
|
{ |
|
242
|
|
|
DB::beginTransaction(); |
|
243
|
|
|
try |
|
244
|
|
|
{ |
|
245
|
|
|
$payment_detail = Payment_detail::findOrFail($id); |
|
246
|
|
|
|
|
247
|
|
|
// Updating cheque status |
|
248
|
|
|
$cheque_detail = Cheque_detail::where('payment_id',$id)->first(); |
|
249
|
|
|
$cheque_detail->status = \constChequeStatus::Cleared; |
|
250
|
|
|
$cheque_detail->updatedBy()->associate(Auth::user()); |
|
251
|
|
|
$cheque_detail->save(); |
|
252
|
|
|
|
|
253
|
|
|
// Updating Invoice Status and amounts |
|
254
|
|
|
$invoice_total = $payment_detail->invoice->total; |
|
255
|
|
|
|
|
256
|
|
|
$payment_total = Payment_detail::leftJoin('trn_cheque_details','trn_payment_details.id','=','trn_cheque_details.payment_id') |
|
257
|
|
|
->whereRaw("trn_payment_details.invoice_id = $payment_detail->invoice_id AND (trn_cheque_details.`status` = 2 or trn_cheque_details.`status` IS NULL)") |
|
258
|
|
|
->sum('trn_payment_details.payment_amount'); |
|
259
|
|
|
|
|
260
|
|
|
$amount_due = $invoice_total - $payment_total; |
|
261
|
|
|
|
|
262
|
|
|
$payment_detail->invoice->pending_amount = $amount_due; |
|
263
|
|
|
$payment_detail->invoice->status = \Utilities::setInvoiceStatus($amount_due,$invoice_total); |
|
264
|
|
|
$payment_detail->invoice->save(); |
|
265
|
|
|
|
|
266
|
|
|
DB::commit(); |
|
267
|
|
|
flash()->success('Cheque has been marked as cleared'); |
|
268
|
|
|
return back(); |
|
269
|
|
|
} |
|
270
|
|
|
catch(Exception $e) |
|
271
|
|
|
{ |
|
272
|
|
|
DB::rollback(); |
|
273
|
|
|
flash()->error('Error while marking the cheque as cleared'); |
|
274
|
|
|
return back(); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function chequeBounce($id) |
|
279
|
|
|
{ |
|
280
|
|
|
Cheque_detail::where('payment_id',$id)->update(['status' => \constChequeStatus::Bounced]); |
|
281
|
|
|
|
|
282
|
|
|
flash()->success('Cheque has been marked as bounced'); |
|
283
|
|
|
return back(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public function chequeReissue($id) |
|
287
|
|
|
{ |
|
288
|
|
|
$payment_detail = Payment_detail::findOrFail($id); |
|
289
|
|
|
|
|
290
|
|
|
return view('payments.reissue',compact('payment_detail')); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|