mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
1455 lines
71 KiB
PHP
Executable File
1455 lines
71 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Expenses\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Streamline\Models\Banking;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\HospitalBill;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\PatientPaymentMethod;
|
|
use Streamline\Models\Payment;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Models\PaymentItem;
|
|
use Streamline\Models\Quotation;
|
|
use Streamline\Models\QuotationType;
|
|
use Streamline\Models\Supplier;
|
|
use Streamline\Models\TrackInvoice;
|
|
use Streamline\Models\TrackReceipt;
|
|
use Streamline\Models\User;
|
|
use Illuminate\Database\QueryException;
|
|
use Streamline\Models\FamilyAccount;
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
|
|
|
class PaymentController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:Payments-create-payment-item');
|
|
$this->middleware('permission:Payments-edit-payment-item');
|
|
$this->middleware('permission:Payments-list-payment-items');
|
|
$this->middleware('permission:Payments-delete-payment-item');
|
|
$this->middleware('permission:Payments-activate-payment-item');
|
|
$this->middleware('permission:Payments-make-payment');
|
|
$this->middleware('permission:Payments-view-payments-history');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$hospital_information = HospitalInformation::first();
|
|
$suppliers = Supplier::get();
|
|
$banks = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 4)->get()->toArray();
|
|
$items = DB::table('payment_items')->whereNull('deleted_at')->get()->toArray();
|
|
$expense_accounts = DB::table('chart_of_accounts')->whereNull('deleted_at')->whereIn('type', [2])->get()->toArray();
|
|
|
|
return view('expenses::payments.index', compact('banks', 'items', 'suppliers', 'hospital_information', 'expense_accounts'));
|
|
}
|
|
|
|
public function make_payment(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'account_balance' => 'required',
|
|
'bank' => 'required',
|
|
'memo' => 'required',
|
|
'item_id' => 'required',
|
|
'expense_date' => 'required',
|
|
'total_amount' => 'required',
|
|
]);
|
|
|
|
$is_payment_of_greater_expense_allowed = is_payment_of_greater_expense_allowed();
|
|
|
|
//if the stre@mline is set to not allow banks with less balance pay for bigger expenses then fail & go back
|
|
if (($is_payment_of_greater_expense_allowed == 0) && ((int)$request->total_amount > (int)$request->account_balance)) {
|
|
flash('Your account balance is low.')->error();
|
|
return back()->withInput();
|
|
}
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
// actual number of records saved because those with missing fields are disregarded
|
|
$payment_counter = 0;
|
|
$memo = isset($request->memo) ? $request->memo : "N/A";
|
|
$trans_id = generateReceiptNumberFromDB($memo);
|
|
$qty_array = $request->quantity;
|
|
$amt_array = $request->amount;
|
|
$item_array = $request->item_id;
|
|
$expense_acc_id_array = $request->expense_acc_id;
|
|
$vendor_array = $request->supplier_id;
|
|
$unit_cost_array = $request->amount;
|
|
$item_memo_array = $request->item_memo;
|
|
|
|
$account_balance = ((int)$request->account_balance - (int)$request->total_amount);
|
|
$bank = ChartOfAccount::where('id', $request->bank)->update(['balance' => $account_balance]);
|
|
|
|
// insert this payment
|
|
$expense_date = Carbon::parse($request->expense_date)->toDateString();
|
|
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($request->bank, $expense_date);
|
|
$account_balance_on_expense_date = $account_balance_record_on_expense_date ? (int)$account_balance_record_on_expense_date->account_balance : 0;
|
|
$account_balance_on_expense_date_after_payment = ($account_balance_on_expense_date - (int)$request->total_amount);
|
|
$last_insert_id = capture_bank_record(
|
|
'PAYMENT',
|
|
$expense_date,
|
|
$request->bank,
|
|
implode(',', $expense_acc_id_array),
|
|
$account_balance_on_expense_date_after_payment,
|
|
0,
|
|
(int)$request->total_amount,
|
|
$request->memo,
|
|
$trans_id
|
|
);
|
|
|
|
update_banking_record_balances($expense_date, $request->bank, $last_insert_id, $account_balance_on_expense_date_after_payment);
|
|
|
|
for ($x = 0; $x < count($qty_array); $x++) {
|
|
if ($qty_array[$x]) {
|
|
$payment = new Payment;
|
|
$current = ChartOfAccount::where('id', $expense_acc_id_array[$x])->first();
|
|
$new = ChartOfAccount::where('id', $expense_acc_id_array[$x])->update(['balance' => ($amt_array[$x] + $current->balance)]);
|
|
|
|
$payment->item_id = $item_array[$x];
|
|
$payment->vendor = $vendor_array[$x];
|
|
$payment->unit_cost = $unit_cost_array[$x];
|
|
$payment->amount = $amt_array[$x];
|
|
$payment->quantity = $qty_array[$x];
|
|
$payment->memo = $memo;
|
|
$payment->item_memo = $item_memo_array[$x];
|
|
$payment->account_balance = $account_balance_on_expense_date_after_payment;
|
|
$payment->transaction_id = $trans_id;
|
|
$payment->account_id = $request->bank;
|
|
$payment->created_by = $logged_in_user_id;
|
|
$payment->expense_date = Carbon::parse($request->expense_date)->toDateTimeString();
|
|
$payment->expense_account = $expense_acc_id_array[$x]; //get_name($item_array[$x], 'id', 'account_id', 'payment_items');
|
|
|
|
$track_receipt = new TrackReceipt;
|
|
$track_receipt->created_by = $logged_in_user_id;
|
|
$track_receipt->reason = "Payment transaction of id " . $trans_id;
|
|
|
|
try {
|
|
if (!is_null($new) && !is_null($bank)) {
|
|
$payment->save();
|
|
$track_receipt->save();
|
|
++$payment_counter;
|
|
}
|
|
|
|
++$payment_counter;
|
|
} catch (QueryException $e) {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
flash(" Payment has been successfully made")->success();
|
|
return redirect()->route('payments.payment_details', $trans_id);
|
|
}
|
|
}
|
|
|
|
public function history(Request $request)
|
|
{
|
|
$payments = [];
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$display = expense_report_label_setter($request);
|
|
$today = Carbon::today()->toDateString();
|
|
$yesterday = Carbon::yesterday()->toDateString();
|
|
$hospital_information = HospitalInformation::first();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
|
|
if ($request->vendor == "ALL VENDORS") {
|
|
if ($request->staff_member == "ALL STAFF") {
|
|
switch ($request->dates) {
|
|
case 'today':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $today)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'yesterday':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $yesterday)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $start)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date_range':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereBetween('expense_date', [$start, $end])->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
}
|
|
} else {
|
|
switch ($request->dates) {
|
|
case 'today':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $today)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'yesterday':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $yesterday)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereDate('expense_date', $start)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date_range':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->whereBetween('expense_date', [$start, $end])->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if ($request->staff_member == "ALL STAFF") {
|
|
switch ($request->dates) {
|
|
case 'today':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $today)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'yesterday':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $yesterday)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $start)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date_range':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereBetween('expense_date', [$start, $end])->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
}
|
|
} else {
|
|
switch ($request->dates) {
|
|
case 'today':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $today)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'yesterday':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $yesterday)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereDate('expense_date', $start)->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
case 'custom_date_range':
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('vendor', $request->vendor)->whereBetween('expense_date', [$start, $end])->where('created_by', $request->staff_member)->groupBy('transaction_id')->selectRaw('*, sum(amount) as sum')
|
|
->orderBy('id', 'asc')->get()->toArray();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return view('expenses::payments.history', compact('payments', 'hospital_information', 'staff_members', 'suppliers', 'display', 'request'));
|
|
}
|
|
|
|
public function detail($trans_id)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$payment = DB::table('payments')->whereNull('deleted_at')->where('transaction_id', $trans_id)->get()->toArray();
|
|
if (count($payment) > 0) {
|
|
foreach ($payment as $item) {
|
|
$memo = $item->memo;
|
|
$date = $item->created_at;
|
|
$expense_date = $item->expense_date;
|
|
}
|
|
}
|
|
return view('expenses::payments.details', compact('payment', 'hospital_information', 'memo', 'date', 'expense_date', 'trans_id'));
|
|
}
|
|
|
|
public function print_payment_voucher_detail(Request $request)
|
|
{
|
|
$hospital_information = json_decode($request->hospital_information);
|
|
$payment = json_decode($request->payment);
|
|
$memo = $request->memo;
|
|
$date = $request->created_at;
|
|
$expense_date = $request->expense_date;
|
|
$trans_id = $request->trans_id;
|
|
$pdf = SnappyPDF::loadView("expenses::payments.print_voucher_details", compact('payment', 'hospital_information', 'memo', 'date', 'expense_date', 'trans_id'))
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 5)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>'.config('app.name').' - Printed On ' . date('Y-m-d') . ' By ' . auth()->user()->first_name . " " . auth()->user()->last_name . '</i>');
|
|
return $pdf->inline('Payment Voucher' . date(" d-m-y h:ia") . '.pdf');
|
|
}
|
|
|
|
public function edit_payments($trans_id)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$suppliers = Supplier::get();
|
|
$items = DB::table('payment_items')->whereNull('deleted_at')->get()->toArray();
|
|
$expense_and_payable_accounts = DB::table('chart_of_accounts')->whereIn('type', [2, 6, 9])->pluck('name', 'id')->toArray();
|
|
$payments_total = 0;
|
|
|
|
$payments = DB::table('payments')->whereNull('deleted_at')->where('transaction_id', $trans_id)->orderBy('id', 'asc')->get()->toArray();
|
|
if (count($payments) > 0) {
|
|
foreach ($payments as $item) {
|
|
$memo = $item->memo;
|
|
$date = $item->created_at;
|
|
$expense_date = $item->expense_date;
|
|
$bank = $item->account_id;
|
|
$payments_total += (int)$item->amount;
|
|
$max_id = $item->id;
|
|
}
|
|
}
|
|
|
|
$today = Carbon::today()->toDateString();
|
|
$latest_bank_record = get_latest_banking_record_based_on_transaction_date($bank, $today);
|
|
$bank_balance = $latest_bank_record->account_balance;
|
|
|
|
return view('expenses::payments.details_edit', compact('trans_id', 'max_id', 'payments', 'payments_total', 'hospital_information', 'memo', 'date', 'expense_date', 'trans_id', 'bank', 'bank_balance', 'items', 'suppliers', 'expense_and_payable_accounts'));
|
|
}
|
|
|
|
public function update_payments(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'account_balance' => 'required',
|
|
'bank' => 'required',
|
|
'memo' => 'required',
|
|
'item_id' => 'required',
|
|
'expense_date' => 'required',
|
|
'total_amount' => 'required',
|
|
]);
|
|
|
|
if ((int)$request->total_amount <= (int)$request->account_balance) {
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
$qty_array = $request->quantity;
|
|
$amt_array = $request->amount;
|
|
$item_array = $request->item_id;
|
|
$expense_acc_id_array = $request->expense_acc_id;
|
|
$vendor_array = $request->supplier_id;
|
|
$unit_cost_array = $request->amount;
|
|
|
|
$expense_date = Carbon::parse($request->expense_date)->toDateString(); // same as the one in the db since edit is blocked on UI
|
|
|
|
// simply editing the past records
|
|
$past_total_expense = 0;
|
|
$past_total_expense_update = 0;
|
|
$payment_index = 0;
|
|
$past_payments = Payment::where('transaction_id', $request->trans_id)->whereNull('deleted_at')->orderBy('id', 'asc')->get();
|
|
if (count($past_payments) > 0) {
|
|
foreach ($past_payments as $payment) {
|
|
$past_total_expense += $payment->amount;
|
|
$past_total_expense_update += $amt_array[$payment_index];
|
|
|
|
// if changes have been submitted
|
|
$payment_copy = clone $payment;
|
|
$payment->quantity = $qty_array[$payment_index];
|
|
$old_payment_amount = $payment->amount;
|
|
$new_payment_amount = $amt_array[$payment_index];
|
|
$payment->amount = $new_payment_amount;
|
|
$payment->item_id = $item_array[$payment_index];
|
|
$payment->expense_account = $expense_acc_id_array[$payment_index];
|
|
$payment->vendor = $vendor_array[$payment_index];
|
|
$payment->unit_cost = $unit_cost_array[$payment_index];
|
|
if ($payment_copy == $payment) {
|
|
continue;
|
|
} else {
|
|
$bill_id = $payment->bill_id;
|
|
if ($bill_id != null) {
|
|
$bill = HospitalBill::where('id', $bill_id)->first();
|
|
if ($bill) {
|
|
$bill->balance = $bill->balance + $old_payment_amount - $new_payment_amount;
|
|
$bill->updated_by = $logged_in_user_id;
|
|
$bill->update();
|
|
flash("Bill (" . $bill->bill_number . ") has been updated")->success();
|
|
}
|
|
}
|
|
$payment->updated_by = $logged_in_user_id;
|
|
$payment->update();
|
|
flash("Payment (" . $payment->id . ") has been successfully updated")->success();
|
|
}
|
|
$payment_index++;
|
|
}
|
|
|
|
// if total payment changed, the bank payment record needs an update (amount)
|
|
if ($past_total_expense != $past_total_expense_update) {
|
|
$past_bank_records = Banking::where('trans_id', $request->trans_id)
|
|
->whereNull('deleted_at')
|
|
->where('trans_type', 'PAYMENT')
|
|
->get(); // expect one record
|
|
if (count($past_bank_records) > 0) {
|
|
foreach ($past_bank_records as $record) {
|
|
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($record->bank, $expense_date);
|
|
$account_balance_on_expense_date = (int)$account_balance_record_on_expense_date->account_balance;
|
|
$account_balance_on_expense_date_after_payment = ($account_balance_on_expense_date - $past_total_expense_update);
|
|
|
|
$record->debit = $past_total_expense_update;
|
|
$record->account_balance = $account_balance_on_expense_date_after_payment;
|
|
$record->update();
|
|
flash("Bank Payment record: " . $record->id . " updated!");
|
|
|
|
// use the bank record before the one just deleted
|
|
$previous_bank_record = Banking::where('bank', $record->bank)
|
|
->whereNull('deleted_at')
|
|
->where('id', '<', $record->id)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
|
|
if ($previous_bank_record) {
|
|
update_banking_record_balances(Carbon::parse($previous_bank_record->trans_date)->toDateString(), $record->bank, $previous_bank_record->id, $previous_bank_record->account_balance);
|
|
} else {
|
|
update_banking_record_balances($expense_date, $record->bank, $record->id, $account_balance_on_expense_date_after_payment);
|
|
}
|
|
flash("Payment has been deleted successfully from Bank: " . get_name($record->bank, 'id', 'name', 'chart_of_accounts'))->success();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return redirect()->route('payments.payment_details', $request->trans_id);
|
|
}
|
|
} else {
|
|
flash('Your account balance is low.')->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function delete_payments(Request $request)
|
|
{
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
// deleting the past records
|
|
$past_total_expense = 0;
|
|
$past_payments = Payment::where('transaction_id', $request->trans_id)->whereNull('deleted_at')->orderBy('id', 'asc')->get();
|
|
if (count($past_payments) > 0) {
|
|
foreach ($past_payments as $payment) {
|
|
$past_total_expense += $payment->amount;
|
|
|
|
$payment->updated_by = $logged_in_user_id;
|
|
$payment->update();
|
|
$payment->delete();
|
|
flash("Payment (" . $payment->id . ") has been successfully deleted")->success();
|
|
}
|
|
|
|
// delete related bank records
|
|
if ($past_total_expense > 0) {
|
|
$past_bank_records = Banking::where('trans_id', $request->trans_id)
|
|
->whereNull('deleted_at')
|
|
->where('trans_type', 'PAYMENT')
|
|
->get(); // expect one record
|
|
if (count($past_bank_records) > 0) {
|
|
foreach ($past_bank_records as $record) {
|
|
$expense_date = Carbon::parse($record->trans_date)->toDateString();
|
|
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($record->bank, $expense_date);
|
|
$account_balance_on_expense_date = (int)$account_balance_record_on_expense_date->account_balance;
|
|
$account_balance_on_expense_date_after_payment = ($account_balance_on_expense_date - $past_total_expense);
|
|
|
|
$record->updated_by = $logged_in_user_id;
|
|
$record->update();
|
|
$record->delete();
|
|
flash("Bank Payment record: " . $record->id . " deleted!")->success();
|
|
|
|
// use the bank record before the one just deleted
|
|
$previous_bank_record = Banking::where('bank', $record->bank)
|
|
->whereNull('deleted_at')
|
|
->where('id', '<', $record->id)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
|
|
if ($previous_bank_record) {
|
|
flash("Recursive update based on previous_bank_record: " . $previous_bank_record->id . "!")->success();
|
|
update_banking_record_balances(Carbon::parse($previous_bank_record->trans_date)->toDateString(), $record->bank, $previous_bank_record->id, $previous_bank_record->account_balance);
|
|
} else {
|
|
flash("Recursive update based on current_bank_record: " . $record->id . "!")->success();
|
|
update_banking_record_balances($expense_date, $record->bank, $record->id, $account_balance_on_expense_date_after_payment);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 'success';
|
|
}
|
|
return 'fail';
|
|
}
|
|
|
|
public function get_account_balance(Request $request)
|
|
{
|
|
$acc = DB::table('chart_of_accounts')->where('id', $request->bank_id)->get()->toArray();
|
|
return $acc;
|
|
}
|
|
|
|
public function __get_payment_item_details(Request $request)
|
|
{
|
|
|
|
$item = DB::table('payment_items')->where('id', $request->item_id)->get()->toArray();
|
|
foreach ($item as $i) {
|
|
$account = get_name($i->account_id, 'id', 'name', 'chart_of_accounts');
|
|
$unit_cost = $i->unit_cost;
|
|
}
|
|
return array($item, $account, $unit_cost);
|
|
}
|
|
|
|
public function get_payment_item_details(Request $request)
|
|
{
|
|
|
|
$response_text = "";
|
|
|
|
$item = DB::table('payment_items')->where('id', $request->item_id)->first();
|
|
|
|
$response_text .= "<div class='form-inline'>";
|
|
|
|
$response_text .= "<div class='form-group' style='margin-left: 70px'>";
|
|
// $response_text .= "<label>Quantity:</label>";
|
|
$response_text .= "<input type='number' class='form-control compulsory' onchange = 'change_amt(this.value)' id='qty' name='qty[]' value='" . 1 . "'/>";
|
|
$response_text .= "</div>";
|
|
|
|
$response_text .= "<div class='form-group' style='margin-left: 40px'>";
|
|
// $response_text .= "<label>Amount:</label>";
|
|
$response_text .= "<input type='text' class='form-control compulsory amt' id='amt' readonly name='amt[]' value='" . $item->unit_cost . "'/>";
|
|
$response_text .= "</div>";
|
|
|
|
$response_text .= "<div class='form-group' style='margin-left: 40px'>";
|
|
// $response_text .= "<label>Expense Account:</label>";
|
|
$response_text .= "<input type='text' class='form-control compulsory' id='exs' readonly name='exs[]' value='" . get_name($item->account_id, 'id', 'name', 'chart_of_accounts') . "'/>";
|
|
$response_text .= "<input type='hidden' class='form-control compulsory' id='unit_cost' readonly name='unit_cost[]' value='" . $item->unit_cost . "'/>";
|
|
$response_text .= "<input type='hidden' class='form-control compulsory' id='account_id' readonly name='account_id[]' value='" . $item->account_id . "'/>";
|
|
|
|
$response_text .= "</div>";
|
|
|
|
$response_text .= "</div>";
|
|
|
|
return $response_text;
|
|
}
|
|
|
|
public function get_expense_acc(Request $request)
|
|
{
|
|
|
|
$item = PaymentItem::where('id', $request->item_id)->first();
|
|
$expense_acc = ChartOfAccount::where('id', $item->account_id)->first();
|
|
$payable_acc = ChartOfAccount::where('id', $item->payable_account)->first();
|
|
|
|
return array($expense_acc, $payable_acc);
|
|
}
|
|
|
|
public function total_price(Request $request)
|
|
{
|
|
|
|
$unit_cost = get_name($request->item_id, 'id', 'unit_cost', 'payment_items');
|
|
$quantity = $request->quantity;
|
|
$amount = (int)$unit_cost * (int)$quantity;
|
|
return array($amount, $unit_cost);
|
|
}
|
|
|
|
public function store_bill_payment(Request $request) {
|
|
$logged_in_user = Auth::id();
|
|
|
|
$bill_update = HospitalBill::find($request->bill_id);
|
|
|
|
$payment_memo = (isset($request->payment_memo) && $request->payment_memo != "") ? $request->payment_memo : "N/A";
|
|
|
|
$track_receipt = new TrackReceipt;
|
|
$track_receipt->reason = "Bill Payment on Bill number " . $bill_update->bill_number;
|
|
$track_receipt->created_by = $logged_in_user;
|
|
$track_receipt->save();
|
|
$receipt_number = sprintf("%04u", $track_receipt->id);
|
|
|
|
$bill_update->balance = (int)$request->bill_balance;
|
|
|
|
$date_paid_history = unserialize($bill_update->date_paid_history);
|
|
array_push($date_paid_history, $request->payment_date);
|
|
$date_paid_serial = serialize($date_paid_history);
|
|
|
|
$amount_paid_history = unserialize($bill_update->amount_paid_history);
|
|
array_push($amount_paid_history, (int)$request->amount_to_pay);
|
|
$amount_paid_serial = serialize($amount_paid_history);
|
|
|
|
$staff_incharge_history = unserialize($bill_update->staff_incharge_history);
|
|
array_push($staff_incharge_history, Auth::id());
|
|
$staff_in_charge_serial = serialize($staff_incharge_history);
|
|
|
|
$receipt_history = unserialize($bill_update->receipt_history);
|
|
array_push($receipt_history, $receipt_number);
|
|
$receipt_serial = serialize($receipt_history);
|
|
|
|
$balance_history = unserialize($bill_update->balance_history);
|
|
array_push($balance_history, (int)$request->bill_balance);
|
|
$balance_serial = serialize($balance_history);
|
|
|
|
$bill_update->amount_paid_history = $amount_paid_serial;
|
|
$bill_update->date_paid_history = $date_paid_serial;
|
|
$bill_update->staff_incharge_history = $staff_in_charge_serial;
|
|
$bill_update->receipt_history = $receipt_serial;
|
|
$bill_update->balance_history = $balance_serial;
|
|
|
|
$bill_update->payment_status = 1;
|
|
$bill_update->updated_by = $logged_in_user;
|
|
$bill_update->update();
|
|
|
|
clearBill($request->bill_id);
|
|
|
|
$item_id_array = explode(',', $bill_update->item_ids);
|
|
$item_accounts = ($bill_update->item_accounts != '' && $bill_update->item_accounts != null) ? $bill_update->item_accounts : '' . $bill_update->payable_account;
|
|
$items_quantity_array = explode(',', $bill_update->item_quantities);
|
|
$items_amount_array = explode(',', $bill_update->item_subtotals);
|
|
|
|
$account_balance = ((int)$request->current_account_balance - (int)$request->amount_to_pay);
|
|
ChartOfAccount::where('id', $request->bank_account)->update(['balance' => $account_balance]);
|
|
|
|
// insert this payment
|
|
$payment_date = Carbon::parse($request->payment_date)->toDateString();
|
|
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($request->bank_account, $payment_date);
|
|
$account_balance_on_expense_date = (int)$account_balance_record_on_expense_date->account_balance;
|
|
$account_balance_on_expense_date_after_payment = ($account_balance_on_expense_date - (int)$request->amount_to_pay);
|
|
$last_insert_id = capture_bank_record(
|
|
'PAYMENT',
|
|
$payment_date,
|
|
$request->bank_account,
|
|
$item_accounts,
|
|
$account_balance_on_expense_date_after_payment,
|
|
0,
|
|
(int)$request->amount_to_pay,
|
|
$payment_memo,
|
|
$receipt_number
|
|
);
|
|
|
|
update_banking_record_balances($payment_date, $request->bank_account, $last_insert_id, $account_balance_on_expense_date_after_payment);
|
|
|
|
$bill_payment = new Payment;
|
|
$bill_payment->item_id = $item_id_array[0];
|
|
$bill_payment->vendor = $bill_update->vendor;
|
|
$bill_payment->unit_cost = get_name($item_id_array[0], 'id', 'unit_cost', 'payment_items');
|
|
$bill_payment->amount = (int)$request->amount_to_pay;
|
|
$bill_payment->quantity = $items_quantity_array[0];
|
|
$bill_payment->memo = $payment_memo;
|
|
$bill_payment->account_balance = $account_balance;
|
|
$bill_payment->transaction_id = $receipt_number;
|
|
$bill_payment->account_id = $request->bank_account;
|
|
$bill_payment->created_by = $logged_in_user;
|
|
$bill_payment->expense_date = Carbon::parse($request->payment_date)->toDateTimeString();
|
|
$bill_payment->bill_id = $request->bill_id;
|
|
|
|
if ($bill_update->bill_type != "ASSETS") {
|
|
$bill_payment->expense_account = get_name($item_id_array[0], 'id', 'account_id', 'payment_items');
|
|
}
|
|
|
|
$bill_payment->save();
|
|
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$accounts_payable_balance->balance - (int)$request->amount_to_pay]);
|
|
|
|
return (int)$request->bill_balance;
|
|
}
|
|
|
|
public function create_bills()
|
|
{
|
|
|
|
$suppliers = Supplier::orderBy('name', 'asc')->get();
|
|
$staff_members = User::orderBy('first_name', 'asc')->get();
|
|
$payments = [];
|
|
$banks = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 4)->orderBy('name', 'asc')->get()->toArray();
|
|
$items = DB::table('payment_items')->whereNull('deleted_at')->orderBy('name', 'asc')->get()->toArray();
|
|
$hospital_information = HospitalInformation::first();
|
|
|
|
return view('expenses::payments.bills.create', compact(
|
|
'staff_members',
|
|
'suppliers',
|
|
'hospital_information',
|
|
'payments',
|
|
'banks',
|
|
'items'
|
|
));
|
|
}
|
|
|
|
public function edit_bill($id)
|
|
{
|
|
$suppliers = Supplier::orderBy('name', 'asc')->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$staff_members = User::orderBy('first_name', 'asc')->get();
|
|
$banks = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 4)->orderBy('name', 'asc')->get()->toArray();
|
|
$payment_items = PaymentItem::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$option_items_array = DB::table('payment_items')->whereNull('deleted_at')->orderBy('name', 'asc')->get()->toArray();
|
|
$option_suppliers_array = DB::table('suppliers')->where('available', 1)->whereNull('deleted_at')->orderBy('name', 'asc')->get()->toArray();
|
|
$hospital_information = HospitalInformation::first();
|
|
$bill = HospitalBill::find($id);
|
|
$payments = Payment::where('bill_id', $id)->get();
|
|
|
|
return view('expenses::payments.bills.edit', compact(
|
|
'staff_members',
|
|
'suppliers',
|
|
'hospital_information',
|
|
'payments',
|
|
'option_items_array',
|
|
'chart_of_accounts',
|
|
'option_suppliers_array',
|
|
'banks',
|
|
'payment_items',
|
|
'bill'
|
|
));
|
|
}
|
|
|
|
public function update_bill(Request $request, $id)
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'vendor' => 'required',
|
|
'bill_date' => 'required',
|
|
'bill_number' => 'required',
|
|
'bill_due_date' => 'required',
|
|
'bill_memo' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
if ($request->bill_action == 'save') {
|
|
|
|
$logged_in_user = Auth::id();
|
|
$bill_date = Carbon::parse($request->bill_date)->toDateString();
|
|
$bill_due_date = Carbon::parse($request->bill_due_date)->toDateString();
|
|
|
|
$bill = HospitalBill::find($id);
|
|
$bill->vendor = $request->vendor;
|
|
$bill->bill_date = $bill_date;
|
|
$bill->bill_term = $request->bill_term;
|
|
$bill->bill_memo = $request->bill_memo;
|
|
$bill->bill_number = $request->bill_number;
|
|
$bill->bill_due_date = $bill_due_date;
|
|
$bill->total_amount = (int)$request->total_amount;
|
|
$bill->item_ids = implode(',', $request->item_id);
|
|
$bill->item_accounts = implode(',', $request->expense_acc_id);
|
|
$bill->item_quantities = implode(',', $request->quantity);
|
|
$bill->item_subtotals = implode(',', $request->amount);
|
|
$bill->payable_account = implode(',', $request->payable_acc_id);
|
|
$bill->created_by = $logged_in_user;
|
|
$bill->bill_type = 'PAY';
|
|
|
|
// update the bill balance
|
|
$item_amounts_paid = array_map('intval', explode(',', $bill->item_amount_paid));
|
|
$amount_paid = array_sum($item_amounts_paid);
|
|
$bill->balance = (int)$request->total_amount - $amount_paid;
|
|
|
|
$track_invoice = new TrackInvoice;
|
|
$track_invoice->reason = $request->bill_memo;
|
|
$track_invoice->created_by = $logged_in_user;
|
|
|
|
if ($bill->save() && $track_invoice->save()) {
|
|
|
|
flash('Bill has successfully been Updated.')->info();
|
|
} else {
|
|
|
|
flash('Failed to save Bill.')->error();
|
|
}
|
|
return redirect()->route('payments.bills');
|
|
}
|
|
}
|
|
}
|
|
|
|
public function save_bill(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'vendor' => 'required',
|
|
'bill_date' => 'required',
|
|
'bill_number' => 'required',
|
|
'bill_due_date' => 'required',
|
|
'bill_memo' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
if ($request->bill_action == 'save') {
|
|
|
|
$logged_in_user = Auth::id();
|
|
$bill_date = Carbon::parse($request->bill_date)->toDateString();
|
|
$bill_due_date = Carbon::parse($request->bill_due_date)->toDateString();
|
|
|
|
$bill = new HospitalBill;
|
|
$bill->vendor = $request->vendor;
|
|
$bill->bill_date = $bill_date;
|
|
$bill->bill_term = $request->bill_term;
|
|
$bill->bill_memo = $request->bill_memo;
|
|
$bill->bill_number = $request->bill_number;
|
|
$bill->bill_due_date = $bill_due_date;
|
|
$bill->total_amount = (int)$request->total_amount;
|
|
$bill->item_ids = implode(',', $request->item_id);
|
|
$bill->item_accounts = implode(',', $request->expense_acc_id);
|
|
$bill->item_quantities = implode(',', $request->quantity);
|
|
$bill->item_subtotals = implode(',', $request->amount);
|
|
$bill->payable_account = implode(',', $request->payable_acc_id);
|
|
$bill->created_by = $logged_in_user;
|
|
$bill->bill_type = 'PAY';
|
|
|
|
$bill->balance_history = serialize(array());
|
|
$bill->receipt_history = serialize(array());
|
|
$bill->staff_incharge_history = serialize(array());
|
|
$bill->amount_paid_history = serialize(array());
|
|
$bill->date_paid_history = serialize(array());
|
|
|
|
$track_invoice = new TrackInvoice;
|
|
$track_invoice->reason = $request->bill_memo;
|
|
$track_invoice->created_by = $logged_in_user;
|
|
|
|
if ($bill->save() && $track_invoice->save()) {
|
|
// TODO: tag the correct accounts payable each time
|
|
// Note that multiple items can be selected when paying a bill and can be linked to different accounts payables
|
|
// CC: @koomabenjamin
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$request->total_amount + (int)$accounts_payable_balance->balance]);
|
|
flash('Bill has successfully been saved.')->success();
|
|
} else {
|
|
flash('Failed to save Bill.')->error();
|
|
}
|
|
return redirect()->route('payments.bills');
|
|
}
|
|
}
|
|
}
|
|
|
|
public function save_inventory_bill(Request $request)
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'vendor' => 'required',
|
|
'bill_date' => 'required',
|
|
'bill_number' => 'required',
|
|
'bill_due_date' => 'required',
|
|
'bill_memo' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
// return back()->withErrors($validator)->withInput();
|
|
return redirect()->route('payments.bills.inventory');
|
|
} else {
|
|
$logged_in_user = Auth::id();
|
|
$bill_date = Carbon::parse($request->bill_date)->toDateString();
|
|
$bill_due_date = Carbon::parse($request->bill_due_date)->toDateString();
|
|
|
|
/* == start:delete this existing temporary bill that was attached to quotation used for enforcing double entry === */
|
|
$quotation_record = Quotation::where('quotation_number', $request->quotation_number)->first();
|
|
$dentals_radiologies_and_general_items_expe_accounts = "";
|
|
$item_type = null;
|
|
if($quotation_record){
|
|
$temp_bill_id_to_delete = $quotation_record->bill_id;
|
|
$bill_to_delete = HospitalBill::find($temp_bill_id_to_delete);
|
|
|
|
if($bill_to_delete){
|
|
$item_type = $bill_to_delete->quotation_type_id;
|
|
$dentals_radiologies_and_general_items_expe_accounts = $bill_to_delete->item_accounts;
|
|
$bill_to_delete->delete();
|
|
$restore_order = Quotation::where('quotation_number', $request->quotation_number)
|
|
->update(['bill_id' => null, 'invoice_generated' => null, 'invoice_date' => null, 'invoice_number' => null]);
|
|
}
|
|
}
|
|
/* =============== end: ==================== */
|
|
|
|
|
|
$bill = new HospitalBill;
|
|
$bill->vendor = $request->vendor;
|
|
$bill->bill_date = $bill_date;
|
|
$bill->bill_term = $request->bill_term;
|
|
$bill->bill_memo = $request->bill_memo;
|
|
$bill->bill_number = $request->bill_number;
|
|
$bill->bill_due_date = $bill_due_date;
|
|
$bill->total_amount = (int)$request->total_amount;
|
|
$bill->item_ids = implode(',', $request->item_id);
|
|
$bill->item_accounts = in_array($item_type, [1,2]) ? implode(',', $request->expense_acc_id) : $dentals_radiologies_and_general_items_expe_accounts;
|
|
$bill->item_quantities = implode(',', $request->quantity);
|
|
$bill->item_subtotals = implode(',', $request->amount);
|
|
$bill->created_by = $logged_in_user;
|
|
|
|
$bill->balance_history = serialize(array());
|
|
$bill->receipt_history = serialize(array());
|
|
$bill->staff_incharge_history = serialize(array());
|
|
$bill->amount_paid_history = serialize(array());
|
|
$bill->date_paid_history = serialize(array());
|
|
|
|
$track_invoice = new TrackInvoice;
|
|
$track_invoice->reason = $request->bill_memo;
|
|
$track_invoice->created_by = $logged_in_user;
|
|
|
|
switch ($request->quotation_type_id) {
|
|
case 1:
|
|
$bill->bill_type = 'DRU';
|
|
break;
|
|
case 2:
|
|
$bill->bill_type = 'SUN';
|
|
break;
|
|
case 3:
|
|
$bill->bill_type = 'DEN';
|
|
break;
|
|
case 4:
|
|
$bill->bill_type = 'RAD';
|
|
break;
|
|
case 5:
|
|
$bill->bill_type = 'LAB';
|
|
break;
|
|
case 6:
|
|
$bill->bill_type = 'GEN';
|
|
break;
|
|
case 7:
|
|
$bill->bill_type = 'EYE';
|
|
break;
|
|
default:
|
|
// bill type is supposed to be defined
|
|
throw new \Exception("Bill Type was not found");
|
|
}
|
|
|
|
if ($bill->save() && $track_invoice->save()) {
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$request->total_amount + (int)$accounts_payable_balance->balance]);
|
|
flash('Bill has successfully been saved.')->success();
|
|
} else {
|
|
flash('Failed to save Bill.')->error();
|
|
}
|
|
|
|
$update = Quotation::where('quotation_number', $request->quotation_number)
|
|
->update(['bill_id' => $bill->id, 'invoice_generated' => 1, 'invoice_date' => $request->bill_date, 'invoice_number' => $request->bill_number]);
|
|
// ->update(['invoice_generated' => 1, 'invoice_date' => $request->bill_date, 'invoice_number' => $request->bill_number]);
|
|
|
|
return redirect('/payments/bills/inventory');
|
|
}
|
|
}
|
|
|
|
public function voucher($id) {
|
|
$payments = Payment::where('bill_id', $id)->groupBy('transaction_id')->get();
|
|
$hospital_information = HospitalInformation::first();
|
|
return view('expenses::payments.bills.voucher', compact('payments', 'hospital_information'));
|
|
}
|
|
|
|
public function get_bill_number()
|
|
{
|
|
return generateInvoiceNumberFromDB("Get Bill Number");
|
|
}
|
|
|
|
public function bills(Request $request) {
|
|
$banks = ChartOfAccount::where('type', 4)->get();
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$display = "";
|
|
$bank_accounts = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$filters = [];
|
|
|
|
if ($request->dates == "today") {
|
|
$filters[] = ['bill_date', '=', date('Y-m-d')];
|
|
$display .= "Today | ";
|
|
} elseif ($request->dates == "yesterday") {
|
|
$filters[] = ['bill_date', '=', date("Y-m-d", strtotime("yesterday"))];
|
|
$display .= "Yesterday | ";
|
|
} elseif ($request->dates == "custom_date") {
|
|
$filters[] = ['bill_date', '=', date('Y-m-d', strtotime($request->start_date))];
|
|
$display .= "On: " . streamline_date($request->start_date) . " | ";
|
|
} elseif ($request->dates == "custom_date_range") {
|
|
$filters[] = ['bill_date', '>=', date('Y-m-d', strtotime($request->start_date))];
|
|
$filters[] = ['bill_date', '<=', date('Y-m-d', strtotime($request->end_date))];
|
|
$display .= "From: " . streamline_date($request->start_date) . " To: " . streamline_date($request->end_date) . " | ";
|
|
}
|
|
|
|
if ($request->vendor != "ALL VENDORS") {
|
|
$filters[] = ['vendor', '=', $request->vendor];
|
|
$display .= "Vendor: " . get_name($request->vendor, 'id', 'name', 'suppliers') . " | ";
|
|
} else {
|
|
$display .= "All Vendors | ";
|
|
}
|
|
|
|
if ($request->staff_member != "ALL STAFF") {
|
|
$filters[] = ['created_by', '=', $request->staff_member];
|
|
$display .= "Staff: " . get_full_name($request->staff_member, 'id', 'first_name', 'last_name', 'users') . " | ";
|
|
} else {
|
|
$display .= "All Staff | ";
|
|
}
|
|
|
|
if ($request->bill_status == "paid") {
|
|
$display .= 'Paid Bills';
|
|
$filters[] = ['balance', '=', 0];
|
|
} elseif ($request->bill_status == "unpaid") {
|
|
$display .= 'Unpaid Bills';
|
|
$filters[] = ['balance', '=', NULL];
|
|
} elseif ($request->bill_status == "paid_but_with_balance") {
|
|
$display .= 'Paid Bills with balance';
|
|
$filters[] = ['balance', '>', 0];
|
|
} else {
|
|
$display .= 'All Bills';
|
|
}
|
|
|
|
$bills = DB::table('hospital_bills')->whereNull('deleted_at')->where($filters)->get()->toArray();
|
|
|
|
return view('expenses::payments.bills.index', compact('bills', 'staff_members', 'suppliers', 'display', 'banks', 'request', 'bank_accounts'));
|
|
}
|
|
|
|
public function bills_bulk_pay(Request $request)
|
|
{
|
|
$bill_id_array = explode(',', $request->selected_bill_ids);
|
|
$deductions = $bill_deductions = [];
|
|
$logged_in_user = Auth::id();
|
|
|
|
$track_receipt = new TrackReceipt;
|
|
$track_receipt->reason = "Bulk Bill Payment";
|
|
$track_receipt->created_by = $logged_in_user;
|
|
$track_receipt->save();
|
|
$receipt_number = sprintf("%04u", $track_receipt->id);
|
|
|
|
$bill_amount_paid = (int)$request->amount_to_pay + (int)$request->total_amount_paid_off;
|
|
|
|
// split amounts for the bills
|
|
|
|
for ($i = 0; $i < count($bill_id_array); $i++) {
|
|
|
|
$bill = HospitalBill::find($bill_id_array[$i]);
|
|
$bill_total = $bill->total_amount;
|
|
$bill_balance = $bill->balance;
|
|
$item_amount_paid = [];
|
|
$item_balance_remaining = [];
|
|
$items_array = explode(',', $bill->item_ids);
|
|
$items_quantity_array = explode(',', $bill->item_quantities);
|
|
$items_subtotal_array = explode(',', $bill->item_subtotals);
|
|
|
|
for ($x = 0; $x < count($items_array); $x++) {
|
|
|
|
$new_amount = $bill_amount_paid - array_sum($deductions);
|
|
|
|
$bill_payment = new Payment;
|
|
|
|
if ($items_subtotal_array[$x] <= $new_amount) {
|
|
|
|
$item_amount_paid[] = $items_subtotal_array[$x];
|
|
$item_balance_remaining[] = 0;
|
|
$deductions[] = $items_subtotal_array[$x];
|
|
$bill_payment->amount = $items_subtotal_array[$x];
|
|
} else {
|
|
|
|
$item_amount_paid[] = $new_amount;
|
|
$item_balance_remaining[] = (int)$items_subtotal_array[$x] - (int)$new_amount;
|
|
$deductions[] = $new_amount;
|
|
$bill_payment->amount = $new_amount;
|
|
}
|
|
|
|
if ($bill->bill_type == "PAY") {
|
|
|
|
$bill_payment->item_id = $items_array[$x];
|
|
$bill_payment->vendor = $bill->vendor;
|
|
$bill_payment->unit_cost = get_name($items_array[$x], 'id', 'unit_cost', 'payment_items');
|
|
$bill_payment->quantity = $items_quantity_array[$x];
|
|
$bill_payment->memo = 'A portion of a Bulk Payment';
|
|
$bill_payment->account_balance = 0;
|
|
$bill_payment->transaction_id = $receipt_number;
|
|
$bill_payment->account_id = $request->bank_account;
|
|
$bill_payment->created_by = $logged_in_user;
|
|
$bill_payment->expense_date = Carbon::parse($request->payment_date)->toDateTimeString();
|
|
$bill_payment->bill_id = $bill->id;
|
|
$bill_payment->expense_account = get_name($items_array[$x], 'id', 'account_id', 'payment_items');
|
|
$bill_payment->save();
|
|
}
|
|
}
|
|
|
|
HospitalBill::where('id', $bill_id_array[$i])->update([
|
|
'balance' => array_sum($item_balance_remaining),
|
|
'item_amount_paid' => implode(',', $item_amount_paid),
|
|
'item_balance_remaining' => implode(',', $item_balance_remaining)
|
|
]);
|
|
|
|
$date_paid_history = unserialize($bill->date_paid_history);
|
|
array_push($date_paid_history, $request->payment_date);
|
|
$date_paid_serial = serialize($date_paid_history);
|
|
|
|
$amount_paid_history = unserialize($bill->amount_paid_history);
|
|
array_push($amount_paid_history, array_sum($item_amount_paid));
|
|
$amount_paid_serial = serialize($amount_paid_history);
|
|
|
|
$staff_incharge_history = unserialize($bill->staff_incharge_history);
|
|
array_push($staff_incharge_history, Auth::id());
|
|
$staff_in_charge_serial = serialize($staff_incharge_history);
|
|
|
|
$receipt_history = unserialize($bill->receipt_history);
|
|
array_push($receipt_history, $receipt_number);
|
|
$receipt_serial = serialize($receipt_history);
|
|
|
|
$balance_history = unserialize($bill->balance_history);
|
|
array_push($balance_history, array_sum($item_balance_remaining));
|
|
$balance_serial = serialize($balance_history);
|
|
|
|
$bill->amount_paid_history = $amount_paid_serial;
|
|
$bill->date_paid_history = $date_paid_serial;
|
|
$bill->staff_incharge_history = $staff_in_charge_serial;
|
|
$bill->receipt_history = $receipt_serial;
|
|
$bill->balance_history = $balance_serial;
|
|
|
|
$bill->payment_status = 1;
|
|
$bill->updated_by = $logged_in_user;
|
|
$bill->save();
|
|
}
|
|
|
|
if (Carbon::parse($request->payment_date)->toDateString() < Carbon::today()->toDateString()) {
|
|
$to_banking_records = Banking::where('bank', $request->transfer_to)->where('trans_date', '>', Carbon::parse($request->transfer_date)->toDateString())->get();
|
|
$from_banking_records = Banking::where('bank', $request->transfer_from)->where('trans_date', '>', Carbon::parse($request->transfer_date)->toDateString())->get();
|
|
|
|
foreach ($to_banking_records as $record) {
|
|
Banking::where('id', $record->id)->update(['account_balance' => ((int)$record->account_balance + (int)$request->transfer_amount)]);
|
|
}
|
|
|
|
foreach ($from_banking_records as $record) {
|
|
Banking::where('id', $record->id)->update(['account_balance' => ((int)$record->account_balance - (int)$request->transfer_amount)]);
|
|
}
|
|
}
|
|
|
|
// record bank transfers / payments
|
|
capture_bank_record(
|
|
'PAYMENT',
|
|
Carbon::parse($request->payment_date)->toDateString(),
|
|
$request->bank_account,
|
|
"",
|
|
((int)$request->account_balance - (int)$request->amount_to_pay),
|
|
0,
|
|
(int)$request->amount_to_pay,
|
|
$request->payment_memo,
|
|
$receipt_number
|
|
);
|
|
|
|
return redirect()->route('payments.bills');
|
|
}
|
|
|
|
public function delete_bill(Request $request)
|
|
{
|
|
|
|
$bill_id = $request->bill_id;
|
|
$bill = HospitalBill::find($bill_id);
|
|
|
|
if ($bill) {
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$accounts_payable_balance->balance - (int)$bill->total_amount]);
|
|
Quotation::where('bill_id', $bill_id)->update(['bill_id' => null, 'invoice_generated' => 0, 'invoice_date' => null, 'invoice_number' => null]);
|
|
$bill->delete();
|
|
return 'success';
|
|
} else {
|
|
return 'fail';
|
|
}
|
|
}
|
|
|
|
public function get_bill_details($id)
|
|
{
|
|
$bill = HospitalBill::find($id);
|
|
//dd($bill);
|
|
return response()->json($bill);
|
|
}
|
|
|
|
public function bills_inactive()
|
|
{
|
|
$bills = HospitalBill::onlyTrashed()->orderBy('created_at', 'asc')->paginate(50);
|
|
$banks = ChartOfAccount::where('type', 4)->get();
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
|
|
if (empty($bills)) {
|
|
flash()->error("There is no inactive bills");
|
|
return redirect('/payments/bills/');
|
|
} else {
|
|
return view('expenses::payments.bills.inactive', compact('bills', 'staff_members', 'suppliers', 'banks'));
|
|
}
|
|
}
|
|
|
|
public function bill_activate($id)
|
|
{
|
|
$bill = HospitalBill::withTrashed()->find($id);
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$accounts_payable_balance->balance - (int)$bill->total_amount]);
|
|
|
|
if ($bill->restore()) :
|
|
flash("Bill has been activated.")->success();
|
|
return redirect('/payments/bills/inactive');
|
|
endif;
|
|
}
|
|
|
|
public function inventory(Request $request) {
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$display = "";
|
|
$filters = [];
|
|
|
|
if ($request->dates == "yesterday") {
|
|
$start = Carbon::yesterday()->startOfDay();
|
|
$end = Carbon::yesterday()->endOfDay();
|
|
$display .= "Yesterday | ";
|
|
} elseif ($request->dates == "custom_date") {
|
|
$end = Carbon::parse($request->start_date)->endOfDay()->toDateTimeString();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$display .= "On: " . streamline_date($request->start_date) . " | ";
|
|
} elseif ($request->dates == "custom_date_range") {
|
|
$end = Carbon::parse($request->end_date)->endOfDay();
|
|
$start = Carbon::parse($request->start_date)->startOfDay();
|
|
$display .= "From: " . streamline_date($request->start_date) . " To: " . streamline_date($request->end_date) . " | ";
|
|
} else {
|
|
$start = Carbon::today()->startOfDay();
|
|
$end = Carbon::today()->endOfDay();
|
|
$display .= "Today | ";
|
|
}
|
|
|
|
if ($request->vendor != "ALL VENDORS") {
|
|
$filters[] = ['supplier_id', '=', $request->vendor];
|
|
$display .= "Vendor: " . get_name($request->vendor, 'id', 'name', 'suppliers') . " | ";
|
|
} else {
|
|
$display .= "All Vendors | ";
|
|
}
|
|
|
|
if ($request->staff_member != "ALL STAFF") {
|
|
$filters[] = ['created_by', '=', $request->staff_member];
|
|
$display .= "Staff: " . get_full_name($request->staff_member, 'id', 'first_name', 'last_name', 'users');
|
|
} else {
|
|
$display .= "All Staff";
|
|
}
|
|
|
|
$quotations = Quotation::whereBetween('receive_date', [$start, $end])->where($filters)
|
|
->where('receive_status', 1)
|
|
->where('invoice_generated', 0)
|
|
->groupBy('quotation_number')
|
|
->get();
|
|
|
|
return view('expenses::payments.inventory.index', compact('quotations', 'staff_members', 'suppliers', 'display'));
|
|
}
|
|
|
|
public function create_inventory_bill(Request $request)
|
|
{
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$chart_of_accounts = ChartOfAccount::get();
|
|
$quotation_type_options = QuotationType::pluck('name', 'id')->toArray();
|
|
$quotation_number = $request->quotation_number;
|
|
$quotations = Quotation::where('quotation_number', $quotation_number)->get();
|
|
$quotation = Quotation::where('quotation_number', $quotation_number)->first();
|
|
|
|
return view('expenses::payments.inventory.create', compact(
|
|
'quotations',
|
|
'quotation',
|
|
'quotation_type_options',
|
|
'quotation_number',
|
|
'suppliers',
|
|
'staff_members',
|
|
'chart_of_accounts'
|
|
));
|
|
}
|
|
|
|
public function custom_bills($vendor, $category)
|
|
{
|
|
$bills = [];
|
|
$request = new Request();
|
|
$banks = ChartOfAccount::where('type', 4)->get();
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$hospital_information = HospitalInformation::first();
|
|
$bank_accounts = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
|
|
$today = Carbon::now()->toDateString();
|
|
$today_minus_thirty = Carbon::now()->subDays(30)->toDateString();
|
|
$today_minus_sixty = Carbon::now()->subDays(60)->toDateString();
|
|
$today_minus_ninety = Carbon::now()->subDays(90)->toDateString();
|
|
|
|
$display = get_name($vendor, 'id', 'name', 'suppliers') . ' - UNPAID BILLS AND PAID BILLS (BUT WITH BALANCE) ';
|
|
|
|
$request->staff_member = "ALL STAFF";
|
|
$request->vendor = $vendor;
|
|
$request->bill_status = "UNPAID BILLS";
|
|
$request->dates = 'custom_date_range';
|
|
$request->start_date = $today;
|
|
$request->end_date = $today;
|
|
|
|
switch ($category) {
|
|
case 'current':
|
|
$display .= " for today, the " . streamline_date($today);
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor = {$vendor} AND bill_date = '{$today}' ORDER BY id ASC";
|
|
if ($vendor == 0) {
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor IS NULL AND bill_date = '{$today}' ORDER BY id ASC";
|
|
}
|
|
$bills = DB::select($bills_query);
|
|
break;
|
|
|
|
case 'one_to_thirty':
|
|
$request->start_date = $today_minus_thirty;
|
|
$display .= " from " . streamline_date($today_minus_thirty) . " to " . streamline_date($today);
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor = {$vendor} AND bill_date BETWEEN '{$today_minus_thirty}' AND '{$today}' ORDER BY id ASC";
|
|
if ($vendor == 0) {
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor IS NULL AND bill_date BETWEEN '{$today_minus_thirty}' AND '{$today}' ORDER BY id ASC";
|
|
}
|
|
$bills = DB::select($bills_query);
|
|
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);
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor = {$vendor} AND bill_date BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' ORDER BY id ASC";
|
|
if ($vendor == 0) {
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor IS NULL AND bill_date BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' ORDER BY id ASC";
|
|
}
|
|
$bills = DB::select($bills_query);
|
|
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);
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor = {$vendor} AND bill_date BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' ORDER BY id ASC";
|
|
if ($vendor == 0) {
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor IS NULL AND bill_date BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' ORDER BY id ASC";
|
|
}
|
|
$bills = DB::select($bills_query);
|
|
break;
|
|
|
|
case 'ninety_one_and_over':
|
|
$request->start_date = Carbon::now()->startOfYear()->toDateString();
|
|
$request->end_date = $today_minus_ninety;
|
|
$display .= " before " . streamline_date($today_minus_ninety);
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor = {$vendor} AND bill_date < '{$today_minus_ninety}' ORDER BY id ASC";
|
|
if ($vendor == 0) {
|
|
$bills_query = "SELECT * FROM hospital_bills WHERE deleted_at IS NULL AND (balance IS NULL OR balance > 0) AND vendor IS NULL AND bill_date < '{$today_minus_ninety}' ORDER BY id ASC";
|
|
}
|
|
$bills = DB::select($bills_query);
|
|
break;
|
|
}
|
|
|
|
return view('expenses::payments.bills.index', compact('bills', 'hospital_information', 'staff_members', 'suppliers', 'display', 'banks', 'request', 'bank_accounts'));
|
|
}
|
|
|
|
public function custom_bill($vendor, $category, $id)
|
|
{
|
|
$bills = [];
|
|
$request = new Request();
|
|
$banks = ChartOfAccount::where('type', 4)->get();
|
|
$suppliers = Supplier::get();
|
|
$staff_members = User::get();
|
|
$hospital_information = HospitalInformation::first();
|
|
$bank_accounts = ChartOfAccount::where('type', 4)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
|
|
$today = Carbon::now()->toDateString();
|
|
$today_minus_thirty = Carbon::now()->subDays(30)->toDateString();
|
|
$today_minus_sixty = Carbon::now()->subDays(60)->toDateString();
|
|
$today_minus_ninety = Carbon::now()->subDays(90)->toDateString();
|
|
|
|
$display = get_name($vendor, 'id', 'name', 'suppliers') . ' - UNPAID BILLS AND PAID BILLS (BUT WITH BALANCE) ';
|
|
|
|
$request->staff_member = "ALL STAFF";
|
|
$request->vendor = $vendor;
|
|
$request->bill_status = "UNPAID BILLS";
|
|
$request->dates = 'custom_date_range';
|
|
$request->start_date = $today;
|
|
$request->end_date = $today;
|
|
|
|
if ($vendor == 0) {
|
|
$vendor = null;
|
|
}
|
|
|
|
$bills = DB::table('hospital_bills')->where('id', $id)->get()->toArray();
|
|
|
|
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::now()->startOfYear()->toDateString();
|
|
$request->end_date = $today_minus_ninety;
|
|
$display .= " before " . streamline_date($today_minus_ninety);
|
|
break;
|
|
}
|
|
|
|
return view('expenses::payments.bills.index', compact('bills', 'hospital_information', 'staff_members', 'suppliers', 'display', 'banks', 'request', 'bank_accounts'));
|
|
}
|
|
|
|
public function undo_bill_payment($transaction_id) {
|
|
$payment = Payment::where('transaction_id', $transaction_id)->get();
|
|
|
|
if (isset($payment[0])) {
|
|
$amount = $payment[0]->amount;
|
|
$bill_id = $payment[0]->bill_id;
|
|
$account_id = $payment[0]->account_id;
|
|
|
|
if (Payment::where('transaction_id', $transaction_id)->delete()) {
|
|
$bill_update = HospitalBill::find($bill_id);
|
|
$new_bill_balance = $bill_update->balance + $amount;
|
|
$bill_update->balance = $new_bill_balance;
|
|
$bill_update->payment_status = 0;
|
|
$bill_update->updated_by = Auth::id();
|
|
$bill_update->save();
|
|
|
|
// update general payables balance
|
|
$accounts_payable_balance = ChartOfAccount::find(14);
|
|
ChartOfAccount::where('id', 14)->update(['balance' => (int)$accounts_payable_balance->balance + $amount]);
|
|
|
|
// Delete related bank entry
|
|
$bank_record = Banking::where('bank', $account_id)
|
|
->where('trans_id', $transaction_id)
|
|
->where('trans_type', 'PAYMENT')
|
|
->where('debit', $amount)
|
|
|
|
->first();
|
|
|
|
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($bank_record->bank, $bank_record->trans_date);
|
|
$account_balance_on_expense_date = ($account_balance_record_on_expense_date != null) ? (int)$account_balance_record_on_expense_date->account_balance : 0;
|
|
$account_balance_on_expense_date_after_payment_reversal = ($account_balance_on_expense_date + $amount);
|
|
|
|
$bank_record->debit = 0;
|
|
$bank_record->account_balance = $account_balance_on_expense_date_after_payment_reversal;
|
|
$bank_record->updated_by = Auth::id();
|
|
$bank_record->update();
|
|
flash("Bank Payment record: " . $bank_record->id . " updated!");
|
|
|
|
// use the bank record before the one just deleted
|
|
$previous_bank_record = Banking::where('bank', $bank_record->bank)
|
|
->whereNull('deleted_at')
|
|
->where('id', '<', $bank_record->id)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
|
|
if ($previous_bank_record) {
|
|
update_banking_record_balances(Carbon::parse($previous_bank_record->trans_date)->toDateString(), $bank_record->bank, $previous_bank_record->id, $previous_bank_record->account_balance);
|
|
} else {
|
|
update_banking_record_balances(Carbon::parse($bank_record->trans_date)->toDateString(), $bank_record->bank, $bank_record->id, $account_balance_on_expense_date_after_payment_reversal);
|
|
}
|
|
flash("Payment has been deleted successfully from Bank: " . get_name($bank_record->bank, 'id', 'name', 'chart_of_accounts') . " and the bill balance has been update to " . ugandan_shillings($new_bill_balance))->success();
|
|
return 'success';
|
|
} else {
|
|
return 'fail';
|
|
}
|
|
} else {
|
|
return 'fail';
|
|
}
|
|
}
|
|
}
|