Files
streamline-emr/docker/streamline-src/Modules/PatientDiscounts/Http/Controllers/FamilyAccountsController.php
T

1018 lines
50 KiB
PHP
Executable File

<?php
namespace Modules\PatientDiscounts\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Modules\PatientFinance\Http\Controllers\PatientFinanceController;
use Streamline\Models\FamilyAccount;
use Streamline\Models\FamilyAccountDeposit;
use Streamline\Models\FamilyAccountDepositCancellation;
use Streamline\Models\FamilyAccountsRefund;
use Streamline\Models\HospitalInformation;
use Streamline\Models\Patient;
use Streamline\Models\FamilyAccountConsumption;
use Carbon\Carbon;
use Streamline\Models\ChartOfAccount;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Streamline\Models\PatientPaymentMethod;
use Streamline\Models\PaymentMethodsTransaction;
use Streamline\Models\TrackReceipt;
use Streamline\Models\User;
class FamilyAccountsController extends Controller
{
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:view-family-account', ['only' => ['index']]);
$this->middleware('permission:create-family-account', ['only' => ['create','store']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$family_accounts = FamilyAccount::orderBy('id','desc')->get();
$reg_date = null; $start_date = null; $end_date = null;
$family_accounts_array = [];
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = ['' => '- select -'] + $family_accounts_array;
return view('patient_discounts::family_accounts.index',compact('family_accounts', 'reg_date', 'start_date', 'end_date', 'family_accounts_array'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$patients = [];
$chart_of_accounts = ChartOfAccount::where('type', 5)->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
return view('patient_discounts::family_accounts.create',compact('patients', 'chart_of_accounts'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'family_head' => 'required || unique:family_accounts,family_head_id',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$family_account = new FamilyAccount;
$family_account->family_head_id = $request->family_head;
$head_id = $request->family_head;
$members_array = is_array($request->family_members) ? $request->family_members : [];
if (!in_array($head_id, $members_array)) {
array_push($members_array, $head_id);
}
$members_array = $members_array[0] == "" ? array_shift($members_array) : $members_array;
$family_account->family_members_ids = implode(",", $members_array);
$family_account->current_balance = $request->deposit_amount;
$family_account->credit_limit = ($request->credit_limit) != 0 ? $request->credit_limit : null;
if ($request->is_it_opening_family_amount == 1) {
$family_account->opening_account_id = $request->opening_account_id;
$family_account->opening_amount = $request->deposit_amount;
$family_account->opening_amount_date = is_null($request->opening_amount_date) ? Carbon::now(): $request->opening_amount_date;
record_opening_balance_for_family_account($request->family_head, $family_account->opening_account_id, $request->deposit_amount, $family_account->opening_amount_date);
if (!is_null($request->opening_amount_date) && Carbon::now()->isAfter(Carbon::parse($request->opening_amount_date))) {
//today is greater or after than the opening amount date so make the created_at set to opening amount date
$family_account->created_at = $request->opening_amount_date;
}
}
$family_account->created_by = Auth::id();
$family_account->save();
/* update the family deposits table with the deposit if it has been filled */
if ($request->deposit_amount != 0) {
$family_deposit = new FamilyAccountDeposit;
$family_deposit->family_account_id = $family_account->id;
$family_deposit->deposit_amount = $request->deposit_amount;
$family_deposit->deposited_by = $request->deposited_by;
$family_deposit->is_opening_amount = ($request->is_it_opening_family_amount == 1) ? 1 : 0;
$family_deposit->deposit_date = ($request->is_it_opening_family_amount == 1 && !is_null($request->opening_amount_date)) ? $request->opening_amount_date : Carbon::now();
$family_deposit->created_by = Auth::id();
$family_deposit->save();
if ($request->is_it_opening_family_amount != 1) {
record_cash_credits_to_daily_collection_account($request->deposit_amount, generateReceiptNumberFromDB(), "Family Account Deposits");
}
}
flash('New family account has been created')->success();
return redirect('family_accounts');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
*/
public function edit($id) {
$family_account = FamilyAccount::find($id);
$family_head_id = $family_account->family_head_id;
$family_members_array = explode(",", $family_account->family_members_ids);
$current_balance = $family_account->current_balance;
$family_head_name = get_full_name($family_head_id, 'id', 'first_name', 'last_name', 'patients');
$chart_of_accounts = ChartOfAccount::where('type', 5)->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
$opening_deposit_record = FamilyAccountDeposit::where(['family_account_id' => $id, 'is_opening_amount' => 1])->first();
return view('patient_discounts::family_accounts.edit',compact('family_head_name', 'family_account','family_head_id','family_members_array','current_balance','chart_of_accounts', 'opening_deposit_record'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$family_account = FamilyAccount::find($id);
$family_account->family_head_id = $request->family_head;
$members_array = $request->family_members;
$family_account->family_members_ids = implode(",", $members_array);
//adjust current balance based on edited opening amount
$old_opening_amount = is_null($family_account->opening_amount) ? 0 : $family_account->opening_amount;
$difference_in_opening_amounts = $request->opening_amount - $old_opening_amount;
$family_account->current_balance = $family_account->current_balance + $difference_in_opening_amounts;
$family_account->updated_by = Auth::id();
$family_account->credit_limit = ($request->credit_limit != 0) ? $request->credit_limit : null;
if ($request->is_it_opening_family_amount == 1) {
$family_account->opening_account_id = $request->opening_account_id;
$family_account->opening_amount = $request->opening_amount;
$family_account->opening_amount_date = is_null($request->opening_amount_date) ? \Carbon\Carbon::now(): $request->opening_amount_date;
update_opening_balance_equity_record_for_family_account($request->family_head, $family_account->opening_account_id, $request->opening_amount, $family_account->opening_amount_date);
if (!is_null($request->opening_amount_date) && Carbon::now()->isAfter(Carbon::parse($request->opening_amount_date))) {
//today is greater or after than the opening amount date so make the created_at set to opening amount date
$family_account->created_at = $request->opening_amount_date;
}
}
$family_account->update();
/* update the family deposits table with the deposit if it has been filled */
$opening_deposit_record = FamilyAccountDeposit::where(['family_account_id' => $id, 'is_opening_amount' => 1])->first();
if ($request->opening_amount != 0) {
$family_deposit = is_null($opening_deposit_record) ? new FamilyAccountDeposit : $opening_deposit_record;
$family_deposit->family_account_id = $family_account->id;
$family_deposit->deposit_amount = $request->opening_amount;
$family_deposit->deposited_by = $request->deposited_by;
$family_deposit->is_opening_amount = ($request->is_it_opening_family_amount == 1) ? 1 : 0;
$family_deposit->deposit_date = ($request->is_it_opening_family_amount == 1 && !is_null($request->opening_amount_date)) ? $request->opening_amount_date : Carbon::now();
$family_deposit->created_by = Auth::id();
$family_deposit->save();
}
flash('Family account has been updated')->success();
return redirect('family_accounts');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if (FamilyAccount::destroy($id)) {
flash("Family account has been deleted.")->success();
return redirect('family_accounts');
} else {
flash("Family account failed to be deleted.")->error();
return redirect('family_accounts');
}
}
/**
* Display a listing of the inactive Family Accounts.
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$family_accounts = FamilyAccount::onlyTrashed()
->orderBy('family_head_id', 'asc')
->paginate(50);
if (count($family_accounts) < 1) {
flash()->error("There is no inactive Family Accounts");
return redirect('family_accounts');
} else {
return view('patient_discounts::family_accounts.inactive_family_accounts', compact('family_accounts'));
}
}
/*
* display create deposit blade from the family
*/
public function add_family_deposit(Request $request) {
$family_account_id = $request->family_account_id;
$family_account_details = FamilyAccount::find($family_account_id);
$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('patient_discounts::family_accounts.add_family_deposit',compact('family_account_details', 'patient_payment_methods_options'));
}
/*
* store a new family deposit
*/
public function store_family_deposit(Request $request) {
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = 'Procedures';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
//add this deposit to the chart of accounts sub account account
$new_family_deposit = new FamilyAccountDeposit;
$new_family_deposit->family_account_id = $request->family_account_id;
$new_family_deposit->deposit_amount = $request->deposit_amount;
$new_family_deposit->deposit_date = Carbon::createFromFormat('d-m-Y', $request->deposit_date)->toDateString();
$new_family_deposit->deposited_by = $request->deposited_by;
$new_family_deposit->receipt_number = $receipt_number;
$new_family_deposit->created_by = Auth::id();
if ($new_family_deposit->save()) {
$family_account_to_update = FamilyAccount::find($request->family_account_id);
$family_account_to_update->current_balance = $family_account_to_update->current_balance + (int)$request->deposit_amount;
$family_account_to_update->update();
//increase the balance on chart of accounts called "Family deposits"
$family_account_deposits_id = get_name("family_account_deposits", "slug", "id", "chart_of_accounts");
if (is_numeric($family_account_deposits_id)) {
//increase the amount of "family" chart of accounts
$family_chart_of_account = ChartOfAccount::find($family_account_deposits_id);
$family_chart_of_account->balance = $family_chart_of_account->balance + (int)$request->deposit_amount;
$family_chart_of_account->update();
} else {
flash('A chart of accounts is missing. Please contact Stre@mline support immediately!')->error();
}
record_cash_credits_to_daily_collection_account($request->deposit_amount, $receipt_number, "Family Account Deposits");
}
$family_account_details = FamilyAccount::find($request->family_account_id);
$family_members_array = explode(",", $family_account_details->family_members_ids);
//create a receipt numbers from the id of the family account deposits table
$receipt_date = date('Y-m-d h:i:s');
$return_payment_methods = PatientFinanceController::register_payment_method($family_account_details->family_head_id, 0, $request->original_cash_to_pay, $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), $receipt_number, 14, Auth::id(), 0);
flash('Family account deposit has been updated')->success();
if (is_cashier_receipt_type_print_html()) {
return view('patient_discounts::family_accounts.deposit_receipt',compact('new_family_deposit', 'receipt_number','receipt_date','family_members_array', 'return_payment_methods'));
} else {
// so we first have to set a session and then go back to the payment page
// on the payment page we can then set a JS variable that can help redirect us to our pdf print
session()->put('print_family_account_receipt_pdf', 1);
// since we want to reduce the amount of duplicate code, we shall send all items to one file for printing
$data = [
"new_family_deposit" => $new_family_deposit, "receipt_number" => $receipt_number, "return_payment_methods" => $return_payment_methods,
"receipt_date" => $receipt_date, "family_members_array" => $family_members_array
];
session()->put('print_family_account_receipt_pdf_details', $data);
return redirect('/family_accounts/' . $request->family_account_id . '/statement/');
}
}
public function print_family_account_receipt_pdf_details() {
$data = session()->get("print_family_account_receipt_pdf_details");
// add check for when the people try to reload the page
if (!$data) {
return redirect('/home');
}
$data['hospital_information'] = HospitalInformation::first();
// lest i forget Thy love for me
session()->forget('print_family_account_receipt_pdf');
session()->forget('print_family_account_receipt_pdf_details');
$pdf = SnappyPDF::loadView("patient_discounts::family_accounts.print_family_account_receipt_pdf_details", $data)
->setOrientation('portrait')
->setPaper('a4')
->setOption('margin-bottom', 5)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>&copy; ' . date('Y') . ' Stre@mline</i>');
return $pdf->inline('Family Accounts Receipt' . date(" d-m-y h:ia") . '.pdf');
}
/*
* family accounts consumption report
*/
public function family_accounts_consumption_report(Request $request) {
$family_accounts_array = [];
$search_text = "";
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = [0 => 'All Families'] + $family_accounts_array;
$staff_members = User::pluck('username', 'id')->toArray();
$staff_members = [0 => 'All Staff'] + $staff_members;
$filters = [];
if ($request->search_by == 1) {
$end = Carbon::yesterday()->endOfDay();
$start = Carbon::yesterday()->startOfDay();
$search_text .= "Yesterday | ";
} else if ($request->search_by == 2) {
$end = Carbon::parse($request->start_date)->endOfDay();
$start = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "On: " . streamline_date($request->start_date) . " | ";
} else if ($request->search_by == 3) {
$end = Carbon::parse($request->end_date)->endOfDay();
$start = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "From: " . streamline_date($request->start_date) . " to " . streamline_date($request->end_date) . " | ";
} else {
$end = Carbon::today()->endOfDay();
$start = Carbon::today()->startOfDay();
$search_text .= "Today | ";
}
$family_account_id = $request->family_account_id;
if (!is_null($family_account_id) && $family_account_id != 0) {
$filters[] = ['family_account_consumptions.family_account_id', '=', $family_account_id];
$search_text .= "Family Of: " . get_full_name($family_account_id, 'id', 'first_name', 'last_name', 'patients') . " | ";
} else {
$search_text .= "Family Of: All | ";
}
$staff_member_id = $request->staff_member;
if (!is_null($staff_member_id) && $staff_member_id != 0) {
$filters[] = ['family_account_consumptions.created_by', '=', $staff_member_id];
$search_text .= "Staff Member: " . get_full_name($staff_member_id, 'id', 'first_name', 'last_name', 'users');
} else {
$search_text .= "Staff Member: All";
}
$family_consumptions = FamilyAccountConsumption::whereBetween('family_account_consumptions.created_at', [$start, $end])
->where($filters)
->leftJoin('patients', 'family_account_consumptions.patient_id', '=', 'patients.id')
->leftJoin('users', 'family_account_consumptions.created_by', '=', 'users.id')
->whereNotIn('patient_id', findTestOrDemoPatients())
->select('family_account_consumptions.*', 'patients.first_name', 'patients.last_name', 'patients.number', 'users.first_name as user_first_name', 'users.last_name as user_last_name')
->get();
return view('patient_discounts::family_accounts.family_account_consumption_report',compact('family_consumptions','family_accounts_array', 'staff_members', 'search_text'));
}
/* family accounts deposits report */
public function deposits_report(Request $request) {
$family_accounts_array = [];
$search_text = "";
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = [0 => 'All Families'] + $family_accounts_array;
$staff_members = User::pluck('username', 'id')->toArray();
$staff_members = [0 => 'All Staff'] + $staff_members;
$filters = [];
if ($request->search_by == 1) {
$end = Carbon::yesterday()->endOfDay();
$start = Carbon::yesterday()->startOfDay();
$search_text .= "Yesterday | ";
} else if ($request->search_by == 2) {
$end = Carbon::parse($request->start_date)->endOfDay();
$start = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "On: " . streamline_date($request->start_date) . " | ";
} else if ($request->search_by == 3) {
$end = Carbon::parse($request->end_date)->endOfDay();
$start = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "From: " . streamline_date($request->start_date) . " to " . streamline_date($request->end_date) . " | ";
} else {
$end = Carbon::today()->endOfDay();
$start = Carbon::today()->startOfDay();
$search_text .= "Today | ";
}
$family_account_id = $request->family_account_id;
if (!is_null($family_account_id) && $family_account_id != 0) {
$filters[] = ['family_account_deposits.family_account_id', '=', $family_account_id];
$search_text .= "Family Of: " . get_full_name($family_account_id, 'id', 'first_name', 'last_name', 'patients') . " | ";
} else {
$search_text .= "Family Of: All | ";
}
$staff_member_id = $request->staff_member;
if (!is_null($staff_member_id) && $staff_member_id != 0) {
$filters[] = ['family_account_deposits.created_by', '=', $staff_member_id];
$search_text .= "Staff Member: " . get_full_name($staff_member_id, 'id', 'first_name', 'last_name', 'users');
} else {
$search_text .= "Staff Member: All";
}
$family_deposits = FamilyAccountDeposit::whereBetween('family_account_deposits.created_at', [$start, $end])
->where($filters)
->where('family_account_deposits.is_opening_amount', 0)
->leftJoin('users', 'family_account_deposits.created_by', '=', 'users.id')
->select('family_account_deposits.*', 'users.first_name as user_first_name', 'users.last_name as user_last_name')
->get();
return view('patient_discounts::family_accounts.deposit_report',compact('family_deposits','search_text','staff_members','family_accounts_array'));
}
public function family_accounts_search(Request $request)
{
$search_by = $request->search_by;
$reg_date = $request->reg_date;
$start_date = $request->start_date;
$end_date = $request->end_date;
$family_account_id = $request->family_account_id;
$family_accounts_array = [];
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = ['' => '- select -'] + $family_accounts_array;
if (!is_null($search_by)){
$filters = [];
if($search_by == "0"){
// last 24 hours
$last_day = Carbon::now()->subDay();
array_push($filters, ['created_at', '>', $last_day]);
} elseif($search_by == "1"){
// custom date
$start_date_search = Carbon::parse($reg_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($reg_date)->endOfDay()->toDateTimeString();
array_push($filters, ['created_at', '>', $start_date_search]);
array_push($filters, ['created_at', '<', $end_date_search]);
} elseif($search_by == "2"){
// custom date range
$start_date_search = Carbon::parse($start_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($end_date)->endOfDay()->toDateTimeString();
array_push($filters, ['created_at', '>', $start_date_search]);
array_push($filters, ['created_at', '<', $end_date_search]);
}
if (!is_null($family_account_id)) {
$family_accounts = FamilyAccount::where('id', $family_account_id)->where($filters)->get();
} else {
$family_accounts = FamilyAccount::where($filters)->get();
}
} else {
$family_accounts = FamilyAccount::orderBy('id','desc')->get();
}
return view('patient_discounts::family_accounts.index',compact('family_accounts','reg_date','start_date','end_date', 'family_accounts_array'));
}
public function search_family_account_name(Request $request) {
$data = [];
if ($request->has('q')) {
$search = $request->q;
$data = Patient::select("id", "first_name", "last_name", "number", "phone")
->where('first_name', 'LIKE', "%$search%")
->orWhere('last_name', 'LIKE', "%$search%")
->get();
}
return response()->json($data);
}
public function family_accounts_statements($family_account, Request $request) {
$family_account_id = $family_account;
$search_by = $request->search_by;
$reg_date = $request->reg_date;
$start_date = $request->start_date;
$end_date = $request->end_date;
if (!is_null($search_by)){
$filters = [];
if($search_by == "0"){
// last 24 hours
$last_day = Carbon::now()->subDay();
$filters[] = ['created_at', '>', $last_day];
} elseif($search_by == "1"){
// custom date
$start_date_search = Carbon::parse($reg_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($reg_date)->endOfDay()->toDateTimeString();
$filters[] = ['created_at', '>', $start_date_search];
$filters[] = ['created_at', '<', $end_date_search];
} elseif($search_by == "2"){
// custom date range
$start_date_search = Carbon::parse($start_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($end_date)->endOfDay()->toDateTimeString();
$filters[] = ['created_at', '>', $start_date_search];
$filters[] = ['created_at', '<', $end_date_search];
}
$family_consumptions = FamilyAccountConsumption::where($filters)->where('family_account_id', $family_account_id)->get();
$family_deposits = FamilyAccountDeposit::where($filters)->where('family_account_id', $family_account_id)->orderBy('created_at')->get();
$family_refunds = FamilyAccountsRefund::where($filters)->where('family_account_id', $family_account_id)->get();
} else {
$family_consumptions = FamilyAccountConsumption::where('family_account_id', $family_account_id)->get();
$family_deposits = FamilyAccountDeposit::where('family_account_id', $family_account_id)->orderBy('created_at')->get();
$family_refunds = FamilyAccountsRefund::where('family_account_id', $family_account_id)->get();
}
$family_account_details = FamilyAccount::withTrashed()->find($family_account_id);
return view('patient_discounts::family_accounts.statements',compact('family_consumptions','family_deposits','family_account_id','family_account_details','reg_date','start_date','end_date', 'family_refunds'));
}
public function family_statement_print(Request $request)
{
$family_account_id = $request->family_account_id;
$search_by = $request->search_by;
$reg_date = $request->reg_date;
$start_date = $request->start_date;
$end_date = $request->end_date;
$filters = [];
if(is_null($reg_date) && is_null($start_date) && is_null($end_date)){
// last 24 hours
$last_day = Carbon::now()->subDay();
array_push($filters, ['created_at', '>', $last_day]);
} elseif($reg_date){
// custom date
$start_date_search = Carbon::parse($reg_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($reg_date)->endOfDay()->toDateTimeString();
array_push($filters, ['created_at', '>', $start_date_search]);
array_push($filters, ['created_at', '<', $end_date_search]);
} elseif($start_date && $end_date){
// custom date range
$start_date_search = Carbon::parse($start_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($end_date)->endOfDay()->toDateTimeString();
array_push($filters, ['created_at', '>', $start_date_search]);
array_push($filters, ['created_at', '<', $end_date_search]);
}
$family_consumptions = FamilyAccountConsumption::where($filters)->where('family_account_id', $family_account_id)->get();
$family_deposits = FamilyAccountDeposit::where($filters)->where('family_account_id', $family_account_id)->get();
$family_refunds = FamilyAccountsRefund::where($filters)->where('family_account_id', $family_account_id)->get();
$family_account_details = FamilyAccount::withTrashed()->find($family_account_id);
if(is_null($reg_date) && is_null($start_date) && is_null($end_date)){
$family_consumptions = FamilyAccountConsumption::where('family_account_id', $family_account_id)->get();
$family_deposits = FamilyAccountDeposit::where('family_account_id', $family_account_id)->get();
$family_refunds = FamilyAccountsRefund::where('family_account_id', $family_account_id)->get();
}
$hospital_information = HospitalInformation::first();
$data = [
'family_consumptions' => $family_consumptions,
'family_deposits' => $family_deposits,
'family_account_details' => $family_account_details,
'family_refunds' => $family_refunds,
'hospitalInfo' => $hospital_information,
];
$pdf = SnappyPDF::loadView("patient_discounts::family_accounts/statements_print", $data)
->setOrientation('portrait')
->setOption('margin-bottom', 7)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>&copy; ' . date('Y') . ' Stre@mline</i>');
return $pdf->inline('Family Account Statement' . date(" d-m-y h:ia") . '.pdf');
}
public function cancel_deposit_reason(Request $request)
{
$deposit_id = $request->deposit_id;
$family_deposit = FamilyAccountDeposit::find($deposit_id);
$family_account_id = $family_deposit->family_account_id;
$family_account = FamilyAccount::withTrashed()->find($family_account_id);
return view('patient_discounts::family_accounts.cancel_family_account_deposit',compact('deposit_id','family_deposit','family_account_id','family_account'));
}
public function cancel_deposit(Request $request)
{
$deposit_id = $request->deposit_id;
$family_deposit = FamilyAccountDeposit::find($deposit_id);
$family_account_id = $family_deposit->family_account_id;
if ($family_deposit->delete()) {
//reduce the family's current balance by deleted amount
$family_account_to_update = FamilyAccount::find($family_account_id);
$family_account_to_update->current_balance = $family_account_to_update->current_balance - (int)$family_deposit->deposit_amount;
$family_account_to_update->update();
$family_account_deposits_id = get_name("family_account_deposits", "slug", "id", "chart_of_accounts");
if (is_numeric($family_account_deposits_id)) {
// decrease the balance on chart of accounts called "family account deposit"
$chart_of_account = ChartOfAccount::find($family_account_deposits_id);
$chart_of_account->balance = $chart_of_account->balance - (int)$family_deposit->deposit_amount;
$chart_of_account->update();
} else {
flash('A chart of accounts is missing. Please contact Stre@mline support immediately!')->error();
}
// delete the patient payment methods
DB::table('payment_methods_transactions')
->where('receipt_number', $family_deposit->receipt_number)
->delete();
record_cash_debits_to_daily_collection_account($family_deposit->deposit_amount, generateReceiptNumberFromDB(), 'Cancel Family Account Deposit');
//record the cancellation in respective table
$this->record_family_deposit_cancellation($family_account_id, $deposit_id, $request->reason);
}
flash("Family account deposit has been cancelled")->success();
return redirect("family_accounts_deposits_report");
}
public function record_family_deposit_cancellation($family_account_id, $deposit_id, $reason)
{
$family_deposit_cancellation = new FamilyAccountDepositCancellation;
$family_deposit_cancellation->family_account_id = $family_account_id;
$family_deposit_cancellation->family_account_deposit_id = $deposit_id;
$family_deposit_cancellation->cancelled_by = Auth::id();
$family_deposit_cancellation->reason = $reason;
$family_deposit_cancellation->save();
}
public function refund($family_account_id) {
$family_account_details = FamilyAccount::find($family_account_id);
$banks = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 4)->pluck('name', 'id')->prepend('--select--', '');
if ($family_account_details->current_balance < 1) {
flash("Family account refund can not be completed because balance is less than refund limit")->error();
return redirect('/family_accounts/' . $family_account_id . '/statement/');
} else {
return view('patient_discounts::family_accounts.refund',compact('family_account_details', 'banks'));
}
}
public function store_family_refund(Request $request) {
$family_account_id = $request->family_account_id;
$current_balance = $request->current_balance;
$refund_amount = $request->refund_amount;
$refund_date = $request->refund_date;
$refunded_to = $request->refunded_to;
$account_id = $request->account_id;
$account_balance = $request->account_balance;
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = 'Procedures';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
if (($current_balance < $refund_amount) || ($refund_amount < 1) || ($account_balance < $refund_amount)) {
flash("Please enter a valid refund amount")->error();
return redirect('/family_accounts/refund/' . $family_account_id);
}
//increase the family's current balance by deleted amount
$family_account_to_update = FamilyAccount::find($family_account_id);
$family_account_to_update->current_balance = (int)$current_balance + (int)$refund_amount;
$family_account_to_update->update();
// decrease the balance on select account
$chart_of_account = ChartOfAccount::find($account_id);
$chart_of_account->balance = $chart_of_account->balance - (int)$refund_amount;
$chart_of_account->update();
$account_balance_record_on_expense_date = get_latest_banking_record_based_on_transaction_date($account_id, date('Y-m-d', strtotime($refund_date)));
$account_balance_on_expense_date_after_payment = ($account_balance_record_on_expense_date ? (int)$account_balance_record_on_expense_date->account_balance : 0) - $refund_amount;
$last_insert_id = capture_bank_record('Family Account Refunds', date('Y-m-d', strtotime($refund_date)), $account_id, 0,
$account_balance_on_expense_date_after_payment, 0, $refund_amount, 'Family Account Refunds', $receipt_number);
update_banking_record_balances(date('Y-m-d', strtotime($refund_date)), $account_id, $last_insert_id, $account_balance_on_expense_date_after_payment);
$family_account_deposits_id = get_name("family_account_deposits", "slug", "id", "chart_of_accounts");
if (is_numeric($family_account_deposits_id)) {
// decrease the balance on chart of accounts called "family account deposit"
$chart_of_account = ChartOfAccount::find($family_account_deposits_id);
$chart_of_account->balance = $chart_of_account->balance - (int)$refund_amount;
$chart_of_account->update();
} else {
flash('A chart of accounts is missing. Please contact Stre@mline support immediately!')->error();
}
$refund = new FamilyAccountsRefund();
$refund->family_account_id = $family_account_id;
$refund->refund_amount = $refund_amount;
$refund->refund_date = Carbon::createFromFormat('d-m-Y', $refund_date)->toDateString();
$refund->refund_to = $refunded_to;
$refund->created_by = Auth::id();
$refund->save();
$family_members_array = explode(",", $family_account_to_update->family_members_ids);
if (is_cashier_receipt_type_print_html()) {
return view('patient_discounts::family_accounts.refund_receipt',compact('refund_amount', 'receipt_number', 'refund_date', 'family_members_array'));
} else {
// so we first have to set a session and then go back to the payment page
// on the payment page we can then set a JS variable that can help redirect us to our pdf print
session()->put('print_family_account_refund_receipt_pdf', 1);
// since we want to reduce the amount of duplicate code, we shall send all items to one file for printing
$data = [
"refund_amount" => $refund_amount, "receipt_number" => $receipt_number,
"refund_date" => $refund_date, "family_members_array" => $family_members_array
];
session()->put('print_family_account_refund_receipt_pdf_details', $data);
return redirect('/family_accounts/' . $request->family_account_id . '/statement/');
}
}
public function print_family_account_refund_receipt_pdf_details() {
$data = session()->get("print_family_account_refund_receipt_pdf_details");
// add check for when the people try to reload the page
if (!$data) {
return redirect('/home');
}
$data['hospital_information'] = HospitalInformation::first();
// lest i forget Thy love for me
session()->forget('print_family_account_refund_receipt_pdf');
session()->forget('print_family_account_refund_receipt_pdf_details');
$pdf = SnappyPDF::loadView("patient_discounts::family_accounts.print_family_account_refund_receipt_pdf_details", $data)
->setOrientation('portrait')
->setPaper('a4')
->setOption('margin-bottom', 5)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>&copy; ' . date('Y') . ' Stre@mline</i>');
return $pdf->inline('Family Accounts Refund Receipt' . date(" d-m-y h:ia") . '.pdf');
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$family_accounts= FamilyAccount::withTrashed()->find($id);
if($family_accounts->restore()){
flash("Family Account has been activated.")->success();
return redirect('family_accounts');
}
}
public function get_family_account_balance($id)
{
$family_account = FamilyAccount::find($id);
if ($family_account) {
return $family_account->current_balance;
}
return 0;
}
public function custom_family_accounts($category)
{
$family_accounts = [];
$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();
$reg_date = $today;
$start_date = $today;
$end_date = $today;
$family_accounts_array = [];
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = ['' => '- select -'] + $family_accounts_array;
switch ($category) {
case 'current':
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND created_at >= '{$today}' ORDER BY id ASC";
$family_accounts = DB::select($family_accounts_query);
break;
case 'one_to_thirty':
$reg_date = null;
$start_date = $today_minus_thirty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND created_at BETWEEN '{$today_minus_thirty}' AND '{$today}' ORDER BY id ASC";
$family_accounts = DB::select($family_accounts_query);
break;
case 'thirty_one_to_sixty':
$reg_date = null;
$start_date = $today_minus_sixty;
$end_date = $today_minus_thirty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND created_at BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' ORDER BY id ASC";
$family_accounts = DB::select($family_accounts_query);
break;
case 'sixty_one_to_ninety':
$reg_date = null;
$start_date = $today_minus_ninety;
$end_date = $today_minus_sixty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND created_at BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' ORDER BY id ASC";
$family_accounts = DB::select($family_accounts_query);
break;
case 'ninety_one_and_over':
$reg_date = null;
$start_date = null;
$end_date = $today_minus_ninety;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND created_at < '{$today_minus_ninety}' ORDER BY id ASC";
$family_accounts = DB::select($family_accounts_query);
break;
}
return view('patient_discounts::family_accounts.index', compact('family_accounts','reg_date','start_date','end_date', 'family_accounts_array'));
}
public function custom_family_account($patient_id, $category)
{
$family_accounts = [];
$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();
$reg_date = $today;
$start_date = $today;
$end_date = $today;
$family_accounts_array = [];
$family_accounts_records = FamilyAccount::orderBy('id','desc')->get();
foreach ($family_accounts_records as $record) {
$family_accounts_array[$record->id] = get_full_name($record->family_head_id, 'id', 'first_name', 'last_name', 'patients');
}
$family_accounts_array = ['' => '- select -'] + $family_accounts_array;
switch ($category) {
case 'current':
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id = {$patient_id} AND created_at >= '{$today}' ORDER BY id ASC";
if ($patient_id == 0) {
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id IS NULL AND created_at >= '{$today}' ORDER BY id ASC";
}
$family_accounts = DB::select($family_accounts_query);
break;
case 'one_to_thirty':
$reg_date = null;
$start_date = $today_minus_thirty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id = {$patient_id} AND created_at BETWEEN '{$today_minus_thirty}' AND '{$today}' ORDER BY id ASC";
if ($patient_id == 0) {
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id IS NULL AND created_at BETWEEN '{$today_minus_thirty}' AND '{$today}' ORDER BY id ASC";
}
$family_accounts = DB::select($family_accounts_query);
break;
case 'thirty_one_to_sixty':
$reg_date = null;
$start_date = $today_minus_sixty;
$end_date = $today_minus_thirty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id = {$patient_id} AND created_at BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' ORDER BY id ASC";
if ($patient_id == 0) {
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id IS NULL AND created_at BETWEEN '{$today_minus_sixty}' AND '{$today_minus_thirty}' ORDER BY id ASC";
}
$family_accounts = DB::select($family_accounts_query);
break;
case 'sixty_one_to_ninety':
$reg_date = null;
$start_date = $today_minus_ninety;
$end_date = $today_minus_sixty;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id = {$patient_id} AND created_at BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' ORDER BY id ASC";
if ($patient_id == 0) {
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id IS NULL AND created_at BETWEEN '{$today_minus_ninety}' AND '{$today_minus_sixty}' ORDER BY id ASC";
}
$family_accounts = DB::select($family_accounts_query);
break;
case 'ninety_one_and_over':
$reg_date = null;
$start_date = null;
$end_date = $today_minus_ninety;
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id = {$patient_id} AND created_at < '{$today_minus_ninety}' ORDER BY id ASC";
if ($patient_id == 0) {
$family_accounts_query = "SELECT * FROM family_accounts WHERE deleted_at IS NULL AND current_balance < 0 AND family_head_id IS NULL AND created_at < '{$today_minus_ninety}' ORDER BY id ASC";
}
$family_accounts = DB::select($family_accounts_query);
break;
}
return view('patient_discounts::family_accounts.index', compact('family_accounts','reg_date','start_date','end_date', 'family_accounts_array'));
}
public function family_deposit_reprint($id){
$new_family_deposit = FamilyAccountDeposit::find($id);
$family_account_details = FamilyAccount::find($new_family_deposit->family_account_id);
$family_members_array = explode(",", $family_account_details->family_members_ids);
$receipt_number = $new_family_deposit->receipt_number;
$receipt_date = TrackReceipt::find($receipt_number);
$receipt_date = $receipt_date ?? date('Y-m-d h:i:s');
$return_payment_methods_record = PaymentMethodsTransaction::where(['tag_id' => 14, 'patient_id' => $family_account_details->family_head_id, 'amount' => $new_family_deposit->deposit_amount, 'receipt_number' => $receipt_number])->first();
if ($return_payment_methods_record) {
$return_payment_methods[$return_payment_methods_record->payment_method_id] = $return_payment_methods_record->amount;
} else{
$return_payment_methods = [];
}
if (is_cashier_receipt_type_print_html()) {
return view('patient_discounts::family_accounts.deposit_receipt',compact('new_family_deposit', 'receipt_number','receipt_date','family_members_array', 'return_payment_methods'));
} else {
// so we first have to set a session and then go back to the payment page
// on the payment page we can then set a JS variable that can help redirect us to our pdf print
session()->put('print_family_account_receipt_pdf', 1);
// since we want to reduce the amount of duplicate code, we shall send all items to one file for printing
$data = [
"new_family_deposit" => $new_family_deposit, "receipt_number" => $receipt_number, "return_payment_methods" => $return_payment_methods,
"receipt_date" => $receipt_date, "family_members_array" => $family_members_array
];
session()->put('print_family_account_receipt_pdf_details', $data);
return redirect('/family_accounts/' . $request->family_account_id . '/statement/');
}
}
}