PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Http/Controllers/PaymentController.php

https://bitbucket.org/Silali/property_manager
PHP | 187 lines | 167 code | 20 blank | 0 comment | 22 complexity | e2840eb99b2aebc1629324a65983f84d MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\PaymentAdded;
  4. use App\Invoice;
  5. use Barryvdh\DomPDF\Facade as PDF;
  6. use App\Payment;
  7. use App\PaymentMethod;
  8. use App\Traits\HandlesInvoices;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Kamaln7\Toastr\Facades\Toastr;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. use Plank\Mediable\MediaUploaderFacade as MediaUploader;
  14. class PaymentController extends Controller
  15. {
  16. use HandlesInvoices;
  17. public function index(Request $request)
  18. {
  19. $invoice = new Invoice();
  20. $query = $invoice->newQuery();
  21. if ($request->has('from') && $request->has('to')) {
  22. if (!empty($request->to) && !empty($request->from)) {
  23. $from = Carbon::parse($request->from)->format('Y-m-d H:i:s');;
  24. $to = Carbon::parse($request->to)->addSeconds(86399)->format('Y-m-d H:i:s');
  25. $query->whereBetween('created_at', [$from, $to]);
  26. }
  27. } else {
  28. $start_date = Carbon::now()->startOfDay();
  29. $end_date =Carbon::now()->endOfDay();
  30. $query->whereBetween('created_at', [$start_date, $end_date]);
  31. }
  32. $invoices = $query->orderBy('created_at', 'desc')->get();
  33. $title = "Invoices";
  34. $filter = session()->get('filter_params');
  35. if ($request->has('excel')) {
  36. if (count($invoices) > 0) {
  37. return $this->exportRecords($invoices, $title,'excel');
  38. } else {
  39. Toastr::info('no records to export');
  40. return redirect('payments');
  41. }
  42. }
  43. if ($request->has('pdf')) {
  44. if (count($invoices) > 0) {
  45. return $this->exportRecords($invoices, $title,'pdf');
  46. } else {
  47. Toastr::info('no records to export');
  48. return redirect('payments');
  49. }
  50. }
  51. return view('payments.index', get_defined_vars());
  52. }
  53. public function create($id)
  54. {
  55. $invoice = Invoice::find($id);
  56. if ($invoice->cleared) {
  57. Toastr::info("Invoice #$invoice->id cleared");
  58. return redirect('payments');
  59. }
  60. $payments = $invoice->payments()->orderBy('date_paid')->get();
  61. $payment_methods = PaymentMethod::all();
  62. $title = "payment for invoice #$invoice->id";
  63. return view('payments.create', get_defined_vars());
  64. }
  65. public function store(Request $request, $invoice_id)
  66. {
  67. $this->validate($request, [
  68. 'date_paid' => 'required',
  69. 'amount' => 'required',
  70. 'payment_method' => 'required',
  71. 'document_file' => 'required | mimes:jpeg,png,pdf'
  72. ]);
  73. $invoice = Invoice::find($invoice_id);
  74. $payment_method = PaymentMethod::find($request->payment_method);
  75. $data = [
  76. 'invoice_id' => $invoice_id,
  77. 'amount_paid' => $request->amount,
  78. 'date_paid' => Carbon::parse($request->date_paid),
  79. 'payment_method' => $payment_method->name
  80. ];
  81. $payment = $invoice->payments()->create($data);
  82. $latest = $invoice->tenant->statement()->orderBy('id', 'desc')->first();
  83. $balance = 0;
  84. if (count($latest) > 0) {
  85. $balance = -1 * ($payment->amount_paid - $latest->balance);
  86. }
  87. $statement = [
  88. 'date' => $payment->created_at,
  89. 'receipt_id' => $payment->id,
  90. 'balance' => $balance,
  91. 'debit' => $payment->amount_paid
  92. ];
  93. $invoice->tenant->statement()->create($statement);
  94. if ((bool) $payment) {
  95. $tenant = str_replace(' ', '_', strtolower($invoice->tenant->name));
  96. $date_paid = Carbon::parse($request->date_paid)->format('Y_m_d');
  97. $file = $request->file('document_file');
  98. $media = MediaUploader::fromSource($file)
  99. ->useFilename($tenant."_".$payment_method->slug."_".$date_paid."_".uniqid())
  100. ->toDestination('public', 'payments')
  101. ->upload();
  102. $payment->attachMedia($media, 'document');
  103. if ($this->clearInvoice($invoice, $invoice->payments()->sum('amount_paid'))) {
  104. Toastr::info("Invoice #$invoice->id cleared");
  105. event(new PaymentAdded($payment));
  106. return redirect('payments');
  107. }
  108. $message = "payment for invoice #$invoice_id made successfully";
  109. event(new PaymentAdded($payment));
  110. Toastr::success($message);
  111. return back();
  112. } else {
  113. $message = "error adding payment try again";
  114. Toastr::error($message);
  115. return back();
  116. }
  117. }
  118. public function exportRecords($records, $title, $format)
  119. {
  120. $filename = strtolower($title)."_".uniqid();
  121. $items = $this->transform($records);
  122. if ($format == 'excel') {
  123. return Excel::create($filename, function($excel) use ($items) {
  124. $excel->sheet("invoices", function($sheet) use ($items){
  125. $sheet->cells('A1:Z1', function ($cells) {
  126. $cells->setBackground('#9BD3FF');
  127. $cells->setAlignment('center');
  128. $cells->setFont([
  129. 'bold' => true
  130. ]);
  131. });
  132. $sheet->setBorder('A1:Z1', 'medium');
  133. $sheet->fromArray($items);
  134. });
  135. })->download('xlsx');
  136. } elseif($format == 'pdf') {
  137. $pdf = PDF::loadView('pdf.payments', get_defined_vars());
  138. $pdf->setPaper('A4', 'landscape');
  139. return $pdf->download($filename.".pdf");
  140. }
  141. }
  142. public function transform($records)
  143. {
  144. $items = [];
  145. foreach ($records as $record) {
  146. $item = array(
  147. 'Tenant' => (string)$record->tenant ? $record->tenant->name : 'N/A',
  148. 'Building' => (string)$record->property_name,
  149. 'Floor' => (string)$record->floor_level,
  150. 'Unit' => (string)$record->unit_number,
  151. 'Amount Due' => (string) number_format($record->amount_payable),
  152. 'Payment Term' => (string)$record->payment_term,
  153. 'Rate (%)' => (string)$record->rate_of_contract != 'N/A'? : 'N/A',
  154. 'Period' => (string)$record->period_of_contract,
  155. 'Status' =>(string) $record->cleared ? 'paid' : 'unpaid'
  156. );
  157. array_push($items, $item);
  158. }
  159. return $items;
  160. }
  161. public function getDocument($id)
  162. {
  163. $payment = Payment::find($id);
  164. $path = $payment->document_url;
  165. return response()->download(storage_path("app/".$path));
  166. }
  167. }