Files
streamline-emr/docker/statistics/Modules/ClinicalData/Http/Controllers/ChartOfAccountController.php
T
2025-03-31 03:46:05 +03:00

298 lines
11 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Streamline\Models\AccountType;
use Streamline\Models\Banking;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\ChartOfAccountSlug;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
use Streamline\Models\TrackReceipt;
class ChartOfAccountController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:chart-of-accounts-list', ['only' => ['index']]);
$this->middleware('permission:create-chart-of-accounts', ['only' => ['create','store','edit','update']]);
$this->middleware('permission:view-chart-of-account-details', ['only' => ['show']]);
$this->middleware('permission:activate-chart-of-accounts', ['only' => ['activate']]);
$this->middleware('permission:de-activate-chart-of-accounts', ['only' => ['inactive']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->get();
$chart_of_accounts_list = ChartOfAccount::orderBy('name', 'asc')
->pluck('name', 'id');
$account_types = DB::table('account_types')
->orderBy('name', 'asc')
->pluck('name', 'id');
return view('clinical_data::chart_of_accounts.index', compact('chart_of_accounts', 'chart_of_accounts_list', 'account_types'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$chart_of_accounts_list = ChartOfAccount::orderBy('name', 'asc')
->pluck('name', 'id')
->toArray();
$chart_of_accounts_list = ['' => '- select -'] + $chart_of_accounts_list;
$account_types = AccountType::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$coa_slugs = ChartOfAccountSlug::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$slugs = [];
$slugs = [" " => "- Select -"];
foreach ($coa_slugs as $key => $value) {
$slug_item = str_replace("_", " ", $value);
$slugs[$value] = $slug_item;
}
return view('clinical_data::chart_of_accounts.create', compact('chart_of_accounts_list', 'account_types', 'slugs'));
}
/**
* 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(), [
'name' => 'required'
]);
if ($validator->fails()) {
//validation failed
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
//validation passed
$chart_of_account = new ChartOfAccount;
$logged_in_user_id = Auth::user()->id;
$chart_of_account->name = $request->name;
$chart_of_account->type = $request->type;
$chart_of_account->description = $request->description;
$chart_of_account->sub_account_of = $request->sub_account_of;
$chart_of_account->balance = $request->balance ? $request->balance : 0;
$chart_of_account->slug = str_replace(" ", "_", strtolower($chart_of_account->name));
$chart_of_account->core = $request->core;
$chart_of_account->created_by = $logged_in_user_id;
$chart_of_account->updated_by = $logged_in_user_id;
try {
$chart_of_account->save();
$trans_id = generateReceiptNumberFromDB('Initial Bank Deposit');
if($request->type == '4'){
capture_bank_record('DEPOSIT', Carbon::parse($request->opening_balance_date)->toDateTimeString(), $chart_of_account->id, 'Initial Deposit', ($request->opening_balance ? $request->opening_balance : 0),
($request->opening_balance ? $request->opening_balance : 0), 0, 'Initial Deposit', $trans_id);
/* capture_bank_record('DEPOSIT', Carbon::parse($current_balance_date=0)->toDateTimeString(), $chart_of_account->id, 'Current Balance', ($request->opening_balance ? $request->opening_balance : 0),
($request->opening_balance ? $request->opening_balance : 0), 0, 'Current Balance', $trans_id_2); */
}
flash($request->name . " Chart of Account has been saved")->success();
return redirect("/chart_of_accounts/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$chart_of_account = ChartOfAccount::where('id', $id)->first();
$chart_of_accounts_list = ChartOfAccount::orderBy('name', 'asc')
->pluck('name', 'id')
->toArray();
$chart_of_accounts_list = ['' => '- select -'] + $chart_of_accounts_list;
$account_types = AccountType::orderBy('name', 'asc')
->pluck('name', 'id')
->toArray();
$account_types = ['' => '- select -'] + $account_types;
$coa_slugs = ChartOfAccountSlug::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$slugs = [" " => "- Select -"];
foreach ($coa_slugs as $key => $value) {
$slug_item = str_replace("_", " ", $value);
$slugs[$value] = $slug_item;
}
if (!$chart_of_account) {
flash()->error("Chart of Account not found");
return redirect('/chart_of_accounts/');
} else {
return view('clinical_data::chart_of_accounts.edit', compact('chart_of_account', 'chart_of_accounts_list', 'account_types', 'slugs'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$validator = Validator::make($request->all(), [
'name' => 'required'
]);
if ($validator->fails()) {
//validation failed
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
//validation passed
$chart_of_account = ChartOfAccount::find($id);
$logged_in_user_id = Auth::user()->id;
$chart_of_account->name = $request->name;
$chart_of_account->type = $request->type ?? $chart_of_account->type;
$chart_of_account->description = $request->description;
$chart_of_account->sub_account_of = $request->sub_account_of ?? NULL;
$chart_of_account->balance = $request->balance;
$chart_of_account->core = $request->core ?? $chart_of_account->core;
$chart_of_account->updated_by = $logged_in_user_id;
if (!is_null($request->slug)) {
$chart_of_account->slug = $request->slug;
}
try {
$initial_transaction = Banking::where(['bank' => $id, 'memo' => 'Initial Deposit'])
->update([
'account_balance' => $request->balance
]);
}catch (QueryException $_e){
flash('Failed To Update Opening Bank Balance.')->error();
}
try {
$chart_of_account->save();
flash($request->name . " Chart of Account has been updated")->success();
return redirect("/chart_of_accounts/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$undeletable_core_chart_of_accounts = [];
//check if the account is a core before allowing deletion
$undeletable_core_chart_of_accounts = ChartOfAccount::where('core',1)->orderBy('id', 'asc')->pluck('id')->toArray();
if (in_array($id, $undeletable_core_chart_of_accounts)) {
flash('You can not delete this chart of accounts. It is a core chart of account used by the system')->error();
return redirect()->back();
}
//check if the account has any attached transaction without and not delete it
//Table to check 1. banking, 2. Quotations, 3.payments, 4.patient_category_invoices
//maybe checking many tables isn't the best idea.
$chart_of_account = ChartOfAccount::find($id);
if ($chart_of_account->delete()):
flash("Chart of Account has been deleted.")->success();
return redirect('/chart_of_accounts/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$chart_of_accounts = ChartOfAccount::onlyTrashed()
->orderBy('name', 'asc')
->get();
$chart_of_accounts_list = ChartOfAccount::onlyTrashed()
->orderBy('name', 'asc')
->pluck('name', 'id');
$account_types = AccountType::orderBy('name', 'asc')
->pluck('name', 'id');
if (count($chart_of_accounts) < 1) {
flash()->error("There is no inactive chart_of_account");
return redirect('/chart_of_accounts/');
} else {
// Log::info($chart_of_accounts);
return view('clinical_data::chart_of_accounts.inactive', compact('chart_of_accounts', 'chart_of_accounts_list', 'account_types'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$chart_of_account = ChartOfAccount::withTrashed()->find($id);
if ($chart_of_account->restore()):
flash("Chart of Account has been activated.")->success();
return redirect('/chart_of_accounts/inactive');
endif;
}
}