mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
2133 lines
112 KiB
PHP
Executable File
2133 lines
112 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Invoices\Http\Controllers;
|
|
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Modules\Invoices\Services\InvoicesService;
|
|
use Streamline\Models\Banking;
|
|
use Streamline\Models\CashierIncome;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\DependantsConsumption;
|
|
use Streamline\Models\DonorDiscountDetail;
|
|
use Streamline\Models\DonorInvoicePayment;
|
|
use Streamline\Models\HospitalInvoicePayment;
|
|
use Streamline\Http\Controllers\Controller;
|
|
use Modules\PatientFinance\Http\Controllers\PatientFinanceController;
|
|
use Streamline\Models\InvoicePayment;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\PatientCategoryInvoice;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Streamline\Models\PatientDiscount;
|
|
|
|
use Carbon\Carbon;
|
|
use Streamline\Models\PatientPaymentMethod;
|
|
use Streamline\Models\Payment;
|
|
use Streamline\Models\Quotation;
|
|
use Streamline\Models\QuotationType;
|
|
use Streamline\Models\TrackInvoice;
|
|
use Streamline\Models\TrackReceipt;
|
|
use Streamline\Services\ReceiptService;
|
|
|
|
class InvoicesController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected InvoicesService $invoicesService,
|
|
protected ReceiptService $receiptService,
|
|
) {
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:Invoices-generate');
|
|
$this->middleware('permission:Invoices-receive-payments');
|
|
$this->middleware('permission:Invoices-view-payments-history');
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
return view('invoices::invoices.index');
|
|
}
|
|
|
|
public function patient_category_detail($id, $start_date, $end_date) {
|
|
$hospital_information = HospitalInformation::first();
|
|
$start_date = Carbon::parse($start_date)->startOfDay()->toDateTimeString();
|
|
$end_date = Carbon::parse($end_date)->endOfDay()->toDateTimeString();
|
|
$invoices = PatientCategoryInvoice::where('episode_id', $id)
|
|
->whereBetween('transaction_date', [$start_date, $end_date])
|
|
->get();
|
|
return view('invoices::invoices.detail.patient_category', compact('invoices', 'hospital_information', 'id', 'start_date', 'end_date'));
|
|
}
|
|
|
|
public function patient_category_detail_print($id, $start_date, $end_date) {
|
|
$hospital_information = HospitalInformation::first();
|
|
$start_date = Carbon::parse($start_date)->startOfDay();
|
|
$end_date = Carbon::parse($end_date)->endOfDay();
|
|
$invoices = PatientCategoryInvoice::where('episode_id', $id)
|
|
->whereBetween('transaction_date', [$start_date, $end_date])
|
|
->get();
|
|
|
|
$data = [
|
|
'invoices' => $invoices,
|
|
'hospitalInfo' => $hospital_information
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView("invoices::invoices/detail/patient_category_print", $data)
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 5)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>© ' . date('Y') . ' Stre@mline</i>');
|
|
|
|
|
|
return $pdf->inline('Investigation Details' . date(" d-m-y h:ia") . '.pdf');
|
|
|
|
return view('invoices::invoices.detail.patient_category', compact('invoices', 'hospital_information'));
|
|
}
|
|
|
|
public function viewPatientCategory(Request $request)
|
|
{
|
|
$total = 0;
|
|
$patient_categories = DB::table('patient_discounts')->where(['pay_later' => 1])->pluck('patient_category')->toArray();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
if ($request->has('patient_number')) {
|
|
//if the guy is a main patient with dependants, fetch all records of him and his dependants
|
|
$patient_and_their_dependants_array = get_patient_and_their_dependants($request->patient_number);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('transaction_date', [$start, $end])
|
|
->where('patient_category', $request->patient_category)
|
|
->whereIn('patient_id', $patient_and_their_dependants_array)
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
} else{
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('transaction_date', [$start, $end])
|
|
->where('patient_category', $request->patient_category)
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
if (count($invoices_not_generated) > 0) {
|
|
foreach ($invoices_not_generated as $item) {
|
|
$total += $item->sum;
|
|
}
|
|
}
|
|
|
|
$display = dateLabelSetter($request);
|
|
|
|
return view('invoices::invoices.patient_category', compact(
|
|
'patient_categories',
|
|
'invoices_not_generated',
|
|
'request',
|
|
'display',
|
|
'total'
|
|
));
|
|
}
|
|
|
|
public function generate_invoice(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$users_name = get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users');
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
$invoices = [];
|
|
$invoice_with_payment_amount_paid = $invoice_with_payment_balance = 0;
|
|
|
|
if ($request->status == "reprint") {
|
|
|
|
$invoices = DB::table('patient_category_invoices')->where('invoice_number', $request->invoice_number)
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->where('invoice_generated', 1)
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
|
|
$invoice_payment_records = InvoicePayment::where('invoice_number', $request->invoice_number)->get();
|
|
if (count($invoice_payment_records) > 0) {
|
|
foreach ($invoice_payment_records as $invoice_payment) {
|
|
$invoice_with_payment_amount_paid += $invoice_payment->amount_paid;
|
|
$invoice_with_payment_balance += $invoice_payment->balance;
|
|
}
|
|
}
|
|
} elseif ($request->status == "new") {
|
|
$last_invoice = DB::table('patient_category_invoices')->select('invoice_number')->distinct()->orderBy('invoice_number', 'desc')->first();
|
|
$last_invoice_number = $last_invoice->invoice_number;
|
|
|
|
$invoices = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('transaction_date', [$start, $end])
|
|
->where('patient_category', $request->patient_category)
|
|
->where('invoice_generated', 1)
|
|
->where('status', 0)
|
|
->where('invoice_number', $last_invoice_number)
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
return view('invoices::invoices.generate_invoice.patient_invoice', compact(
|
|
'invoices',
|
|
'request',
|
|
'users_name',
|
|
'hospital_information',
|
|
'invoice_with_payment_amount_paid',
|
|
'invoice_with_payment_balance'
|
|
));
|
|
}
|
|
|
|
public function print_invoice($invoice_number)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$invoices = PatientCategoryInvoice::where('invoice_number', $invoice_number)
|
|
->groupBy('episode_id')
|
|
->selectRaw('*, sum(patient_amount) as sum')
|
|
->get();
|
|
|
|
$patient_category = PatientCategoryInvoice::where('invoice_number', $invoice_number)
|
|
->pluck('patient_category')
|
|
->first();
|
|
|
|
$invoice_with_payment_amount_paid = $invoice_with_payment_balance = 0;
|
|
$invoice_payment_records = InvoicePayment::where('invoice_number', $invoice_number)->get();
|
|
if (count($invoice_payment_records) > 0) {
|
|
foreach ($invoice_payment_records as $invoice_payment) {
|
|
$invoice_with_payment_amount_paid += $invoice_payment->amount_paid;
|
|
$invoice_with_payment_balance += $invoice_payment->balance;
|
|
}
|
|
}
|
|
|
|
$data = [
|
|
'invoices' => $invoices,
|
|
'hospitalInfo' => $hospital_information,
|
|
'patient_category' => $patient_category,
|
|
'invoice_number' => $invoice_number,
|
|
'invoice_with_payment_amount_paid' => $invoice_with_payment_amount_paid,
|
|
'invoice_with_payment_balance' => $invoice_with_payment_balance,
|
|
'served_by' => get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users')
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView("invoices::invoices/print/patient_category_invoice", $data)
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 10)
|
|
->setOption('margin-top', 10)
|
|
->setOption('footer-html', '<i>© ' . date('Y') . ' Stre@mline</i>');
|
|
|
|
return $pdf->inline('Patient Category Invoice' . date(" d-m-y h:ia") . '.pdf');
|
|
}
|
|
|
|
public function receive_patient_invoices(Request $request)
|
|
{
|
|
$invoices_counter = 0;
|
|
$invoices = [];
|
|
$generated_invoice_numbers_array = [];
|
|
|
|
$patient_categories = DB::table('patient_discounts')->where(['pay_later' => 1])->pluck('patient_category')->toArray();
|
|
|
|
if (isset($request->start_date)) {
|
|
$start_date = Carbon::parse($request->start_date)->startOfDay();
|
|
$end_date = Carbon::parse($request->end_date)->endOfDay();
|
|
} else {
|
|
$start_date = Carbon::now()->startOfDay();
|
|
$end_date = Carbon::now()->endOfDay();
|
|
}
|
|
|
|
if (isset($request->patient_category) && $request->patient_category != 0) {
|
|
$invoices_temp = PatientCategoryInvoice::where('invoice_generated', 1)
|
|
->where('patient_category', $request->patient_category)
|
|
->whereBetween('transaction_date', [$start_date, $end_date])
|
|
->groupBy('invoice_number')
|
|
->get();
|
|
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
} else {
|
|
$invoices_temp = PatientCategoryInvoice::where('invoice_generated', 1)
|
|
->whereBetween('transaction_date', [$start_date, $end_date])
|
|
->groupBy('invoice_number')
|
|
->get();
|
|
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
|
|
foreach ($invoices_temp as $record) {
|
|
$patient_category_invoice_numbers = PatientCategoryInvoice::where('invoice_number', $record->invoice_number)
|
|
->orderBy('transaction_date', 'asc')->get();
|
|
|
|
$total_invoice_payment = 0;
|
|
|
|
foreach ($patient_category_invoice_numbers as $patient_category_invoice_number) {
|
|
$total_invoice_payment += (int)$patient_category_invoice_number->patient_amount;
|
|
}
|
|
|
|
// check if invoice has a payment record
|
|
$invoice_payment_record = InvoicePayment::where('invoice_number', $record->invoice_number)->first();
|
|
|
|
if ($invoice_payment_record) {
|
|
if ($invoice_payment_record->balance > 0) {
|
|
// for those invoices with payments, only bring those with a balance
|
|
$invoices[$invoices_counter]['has_payment'] = 1;
|
|
$invoices[$invoices_counter]['amount_paid'] = $invoice_payment_record->amount_paid;
|
|
$invoices[$invoices_counter]['payment_id'] = $invoice_payment_record->id;
|
|
$invoices[$invoices_counter]['balance'] = $invoice_payment_record->balance;
|
|
} else {
|
|
// do not continue to add an invoice record
|
|
continue;
|
|
}
|
|
} else {
|
|
$invoices[$invoices_counter]['has_payment'] = 0;
|
|
$invoices[$invoices_counter]['amount_paid'] = 0;
|
|
$invoices[$invoices_counter]['payment_id'] = 0;
|
|
$invoices[$invoices_counter]['balance'] = 0;
|
|
}
|
|
|
|
$invoices[$invoices_counter]['patient_amount'] = $total_invoice_payment;
|
|
$invoices[$invoices_counter]['invoice_number'] = $record->invoice_number;
|
|
$invoices[$invoices_counter]['invoice_date'] = $record->invoice_date;
|
|
$invoices[$invoices_counter]['patient_category'] = $record->patient_category;
|
|
$invoices[$invoices_counter]['created_at'] = $record->transaction_date;
|
|
$invoices[$invoices_counter]['updated_at'] = $record->updated_at;
|
|
$invoices[$invoices_counter]['created_by'] = $record->created_by;
|
|
$generated_invoice_numbers_array[] = $record->invoice_number;
|
|
|
|
$invoices_counter++;
|
|
}
|
|
|
|
/* Arranging the data*/
|
|
asort($invoices);
|
|
|
|
if (isset($request->patient_category) && $request->patient_category != 0) {
|
|
$payments = InvoicePayment::whereBetween('created_at', [$start_date, $end_date])
|
|
->where('patient_category', $request->patient_category)->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get();
|
|
} else {
|
|
$payments = InvoicePayment::whereBetween('created_at', [$start_date, $end_date])
|
|
->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get();
|
|
}
|
|
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$patient_payment_methods = PatientPaymentMethod::pluck('name', 'id');
|
|
$patient_payment_methods_options = "";
|
|
foreach ($patient_payment_methods as $key => $value) {
|
|
$patient_payment_methods_options .= '<option value="' . $key . '">' . $value . '</option>';
|
|
}
|
|
|
|
return view('invoices::invoices.receive_payments.receive_patient_payments', compact('invoices', 'start_date', 'end_date', 'patient_categories', 'searched_patient_category',
|
|
'payments', 'banks', 'patient_payment_methods_options'));
|
|
}
|
|
|
|
public function process_invoice(Request $request)
|
|
{
|
|
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
if ($request->has('patient_id') && $request->patient_id != null) {
|
|
//if the guy is a main patient with dependants, fetch all records of him and his dependants
|
|
$patient_and_their_dependants_array = get_patient_and_their_dependants($request->patient_id);
|
|
|
|
$update = PatientCategoryInvoice::whereBetween('transaction_date', [$start, $end])
|
|
->where('invoice_generated', 0)->where('patient_category', $request->patient_category)->whereIn('patient_id', $patient_and_their_dependants_array)->get();
|
|
|
|
|
|
$update = PatientCategoryInvoice::whereBetween('transaction_date', [$start, $end])
|
|
->where('invoice_generated', 0)->where('patient_category', $request->patient_category)->whereIn('patient_id', $patient_and_their_dependants_array)
|
|
->update(
|
|
[
|
|
'invoice_generated' => 1,
|
|
'invoice_date' => $request->invoice_date,
|
|
'invoice_number' => $request->invoice_number,
|
|
'is_invoice_for_individual' => $request->patient_id
|
|
]
|
|
);
|
|
} else{
|
|
$update = PatientCategoryInvoice::whereBetween('transaction_date', [$start, $end])
|
|
->where('invoice_generated', 0)->where('patient_category', $request->patient_category)->get();
|
|
|
|
|
|
$update = PatientCategoryInvoice::whereBetween('transaction_date', [$start, $end])
|
|
->where('invoice_generated', 0)->where('patient_category', $request->patient_category)
|
|
->update(
|
|
[
|
|
'invoice_generated' => 1,
|
|
'invoice_date' => $request->invoice_date,
|
|
'invoice_number' => $request->invoice_number
|
|
]
|
|
);
|
|
}
|
|
|
|
$track_invoice = new TrackInvoice;
|
|
$track_invoice->reason = $request->patient_category;
|
|
$track_invoice->created_by = Auth::id();
|
|
|
|
if (!is_null($update) && $track_invoice->save()) {
|
|
return $this->generate_invoice($request);
|
|
} else {
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function patient_history()
|
|
{
|
|
$payments = DB::table('invoice_payments')->groupBy('invoice_number')->get()->toArray();
|
|
return view('invoices::invoices.payments_history.patient_index', compact('payments'));
|
|
}
|
|
|
|
public function patient_payment_receipt(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
return view('invoices::invoices.receipts.patient_payment_receipt', compact('request', 'hospital_information'));
|
|
}
|
|
|
|
public function delete_patient_category_invoice($number)
|
|
{
|
|
$delete = PatientCategoryInvoice::where('invoice_number', $number)->update([
|
|
'invoice_number' => 0,
|
|
'invoice_generated' => 0,
|
|
'invoice_date' => null,
|
|
]);
|
|
|
|
// remove from dependants if any
|
|
$dependant_delete = DependantsConsumption::where('invoice_number', $number)->update(['invoice_number' => NULL]);
|
|
|
|
if (is_null($delete)) {
|
|
flash('Unable to delete invoice with #' . $number)->error();
|
|
return back();
|
|
} else {
|
|
flash('Invoice #' . $number . " has been deleted successfully.")->success();
|
|
return back();
|
|
}
|
|
}
|
|
|
|
public function viewDonorDiscounts(Request $request)
|
|
{
|
|
|
|
$total = 0;
|
|
$donors = DB::table('donors')->get()->toArray();
|
|
$patient_discounts = PatientDiscount::where(['pay_later' => 1])->get();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
$display = dateLabelSetter($request);
|
|
|
|
foreach ($patient_discounts as $patient_discount) {
|
|
$patient_categories = DB::table('patient_categories')
|
|
->where(['id' => $patient_discount->patient_category])
|
|
->pluck('name', 'id')
|
|
->toArray();
|
|
}
|
|
|
|
$discounts = DB::table('donor_discount_details')
|
|
->whereBetween('created_at', [$start, $end])
|
|
->where('invoice_generated', 0)
|
|
->groupBy('patient_category')
|
|
->get()
|
|
->toArray();
|
|
|
|
foreach ($discounts as $item) {
|
|
$total += $item->donor_to_pay;
|
|
}
|
|
return view('invoices::invoices.donor_discounts', compact('discounts', 'request', 'donors', 'total', 'display'));
|
|
}
|
|
|
|
public function process_donor_invoice(Request $request)
|
|
{
|
|
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
$update = DonorDiscountDetail::whereBetween('created_at', [$start, $end])
|
|
->update(
|
|
[
|
|
'invoice_generated' => 1,
|
|
'invoice_date' => $request->invoice_date,
|
|
'invoice_number' => $request->invoice_number
|
|
]
|
|
);
|
|
|
|
if (!is_null($update)) {
|
|
return $this->generate_donor_invoice($request);
|
|
} else {
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function generate_donor_invoice(Request $request)
|
|
{
|
|
|
|
$hospital_information = HospitalInformation::first();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
$users_name = get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users');
|
|
if ($request->status == "reprint") {
|
|
|
|
$invoices = DB::table('donor_discount_details')
|
|
->where('invoice_number', $request->invoice_number)
|
|
->get()
|
|
->toArray();
|
|
} else {
|
|
$invoices = DB::table('donor_discount_details')
|
|
->whereBetween('created_at', [$start, $end])
|
|
->where('donor_id', $request->donor)
|
|
->where('invoice_generated', 1)
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
return view('invoices::invoices.generate_invoice.donor_invoice',
|
|
compact('invoices', 'request', 'users_name', 'hospital_information')
|
|
);
|
|
}
|
|
|
|
public function receive_donor_invoices()
|
|
{
|
|
|
|
$request = Request::capture();
|
|
$uncleared_invoices = DB::table('donor_discount_details')
|
|
->groupBy('invoice_number')
|
|
->selectRaw('*, sum(donor_to_pay) as sum')
|
|
->where('payment_status', 0)
|
|
->where('invoice_generated', 1)
|
|
->get();
|
|
|
|
$reason = "donor invoice payments";
|
|
|
|
$invoices_with_balance = DB::table('donor_invoice_payments')
|
|
->where('balance', '>', 0)
|
|
->groupBy('invoice_number')
|
|
->get()
|
|
->toArray();
|
|
|
|
return view('invoices::invoices.receive_payments.receive_donor_payments',
|
|
compact('uncleared_invoices', 'reason', 'invoices_with_balance', 'request')
|
|
);
|
|
}
|
|
|
|
public function donor_invoice_payment(Request $request)
|
|
{
|
|
return view('invoices::invoices.payment.pay_for_donor_invoice', compact('request'));
|
|
}
|
|
|
|
public function patient_invoice_payment(Request $request) {
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
|
|
$patient_payment_methods = PatientPaymentMethod::pluck('name', 'id');
|
|
$patient_payment_methods_options = "";
|
|
foreach ($patient_payment_methods as $key => $value) {
|
|
$patient_payment_methods_options .= '<option value="' . $key . '">' . $value . '</option>';
|
|
}
|
|
|
|
$total = $request->total;
|
|
$patient_category = $request->patient_category;
|
|
$balance = $request->balance;
|
|
$status = $request->status;
|
|
$invoice_date = $request->invoice_date;
|
|
$invoice_number = $request->invoice_number;
|
|
$generated_on = $request->generated_on;
|
|
|
|
return view('invoices::invoices.payment.pay_for_invoice', compact( 'banks', 'expense_accounts', 'patient_payment_methods_options',
|
|
'patient_category', 'status', 'balance', 'total', 'invoice_number', 'invoice_date', 'generated_on'));
|
|
}
|
|
|
|
public function update_donor_payment(Request $request)
|
|
{
|
|
$new_receipt_number = $this->receiptService->createReceipt('Donor Payment');
|
|
|
|
$date_paid_history = unserialize($request->date_paid_history);
|
|
array_push($date_paid_history, $request->payment_received_on);
|
|
$date_paid_history = serialize($date_paid_history);
|
|
|
|
$amount_paid_history = unserialize($request->amount_paid_history);
|
|
array_push($amount_paid_history, $request->amount_paid);
|
|
$amount_paid_history = serialize($amount_paid_history);
|
|
|
|
$staff_incharge_history = unserialize($request->staff_incharge_history);
|
|
array_push($staff_incharge_history, Auth::id());
|
|
$staff_incharge_history = serialize($staff_incharge_history);
|
|
|
|
$receipt_number = unserialize($request->receipt_number);
|
|
array_push($receipt_number, $new_receipt_number);
|
|
$receipt_number = serialize($receipt_number);
|
|
|
|
$payment = DonorInvoicePayment::where('id', $request->id)
|
|
->update(
|
|
[
|
|
'total' => $request->total,
|
|
'amount_paid' => $request->amount_paid,
|
|
'balance' => $request->balance,
|
|
'receipt_number' => $receipt_number,
|
|
'amount_paid_history' => $amount_paid_history,
|
|
'staff_incharge_history' => $staff_incharge_history,
|
|
'date_paid_history' => $date_paid_history
|
|
]
|
|
);
|
|
|
|
if (!is_null($payment)) {
|
|
flash('Payment Updated');
|
|
return $this->donor_payment_receipt($request);
|
|
} else {
|
|
flash('Payment Not Updated');
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function update_patient_payment(Request $request) {
|
|
$new_receipt_number = $this->receiptService->createReceipt('Invoice Payment');
|
|
|
|
$logged_in_user_id = Auth::id();
|
|
$banked = null;
|
|
$transfer_date = Carbon::parse($request->payment_received_on)->toDateString();
|
|
|
|
$payment_received_on = Carbon::parse($request->payment_received_on)->toDateString();
|
|
|
|
if (!is_null($request->bank) && !is_null($request->bank_balance)) {
|
|
// first, the money from the invoice has to be received as
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
10,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, 10, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', 10)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$request->amount_paid)]);
|
|
|
|
// next, deal with the transfer between banks
|
|
// ...
|
|
// transfer date balances
|
|
$to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->bank, $transfer_date);
|
|
$to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0;
|
|
$from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// present day balances
|
|
$to_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->bank, Carbon::today()->toDateString());
|
|
$to_account_balance_today = ($to_account_balance_today_record != null) ? $to_account_balance_today_record->account_balance : 0;
|
|
$from_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$from_account_balance_today = ($from_account_balance_today_record != null) ? $from_account_balance_today_record->account_balance : 0;
|
|
|
|
// deal with from account
|
|
$from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - (int)$request->amount_paid);
|
|
$last_insert_id_from_account = capture_bank_record(
|
|
'TRANSFER',
|
|
$transfer_date,
|
|
10,
|
|
$request->bank,
|
|
$from_account_balance_after_transfer,
|
|
0,
|
|
(int)$request->amount_paid,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $request->transfer_from, $last_insert_id_from_account, $from_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', 10)->update(['balance' => ($from_account_balance_today - (int)$request->amount_paid)]);
|
|
|
|
|
|
// deal with to account
|
|
$to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_to_account = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$request->bank,
|
|
10,
|
|
$to_account_balance_after_transfer,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
$bank_record = Banking::where('id', $last_insert_id_to_account)->first();
|
|
$bank_record->created_at = $logged_in_user_id;
|
|
$bank_record->update();
|
|
update_banking_record_balances($transfer_date, $request->bank, $last_insert_id_to_account, $to_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $request->bank)->update(['balance' => ($to_account_balance_today + (int)$request->amount_paid)]);
|
|
|
|
$banked = $last_insert_id_to_account;
|
|
|
|
update_banking_record_balances($payment_received_on, $request->bank, $bank_record->id, ((int)$request->bank_balance + (int)$request->amount_paid));
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $request->amount_paid;
|
|
$cashier_income->reason = $request->memo;
|
|
$cashier_income->amount = $request->amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = (int)$request->undeposited_funds_bank_balance;
|
|
$cashier_income->account_balance = (int)$request->undeposited_funds_bank_balance + (int)$request->amount_paid;
|
|
$cashier_income->banked = $banked;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
$cashier_income_amount = $request->amount_paid;
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $request->invoice_number])->where(['invoice_date' => $request->invoice_date])
|
|
->update(['received' => $cashier_income_id]);
|
|
} else {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
// ...
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
10,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
"Deposit to undeposited funds",
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, 10, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $request->amount_paid;
|
|
$cashier_income->reason = "Received from patient category invoices";
|
|
$cashier_income->amount = $request->amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = $undeposited_funds_account_balance_on_transfer_date;
|
|
$cashier_income->account_balance = $undeposited_funds_account_balance_after_deposit;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
$cashier_income_amount = $request->amount_paid;
|
|
}
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $request->invoice_number])
|
|
->where(['invoice_date' => $request->invoice_date])
|
|
->update(['status' => 1, 'received' => is_null($cashier_income_id) ? null : $cashier_income_id]);
|
|
|
|
$other_parameters = [
|
|
'received_id' => $cashier_income_id,
|
|
'received_amount' => $cashier_income_amount,
|
|
'banked_id' => $banked,
|
|
];
|
|
|
|
$invoice_payment_record = $this->invoicesService->saveInvoicePaymentRecord(
|
|
$request->invoice_number, $new_receipt_number, $request->amount_paid, $request->payment_received_on,
|
|
0, null, $other_parameters
|
|
);
|
|
$this->invoicesService->updateInvoicePayment($request->invoice_number, $request->amount_paid, $request->balance);
|
|
|
|
$request->receipt_number = $new_receipt_number;
|
|
|
|
update_account_balance_by_id(10, $request->amount_paid);
|
|
|
|
$this->invoicesService->clearInvoice($request->invoice_number);
|
|
clear_dependants_consumptions($request->invoice_number);//if dependent invoice clear it
|
|
|
|
$return_payment_methods = PatientFinanceController::register_payment_method(0, 0, $request->amount_paid, $request->payment_method,
|
|
$request->payment_methods_amount, array_fill(0, count($request->payment_methods_amount ?? []), null), array_fill(0, count($request->payment_methods_amount ?? []), 0), $new_receipt_number, 17, Auth::id(), 0);
|
|
|
|
if ($invoice_payment_record) {
|
|
flash('Payment Updated');
|
|
return $this->patient_payment_receipt($request);
|
|
} else {
|
|
flash('Payment Not Updated');
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function process_donor_payment(Request $request)
|
|
{
|
|
|
|
if ($request->status == "update") {
|
|
|
|
return $this->update_donor_payment($request);
|
|
} else {
|
|
$payment = new DonorInvoicePayment;
|
|
$new_receipt_number = $this->receiptService->createReceipt('Donor Payment');
|
|
$today = Carbon::today()->toDateTimeString();
|
|
|
|
$payment->donor_id = $request->donor_id; //
|
|
$payment->invoice_date = $request->invoice_date; //
|
|
$payment->amount_paid = $request->amount_paid; //
|
|
$payment->invoice_number = $request->invoice_number; //
|
|
$payment->total = $request->total; //
|
|
$payment->balance = $request->balance; //
|
|
$payment->receipt_number = serialize(array($new_receipt_number)); //
|
|
$payment->amount_paid_history = serialize(array($request->amount_paid));
|
|
$payment->date_paid_history = serialize(array($today));
|
|
$payment->staff_incharge_history = serialize(array(Auth::id()));
|
|
$payment->created_by = Auth::id();
|
|
|
|
$patient_category_invoice = DonorDiscountDetail::where(['invoice_number' => $request->invoice_number])
|
|
->where(['invoice_date' => $request->invoice_date])
|
|
->update(['payment_status' => 1]);
|
|
|
|
if ($payment->save() && !is_null($patient_category_invoice)) {
|
|
flash('Payment Received');
|
|
return $this->donor_payment_receipt($request);
|
|
} else {
|
|
flash('Payment Not Received');
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function write_off_invoice_balance(Request $request) {
|
|
$logged_in_user = Auth::id();
|
|
$receipt_number = $this->receiptService->createReceipt('Patient Category Invoice Balance Write Off');
|
|
$invoice_payment = InvoicePayment::where('invoice_number', $request->invoice_number)->first();
|
|
$today = (is_null($request->write_off_date) || $request->write_off_date == '') ? Carbon::today()->toDateString() : Carbon::createFromFormat('d-m-Y', $request->write_off_date)->toDateString();
|
|
|
|
if ($invoice_payment) {
|
|
$amount_paid = $invoice_payment->amount_paid + $request->amount;
|
|
$balance = $invoice_payment->balance - $request->amount;
|
|
$this->invoicesService->updateInvoicePayment($request->invoice_number, $amount_paid, $balance);
|
|
} else {
|
|
$this->invoicesService->saveInvoicePayment($request->invoice_number, $request->total, $request->amount, ($request->total - $request->amount),
|
|
$request->invoice_date, $request->patient_category);
|
|
}
|
|
|
|
$this->invoicesService->saveInvoicePaymentRecord($request->invoice_number, '', 0, $today,
|
|
$request->amount, $today);
|
|
|
|
$payment = new Payment;
|
|
$payment->item_id = "";
|
|
$payment->vendor = "N/A";
|
|
$payment->unit_cost = $request->amount;
|
|
$payment->amount = $request->amount;
|
|
$payment->quantity = 1;
|
|
$payment->memo = $request->write_off_memo ?? "N/A";
|
|
$payment->account_balance = 0;
|
|
$payment->transaction_id = $receipt_number;
|
|
$payment->account_id = 0;
|
|
$payment->created_by = $logged_in_user;
|
|
$write_off_date = (is_null($request->write_off_date) || $request->write_off_date == '') ? now() : Carbon::createFromFormat('d-m-Y', $request->write_off_date)->toDateString();
|
|
$payment->expense_date = $write_off_date;
|
|
$payment->expense_account = $request->expense_account;
|
|
|
|
$this->invoicesService->clearInvoice($request->invoice_number);
|
|
|
|
if ($payment->save()) {
|
|
return 'success';
|
|
} else {
|
|
return 'fail';
|
|
}
|
|
}
|
|
|
|
public function process_patient_payment(Request $request) {
|
|
if ($request->status == "update") {
|
|
return $this->update_patient_payment($request);
|
|
} else {
|
|
$logged_in_user_id = Auth::id();
|
|
|
|
$new_receipt_number = $this->receiptService->createReceipt('Invoice Payment');
|
|
|
|
$banked = null;
|
|
$today = Carbon::today()->toDateTimeString();
|
|
$transfer_date = Carbon::parse($request->payment_received_on)->toDateString();
|
|
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_id = get_name("undeposited_funds", "slug", "id", "chart_of_accounts");
|
|
|
|
if (!is_numeric($undeposited_funds_id)) {
|
|
flash('Undeposited funds account is missing')->error();
|
|
return back()->withInput();
|
|
}
|
|
|
|
if (!is_null($request->bank) && !is_null($request->bank_balance)) {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($undeposited_funds_id, $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($undeposited_funds_id, Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_funds_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$request->amount_paid)]);
|
|
|
|
// next, deal with the transfer between banks
|
|
// ...
|
|
// transfer date balances
|
|
$to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->bank, $transfer_date);
|
|
$to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0;
|
|
$from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// present day balances
|
|
$to_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->bank, Carbon::today()->toDateString());
|
|
$to_account_balance_today = ($to_account_balance_today_record != null) ? $to_account_balance_today_record->account_balance : 0;
|
|
$from_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$from_account_balance_today = ($from_account_balance_today_record != null) ? $from_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// deal with from account
|
|
$from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - (int)$request->amount_paid);
|
|
$last_insert_id_from_account = capture_bank_record(
|
|
'TRANSFER',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
$request->bank,
|
|
$from_account_balance_after_transfer,
|
|
0,
|
|
(int)$request->amount_paid,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $request->transfer_from, $last_insert_id_from_account, $from_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($from_account_balance_today - (int)$request->amount_paid)]);
|
|
|
|
|
|
// deal with to account
|
|
$to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_to_account = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$request->bank,
|
|
$undeposited_funds_id,
|
|
$to_account_balance_after_transfer,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $request->bank, $last_insert_id_to_account, $to_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $request->bank)->update(['balance' => ($to_account_balance_today + (int)$request->amount_paid)]);
|
|
|
|
$banked = $last_insert_id_to_account;
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $request->amount_paid;
|
|
$cashier_income->reason = $request->memo;
|
|
$cashier_income->amount = $request->amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = (int)$request->undeposited_funds_bank_balance;
|
|
$cashier_income->account_balance = (int)$request->undeposited_funds_bank_balance + (int)$request->amount_paid;
|
|
$cashier_income->banked = $banked;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
$cashier_income_amount = $request->amount_paid;
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $request->invoice_number])->where(['invoice_date' => $request->invoice_date])
|
|
->update(['received' => $cashier_income_id]);
|
|
} else {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
// ...
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$request->amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$request->amount_paid,
|
|
0,
|
|
"Deposit to undeposited funds",
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_funds_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $request->amount_paid;
|
|
$category_name = get_name($request->patient_category, "id", "name", "patient_categories");
|
|
$cashier_income->reason = "Received from ". $category_name. " for invoice number (". $request->invoice_number.")";
|
|
$cashier_income->amount = $request->amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = $undeposited_funds_account_balance_on_transfer_date;
|
|
$cashier_income->account_balance = $undeposited_funds_account_balance_after_deposit;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
$cashier_income_amount = $request->amount_paid;
|
|
}
|
|
|
|
$other_parameters = [
|
|
'received_id' => $cashier_income_id,
|
|
'received_amount' => $cashier_income_amount,
|
|
'banked_id' => $banked,
|
|
];
|
|
|
|
$invoice_payment_record = $this->invoicesService->saveInvoicePaymentRecord(
|
|
$request->invoice_number, $new_receipt_number, $request->amount_paid, $today,
|
|
0, null, $other_parameters
|
|
);
|
|
$this->invoicesService->saveInvoicePayment(
|
|
$request->invoice_number, $request->total, $request->amount_paid, $request->balance,
|
|
$request->invoice_date, $request->patient_category
|
|
);
|
|
|
|
$request->receipt_number = $new_receipt_number;
|
|
|
|
$patient_category_invoice = PatientCategoryInvoice::where(['invoice_number' => $request->invoice_number])
|
|
->where(['invoice_date' => $request->invoice_date])
|
|
->update(['status' => 1]);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$request->amount_paid)]);
|
|
|
|
$return_payment_methods = PatientFinanceController::register_payment_method(0, 0, $request->amount_paid, $request->payment_method,
|
|
$request->payment_methods_amount, array_fill(0, count($request->payment_methods_amount ?? []), null), array_fill(0, count($request->payment_methods_amount ?? []), 0), $new_receipt_number, 17, Auth::id(), 0);
|
|
|
|
if (!is_null($patient_category_invoice)) {
|
|
$this->invoicesService->clearInvoice($request->invoice_number);
|
|
|
|
//======= enter this function incase it is a patient dependee clearance =============//
|
|
clear_dependants_consumptions($request->invoice_number);//if dependent invoice clear it
|
|
//====================================//
|
|
|
|
flash('Payment Received')->success();
|
|
return $this->patient_payment_receipt($request);
|
|
} else {
|
|
flash('Payment Not Received')->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function patient_payments_history(Request $request): View
|
|
{
|
|
$payments = DB::table('invoice_payment_records')->where('invoice_number', $request->invoice_number)->get();
|
|
$patient_category = DB::table('invoice_payments')->where('invoice_number', $request->invoice_number)->first()->patient_category;
|
|
$patient_category = get_name($patient_category, 'id', 'name', 'patient_categories');
|
|
|
|
return view('invoices::invoices.payments_history.patient_payments_history', compact('payments', 'patient_category'));
|
|
}
|
|
|
|
public function donor_payments_history(Request $request)
|
|
{
|
|
|
|
$payments = DB::table('donor_invoice_payments')->where('id', $request->id)->get()->toArray();
|
|
return view('invoices::invoices.payments_history.donor_payments_history', compact('payments'));
|
|
}
|
|
|
|
public function donor_history()
|
|
{
|
|
|
|
$payments = DB::table('donor_invoice_payments')->groupBy('invoice_number')->get()->toArray();
|
|
return view('invoices::invoices.payments_history.donor_index', compact('payments'));
|
|
}
|
|
|
|
public function donor_payment_receipt(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
return view('invoices::invoices.receipts.donor_payment_receipt', compact('request', 'hospital_information'));
|
|
}
|
|
|
|
public function generate_hospital_invoices(Request $request)
|
|
{
|
|
$quotation_type_options = QuotationType::pluck('name', 'id')->toArray();
|
|
$quotation_type_options = ['' => '- select Quotation type -'] + $quotation_type_options;
|
|
$quotations = [];
|
|
$display = "";
|
|
|
|
if (isset($request->start_date) && isset($request->end_date)) {
|
|
$start_date = Carbon::createFromFormat('d/m/Y', $request->start_date)->toDateString();
|
|
$end_date = Carbon::createFromFormat('d/m/Y', $request->end_date)->toDateString();
|
|
|
|
$quotations = Quotation::where('receive_status', 1)
|
|
->where('invoice_generated', 0)
|
|
->whereBetween('receive_date', [$start_date, $end_date])
|
|
->select('quotation_number', 'receive_date', 'quotation_type_id')
|
|
->orderBy('quotation_number', 'desc')
|
|
->distinct()
|
|
->get();
|
|
|
|
$display = "Received between " . streamline_date($start_date) . " and " . streamline_date($end_date);
|
|
}
|
|
|
|
return view('invoices::invoices.hospital.generate_hospital_invoices', compact('quotations', 'quotation_type_options', 'display'));
|
|
}
|
|
|
|
public function generate_hospital_invoices_confirm(Request $request)
|
|
{
|
|
$quotation_type_options = QuotationType::pluck('name', 'id')->toArray();
|
|
$quotation_number = $request->quotation_number;
|
|
$quotations = Quotation::where('quotation_number', $quotation_number)->get();
|
|
|
|
return view('invoices::invoices.hospital.generate_hospital_invoices_confirm', compact('quotations', 'quotation_type_options', 'quotation_number'));
|
|
}
|
|
|
|
public function save_hospital_invoices($quotation_number)
|
|
{
|
|
// track the invoice
|
|
$track_invoice = new TrackInvoice();
|
|
$track_invoice->reason = 'Hospital Invoice';
|
|
$track_invoice->created_by = Auth::id();
|
|
$track_invoice->save();
|
|
$invoice_number = sprintf("%04u", $track_invoice->id);
|
|
$invoice_date = date('Y-m-d');
|
|
|
|
$update = Quotation::where('quotation_number', $quotation_number)
|
|
->update(
|
|
[
|
|
'invoice_generated' => 1,
|
|
'invoice_date' => $invoice_date,
|
|
'invoice_number' => $invoice_number
|
|
]
|
|
);
|
|
|
|
return redirect('/invoices/print_hospital_invoices/' . $invoice_number);
|
|
}
|
|
|
|
public function print_hospital_invoices($invoice_number)
|
|
{
|
|
$quotations = Quotation::where('invoice_number', $invoice_number)->get();
|
|
$served_by_name = get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users');
|
|
|
|
return view('invoices::invoices.hospital.print_hospital_invoices', compact('quotations', 'invoice_number', 'served_by_name'));
|
|
}
|
|
|
|
public function make_hospital_payments()
|
|
{
|
|
$quotations = Quotation::where('invoice_generated', 1)
|
|
->where('invoice_cleared', 0)
|
|
->select('invoice_number', 'invoice_date', 'quotation_type_id', 'supplier_id')
|
|
->orderBy('invoice_number', 'desc')
|
|
->distinct()
|
|
->get();
|
|
|
|
$quotation_type_options = QuotationType::pluck('name', 'id')->toArray();
|
|
|
|
$quotations_with_balance = HospitalInvoicePayment::where('balance', '>', 0)->get();
|
|
|
|
return view('invoices::invoices.hospital.make_hospital_payments', compact('quotations', 'quotation_type_options', 'quotations_with_balance'));
|
|
}
|
|
|
|
public function delete_hospital_invoices($invoice_number)
|
|
{
|
|
Quotation::where('invoice_number', $invoice_number)
|
|
->update(
|
|
[
|
|
'invoice_generated' => 0,
|
|
'invoice_date' => null,
|
|
'invoice_number' => null
|
|
]
|
|
);
|
|
|
|
flash("Invoice has been deleted successfully")->success();
|
|
return redirect('/invoices/make_hospital_payments');
|
|
}
|
|
|
|
public function complete_hospital_payments($invoice_number)
|
|
{
|
|
$is_update = false;
|
|
$quotations = Quotation::where('invoice_number', $invoice_number)->get();
|
|
$total_amount_to_pay = 0;
|
|
$supplier_id = $quotations[0]->supplier_id;
|
|
$quotation_type = $quotations[0]->quotation_type_id;
|
|
$invoice_date = $quotations[0]->invoice_date;
|
|
|
|
foreach ($quotations as $quotation) {
|
|
$total_amount_to_pay += $quotation->total_price;
|
|
}
|
|
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->toArray();
|
|
$banks = ['' => '- select -'] + $banks;
|
|
|
|
return view('invoices::invoices.hospital.complete_hospital_payments', compact('invoice_number', 'is_update', 'total_amount_to_pay', 'supplier_id', 'quotation_type', 'invoice_date', 'banks'));
|
|
}
|
|
|
|
public function save_hospital_payments(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$payment_reason = "Payment to " . $request->supplier . " for " . $request->payment_for;
|
|
$total_to_pay = $request->amount;
|
|
$amount_paid = $request->amount_paid;
|
|
$balance = $request->balance;
|
|
|
|
$receipt_number = $this->receiptService->createReceipt('Hospital Invoice Payment');
|
|
|
|
// update the account balance
|
|
$chart_of_account = ChartOfAccount::find($request->bank_id);
|
|
$current_balance = $chart_of_account->balance;
|
|
$new_balance = $current_balance - $amount_paid;
|
|
$chart_of_account->balance = $new_balance;
|
|
$chart_of_account->save();
|
|
|
|
if ($request->is_update) {
|
|
// update the record
|
|
$payment = HospitalInvoicePayment::find($request->payment_id);
|
|
|
|
$amount_already_paid = $payment->amount_paid;
|
|
$receipt_number_array = unserialize($payment->receipt_number);
|
|
$amount_paid_array = unserialize($payment->amount_paid_history);
|
|
$date_paid_array = unserialize($payment->date_paid_history);
|
|
$staff_incharge_array = unserialize($payment->staff_incharge_history);
|
|
|
|
$receipt_number_array[] = $receipt_number;
|
|
$amount_paid_array[] = $request->amount_paid;
|
|
$date_paid_array[] = Carbon::today()->toDateTimeString();
|
|
$staff_incharge_array[] = Auth::id();
|
|
|
|
$payment->amount_paid = $amount_already_paid + $request->amount_paid;
|
|
$payment->balance = $request->balance;
|
|
$payment->receipt_number = serialize($receipt_number_array);
|
|
$payment->amount_paid_history = serialize($amount_paid_array);
|
|
$payment->date_paid_history = serialize($date_paid_array);
|
|
$payment->staff_incharge_history = serialize($staff_incharge_array);
|
|
$payment->updated_by = Auth::id();
|
|
|
|
if ($payment->save()) {
|
|
flash("New payment made successfully")->success();
|
|
return view('invoices::invoices.hospital.hospital_payments_receipt', compact('hospital_information', 'receipt_number', 'payment_reason', 'total_to_pay', 'amount_paid', 'balance'));
|
|
} else {
|
|
flash("Unable to process invoice payment")->error();
|
|
return back();
|
|
}
|
|
} else {
|
|
// create a new record
|
|
Quotation::where('invoice_number', $request->invoice_number)
|
|
->update(['invoice_cleared' => 1]);
|
|
|
|
$payment = new HospitalInvoicePayment();
|
|
|
|
$payment->invoice_number = $request->invoice_number;
|
|
$payment->total = $request->amount;
|
|
$payment->amount_paid = $request->amount_paid;
|
|
$payment->balance = $request->balance;
|
|
$payment->invoice_date = $request->invoice_date;
|
|
|
|
// history
|
|
$payment->receipt_number = serialize([$receipt_number]);
|
|
$payment->amount_paid_history = serialize([$request->amount_paid]);
|
|
$payment->date_paid_history = serialize([Carbon::today()->toDateTimeString()]);
|
|
$payment->staff_incharge_history = serialize([Auth::id()]);
|
|
|
|
$payment->created_by = Auth::id();
|
|
|
|
if ($payment->save()) {
|
|
flash("New payment made successfully")->success();
|
|
return view('invoices::invoices.hospital.hospital_payments_receipt', compact('hospital_information', 'receipt_number', 'payment_reason', 'total_to_pay', 'amount_paid', 'balance'));
|
|
} else {
|
|
flash("Unable to process invoice payment")->error();
|
|
return back();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function hospital_invoices_payment_history($id)
|
|
{
|
|
$payment = HospitalInvoicePayment::find($id);
|
|
|
|
$receipt_number_array = unserialize($payment->receipt_number);
|
|
$amount_paid_array = unserialize($payment->amount_paid_history);
|
|
$date_paid_array = unserialize($payment->date_paid_history);
|
|
$staff_incharge_array = unserialize($payment->staff_incharge_history);
|
|
|
|
return view('invoices::invoices.hospital.hospital_invoices_payment_history', compact('receipt_number_array', 'amount_paid_array', 'date_paid_array', 'staff_incharge_array'));
|
|
}
|
|
|
|
public function view_hospital_payments()
|
|
{
|
|
$payments = HospitalInvoicePayment::get();
|
|
|
|
return view('invoices::invoices.hospital.view_hospital_payments', compact('payments'));
|
|
}
|
|
|
|
public function update_hospital_invoice_payment($id)
|
|
{
|
|
$payment = HospitalInvoicePayment::find($id);
|
|
|
|
$is_update = true;
|
|
$payment_id = $id;
|
|
$invoice_number = $payment->invoice_number;
|
|
$quotations = Quotation::where('invoice_number', $invoice_number)->get();
|
|
$total_amount_to_pay = $payment->balance;
|
|
$supplier_id = $quotations[0]->supplier_id;
|
|
$quotation_type = $quotations[0]->quotation_type_id;
|
|
$invoice_date = $quotations[0]->invoice_date;
|
|
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->toArray();
|
|
$banks = ['' => '- select -'] + $banks;
|
|
|
|
return view('invoices::invoices.hospital.complete_hospital_payments', compact('invoice_number', 'is_update', 'total_amount_to_pay', 'supplier_id', 'quotation_type', 'invoice_date', 'payment_id', 'banks'));
|
|
}
|
|
|
|
public function process_invoices_bulk_payment(Request $request)
|
|
{
|
|
/*
|
|
1. Loop through the invoices $request->bulk_invoices and $request->bulk_invoice_amount_to_pay arrays
|
|
2. Check whether it is a new or an update payment
|
|
3. update balance
|
|
*/
|
|
$bulk_invoices_array = $request->bulk_invoices;
|
|
$bulk_invoices_amounts_array = $request->bulk_invoice_amount_to_pay;
|
|
$bulk_patient_categories_array = $request->bulk_patient_category;
|
|
|
|
for ($i=0; $i < count($bulk_invoices_array); $i++) {
|
|
$does_invoice_have_payment = $this->does_invoice_have_a_payment($bulk_invoices_array[$i]);
|
|
if ($does_invoice_have_payment) {
|
|
$this->bulk_update_invoice_payments($request, $bulk_invoices_array[$i], $bulk_invoices_amounts_array[$i], $bulk_patient_categories_array[$i]);
|
|
} else{
|
|
# new invoice payment
|
|
$patient_category_invoice_numbers = DB::table('patient_category_invoices')
|
|
->where('invoice_number', $bulk_invoices_array[$i])
|
|
->whereNull('deleted_at')
|
|
->orderBy('transaction_date', 'asc')->get();
|
|
|
|
$total_invoice_payment = 0;
|
|
|
|
foreach ($patient_category_invoice_numbers as $patient_category_invoice_number) {
|
|
$total_invoice_payment += (int)$patient_category_invoice_number->patient_amount;
|
|
}
|
|
|
|
$logged_in_user_id = Auth::id();
|
|
|
|
$new_receipt_number = $this->receiptService->createReceipt('Invoice Payment');
|
|
|
|
$banked = null;
|
|
$today = Carbon::today()->toDateTimeString();
|
|
$transfer_date = Carbon::parse($request->payment_date)->toDateString();
|
|
|
|
$patient_cate_invoice = PatientCategoryInvoice::where('invoice_number', $bulk_invoices_array[$i])->first();
|
|
$invoice_date = null;
|
|
if ($patient_cate_invoice) {
|
|
$invoice_date = $patient_cate_invoice->invoice_date;
|
|
} else{
|
|
flash('invoice has not yet been generated')->error();
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_id = get_name("undeposited_funds", "slug", "id", "chart_of_accounts");
|
|
|
|
if (!is_numeric($undeposited_funds_id)) {
|
|
flash('Undeposited funds account is missing')->error();
|
|
return back()->withInput();
|
|
}
|
|
|
|
if (!is_null($request->bank) && !is_null($request->bank_balance)) {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($undeposited_funds_id, $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($undeposited_funds_id, Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$bulk_invoices_amounts_array[$i]);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$bulk_invoices_amounts_array[$i],
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_funds_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$bulk_invoices_amounts_array[$i])]);
|
|
|
|
// next, deal with the transfer between banks
|
|
// ...
|
|
// transfer date balances
|
|
$to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->bank, $transfer_date);
|
|
$to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0;
|
|
$from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// present day balances
|
|
$to_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->bank, Carbon::today()->toDateString());
|
|
$to_account_balance_today = ($to_account_balance_today_record != null) ? $to_account_balance_today_record->account_balance : 0;
|
|
$from_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$from_account_balance_today = ($from_account_balance_today_record != null) ? $from_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// deal with from account
|
|
$from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - (int)$bulk_invoices_amounts_array[$i]);
|
|
$last_insert_id_from_account = capture_bank_record(
|
|
'TRANSFER',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
$request->bank,
|
|
$from_account_balance_after_transfer,
|
|
0,
|
|
(int)$bulk_invoices_amounts_array[$i],
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
$transfer_from_bank = get_name("undeposited_funds", "slug", "id", "chart_of_accounts");//undeposited_funds
|
|
update_banking_record_balances($transfer_date, $transfer_from_bank, $last_insert_id_from_account, $from_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($from_account_balance_today - (int)$bulk_invoices_amounts_array[$i])]);
|
|
|
|
|
|
// deal with to account
|
|
$to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + (int)$bulk_invoices_amounts_array[$i]);
|
|
$last_insert_id_to_account = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$request->bank,
|
|
$undeposited_funds_id,
|
|
$to_account_balance_after_transfer,
|
|
(int)$bulk_invoices_amounts_array[$i],
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $request->bank, $last_insert_id_to_account, $to_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $request->bank)->update(['balance' => ($to_account_balance_today + (int)$bulk_invoices_amounts_array[$i])]);
|
|
|
|
|
|
$banked = $last_insert_id_to_account;
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $bulk_invoices_amounts_array[$i];
|
|
$cashier_income->reason = $request->memo;
|
|
$cashier_income->amount = $bulk_invoices_amounts_array[$i];
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = (int)$request->undeposited_funds_bank_balance;
|
|
$cashier_income->account_balance = (int)$request->undeposited_funds_bank_balance + (int)$bulk_invoices_amounts_array[$i];
|
|
$cashier_income->banked = $banked;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $bulk_invoices_array[$i]])->where(['invoice_date' => $invoice_date])->update(['received' => $cashier_income_id]);
|
|
} else {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
// ...
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$bulk_invoices_amounts_array[$i]);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_funds_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$bulk_invoices_amounts_array[$i],
|
|
0,
|
|
"Deposit to undeposited funds",
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_funds_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $bulk_invoices_amounts_array[$i];
|
|
$category_name = get_name($bulk_patient_categories_array[$i], "id", "name", "patient_categories");
|
|
$cashier_income->reason = "Received from ". $category_name. " for invoice number (". $bulk_invoices_array[$i].")";
|
|
$cashier_income->amount = $bulk_invoices_amounts_array[$i];
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = $undeposited_funds_account_balance_on_transfer_date;
|
|
$cashier_income->account_balance = $undeposited_funds_account_balance_after_deposit;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
}
|
|
|
|
$other_parameters = [
|
|
'received_id' => $cashier_income_id,
|
|
'received_amount' => $bulk_invoices_amounts_array[$i],
|
|
'banked_id' => $banked,
|
|
];
|
|
|
|
$invoice_payment_record = $this->invoicesService->saveInvoicePaymentRecord(
|
|
$bulk_invoices_array[$i], $new_receipt_number, $bulk_invoices_amounts_array[$i], $invoice_date,
|
|
0, null, $other_parameters
|
|
);
|
|
$this->invoicesService->saveInvoicePayment(
|
|
$bulk_invoices_array[$i], $total_invoice_payment, $bulk_invoices_amounts_array[$i], 0,
|
|
$invoice_date, $bulk_patient_categories_array[$i]
|
|
);
|
|
|
|
$request->receipt_number = $new_receipt_number;
|
|
|
|
$patient_category_invoice = PatientCategoryInvoice::where(['invoice_number' => $bulk_invoices_array[$i]])
|
|
->where(['invoice_date' => $invoice_date])
|
|
->update(['status' => 1]);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_funds_id)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$bulk_invoices_amounts_array[$i])]);
|
|
|
|
$return_payment_methods = PatientFinanceController::register_payment_method(0, 0, $bulk_invoices_amounts_array[$i], $request->payment_method,
|
|
$request->payment_methods_amount, array_fill(0, count($request->payment_methods_amount ?? []), null), array_fill(0, count($request->payment_methods_amount ?? []), 0), $new_receipt_number, 17, Auth::id(), 0);
|
|
|
|
if (!is_null($patient_category_invoice)) {
|
|
$this->invoicesService->clearInvoice($bulk_invoices_array[$i]);
|
|
|
|
//======= enter this function incase it is a patient dependee clearance =============//
|
|
clear_dependants_consumptions($bulk_invoices_array[$i]);//if dependent invoice clear it
|
|
//====================================//
|
|
|
|
flash('Payment Received for Patient Category Invoice '.$bulk_invoices_array[$i])->success();
|
|
} else {
|
|
flash('Payment Not Received')->error();
|
|
return back();
|
|
}
|
|
}
|
|
}
|
|
|
|
return redirect('invoices/receive_patient_invoices');
|
|
}
|
|
|
|
public function does_invoice_have_a_payment($invoice_number)
|
|
{
|
|
$invoice_payment_record = DB::table('invoice_payments')->where('invoice_number', $invoice_number)->first();
|
|
|
|
if ($invoice_payment_record) {
|
|
if ($invoice_payment_record->balance > 0) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function bulk_update_invoice_payments(Request $request, $invoice_number, $invoice_amount_paid, $invoice_patient_category)
|
|
{
|
|
$new_receipt_number = $this->receiptService->createReceipt('Invoice Payment');
|
|
|
|
$patient_cate_invoice = PatientCategoryInvoice::where('invoice_number', $invoice_number)->orderBy('id', 'desc')->first();
|
|
$invoice_date = $patient_cate_invoice->invoice_date;
|
|
|
|
$undeposited_acc_id = get_name("undeposited_funds", "slug", "id", "chart_of_accounts");
|
|
|
|
$logged_in_user_id = Auth::id();
|
|
$banked = null;
|
|
$transfer_date = Carbon::parse($request->payment_date)->toDateString();
|
|
|
|
$payment_received_on = Carbon::parse($request->payment_date)->toDateString();
|
|
|
|
if (!is_null($request->bank) && !is_null($request->bank_balance)) {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
// ...
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$invoice_amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_acc_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$invoice_amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_acc_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_acc_id)->update(['balance' => ($undeposited_funds_account_balance_today + (int)$invoice_amount_paid)]);
|
|
|
|
// next, deal with the transfer between banks
|
|
// ...
|
|
// transfer date balances
|
|
$to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->bank, $transfer_date);
|
|
$to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0;
|
|
$from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// present day balances
|
|
$to_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->bank, Carbon::today()->toDateString());
|
|
$to_account_balance_today = ($to_account_balance_today_record != null) ? $to_account_balance_today_record->account_balance : 0;
|
|
$from_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$from_account_balance_today = ($from_account_balance_today_record != null) ? $from_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// deal with from account
|
|
$from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - (int)$invoice_amount_paid);
|
|
$last_insert_id_from_account = capture_bank_record(
|
|
'TRANSFER',
|
|
$transfer_date,
|
|
$undeposited_acc_id,
|
|
$request->bank,
|
|
$from_account_balance_after_transfer,
|
|
0,
|
|
(int)$invoice_amount_paid,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_acc_id, $last_insert_id_from_account, $from_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $undeposited_acc_id)->update(['balance' => ($from_account_balance_today - (int)$invoice_amount_paid)]);
|
|
|
|
|
|
// deal with to account
|
|
$to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + (int)$invoice_amount_paid);
|
|
$last_insert_id_to_account = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$request->bank,
|
|
$undeposited_acc_id,
|
|
$to_account_balance_after_transfer,
|
|
(int)$invoice_amount_paid,
|
|
0,
|
|
$request->memo,
|
|
$new_receipt_number
|
|
);
|
|
$bank_record = Banking::where('id', $last_insert_id_to_account)->first();
|
|
$bank_record->created_at = $logged_in_user_id;
|
|
$bank_record->update();
|
|
update_banking_record_balances($transfer_date, $request->bank, $last_insert_id_to_account, $to_account_balance_after_transfer);
|
|
|
|
// update current balance
|
|
ChartOfAccount::where('id', $request->bank)->update(['balance' => ($to_account_balance_today + (int)$invoice_amount_paid)]);
|
|
|
|
|
|
$banked = $last_insert_id_to_account;
|
|
|
|
update_banking_record_balances($payment_received_on, $request->bank, $bank_record->id, ((int)$request->bank_balance + (int)$invoice_amount_paid));
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $invoice_amount_paid;
|
|
$cashier_income->reason = $request->memo;
|
|
$cashier_income->amount = $invoice_amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = (int)$request->undeposited_funds_bank_balance;
|
|
$cashier_income->account_balance = (int)$request->undeposited_funds_bank_balance + (int)$invoice_amount_paid;
|
|
$cashier_income->banked = $banked;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $invoice_number])->where(['invoice_date' => $invoice_date])
|
|
->update(['received' => $cashier_income_id]);
|
|
} else {
|
|
// first, the money from the invoice has to be received as undeposited funds
|
|
// ...
|
|
// undeposited funds before invoice money was deposited (as of deposit date)
|
|
$undeposited_funds_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $transfer_date);
|
|
$undeposited_funds_account_balance_on_transfer_date = ($undeposited_funds_account_balance_on_transfer_date_record != null) ? (int)$undeposited_funds_account_balance_on_transfer_date_record->account_balance : 0;
|
|
|
|
// undeposited funds before invoice money was deposited (as of today)
|
|
$undeposited_funds_account_balance_today_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString());
|
|
$undeposited_funds_account_balance_today = ($undeposited_funds_account_balance_today_record != null) ? $undeposited_funds_account_balance_today_record->account_balance : 0;
|
|
|
|
|
|
// make the deposit transaction
|
|
$undeposited_funds_account_balance_after_deposit = ($undeposited_funds_account_balance_on_transfer_date + (int)$invoice_amount_paid);
|
|
$last_insert_id_undeposited_funds = capture_bank_record(
|
|
'DEPOSIT',
|
|
$transfer_date,
|
|
$undeposited_acc_id,
|
|
'',
|
|
$undeposited_funds_account_balance_after_deposit,
|
|
(int)$invoice_amount_paid,
|
|
0,
|
|
"Deposit to undeposited funds",
|
|
$new_receipt_number
|
|
);
|
|
update_banking_record_balances($transfer_date, $undeposited_acc_id, $last_insert_id_undeposited_funds, $undeposited_funds_account_balance_after_deposit);
|
|
|
|
$cashier_income = new CashierIncome;
|
|
$cashier_income->cashier_id = $logged_in_user_id;
|
|
$cashier_income->brought_by = $logged_in_user_id;
|
|
$cashier_income->expected_amount = $invoice_amount_paid;
|
|
$cashier_income->reason = "Received from patient category invoices";
|
|
$cashier_income->amount = $invoice_amount_paid;
|
|
$cashier_income->balance = 0;
|
|
$cashier_income->receipt_number = $new_receipt_number;
|
|
$cashier_income->previous_balance = $undeposited_funds_account_balance_on_transfer_date;
|
|
$cashier_income->account_balance = $undeposited_funds_account_balance_after_deposit;
|
|
$cashier_income->save();
|
|
|
|
$cashier_income_id = $cashier_income->id;
|
|
}
|
|
|
|
PatientCategoryInvoice::where(['invoice_number' => $invoice_number])
|
|
->where(['invoice_date' => $invoice_date])
|
|
->update(['status' => 1, 'received' => is_null($cashier_income_id) ? null : $cashier_income_id]);
|
|
|
|
$other_parameters = [
|
|
'received_id' => $cashier_income_id,
|
|
'received_amount' => $invoice_amount_paid,
|
|
'banked_id' => $banked,
|
|
];
|
|
|
|
$invoice_payment_record = $this->invoicesService->saveInvoicePaymentRecord(
|
|
$invoice_number, $new_receipt_number, $invoice_amount_paid, $request->payment_date,
|
|
0, null, $other_parameters
|
|
);
|
|
$this->invoicesService->updateInvoicePayment($invoice_number, $invoice_amount_paid, 0);
|
|
|
|
$request->receipt_number = $new_receipt_number;
|
|
|
|
update_account_balance_by_id($undeposited_acc_id, $invoice_amount_paid);
|
|
|
|
$this->invoicesService->clearInvoice($invoice_number);
|
|
clear_dependants_consumptions($invoice_number);//if dependent invoice clear it
|
|
|
|
$return_payment_methods = PatientFinanceController::register_payment_method(0, 0, $invoice_amount_paid, $request->payment_method,
|
|
$request->payment_methods_amount, array_fill(0, count($request->payment_methods_amount ?? []), null), array_fill(0, count($request->payment_methods_amount ?? []), 0), $new_receipt_number, 17, Auth::id(), 0);
|
|
|
|
if (!is_null($invoice_payment_record)) {
|
|
flash('Payment Updated for Patient Category Invoice '.$invoice_number);
|
|
} else {
|
|
flash('Payment Not Updated');
|
|
}
|
|
}
|
|
|
|
public function custom_invoices($patient_category, $category, $is_generated)
|
|
{
|
|
$invoices_counter = 0;
|
|
$invoices = [];
|
|
$request = new Request();
|
|
$searched_patient_category = '';
|
|
|
|
$today = Carbon::today()->toDateTimeString();
|
|
$today_minus_thirty = Carbon::today()->subDays(30)->toDateTimeString();
|
|
$today_minus_sixty = Carbon::today()->subDays(60)->toDateTimeString();
|
|
$today_minus_ninety = Carbon::today()->subDays(90)->toDateTimeString();
|
|
|
|
$display = get_name($patient_category, 'id', 'name', 'patient_categories') . ' - UNPAID INVOICES AND PAID INVOICES (BUT WITH BALANCE) ';
|
|
|
|
$request->staff_member = "ALL STAFF";
|
|
$request->patient_category = $patient_category;
|
|
$request->bill_status = "UNPAID INVOICES";
|
|
$request->dates = 'custom_date_range';
|
|
$request->start_date = $today;
|
|
$request->end_date = $today;
|
|
$patient_categories = DB::table('patient_discounts')->where(['pay_later' => 1])->pluck('patient_category')->toArray();
|
|
|
|
switch ($category) {
|
|
case 'current':
|
|
$display .= " for today, the " . streamline_date($today);
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category = {$patient_category} AND created_at >= '{$today}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
if ($patient_category == 0) {
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category IS NULL AND created_at >= '{$today}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
$invoices_temp = DB::select($invoices_query);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereDate('created_at', $today)
|
|
->where('patient_category', $request->patient_category)
|
|
->whereNull('deleted_at')
|
|
->where('invoice_generated', $is_generated)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
break;
|
|
|
|
case 'one_to_thirty':
|
|
$request->start_date = $today_minus_thirty;
|
|
$display .= " from " . streamline_date($today_minus_thirty) . " to " . streamline_date($today);
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category = {$patient_category} AND created_at BETWEEN '{$today_minus_thirty}' AND '{$today}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
if ($patient_category == 0) {
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category IS NULL AND created_at BETWEEN '{$today_minus_thirty}' AND '{$today}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
$invoices_temp = DB::select($invoices_query);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('created_at', [$today_minus_thirty, $today])
|
|
->where('patient_category', $request->patient_category)
|
|
->whereNull('deleted_at')
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
break;
|
|
|
|
case 'thirty_one_to_sixty':
|
|
$request->start_date = $today_minus_sixty;
|
|
$request->end_date = $today_minus_thirty;
|
|
$display .= " from " . streamline_date($today_minus_sixty) . " to " . streamline_date($today_minus_thirty);
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category = {$patient_category} AND created_at BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
if ($patient_category == 0) {
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category IS NULL AND created_at BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
$invoices_temp = DB::select($invoices_query);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('created_at', [$today_minus_sixty, $today_minus_thirty])
|
|
->where('patient_category', $request->patient_category)
|
|
->whereNull('deleted_at')
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
break;
|
|
|
|
case 'sixty_one_to_ninety':
|
|
$request->start_date = $today_minus_ninety;
|
|
$request->end_date = $today_minus_sixty;
|
|
$display .= " from " . streamline_date($today_minus_ninety) . " to " . streamline_date($today_minus_sixty);
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category = {$patient_category} AND created_at BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
if ($patient_category == 0) {
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category IS NULL AND created_at BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
$invoices_temp = DB::select($invoices_query);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereBetween('created_at', [$today_minus_ninety, $today_minus_sixty])
|
|
->where('patient_category', $request->patient_category)
|
|
->whereNull('deleted_at')
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
break;
|
|
|
|
case 'ninety_one_and_over':
|
|
$request->start_date = Carbon::today()->startOfYear()->toDateTimeString();
|
|
$request->end_date = $today_minus_ninety;
|
|
$display .= " before " . streamline_date($today_minus_ninety);
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category = {$patient_category} AND created_at < '{$today_minus_ninety}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
if ($patient_category == 0) {
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE invoice_generated = {$is_generated} AND deleted_at IS NULL AND (balance_remaining IS NULL OR balance_remaining > 0) AND patient_category IS NULL AND created_at < '{$today_minus_ninety}' GROUP BY invoice_number ORDER BY id ASC";
|
|
$searched_patient_category = "All Patient Categories";
|
|
}
|
|
$invoices_temp = DB::select($invoices_query);
|
|
|
|
$invoices_not_generated = DB::table('patient_category_invoices')
|
|
->whereNotIn('patient_id', findTestOrDemoPatients())
|
|
->whereDate('created_at', '<', $today_minus_ninety)
|
|
->where('patient_category', $request->patient_category)
|
|
->whereNull('deleted_at')
|
|
->where('invoice_generated', 0)
|
|
// group invoices by episode
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
break;
|
|
}
|
|
|
|
if ($is_generated == 1) {
|
|
|
|
$start_date = $request->start_date;
|
|
$end_date = $request->end_date;
|
|
$generated_invoice_numbers_array = [];
|
|
|
|
foreach ($invoices_temp as $record) {
|
|
$patient_category_invoice_numbers = DB::table('patient_category_invoices')
|
|
->where('invoice_number', $record->invoice_number)
|
|
->orderBy('created_at', 'asc')->get();
|
|
|
|
$total_invoice_payment = 0;
|
|
|
|
foreach ($patient_category_invoice_numbers as $patient_category_invoice_number) {
|
|
$total_invoice_payment += (int)$patient_category_invoice_number->patient_amount;
|
|
}
|
|
|
|
// check if invoice has a payment record
|
|
$invoice_payment_record = DB::table('invoice_payments')
|
|
->where('invoice_number', $record->invoice_number)->first();
|
|
|
|
if ($invoice_payment_record) {
|
|
if ($invoice_payment_record->balance > 0) {
|
|
// for those invoices with payments, only bring those with a balance
|
|
$invoices[$invoices_counter]['has_payment'] = 1;
|
|
$invoices[$invoices_counter]['amount_paid'] = $invoice_payment_record->amount_paid;
|
|
$invoices[$invoices_counter]['payment_id'] = $invoice_payment_record->id;
|
|
$invoices[$invoices_counter]['balance'] = $invoice_payment_record->balance;
|
|
} else {
|
|
// do not continue to add an invoice record
|
|
continue;
|
|
}
|
|
} else {
|
|
$invoices[$invoices_counter]['has_payment'] = 0;
|
|
$invoices[$invoices_counter]['amount_paid'] = 0;
|
|
$invoices[$invoices_counter]['payment_id'] = 0;
|
|
$invoices[$invoices_counter]['balance'] = 0;
|
|
}
|
|
|
|
$invoices[$invoices_counter]['patient_amount'] = $total_invoice_payment;
|
|
$invoices[$invoices_counter]['invoice_number'] = $record->invoice_number;
|
|
$invoices[$invoices_counter]['invoice_date'] = $record->invoice_date;
|
|
$invoices[$invoices_counter]['patient_category'] = $record->patient_category;
|
|
$invoices[$invoices_counter]['created_at'] = $record->created_at;
|
|
$invoices[$invoices_counter]['updated_at'] = $record->updated_at;
|
|
$invoices[$invoices_counter]['created_by'] = $record->created_by;
|
|
$generated_invoice_numbers_array[] = $record->invoice_number;
|
|
|
|
$invoices_counter++;
|
|
}
|
|
|
|
/* Arranging the data*/
|
|
asort($invoices);
|
|
|
|
if (isset($request->patient_category) && $request->patient_category != 0) {
|
|
$payments = DB::table('invoice_payments')->where('patient_category', $request->patient_category)->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get()->toArray();
|
|
} else {
|
|
$payments = DB::table('invoice_payments')->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get()->toArray();
|
|
}
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$patient_payment_methods = PatientPaymentMethod::pluck('name', 'id');
|
|
$patient_payment_methods_options = "";
|
|
foreach ($patient_payment_methods as $key => $value) {
|
|
$patient_payment_methods_options .= '<option value="' . $key . '">' . $value . '</option>';
|
|
}
|
|
|
|
return view('invoices::invoices.receive_payments.receive_patient_payments', compact('invoices', 'start_date', 'end_date', 'patient_categories', 'searched_patient_category', 'payments', 'request', 'display', 'banks', 'patient_payment_methods_options'));
|
|
} else {
|
|
$total = 0;
|
|
|
|
if (count($invoices_not_generated) > 0) {
|
|
foreach ($invoices_not_generated as $item) {
|
|
$total += $item->sum;
|
|
}
|
|
}
|
|
|
|
return view('invoices::invoices.patient_category', compact(
|
|
'patient_categories',
|
|
'invoices_not_generated',
|
|
'request',
|
|
'display',
|
|
'total'
|
|
));
|
|
}
|
|
}
|
|
|
|
public function custom_invoice($patient_category, $category, $id, $is_generated) {
|
|
$invoices = [];
|
|
$request = new Request();
|
|
$hospital_information = HospitalInformation::first();
|
|
|
|
$today = Carbon::today()->toDateTimeString();
|
|
$today_minus_thirty = Carbon::today()->subDays(30)->toDateTimeString();
|
|
$today_minus_sixty = Carbon::today()->subDays(60)->toDateTimeString();
|
|
$today_minus_ninety = Carbon::today()->subDays(90)->toDateTimeString();
|
|
|
|
$display = get_name($patient_category, 'id', 'name', 'patient_categories') . ' - UNPAID INVOICES AND PAID INVOICES (BUT WITH BALANCE) ';
|
|
|
|
$invoices_query = "SELECT * FROM patient_category_invoices WHERE id = {$id}";
|
|
$invoices_temp = DB::select($invoices_query);
|
|
$invoices_counter = 0;
|
|
|
|
switch ($category) {
|
|
case 'current':
|
|
$display .= " for today, the " . streamline_date($today);
|
|
break;
|
|
|
|
case 'one_to_thirty':
|
|
$request->start_date = $today_minus_thirty;
|
|
$display .= " from " . streamline_date($today_minus_thirty) . " to " . streamline_date($today);
|
|
break;
|
|
|
|
case 'thirty_one_to_sixty':
|
|
$request->start_date = $today_minus_sixty;
|
|
$request->end_date = $today_minus_thirty;
|
|
$display .= " from " . streamline_date($today_minus_sixty) . " to " . streamline_date($today_minus_thirty);
|
|
break;
|
|
|
|
case 'sixty_one_to_ninety':
|
|
$request->start_date = $today_minus_ninety;
|
|
$request->end_date = $today_minus_sixty;
|
|
$display .= " from " . streamline_date($today_minus_ninety) . " to " . streamline_date($today_minus_sixty);
|
|
break;
|
|
|
|
case 'ninety_one_and_over':
|
|
$request->start_date = Carbon::today()->startOfYear()->toDateTimeString();
|
|
$request->end_date = $today_minus_ninety;
|
|
$display .= " before " . streamline_date($today_minus_ninety);
|
|
break;
|
|
}
|
|
|
|
if ($is_generated == "1") {
|
|
$patient_categories = DB::table('patient_discounts')->where(['pay_later' => 1])->pluck('patient_category')->toArray();
|
|
$searched_patient_category = get_name($request->patient_category, 'id', 'name', 'patient_categories');
|
|
|
|
$start_date = $request->start_date;
|
|
$end_date = $request->end_date;
|
|
|
|
$generated_invoice_numbers_array = [];
|
|
|
|
foreach ($invoices_temp as $record) {
|
|
$patient_category_invoice_numbers = DB::table('patient_category_invoices')
|
|
->where('invoice_number', $record->invoice_number)
|
|
->orderBy('created_at', 'asc')->get();
|
|
|
|
$total_invoice_payment = 0;
|
|
|
|
foreach ($patient_category_invoice_numbers as $patient_category_invoice_number) {
|
|
$total_invoice_payment += (int)$patient_category_invoice_number->patient_amount;
|
|
}
|
|
|
|
// check if invoice has a payment record
|
|
$invoice_payment_record = DB::table('invoice_payments')
|
|
->where('invoice_number', $record->invoice_number)->first();
|
|
|
|
if ($invoice_payment_record) {
|
|
if ($invoice_payment_record->balance > 0) {
|
|
// for those invoices with payments, only bring those with a balance
|
|
$invoices[$invoices_counter]['has_payment'] = 1;
|
|
$invoices[$invoices_counter]['amount_paid'] = $invoice_payment_record->amount_paid;
|
|
$invoices[$invoices_counter]['payment_id'] = $invoice_payment_record->id;
|
|
$invoices[$invoices_counter]['balance'] = $invoice_payment_record->balance;
|
|
} else {
|
|
// do not continue to add an invoice record
|
|
continue;
|
|
}
|
|
} else {
|
|
$invoices[$invoices_counter]['has_payment'] = 0;
|
|
$invoices[$invoices_counter]['amount_paid'] = 0;
|
|
$invoices[$invoices_counter]['payment_id'] = 0;
|
|
$invoices[$invoices_counter]['balance'] = 0;
|
|
}
|
|
|
|
$invoices[$invoices_counter]['patient_amount'] = $total_invoice_payment;
|
|
$invoices[$invoices_counter]['invoice_number'] = $record->invoice_number;
|
|
$invoices[$invoices_counter]['invoice_date'] = $record->invoice_date;
|
|
$invoices[$invoices_counter]['patient_category'] = $record->patient_category;
|
|
$invoices[$invoices_counter]['created_at'] = $record->created_at;
|
|
$invoices[$invoices_counter]['updated_at'] = $record->updated_at;
|
|
$invoices[$invoices_counter]['created_by'] = $record->created_by;
|
|
$generated_invoice_numbers_array[] = $record->invoice_number;
|
|
|
|
$invoices_counter++;
|
|
}
|
|
|
|
/* Arranging the data*/
|
|
asort($invoices);
|
|
|
|
if (isset($request->patient_category) && $request->patient_category != 0) {
|
|
$payments = DB::table('invoice_payments')->where('patient_category', $request->patient_category)->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get()->toArray();
|
|
} else {
|
|
$payments = DB::table('invoice_payments')->whereNotIn('invoice_number', $generated_invoice_numbers_array)->groupBy('invoice_number')->get()->toArray();
|
|
}
|
|
|
|
$banks = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$patient_payment_methods = PatientPaymentMethod::pluck('name', 'id');
|
|
$patient_payment_methods_options = "";
|
|
foreach ($patient_payment_methods as $key => $value) {
|
|
$patient_payment_methods_options .= '<option value="' . $key . '">' . $value . '</option>';
|
|
}
|
|
|
|
return view('invoices::invoices.receive_payments.receive_patient_payments', compact('invoices', 'start_date', 'end_date', 'patient_categories', 'searched_patient_category', 'payments', 'request', 'display', 'banks',
|
|
'patient_payment_methods_options'));
|
|
} else {
|
|
$invoice_with_payment_amount_paid = $invoice_with_payment_balance = 0;
|
|
|
|
$users_name = get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users');
|
|
$invoices = DB::table('patient_category_invoices')
|
|
->where('id', $id)
|
|
->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum')
|
|
->get()
|
|
->toArray();
|
|
|
|
$request->invoice_number = "N/A";
|
|
|
|
return view('invoices::invoices.generate_invoice.patient_invoice', compact(
|
|
'invoices',
|
|
'request',
|
|
'users_name',
|
|
'hospital_information',
|
|
'display',
|
|
'invoice_with_payment_amount_paid',
|
|
'invoice_with_payment_balance'
|
|
));
|
|
}
|
|
}
|
|
}
|