middleware('auth'); $this->middleware('permission:banking-make-deposit'); $this->middleware('permission:banking-make-transfers'); $this->middleware('permission:banking-view-history'); } public function register(Request $request) { $records = []; $bank = $request->bank; $staff_members = User::get(); $display = bank_register_label_setter($request); $today = Carbon::today()->toDateString(); $banks = ChartOfAccount::where('type', 4)->get(); $yesterday = Carbon::yesterday()->toDateString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $orderByTransIdQuery = "CAST(trans_id AS DECIMAL(10,0)) ASC"; if ($request->staff_member == "ALL STAFF") { switch ($request->dates) { case 'today': $records = DB::table('banking')->whereNull('deleted_at')->whereDate('trans_date', $today)->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'yesterday': $records = DB::table('banking')->whereNull('deleted_at')->whereDate('trans_date', $yesterday)->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'custom_date': $records = DB::table('banking')->whereNull('deleted_at')->whereDate('trans_date', $start)->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'custom_date_range': $records = DB::table('banking')->whereNull('deleted_at')->whereBetween('trans_date', [$start, $end])->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; } } else { switch ($request->dates) { case 'today': $records = DB::table('banking')->whereNull('deleted_at')->where('created_by', $request->staff_member)->whereDate('trans_date', $today) ->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'yesterday': $records = DB::table('banking')->whereNull('deleted_at')->where('created_by', $request->staff_member)->whereDate('trans_date', $yesterday) ->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'custom_date': $records = DB::table('banking')->whereNull('deleted_at')->where('created_by', $request->staff_member)->whereDate('trans_date', $start) ->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; case 'custom_date_range': $records = DB::table('banking')->whereNull('deleted_at')->where('created_by', $request->staff_member)->whereBetween('trans_date', [$start, $end]) ->where('bank', $request->bank) ->orderBy('trans_date', 'asc') ->orderByRaw($orderByTransIdQuery) ->get(); break; } } return view('banking::banking.register', compact('records', 'display', 'staff_members', 'banks', 'bank')); } public function reconcile(Request $request) { $orderByTransIdQuery = "CAST(trans_id AS DECIMAL(10,0)) ASC"; $bank = $request->bank; $banks = ChartOfAccount::where('type', 4)->where('id', '!=', 10)->get(); $reconciliation_date = Carbon::parse($request->reconciliation_date)->endOfDay()->toDateTimeString(); $beginning_of_month = Carbon::parse($request->reconciliation_date)->startOfMonth()->toDateTimeString(); $income_expense_accounts = DB::table('chart_of_accounts')->whereNull('deleted_at')->whereIn('type', [1, 2])->orderBy('name', 'asc')->get(); $banking_records = Banking::whereBetween('trans_date', [$beginning_of_month, $reconciliation_date])->where('bank', $request->bank)->where('reconciled',0)->orderBy('trans_date', 'asc')->orderByRaw($orderByTransIdQuery)->get(); $display = bank_reconciliation_label_setter($request->bank, $beginning_of_month, $reconciliation_date); $reconciliation_end_date = $reconciliation_date; $reconciliation_reports = BankReconciliationReport::where('bank', $request->bank)->get(); $option_accounts = ""; foreach ($income_expense_accounts as $account) { $option_accounts .= ""; } return view('banking::banking.reconcile.index', compact('banking_records', 'display', 'banks', 'bank', 'request', 'reconciliation_end_date', 'income_expense_accounts', 'reconciliation_reports', 'option_accounts')); } public function finish_adjustment(Request $request) { DB::beginTransaction(); $logged_in_user = Auth::id(); $reconciliation_period = Carbon::parse($request->reconciliation_period)->toDateString(); $memo = 'bank reconciliation adjustment'; try{ // track the receipt $track_receipt = new TrackReceipt; $track_receipt->reason = $memo; $track_receipt->created_by = $logged_in_user; $track_receipt->save(); $receipt_number = sprintf("%04u", $track_receipt->id); if ($request->amount < 0) { // Post PAYMENT in banking table $bank_record = new Banking; $bank_record->trans_type = 'PAYMENT'; $bank_record->trans_date = $reconciliation_period; $bank_record->bank = $request->bank; $bank_record->other_accounts = $request->account; $bank_record->account_balance = $request->ending_balance; $bank_record->credit = 0; $bank_record->debit = abs($request->amount); $bank_record->memo = $memo; $bank_record->trans_id = $receipt_number; $bank_record->created_by = Auth::id(); $bank_record->reconciliation_adjustment = 1; $bank_record->save(); $last_insert_id = $bank_record->id; // recursively update balances for the undeposited funds account update_banking_record_balances($reconciliation_period, $request->bank, $last_insert_id, $request->ending_balance); // Save entry to Payment table $payment = new Payment; $payment->item_id = ""; $payment->vendor = "N/A"; $payment->unit_cost = abs($request->amount); $payment->amount = abs($request->amount); $payment->quantity = 1; $payment->memo = $memo; $payment->account_balance = $request->ending_balance; $payment->transaction_id = $receipt_number; $payment->account_id = $request->bank; $payment->created_by = $logged_in_user; $payment->expense_date = Carbon::parse($request->reconciliation_period)->toDateTimeString(); $payment->expense_account = $request->account; $payment->save(); } else { $bank_account_balance_record_on_deposit_date = get_latest_banking_record_based_on_transaction_date($request->bank, $reconciliation_period); $bank_account_balance = $bank_account_balance_record_on_deposit_date ? (int)$bank_account_balance_record_on_deposit_date->account_balance : 0; // add the deposit amount to undeposited funds. $this_bank_balance_after_deposit = ($bank_account_balance + $request->amount); // Post DEPOSIT in banking table $bank_record = new Banking; $bank_record->trans_type = 'DEPOSIT'; $bank_record->trans_date = $reconciliation_period; $bank_record->bank = $request->bank; $bank_record->other_accounts = $request->account; $bank_record->account_balance = $this_bank_balance_after_deposit; $bank_record->credit = $request->amount; $bank_record->debit = 0; $bank_record->memo = $memo; $bank_record->trans_id = $receipt_number; $bank_record->created_by = Auth::id(); $bank_record->reconciliation_adjustment = 1; $bank_record->save(); $last_insert_id = $bank_record->id; // recursively update balances for the undeposited funds account update_banking_record_balances($reconciliation_period, $request->bank, $last_insert_id, $this_bank_balance_after_deposit); // Save entry to OtherIncome table $other_income = new OtherIncome; $other_income->income_account = $request->account; $other_income->deposit_amount = $request->amount; $other_income->banked_amount = $request->amount; $other_income->deposit_date = $reconciliation_period; $other_income->deposit_memo = $memo; $other_income->trans_id = $receipt_number; $other_income->created_by = $logged_in_user; $other_income->save(); } DB::commit(); return 'success'; } catch (\Exception | \Throwable | \Error $e){ DB::rollBack(); return 'fail'; } } public function finish_with_balance(Request $request) { $logged_in_user = Auth::id(); $reconciliation_period = Carbon::parse($request->reconciliation_period)->toDateString(); // track the receipt $track_receipt = new TrackReceipt; $track_receipt->reason = 'bank reconciliation process concluded with discrepancy.'; $track_receipt->created_by = $logged_in_user; $track_receipt->save(); $receipt_number = sprintf("%04u", $track_receipt->id); $bank_record = new Banking; $bank_record->trans_type = 'DEPOSIT'; $bank_record->trans_date = $reconciliation_period; $bank_record->bank = $request->bank; $bank_record->other_accounts = $request->bank; $bank_record->account_balance = $request->ending_balance; $bank_record->credit = 0; $bank_record->debit = 0; $bank_record->memo = $request->memo; $bank_record->trans_id = $receipt_number; $bank_record->created_by = Auth::id(); $bank_record->reconciliation_adjustment = 1; if ($bank_record->save()) { return 'success'; } else { return 'fail'; } } public function finish_reconciling(Request $request) { $logged_in_user = Auth::id(); // clean array to remove empty values $reconciled_records_array = $request->reconciled_trans_ids ? array_filter($request->reconciled_trans_ids) : []; $unreconciled_trans_ids_array = $request->unreconciled_trans_ids ? array_filter($request->unreconciled_trans_ids) : []; $report = new BankReconciliationReport; $report->reconciled_trans_ids = implode(',', $reconciled_records_array); $report->unreconciled_trans_ids = implode(',', $unreconciled_trans_ids_array); $report->opening_streamline_balance = $request->opening_streamline_balance; $report->ending_bank_statement_balance = $request->ending_bank_statement_balance; $report->reconciliation_period = $request->reconciliation_period; $report->memo = $request->memo; $report->bank = $request->bank; $report->created_by = $logged_in_user; $report->save(); for ($x = 0; $x < count($reconciled_records_array); $x++) { Banking::where('id', $reconciled_records_array[$x])->update(['reconciled' => $report->id]); } //update the last inserted banking record of this bank with the reconciled id of the report $last_record_of_bank = Banking::where('bank',$request->bank)->where('reconciliation_adjustment', 1)->orderBy('id','desc')->first(); if ($last_record_of_bank) { $last_record_of_bank->reconciled = $report->id; $last_record_of_bank->update(); } flash('Bank Reconciliation Completed Successfully.')->success(); return redirect()->route('bank.reconciliation.report', $report->id); } public function print_bank_reconciliation($id) { $payment_items = PaymentItem::pluck('name', 'id')->toArray(); $chart_of_accounts = ChartOfAccount::pluck('name', 'id')->toArray(); $report = BankReconciliationReport::find($id); $hospital_information = HospitalInformation::first(); $data = [ 'payment_items' => $payment_items, 'chart_of_accounts' => $chart_of_accounts, 'hospital_information' => $hospital_information, 'report' => $report, ]; $pdf = SnappyPDF::loadView("banking::banking/reconcile/print", $data) ->setOrientation('portrait') ->setOption('margin-bottom', 7) ->setOption('margin-top', 5) ->setOption('footer-html', '© ' . streamline_date(date('Y-m-d')) . ' Stre@mline'); return $pdf->inline('Banking Reconciliation Report generated on ' . date(" d-m-y h:ia") . '.pdf'); } public function bank_reconciliation_report($id) { $payment_items = PaymentItem::pluck('name', 'id')->toArray(); $chart_of_accounts = ChartOfAccount::pluck('name', 'id')->toArray(); $report = BankReconciliationReport::find($id); return view('banking::banking.reconcile.report', compact('report', 'chart_of_accounts', 'payment_items')); } public function reverse($trans_id) { // update account balances on banking records $deposit = Banking::where('trans_id', $trans_id)->where('trans_type', 'DEPOSIT')->first(); $transfer = Banking::where('trans_id', $trans_id)->where('trans_type', 'TRANSFER')->first(); $_payment = Banking::where('trans_id', $trans_id)->where('trans_type', 'PAYMENT')->first(); $payment = Payment::where('transaction_id', $trans_id)->first(); if (!is_null($deposit)) { // foreach ($deposits as $deposit){ $deposit_amount = (int)$deposit->credit; $current_deposit_to_balance = Banking::where('bank', $deposit->bank)->pluck('account_balance')->first(); $current_deposit_from_balance = Banking::where('other_accounts', $deposit->other_accounts)->pluck('account_balance')->first(); ChartOfAccount::where('id', $deposit->bank)->update(['balance' => ((int)$current_deposit_to_balance) - (int)$deposit_amount]); ChartOfAccount::where('id', $deposit->other_accounts)->update(['balance' => ((int)$current_deposit_from_balance) + (int)$deposit_amount]); $trans_date = Carbon::parse($deposit->trans_date)->toDateString(); $bank_proceeding_deposits = DB::table('banking')->whereNull('deleted_at') ->where('trans_date', '>', $trans_date) ->where('bank', $deposit->bank)->get(); foreach ($bank_proceeding_deposits as $proceeding_deposit) { Banking::where('id', $proceeding_deposit->id)->update(['account_balance' => ((int)$proceeding_deposit->account_balance - (int)$deposit_amount)]); } $other_accounts_proceeding_deposits = []; $other_accounts_array = explode(',', $deposit->other_accounts); for ($x = 0; $x < count($other_accounts_array); $x++) { $other_accounts_proceeding_deposits[$other_accounts_array[$x]] = DB::table('banking')->whereNull('deleted_at') ->where('trans_date', '>', $trans_date) ->where('bank', $other_accounts_array[$x]) ->get(); } foreach ($other_accounts_array as $account) { foreach ($other_accounts_proceeding_deposits[$account] as $proceeding_deposit) { Banking::where('id', $proceeding_deposit->id)->update(['account_balance' => ((int)$proceeding_deposit->account_balance + (int)$deposit_amount)]); } } $deposit->delete(); flash('Bank Transaction successfully reversed.')->success(); return redirect('/banking/register'); // } } elseif (!is_null($transfer)) { // foreach ($transfers as $transfer) { $transfer_amount = (int)$transfer->debit; $current_transfer_to_balance = ChartOfAccount::where('id', $transfer->bank)->pluck('balance')->first(); $current_transfer_from_balance = ChartOfAccount::where('id', $transfer->other_accounts)->pluck('balance')->first(); ChartOfAccount::where('id', $transfer->bank)->update(['balance' => ((int)$current_transfer_to_balance) + (int)$transfer_amount]); ChartOfAccount::where('id', $transfer->other_accounts)->update(['balance' => ((int)$current_transfer_from_balance) - (int)$transfer_amount]); $bank_proceeding_transfers = DB::table('banking')->whereNull('deleted_at') ->where('trans_date', '>', Carbon::parse($deposit->trans_date)->toDateString()) ->where('bank', $deposit->bank)->get(); foreach ($bank_proceeding_transfers as $transfer) { Banking::where('id', $transfer->id)->update(['account_balance' => ((int)$transfer->account_balance + (int)$transfer_amount)]); } $other_accounts_proceeding_transfers = []; $other_accounts_array = explode(',', $transfer->other_accounts); for ($x = 0; $x < count($other_accounts_array); $x++) { $other_accounts_proceeding_transfers[$other_accounts_array[$x]] = DB::table('banking')->whereNull('deleted_at') ->where('trans_date', '>', Carbon::parse($deposit->trans_date)->toDateString()) ->where('bank', $other_accounts_array[$x]) ->get(); } foreach ($other_accounts_array as $account) { foreach ($other_accounts_proceeding_transfers[$account] as $transfer) { Banking::where('id', $transfer->id)->update(['account_balance' => ((int)$transfer->account_balance - (int)$transfer_amount)]); } } $transfer->delete(); // update chart of account balances flash('Bank Transaction successfully reversed.')->success(); return redirect('/banking/register'); // } } elseif (!is_null($payment)) { // foreach ($payments as $payment){ $bank_account = $payment->account_id; $payment_item_account = get_name($payment->item_id, 'id', 'account_id', 'payment_items'); $payment_amount = (int)$payment->amount; $current_transfer_to_balance = ChartOfAccount::where('id', $payment_item_account)->pluck('balance')->first(); $current_transfer_from_balance = ChartOfAccount::where('id', $bank_account)->pluck('balance')->first(); ChartOfAccount::where('id', $payment_item_account)->update(['balance' => ((int)$current_transfer_to_balance) - (int)$payment_amount]); ChartOfAccount::where('id', $bank_account)->update(['balance' => ((int)$current_transfer_from_balance) + (int)$payment_amount]); $proceeding_transactions = DB::table('banking')->whereNull('deleted_at')->where('trans_date', '>', $payment->expense_date)->get(); foreach ($proceeding_transactions as $transaction) { Banking::where('id', $transaction->id)->update(['account_balance' => ((int)$transaction->account_balance + (int)$payment_amount)]); } $payment->delete(); $_payment->delete(); flash('Bank Transaction successfully reversed.')->success(); return redirect('/banking/register'); // } } // update chart of account balances flash('Bank Transaction Reversal Failed.')->error(); return redirect('/banking/register'); } public function deposit() { // $orderByTransIdQuery = "CAST(trans_id AS DECIMAL(10,0)) DESC"; // $record = DB::table('other_incomes') // ->whereNull('deleted_at') // ->where('income_account', '=', 200) // ->whereDate('donation_date', '<=', Carbon::parse('2021-04-14')->toDateString()) // ->orderBy('donation_date', 'desc') // ->orderByRaw($orderByTransIdQuery) // ->first(); $hospital_information = HospitalInformation::first(); $income_accounts = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 1)->get()->toArray(); $bank_accounts = DB::table('chart_of_accounts')->whereNull('deleted_at')->where('type', 4)->orWhere('type', 9)->get()->toArray(); $payment_methods = DB::table('patient_payment_methods')->get()->toArray(); return view('banking::banking.deposit', compact('bank_accounts', 'income_accounts', 'hospital_information', 'payment_methods')); } public function transfer() { $hospital_information = HospitalInformation::first(); $all_accounts = DB::table('chart_of_accounts')->where('type', 1)->get()->toArray(); $banks = DB::table('chart_of_accounts')->where('type', 4)->get()->toArray(); return view('banking::banking.transfer', compact('all_accounts', 'banks', 'hospital_information')); } public function transfer_history(Request $request) { $staff_members = User::get(); $display = "report for today, the " . streamline_date(Carbon::now()->toDateString()); $today = Carbon::today()->toDateString(); $yesterday = Carbon::yesterday()->toDateString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $chart_of_accounts = ChartOfAccount::pluck('name', 'id')->toArray(); $transfers = []; if ($request->staff_member == "ALL STAFF") { $display = dateLabelSetter2($request); switch ($request->dates) { case 'today': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->whereDate('created_at', $today)->get(); break; case 'yesterday': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->whereDate('created_at', $yesterday)->get(); break; case 'custom_date': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->whereDate('created_at', $start)->get(); break; case 'custom_date_range': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->whereBetween('created_at', [$start, $end])->get(); break; } } else { $display = dateLabelSetter2($request); switch ($request->dates) { case 'today': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->where('created_by', $request->staff_member)->whereDate('created_at', $today)->get(); break; case 'yesterday': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->where('created_by', $request->staff_member)->whereDate('created_at', $yesterday)->get(); break; case 'custom_date': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->where('created_by', $request->staff_member)->whereDate('created_at', $start)->get(); break; case 'custom_date_range': $transfers = DB::table('banking')->where('trans_type', '=', 'TRANSFER')->where('created_by', $request->staff_member)->whereBetween('created_at', [$start, $end])->get(); break; } } return view('banking::banking.transfer_history', compact('transfers', 'request', 'display', 'staff_members', 'chart_of_accounts')); } public function deposit_history(Request $request) { $staff_members = User::get(); $display = dateLabelSetter2($request); $today = Carbon::today()->toDateString(); $yesterday = Carbon::yesterday()->toDateString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $chart_of_accounts = ChartOfAccount::pluck('name', 'id')->toArray(); $deposits = []; if ($request->staff_member == "ALL STAFF") { switch ($request->dates) { case 'today': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->whereDate('created_at', $today)->get(); break; case 'yesterday': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->whereDate('created_at', $yesterday)->get(); break; case 'custom_date': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->whereDate('created_at', $start)->get(); break; case 'custom_date_range': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->whereBetween('created_at', [$start, $end])->get(); break; } } else { switch ($request->dates) { case 'today': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->where('created_by', $request->staff_member)->whereDate('created_at', $today)->get(); break; case 'yesterday': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->where('created_by', $request->staff_member)->whereDate('created_at', $yesterday)->get(); break; case 'custom_date': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->where('created_by', $request->staff_member)->whereDate('created_at', $start)->get(); break; case 'custom_date_range': $deposits = DB::table('banking')->where('trans_type', '=', 'DEPOSIT')->where('created_by', $request->staff_member)->whereBetween('created_at', [$start, $end])->get(); break; } } return view('banking::banking.deposit_history', compact('deposits', 'display', 'request', 'staff_members', 'chart_of_accounts')); } public function process_transfer(Request $request) { $validator = Validator::make($request->all(), [ 'transfer_memo' => 'required', 'transfer_date' => 'required', ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); } else { $logged_in_user_id = Auth::id(); // track the receipt $track_receipt = new TrackReceipt; $track_receipt->created_by = $logged_in_user_id; $track_receipt->save(); $trans_id = sprintf("%04u", $track_receipt->id); $track_receipt_update = TrackReceipt::where('id', $track_receipt->id)->first(); $track_receipt_update->reason = "Bank Transfer of " . $trans_id; $track_receipt_update->update(); // transfer date balances $transfer_date = Carbon::parse($request->transfer_date)->toDateString(); $to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->transfer_to, $transfer_date); $to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0; $from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->transfer_from, $transfer_date); $from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0; // present day balances $to_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->transfer_to, Carbon::today()->toDateString()); $to_account_balance_today = ($to_account_balance_today_record != null) ? $to_account_balance_today_record->account_balance : 0; $from_account_balance_today_record = get_latest_banking_record_based_on_transaction_date($request->transfer_from, Carbon::today()->toDateString()); $from_account_balance_today = ($from_account_balance_today_record != null) ? $from_account_balance_today_record->account_balance : 0; // update current balances ChartOfAccount::where('id', $request->transfer_from)->update(['balance' => ($from_account_balance_today - (int)$request->transfer_amount)]); ChartOfAccount::where('id', $request->transfer_to)->update(['balance' => ($to_account_balance_today + (int)$request->transfer_amount)]); // deal with from account $from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - (int)$request->transfer_amount); $last_insert_id_from_account = capture_bank_record( 'TRANSFER', $transfer_date, $request->transfer_from, $request->transfer_to, $from_account_balance_after_transfer, 0, (int)$request->transfer_amount, $request->transfer_memo, $trans_id ); update_banking_record_balances($transfer_date, $request->transfer_from, $last_insert_id_from_account, $from_account_balance_after_transfer); // deal with to account $to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + (int)$request->transfer_amount); $last_insert_id_to_account = capture_bank_record( 'DEPOSIT', $transfer_date, $request->transfer_to, $request->transfer_from, $to_account_balance_after_transfer, (int)$request->transfer_amount, 0, $request->transfer_memo, $trans_id ); update_banking_record_balances($transfer_date, $request->transfer_to, $last_insert_id_to_account, $to_account_balance_after_transfer); flash(" Account Transfer has been saved")->success(); return redirect("/banking/transfer"); } } public function process_deposit(Request $request) { $validator = Validator::make($request->all(), [ 'deposit_date' => 'required', 'deposit_memo' => 'required', ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); } else { $logged_in_user_id = Auth::id(); $deposit_counter = 0; // track the receipt $track_receipt = new TrackReceipt; $track_receipt->reason = $request->deposit_memo; $track_receipt->created_by = $logged_in_user_id; $track_receipt->save(); $trans_id = sprintf("%04u", $track_receipt->id); $memo_array = $request->memo; $account_array = array_filter($request->account); $payment_method_array = $request->payment_method; $amount_array = $request->amount; $total_deposit_amount = array_sum($amount_array); $deposit_date = Carbon::parse($request->deposit_date)->toDateString(); $account_balance_record_on_deposit_date = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $deposit_date); $account_balance_on_deposit_date = ($account_balance_record_on_deposit_date != null) ? (int)$account_balance_record_on_deposit_date->account_balance : 0; $cashier_income = new CashierIncome; $cashier_income->cashier_id = $logged_in_user_id; $cashier_income->brought_by = $logged_in_user_id; $cashier_income->expected_amount = $total_deposit_amount; $cashier_income->reason = $request->deposit_memo; $cashier_income->amount = $total_deposit_amount; $cashier_income->balance = 0; $cashier_income->receipt_number = $trans_id; $cashier_income->previous_balance = (int)$account_balance_on_deposit_date; $cashier_income->account_balance = (int)$account_balance_on_deposit_date + (int)$total_deposit_amount; $cashier_income->save(); $cashier_income_id = $cashier_income->id; for ($x = 0; $x < count($account_array); $x++) { if ($account_array[$x]) { $other_income = new OtherIncome; $other_income->income_account = $account_array[$x]; $other_income->deposit_amount = $amount_array[$x]; $other_income->banked_amount = $amount_array[$x]; $other_income->deposit_date = Carbon::parse($request->deposit_date)->toDateString(); $other_income->deposit_memo = $memo_array[$x]; $other_income->payment_method = $payment_method_array[$x]; $other_income->trans_id = $trans_id; $other_income->created_by = Auth::id(); $other_income->received = $cashier_income_id; $other_income->save(); $track_receipt = new TrackReceipt; $track_receipt->created_by = $logged_in_user_id; $track_receipt->reason = "Bank Deposit from income accounts and is of id " . $trans_id; $track_receipt->save(); ++$deposit_counter; } } $account_balance_after_deposit = ($account_balance_on_deposit_date + (int)$total_deposit_amount); $last_insert_id = capture_bank_record( 'DEPOSIT', $deposit_date, 10, implode(',', $account_array), $account_balance_after_deposit, $total_deposit_amount, 0, $request->deposit_memo, $trans_id ); update_banking_record_balances($deposit_date, $request->deposit_to, $last_insert_id, $account_balance_after_deposit); if ($request->deposit_to != 10) : $to_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date($request->deposit_to, $deposit_date); $to_account_balance_on_transfer_date = ($to_account_balance_on_transfer_date_record != null) ? (int)$to_account_balance_on_transfer_date_record->account_balance : 0; $from_account_balance_on_transfer_date_record = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $deposit_date); $from_account_balance_on_transfer_date = ($from_account_balance_on_transfer_date_record != null) ? (int)$from_account_balance_on_transfer_date_record->account_balance : 0; // deal with from account $from_account_balance_after_transfer = ($from_account_balance_on_transfer_date - $total_deposit_amount); $last_insert_id_from_account = capture_bank_record( 'TRANSFER', $deposit_date, 10, $request->deposit_to, $from_account_balance_after_transfer, 0, $total_deposit_amount, $request->deposit_memo, $trans_id ); update_banking_record_balances($deposit_date, 10, $last_insert_id_from_account, $from_account_balance_after_transfer); // deal with to account $to_account_balance_after_transfer = ($to_account_balance_on_transfer_date + $total_deposit_amount); $last_insert_id_to_account = capture_bank_record( 'DEPOSIT', $deposit_date, $request->deposit_to, 10, $to_account_balance_after_transfer, $total_deposit_amount, 0, $request->deposit_memo, $trans_id ); update_banking_record_balances($deposit_date, $request->deposit_to, $last_insert_id_to_account, $to_account_balance_after_transfer); $last_cashier_income_record = CashierIncome::latest()->first(); CashierIncome::where('id', $last_cashier_income_record->id)->update(['banked' => $last_insert_id_to_account]); endif; return redirect()->route('banking.bank_deposit_slip', ['trans_id' => $trans_id]); } } public function bank_deposit_slip($trans_id) { $hospital_information = HospitalInformation::first(); $banking_record = DB::table('banking')->where('trans_id', $trans_id)->orderBy('id', 'DESC')->first(); $banks_array = [$banking_record->bank]; $amounts_array = [$banking_record->credit]; $deposit_memo = $banking_record->memo; $deposit_date = $banking_record->trans_date; $deposit_amount = $banking_record->credit; $banked_by = Auth::id(); return view('banking::banking.bank_slip', compact('hospital_information', 'banks_array', 'amounts_array', 'deposit_memo', 'deposit_date', 'deposit_amount', 'banked_by')); } public function get_current_account_balance(Request $request) { $acc = get_latest_banking_record_based_on_transaction_date($request->bank_id, Carbon::today()->toDateString()); return json_encode($acc); } public function get_current_account_balance_per_date(Request $request) { $acc = get_latest_banking_record_based_on_transaction_date($request->bank, $request->date); return json_encode($acc); } public function get_opening_account_balance(Request $request) { switch ($request->date_type) { default: case 'today': $date = Carbon::today()->subMonth(1)->toDateString(); break; case 'yesterday': $date = Carbon::yesterday()->subMonth(1)->toDateString(); break; case 'custom_date': $date = Carbon::parse($request->custom_date)->subMonth(1)->toDateString(); break; } // $acc = Banking::where('bank', $request->bank)->whereDate('trans_date','<=', $date)->latest()->first(); $acc = get_latest_banking_record_based_on_transaction_date($request->bank, $date); return json_encode($acc); } public function quick_bank_deposit(Request $request) { $deposit_id_array = []; $logged_in_user_id = Auth::id(); DB::beginTransaction(); // collect banks, amount to deposit and account balances as arrays. try{ $amounts_array = $request->amount; $banks_array = $request->bank_account; for ($x = 0; $x < count($banks_array); $x++) { // get the most recent undeposited funds balance as per deposit date. $deposit_date = Carbon::parse($request->deposit_date)->toDateString(); $cash_account_balance_record_on_deposit_date = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $deposit_date); $cash_account_balance_on_deposit_date = ($cash_account_balance_record_on_deposit_date != null) ? (int)$cash_account_balance_record_on_deposit_date->account_balance : 0; $bank_balance_record_record_on_deposit_date = get_latest_banking_record_based_on_transaction_date($banks_array[$x], $deposit_date); $bank_balance_on_deposit_date = ($bank_balance_record_record_on_deposit_date != null) ? (int)$bank_balance_record_record_on_deposit_date->account_balance : 0; // get the balances as of today $cash_account_balance_record_today = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), Carbon::today()->toDateString()); $cash_account_balance_today = ($cash_account_balance_record_today != null) ? (int)$cash_account_balance_record_today->account_balance : 0; $bank_balance_record_today = get_latest_banking_record_based_on_transaction_date($banks_array[$x], Carbon::today()->toDateString()); $bank_balance_today = ($bank_balance_record_today != null) ? (int)$bank_balance_record_today->account_balance : 0; // track the receipt $track_receipt = new TrackReceipt; $track_receipt->created_by = $logged_in_user_id; $track_receipt->save(); $trans_id = sprintf("%04u", $track_receipt->id); $track_receipt_update = TrackReceipt::where('id', $track_receipt->id)->first(); $track_receipt_update->reason = "Quick Bank Deposit from Received Cash Report of " . $trans_id; $track_receipt_update->update(); // populate bank deposit model $deposit = new Banking; $deposit->memo = $request->deposit_memo; $deposit->trans_type = 'DEPOSIT'; $deposit->trans_date = $deposit_date; $deposit->bank = $banks_array[$x]; $deposit->account_balance = ((int)$amounts_array[$x] + $bank_balance_on_deposit_date); $deposit->other_accounts = 10; $deposit->trans_id = $trans_id; $deposit->credit = $amounts_array[$x]; $deposit->debit = 0; $deposit->created_by = $logged_in_user_id; // deduct the deposit amount from undeposited funds. $undeposited_funds_balance_after_deduction = ($cash_account_balance_on_deposit_date - (int)$amounts_array[$x]); // record the bank transfer for undeposited funds $last_insert_id_undeposited_funds = capture_bank_record( 'TRANSFER', $deposit_date, 10, $banks_array[$x], $undeposited_funds_balance_after_deduction, 0, $amounts_array[$x], 'Transfer to bank', $trans_id ); // recursively update balances for the undeposited funds account update_banking_record_balances($deposit_date, 10, $last_insert_id_undeposited_funds, $undeposited_funds_balance_after_deduction); // update the bank account balance on the chart of accounts ChartOfAccount::where('id', 10)->update(['balance' => $cash_account_balance_today - (int)$amounts_array[$x]]); ChartOfAccount::where('id', $banks_array[$x])->update(['balance' => ($bank_balance_today + (int)$amounts_array[$x])]); // save the receipts and deposit records $deposit->save(); $deposit_id_array[] = $deposit->id; // recursively update balances for the bank receiving the deposit update_banking_record_balances($deposit_date, $banks_array[$x], $deposit->id, $deposit->account_balance); } // update the cashier income with the string of deposit ids. CashierIncome::where('id', $request->cashier_income_id)->update(['banked' => implode(',', $deposit_id_array)]); DB::commit(); } catch (\Exception | \Throwable $e){ DB::rollBack(); flash('Transaction could not be finished')->error(); return redirect()->back(); } $hospital_information = HospitalInformation::first(); $deposit_memo = $request->deposit_memo; $deposit_amount = $request->deposit_amount; $banked_by = $logged_in_user_id; return view('finance_reports::finance_reports.receipts.bank_slip', compact('hospital_information', 'banks_array', 'amounts_array', 'deposit_memo', 'deposit_date', 'deposit_amount', 'banked_by')); } public function write_off_balance(Request $request) { $logged_in_user = Auth::id(); // track the receipt $track_receipt = new TrackReceipt; $track_receipt->reason = 'Balance Write Off From Received Cashier Income.'; $track_receipt->created_by = $logged_in_user; $track_receipt->save(); $receipt_number = sprintf("%04u", $track_receipt->id); // get the most recent undeposited funds balance as per deposit date. $write_off_date = Carbon::parse($request->write_off_date)->toDateString(); $cash_account_balance_record_on_write_off_date = get_latest_banking_record_based_on_transaction_date(get_name("undeposited_funds", "slug", "id", "chart_of_accounts"), $write_off_date); // $cashier_income_record = CashierIncome::where('id', $request->cashier_income_id)->get(); $undeposited_funds_balance_after_write_off = ((int)$cash_account_balance_record_on_write_off_date->account_balance - (int)$request->amount); $last_insert_id = capture_bank_record( 'PAYMENT', $write_off_date, 10, $request->expense_account, $undeposited_funds_balance_after_write_off, 0, (int)$request->amount, $request->write_off_memo, $receipt_number ); // recursively update balances for the undeposited funds account update_banking_record_balances($write_off_date, 10, $last_insert_id, $undeposited_funds_balance_after_write_off); $payment = new Payment; $payment->item_id = ""; $payment->vendor = "N/A"; $payment->unit_cost = (int)$request->amount; $payment->amount = (int)$request->amount; $payment->quantity = 1; $payment->memo = $request->write_off_memo ?? "N/A"; $payment->account_balance = $undeposited_funds_balance_after_write_off; $payment->transaction_id = $receipt_number; $payment->account_id = $request->bank; $payment->created_by = $logged_in_user; $payment->expense_date = Carbon::parse($request->write_off_date)->toDateTimeString(); $payment->expense_account = $request->expense_account; // CashierIncome::where('id', $request->cashier_income_id)->update(['amount' => ((int)$cashier_income_record->amount - (int)$request->amount) ]); if ($payment->save()) { return 'success'; } else { return 'fail'; } } public function compute_transfer(Request $request) { $amount_to_transfer = $request->amount; $to_account_balance = Banking::where('bank', $request->to_account)->latest()->first(); $from_account_balance = Banking::where('bank', $request->from_account)->latest()->first(); if ($to_account_balance) { $new_to_acc_bal = (int)$to_account_balance->account_balance + (int)$amount_to_transfer; } else{ $new_to_acc_bal = (int)$amount_to_transfer; } $new_from_acc_bal = (int)$from_account_balance->account_balance - (int)$amount_to_transfer; return array('new_to_acc_bal' => $new_to_acc_bal, 'new_from_acc_bal' => $new_from_acc_bal); } public function get_latest_bank_balance(Request $request) { $to_bank = get_latest_banking_record_based_on_transaction_date($request->to, $request->date); $from_bank = get_latest_banking_record_based_on_transaction_date($request->from, $request->date); return array($to_bank ? $to_bank->account_balance : 0, $from_bank ? $from_bank->account_balance : 0); } public function get_last_reconciliation_balance(Request $request) { $start_of_month = Carbon::parse($request->reconciliation_date)->startOfMonth()->toDateTimeString(); $start_of_previous_month = Carbon::parse($start_of_month)->subMonth(1)->startOfMonth()->toDateTimeString(); $end_of_previous_month = Carbon::parse($start_of_previous_month)->endOfMonth()->toDateTimeString(); $latest_reconciliation_report_in_previous_month = BankReconciliationReport::where('bank', $request->bank)->whereBetween('reconciliation_period', [$start_of_previous_month, $end_of_previous_month])->orderBy('reconciliation_period', 'desc')->first(); if ($latest_reconciliation_report_in_previous_month) { // Different from $latest_reconciliation_report_in_previous_month->opening_streamline_balance !!! return json_encode($latest_reconciliation_report_in_previous_month->ending_bank_statement_balance); } else { $date = Carbon::parse($request->reconciliation_date)->subMonthsNoOverflow(1)->endOfMonth()->toDateString(); //$acc = get_latest_banking_record_based_on_transaction_date($request->bank, $date); $acc = get_latest_banking_record_based_on_transaction_date($request->bank, $date); if ($acc) { return json_encode($acc->account_balance); } return json_encode(0); } } public function get_other_income_account_balance(Request $request) { $record = get_latest_other_income_record($request->income_account, $request->date); return json_encode($record); } public function store_other_income_deposit(Request $request) { $other_income = new OtherIncome; $other_income->income_account = $request->income_account; $other_income->donation_amount = $request->donation_amount; $other_income->banked_amount = $request->banked_amount; $other_income->donation_date = Carbon::parse($request->donation_date)->toDateString(); $other_income->account_balance = $request->account_balance; $other_income->donation_memo = $request->donation_memo; $other_income->trans_id = $request->receipt_number; $other_income->created_by = Auth::id(); $other_income->save(); $receipt = new TrackReceipt; $receipt->reason = "Donation"; $receipt->created_by = Auth::id(); $receipt->save(); return 'success'; } public function undo_bank_reconciliation($id) { // delete the recon report but preserve the data previously input $report = BankReconciliationReport::find($id); $reconciled_records_array = explode(',', $report->reconciled_trans_ids); $reconciliation_period = $report->reconciliation_period; $reconciliation_bank = $report->bank; $report->delete(); // delete the reconciliation banking record and re-update the balances $discrepancy_banking_record = Banking::where('reconciled',$id)->where('reconciliation_adjustment', 1)->orderBy('id', 'desc')->first(); if ($discrepancy_banking_record) { // if it was a negative then delete from "payments", if it was a positive then delete from "other_incomes" if ($discrepancy_banking_record->credit > 0) { $other_income = OtherIncome::where('trans_id', $discrepancy_banking_record->trans_id)->orderBy('id','desc')->first(); if ($other_income) { $other_income->delete(); } } if ($discrepancy_banking_record->debit != 0) { $payment = Payment::where('transaction_id', $discrepancy_banking_record->trans_id)->orderBy('id','desc')->first(); if ($payment) { $payment->delete(); } } // delete the last record if there was a discrepancy $disc_id = $discrepancy_banking_record->id; $disc_account_balance = $discrepancy_banking_record->account_balance; $discrepancy_banking_record->delete(); // update the balances of this bank after deletion update_banking_record_balances($reconciliation_period, $reconciliation_bank, $disc_id, $disc_account_balance); } for ($x = 0; $x < count($reconciled_records_array); $x++) { Banking::where('id', $reconciled_records_array[$x])->update(['reconciled' => 0, 'updated_by' => Auth::id()]); } flash("Bank Reconciliation for " . streamline_date($reconciliation_period) . " has been undone!")->success(); return redirect()->route('banking.reconcile'); } public function reconciliation_reports(Request $request) { $records = BankReconciliationReport::get(); $staff_members = User::get(); $bank = $request->bank; $today = Carbon::today()->toDateString(); $banks = ChartOfAccount::where('type', 4)->get(); $yesterday = Carbon::yesterday()->toDateString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $display = bank_reconciliation_label_setter($request->bank, $start, $end); if ($request->staff_member == "ALL STAFF") { switch ($request->dates) { case 'today': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $today) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $today) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; case 'yesterday': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $yesterday) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $yesterday) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; case 'custom_date': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $start) ->orderBy('created_at', 'asc')->get(); break; } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $start) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); break; } case 'custom_date_range': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereBetween('created_at', [$start, $end]) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereBetween('created_at', [$start, $end]) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; } } else { switch ($request->dates) { case 'today': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $today) ->where('created_by', $request->staff_member) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $today) ->where('created_by', $request->staff_member) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; case 'yesterday': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $yesterday) ->where('created_by', $request->staff_member) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $yesterday) ->where('created_by', $request->staff_member) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; case 'custom_date': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $start) ->where('created_by', $request->staff_member) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereDate('created_at', $start) ->where('created_by', $request->staff_member) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; case 'custom_date_range': if ($request->bank == "ALL BANKS" || $request->bank == '') { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereBetween('created_at', [$start, $end]) ->where('created_by', $request->staff_member) ->orderBy('created_at', 'asc')->get(); } else { $records = DB::table('bank_reconciliation_reports')->whereNull('deleted_at')->whereBetween('created_at', [$start, $end]) ->where('created_by', $request->staff_member) ->where('bank', $request->bank) ->orderBy('created_at', 'asc')->get(); } break; } } return view('banking::banking.reconcile.reports', compact('records', 'display', 'staff_members', 'banks', 'bank')); } }