Files

636 lines
32 KiB
PHP
Executable File

<?php
namespace Modules\Finance\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Streamline\Models\Banking;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\FixedAsset;
use Streamline\Models\HospitalBill;
use Streamline\Models\Payment;
use Streamline\Models\Supplier;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Streamline\Models\TrackInvoice;
use Streamline\Models\TrackReceipt;
use Streamline\Models\Equity;
class FixedAssetsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('permission:fixed-assets-list', ['only' => ['index']]);
$this->middleware('permission:create-fixed-assets', ['only' => ['create', 'store']]);
$this->middleware('permission:edit-fixed-assets', ['only' => ['edit', 'update']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$fixed_assets = FixedAsset::orderBy('name', 'asc')->get();
$suppliers = DB::table('suppliers')->where('available', 1)->pluck("name", "id");
return view('finance::fixed_assets.index', compact('fixed_assets', 'suppliers'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$suppliers = Supplier::where('available', 1)->pluck('name', 'id')->toArray();
$suppliers = ['' => '- select -'] + $suppliers;
$fixed_asset_chart_of_accounts = ChartOfAccount::where(['type' => 3])->pluck('name', 'id')->toArray();/*4-fixed asset*/
$fixed_asset_chart_of_accounts = ['' => '- select -'] + $fixed_asset_chart_of_accounts;
$bank_chart_of_accounts = ChartOfAccount::where(['type' => 4])->pluck('name', 'id')->toArray();/*3-bank*/
$bank_chart_of_accounts = ['' => '- select -'] + $bank_chart_of_accounts;
$payable_accounts = ChartOfAccount::whereIn('type', [6, 9])->pluck('name', 'id')->toArray();
return view('finance::fixed_assets.create', compact('suppliers', 'fixed_asset_chart_of_accounts', 'bank_chart_of_accounts', 'payable_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(), [
'acquisition_date' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$fixed_asset = new FixedAsset;
$fixed_asset->name = $request->name;
$fixed_asset->serial_number = $request->serial_number;
$fixed_asset->cost_price = $request->cost_price;
$fixed_asset->acquisition_date = $acquisition_date = $request->acquisition_date != null ? Carbon::createFromFormat('d-m-Y', $request->acquisition_date)->toDateString() : null;
$fixed_asset->item_condition = $request->item_condition;
$fixed_asset->depreciation = $request->depreciation;
$fixed_asset->supplier_id = $request->supplier;
$fixed_asset->fixed_asset_account_id = $request->fixed_asset_account;
$fixed_asset->warranty_expiration_date = $request->warranty_expiration_date != null ? Carbon::createFromFormat('d-m-Y', $request->warranty_expiration_date)->toDateString() : null;
$fixed_asset->bank_account_id = $request->bank_account_id;
$fixed_asset->description = $request->description;
$fixed_asset->payment_type = $request->payment_type;
$fixed_asset->created_by = Auth::id();
if ($fixed_asset->save()) {
if ($request->payment_type == 'cash') :
$current_bank_balance = get_latest_banking_record_based_on_transaction_date($request->bank_account_id, getTodayCarbon()->toDateString());
if (!is_null($current_bank_balance)) :
$balance_after_deduction = (int)$current_bank_balance->account_balance - (int)$request->amount_paid;
$track_receipt = new TrackReceipt;
$track_receipt->created_by = Auth::id();
$track_receipt->reason = 'Fixed Asset Purchase';
$track_receipt->save();
$trans_id = sprintf("%04u", $track_receipt->id);
$last_insert_id_from_account = capture_bank_record(
'PAYMENT',
$acquisition_date,
$request->bank_account_id,
$request->fixed_asset_account,
$balance_after_deduction,
0,
$request->cost_price,
'Fixed Asset Purchase',
$trans_id
);
$fixed_asset->banking_id = $last_insert_id_from_account;
$fixed_asset->save();
//update running bank balance
update_banking_record_balances($acquisition_date, $request->bank_account_id, $last_insert_id_from_account, $balance_after_deduction);
if ($request->balance > 0) :
// track the invoice
$track_invoice = new TrackInvoice;
$track_invoice->reason = (!is_null($request->description) ? $request->description : "Fixed Assets Creation");
$track_invoice->created_by = Auth::id();
$track_invoice->save();
$invoice_number = sprintf("%04u", $track_invoice->id);
$bill = new HospitalBill;
$bill->vendor = $request->supplier;
$bill->bill_date = $acquisition_date;
$bill->bill_memo = $request->description;
$bill->bill_number = $invoice_number;
$bill->total_amount = $request->balance;
$bill->item_ids = $fixed_asset->id;
$bill->item_accounts = $request->fixed_asset_account;
$bill->item_quantities = 1;
$bill->item_subtotals = $request->balance;
$bill->payable_account = $request->payable_account;
$bill->account_type = 3;
$bill->created_by = Auth::id();
$bill->bill_type = 'ASSETS';
$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());
$bill->save();
endif;
else :
flash('error occured. contact system admin')->error();
return redirect()->back()->withInput();
endif;
elseif ($request->payment_type == 'bill') :
// track the invoice
$track_invoice = new TrackInvoice;
$track_invoice->reason = (!is_null($request->description) ? $request->description : "Fixed Assets Creation");
$track_invoice->created_by = Auth::id();
$track_invoice->save();
$invoice_number = sprintf("%04u", $track_invoice->id);
$bill = new HospitalBill;
$bill->vendor = $request->supplier;
$bill->bill_date = $acquisition_date;
$bill->bill_memo = $request->description;
$bill->bill_number = $invoice_number;
$bill->total_amount = $request->cost_price;
$bill->item_ids = $fixed_asset->id;
$bill->item_accounts = $request->fixed_asset_account;
$bill->item_quantities = 1;
$bill->item_subtotals = $request->cost_price;
$bill->payable_account = $request->payable_account;
$bill->account_type = 3;
$bill->created_by = Auth::id();
$bill->bill_type = 'ASSETS';
$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());
$bill->save();
elseif ($request->payment_type == 'asset_exists') :
// Update the opening_fixed_assets Equity Account
$opening_fixed_assets = ChartOfAccount::where(['slug' => 'opening_fixed_assets'])->first();
$opening_fixed_assets_balance = $opening_fixed_assets->balance;
$opening_fixed_assets->balance = $opening_fixed_assets_balance + $request->cost_price;
$opening_fixed_assets->updated_by = Auth::id();
$opening_fixed_assets->update();
$equity = new Equity;
$equity->name = $fixed_asset->name;
$equity->amount = $request->cost_price;
$equity->account_id = $opening_fixed_assets->id;
$equity->created_by = Auth::id();
$equity->updated_by = Auth::id();
$equity->save();
endif;
flash($fixed_asset->name . ' has been saved')->success();
return redirect('fixed_assets');
}
flash('error occured. contact system admin')->error();
return redirect()->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)
{
$fixed_asset = FixedAsset::find($id);
$suppliers = Supplier::where('available', 1)->pluck('name', 'id')->toArray();
$suppliers = ['' => '- select -'] + $suppliers;
$fixed_asset_chart_of_accounts = ChartOfAccount::where(['type' => 3])->pluck('name', 'id')->toArray();/*4-fixed asset*/
$fixed_asset_chart_of_accounts = ['' => '- select -'] + $fixed_asset_chart_of_accounts;
$bank_chart_of_accounts = ChartOfAccount::where(['type' => 4])->pluck('name', 'id')->toArray();/*3-bank*/
$bank_chart_of_accounts = ['' => '- select -'] + $bank_chart_of_accounts;
$payable_accounts = ChartOfAccount::whereIn('type', [6, 9])->pluck('name', 'id')->toArray();
return view('finance::fixed_assets.edit', compact('suppliers', 'fixed_asset_chart_of_accounts', 'bank_chart_of_accounts', 'fixed_asset', 'payable_accounts'));
}
/**
* 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(), [
'acquisition_date' => 'required',
'warranty_expiration_date' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
//1. check if payment type's been edited
//2. if it is a bill, ensure that the bill does not have a payment else reject
//3. if already existing, do adjustments in equities
//4. if cash, check if all was paid then check for the bill created for unpaid balance
$hospital_bill = null;
if ($request->edit_payment_type == 1) {
$hospital_bill = HospitalBill::where(['item_ids' => $id, 'bill_type' => 'ASSETS'])->first();
$amount_paid_history_array = unserialize($hospital_bill->amount_paid_history);
if (count($amount_paid_history_array) > 0) {
//bill has a paid payment so can not be edited
flash('Bill has some payments so first undo the payments')->error();
return redirect()->back()->withInput();
}
}
//dd($request->all());
$acquisition_date = Carbon::parse($request->acquisition_date)->toDateString();
$fixed_asset = FixedAsset::find($id);
$previous_acquisition_date = $fixed_asset->acquisition_date;
$previous_asset_name = $fixed_asset->name;
$previous_bank_account_id = $fixed_asset->bank_account_id;
$previous_cost_price = $fixed_asset->cost_price;
$previous_asset_account = $fixed_asset->fixed_asset_account_id;
$fixed_asset->name = $request->name;
$fixed_asset->serial_number = $request->serial_number;
$fixed_asset->cost_price = $request->cost_price;
$fixed_asset->acquisition_date = $acquisition_date;
$fixed_asset->item_condition = $request->item_condition;
$fixed_asset->supplier_id = $request->supplier;
$fixed_asset->depreciation = $request->depreciation;
$fixed_asset->fixed_asset_account_id = $request->fixed_asset_account;
$fixed_asset->warranty_expiration_date = Carbon::parse($request->warranty_expiration_date)->toDateString();
$fixed_asset->bank_account_id = $request->bank_account_id;
$fixed_asset->description = $request->description;
$fixed_asset->created_by = Auth::id();
//if the payment has been edited then we do
if ($request->edit_payment_type == 1) {
//payment type has not changed so handle accordingly
if ($fixed_asset->payment_type == $request->payment_type) {
# if it has a bill or bank and has no payment then adjust amount, if exisiting asset adjust the equity
if ($fixed_asset->payment_type == "bill") {
// track the invoice
$track_invoice = new TrackInvoice;
$track_invoice->reason = (!is_null($request->description) ? $request->description : "Fixed Assets Creation");
$track_invoice->created_by = Auth::id();
$track_invoice->save();
$invoice_number = sprintf("%04u", $track_invoice->id);
$bill = HospitalBill::where(['item_ids' => $id, 'bill_type' => 'ASSETS'])->first();
$bill->vendor = $request->supplier;
$bill->bill_date = $fixed_asset->acquisition_date;
$bill->bill_memo = $request->description;
$bill->bill_number = $invoice_number;
$bill->total_amount = $request->balance;
$bill->item_ids = $fixed_asset->id;
$bill->item_accounts = $request->fixed_asset_account;
$bill->item_quantities = 1;
$bill->item_subtotals = $request->balance;
$bill->payable_account = $request->payable_account;
$bill->account_type = 3;
$bill->created_by = Auth::id();
$bill->bill_type = 'ASSETS';
$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());
$bill->save();
} elseif ($fixed_asset->payment_type == "cash" && ($fixed_asset->cost_price != $previous_cost_price)) {
//but first check it the money has changed
$banking_record = Banking::where(["bank" => $previous_bank_account_id, "debit" => $previous_cost_price, "memo" => "Fixed Asset Purchase", "trans_date" => $previous_acquisition_date])->first();
if ($banking_record) {
$banking_record->trans_date = $acquisition_date;
$banking_record->bank = $request->bank_account_id;
$banking_record->credit = $request->amount_paid;
$current_bank_balance_record = get_latest_banking_record_based_on_transaction_date($request->bank_account_id, getTodayCarbon()->toDateString());
$balance_after_deduction = is_null($current_bank_balance_record) ? 0 : (int)$current_bank_balance_record->account_balance - (int)$request->amount_paid;
$banking_record->account_balance = $balance_after_deduction;
$banking_record->update();
}
//remember to re-calculate the running balance of this bank
update_banking_record_balances($acquisition_date, $request->bank_account_id, $banking_record, $balance_after_deduction);
// track the invoice
$track_invoice = new TrackInvoice;
$track_invoice->reason = (!is_null($request->description) ? $request->description : "Fixed Assets Creation");
$track_invoice->created_by = Auth::id();
$track_invoice->save();
$invoice_number = sprintf("%04u", $track_invoice->id);
//If the balance is more, find existing bill and adjust figures or create a new bill
$hospital_bill = HospitalBill::where(['item_ids' => $id,'bill_type'=>'ASSETS'])->first();
$bill = is_null($hospital_bill) ? new HospitalBill : $hospital_bill;
$bill->vendor = $request->supplier;
$bill->bill_date = $acquisition_date;
$bill->bill_memo = $request->description;
$bill->bill_number = $invoice_number;
$bill->total_amount = $request->balance;
$bill->item_ids = $fixed_asset->id;
$bill->item_accounts = $request->fixed_asset_account;
$bill->item_quantities = 1;
$bill->item_subtotals = $request->balance;
$bill->payable_account = $request->payable_account;
$bill->account_type = 3;
$bill->created_by = Auth::id();
$bill->bill_type = 'ASSETS';
$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());
$bill->save();
} elseif ($fixed_asset->payment_type == "asset_exists") {
$equity = Equity::where(['account_id' => $previous_asset_account, 'name' => $previous_asset_name, 'amount' => $previous_cost_price])->first();
// Update the opening_fixed_assets Equity Account
$opening_fixed_assets = ChartOfAccount::where(['slug' => 'opening_fixed_assets'])->first();
$opening_fixed_assets_balance = $opening_fixed_assets->balance;
$opening_fixed_assets->balance = $opening_fixed_assets_balance + $request->cost_price;
$opening_fixed_assets->updated_by = Auth::id();
$opening_fixed_assets->update();
$equity->name = $fixed_asset->name;
$equity->amount = $request->cost_price;
$equity->account_id = $opening_fixed_assets->id;
$equity->updated_by = Auth::id();
$equity->save();
}
}
//payment type has changed deal with new payment type and old payment type too
else {
// if the previous payment type is bill and the payment type has changed then delete the bill
if ($fixed_asset->payment_type == "bill") {
$hospital_bill->delete();
}
// if previous payment was already existing and payment has changed then delete equity and update chart of account with deduction
if ($fixed_asset->payment_type == "asset_exists") {
$equity = Equity::where(['account_id' => $previous_asset_account, 'name' => $previous_asset_name, 'amount' => $previous_cost_price])->first();
$equity->delete();
$opening_fixed_assets = ChartOfAccount::where(['slug' => 'opening_fixed_assets'])->first();
$opening_fixed_assets->balance = $opening_fixed_assets->balance - $fixed_asset->cost_price;
$opening_fixed_assets->updated_by = Auth::id();
$opening_fixed_assets->update();
}
//if previous payment was cash and payment has changed then increase the balance of the bank the asset was paid from
if ($fixed_asset->payment_type == "cash") {
$track_receipt = new TrackReceipt;
$track_receipt->created_by = Auth::id();
$track_receipt->reason = 'Fixed Asset Purchase Edit';
$track_receipt->save();
$trans_id = sprintf("%04u", $track_receipt->id);
$current_bank_balance = get_latest_banking_record_based_on_transaction_date($previous_bank_account_id, getTodayCarbon()->toDateString());
if (!is_null($current_bank_balance)) :
$balance_after_addition = (int)$current_bank_balance->account_balance + (int)$fixed_asset->amount_paid;
endif;
$last_insert_id_from_account = capture_bank_record(
'DEPOSIT',
$fixed_asset->acquisition_date,
$previous_bank_account_id,
$fixed_asset->fixed_asset_account_id,
$balance_after_addition,
$previous_cost_price,
0,
'Fixed Asset Purchase Edit',
$trans_id
);
update_banking_record_balances($fixed_asset->acquisition_date, $previous_bank_account_id, $last_insert_id_from_account, $balance_after_addition);
}
//deal with the new selected payment type
if ($request->payment_type == 'cash') :
$current_bank_balance = get_latest_banking_record_based_on_transaction_date($request->bank_account_id, getTodayCarbon()->toDateString());
if (!is_null($current_bank_balance)) :
$balance_after_deduction = (int)$current_bank_balance->account_balance - (int)$request->amount_paid;
$track_receipt = new TrackReceipt;
$track_receipt->created_by = Auth::id();
$track_receipt->reason = 'Fixed Asset Purchase';
$track_receipt->save();
$trans_id = sprintf("%04u", $track_receipt->id);
$last_insert_id_from_account = capture_bank_record(
'PAYMENT',
$fixed_asset->acquisition_date,
$request->bank_account_id,
$request->fixed_asset_account,
$balance_after_deduction,
0,
$request->cost_price,
'Fixed Asset Purchase',
$trans_id
);
update_banking_record_balances($fixed_asset->acquisition_date, $request->bank_account_id, $last_insert_id_from_account, $balance_after_deduction);
if ($request->balance > 0) :
// track the invoice
$track_invoice = new TrackInvoice;
$track_invoice->reason = (!is_null($request->description) ? $request->description : "Fixed Assets Creation");
$track_invoice->created_by = Auth::id();
$track_invoice->save();
$invoice_number = sprintf("%04u", $track_invoice->id);
$bill = new HospitalBill;
$bill->vendor = $request->supplier;
$bill->bill_date = $fixed_asset->acquisition_date;
$bill->bill_memo = $request->description;
$bill->bill_number = $invoice_number;
$bill->total_amount = $request->balance;
$bill->item_ids = $fixed_asset->id;
$bill->item_accounts = $request->fixed_asset_account;
$bill->item_quantities = 1;
$bill->item_subtotals = $request->balance;
$bill->payable_account = $request->payable_account;
$bill->account_type = 3;
$bill->created_by = Auth::id();
$bill->bill_type = 'ASSETS';
$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());
$bill->save();
endif;
else :
flash('error occured. contact system admin')->error();
return redirect()->back()->withInput();
endif;
elseif ($request->payment_type == 'asset_exists') :
// Update the opening_fixed_assets Equity Account
$opening_fixed_assets = ChartOfAccount::where(['slug' => 'opening_fixed_assets'])->first();
$opening_fixed_assets_balance = $opening_fixed_assets->balance;
$opening_fixed_assets->balance = $opening_fixed_assets_balance + $request->cost_price;
$opening_fixed_assets->updated_by = Auth::id();
$opening_fixed_assets->update();
$equity = new Equity;
$equity->name = $fixed_asset->name;
$equity->amount = $request->cost_price;
$equity->account_id = $opening_fixed_assets->id;
$equity->created_by = Auth::id();
$equity->updated_by = Auth::id();
$equity->save();
endif;
}
}
if ($fixed_asset->update()) {
flash($fixed_asset->name . ' has been updated')->success();
return redirect('fixed_assets');
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*/
public function destroy($id)
{
$fixed_asset = FixedAsset::find($id);
$bill = HospitalBill::where(['item_ids' => $id, 'bill_type' => 'ASSETS'])->first();
if ($bill) {
if (is_null($bill->balance)) {
# no money has been paid on this bill so delete it
$bill->delete();
} elseif ($bill->total_amount > 0) {
# reverse the partial payment back to the respective bank then delete the bill
$payments = Payment::where('bill_id', $bill->id)->get();
foreach ($payments as $payment) {
$delete_result = delete_hospital_bill_payment($payment->id);
}
# finally the bill with the fixed asset
$bill->delete();
}
}
if ($fixed_asset->payment_type == 'cash') {
if (is_null($fixed_asset->banking_id)) {
$current_bank_balance = get_latest_banking_record_based_on_transaction_date($fixed_asset->bank_account_id, getTodayCarbon()->toDateString());
if (!is_null($current_bank_balance)) {
$balance_after_deduction = (int)$current_bank_balance->account_balance + (int)$fixed_asset->cost_price;
$track_receipt = new TrackReceipt;
$track_receipt->created_by = Auth::id();
$track_receipt->reason = 'Fixed Asset Purchase Deletion';
$track_receipt->save();
$trans_id = sprintf("%04u", $track_receipt->id);
$last_insert_id = capture_bank_record('DEPOSIT', getTodayCarbon()->toDateString(), $fixed_asset->bank_account_id, $fixed_asset->fixed_asset_account_id,
$balance_after_deduction, $fixed_asset->cost_price, 0, 'Fixed Asset Purchase Deletion', $trans_id);
update_banking_record_balances(date('Y-m-d'), $fixed_asset->bank_account_id, $last_insert_id, $balance_after_deduction);
}
} else {
$bank_record = Banking::find($fixed_asset->banking_id);
if ($bank_record) {
$bank_record->account_balance = $bank_record->account_balance + $fixed_asset->cost_price;
$bank_record->debit = 0;
$bank_record->update();
update_banking_record_balances($bank_record->trans_date, $fixed_asset->bank_account_id, $fixed_asset->banking_id, $bank_record->account_balance);
}
}
}
if ($fixed_asset->delete()) {
flash($fixed_asset->name . ' has been deleted')->success();
return redirect('fixed_assets');
}
return redirect()->back()->withInput();
}
public function is_fixed_asset_attached_to_bill(Request $request)
{
$fixed_asset = FixedAsset::find($request->fixed_asset_id);
$bill = HospitalBill::where(['item_ids' => $fixed_asset->id, 'bill_type' => 'ASSETS'])->first();
if ($bill) {
if ($bill->total_amount > 0) {
return "has_attached_bill";
}
}
return "no_attached_bill";
}
}