CasperSecurity
<?php
namespace App\Http\Livewire\Report;
use App\Models\Bank;
use App\Models\CashType;
use App\Models\FinancialTransactions;
use App\Models\FinancialTransactionsLedgerHead;
use App\Models\GlOpeningBalance;
use Carbon\Carbon;
use Livewire\Component;
class BankBookLivewire extends Component
{
public $start_date, $end_date;
public $bank_name, $cash_type;
public $book_type;
public $ob = 0;
public $book_data = [];
public $showData = false;
public function mount()
{
$this->start_date = $this->end_date = date('Y-m-d');
$this->book_type = 'bank_book';
}
public function book_rule()
{
$rules = [
'start_date' => 'required',
'end_date' => 'required',
];
if ($this->book_type == 'bank_book') {
$rules['bank_name'] = 'required';
} else {
$rules['cash_type'] = 'required';
}
return $rules;
}
public $messages = [
'start_date.required' => 'This field is required',
'end_date.required' => 'This field is required',
'bank_name.required' => 'This field is required',
'cash_type.required' => 'This field is required',
];
public function switchTab($type){
if($type == 'bb'){
$this->book_type = 'bank_book';
}elseif($type == 'cb'){
$this->book_type = 'cash_book';
}
}
function getFinancialYearStartDate()
{
$now = Carbon::now();
$year = $now->year;
$fyStart = Carbon::create($year, 4, 1);
if ($now->lessThan($fyStart)) {
$fyStart->year = $year - 1;
}
return $fyStart;
}
function getFinancialYearEndDate()
{
$now = Carbon::now();
$year = $now->year;
$fyEnd = Carbon::create($year + 1, 3, 31, 23, 59, 59);
if ($now < Carbon::create($year, 4, 1)) {
$fyEnd = Carbon::create($year, 3, 31, 23, 59, 59);
}
return $fyEnd;
}
public function show(){
$this->validate($this->book_rule(),$this->messages);
if($this->bank_name != null && $this->start_date != null && $this->end_date != null){
$this->ob = 0;
$financial_year_start_date = $this->getFinancialYearStartDate();
$financial_year_end_date = $this->getFinancialYearEndDate();
$bank_nm = $account_no = null;
$balance = 0;
list($bank_nm, $account_no) = explode('_', $this->bank_name);
$bank = Bank::where('bank_name', $bank_nm)->where('bank_account_no', $account_no)->latest()->first();
if ($bank != null) {
$opening = GlOpeningBalance::where('opening_balance_date', Carbon::parse($financial_year_start_date)->format('Y-m-d'))->where('ledger_head_id', $bank['ledger_head_id'])->where('status', 1)->latest()->first();
$opening_balance = 0;
if ($opening != null) {
$opening_balance += $opening['amount'];
}
$this->ob = $opening_balance;
$balance = $opening_balance;
$financial_transaction_old = FinancialTransactions::where('bank_id', $bank['id'])->whereDate('voucher_date', '>=', Carbon::parse($financial_year_start_date)->format('Y-m-d'))->whereDate('voucher_date', '<=', Carbon::parse($this->start_date)->format('Y-m-d'))->where('voucher_status', 2)->orderBy('voucher_date', 'ASC')->get();
if (sizeof($financial_transaction_old) > 0) {
$opening_balance = 0;
foreach ($financial_transaction_old as $key1 => $ft_old) {
$financial_details = FinancialTransactionsLedgerHead::where('finance_transaction_id', $ft_old['id'])->where('ledger_head_id', '!=', $bank['ledger_head_id'])->get();
if ($ft_old['total_credit_amount'] != null && $ft_old['total_credit_amount'] > 0) {
$opening_balance += $ft_old['total_credit_amount'];
} elseif ($ft_old['total_debit_amount'] != null && $ft_old['total_debit_amount'] > 0) {
$opening_balance -= $ft_old['total_debit_amount'];
}
if (sizeof($financial_details) > 0) {
$financial_transaction_old[$key1]['details'] = $financial_details->toArray();
}
}
/*if($opening != null){
$opening_balance += $opening['amount'];
}
$this->ob = $opening_balance;
$balance = $opening_balance;*/
}
$financial_transaction = FinancialTransactions::where('bank_id', $bank['id'])->whereDate('voucher_date', '>=', Carbon::parse($this->start_date)->format('Y-m-d'))->whereDate('voucher_date', '<=', Carbon::parse($this->end_date)->format('Y-m-d'))->where('voucher_status', 2)->orderBy('voucher_date', 'ASC')->get();
if (sizeof($financial_transaction) > 0) {
foreach ($financial_transaction as $key => $ft) {
$selected_financial_details = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*', 'ledger_heads.ledger_head_name')->join('ledger_heads', 'financial_transactions_ledger_heads.ledger_head_id', '=', 'ledger_heads.id')->where('finance_transaction_id', $ft['id'])->where('ledger_head_id', '!=', $bank['ledger_head_id'])->get();
if (sizeof($selected_financial_details) > 0) {
$selected_financial_details = $selected_financial_details->toArray();
foreach ($selected_financial_details as $sfd_key => $sfd) {
if ($ft['total_credit_amount'] != null && $ft['total_credit_amount'] > 0) {
$balance += abs($sfd['amount']);
$selected_financial_details[$sfd_key]['balance_amt'] = $balance;
} elseif ($ft['total_debit_amount'] != null && $ft['total_debit_amount'] > 0) {
$balance -= abs($sfd['amount']);
$selected_financial_details[$sfd_key]['balance_amt'] = $balance;
}
}
$financial_transaction[$key]['details'] = $selected_financial_details;
}
}
$this->book_data = $financial_transaction;
//dd($this->book_data);
}
}
} elseif ($this->cash_type != null && $this->start_date != null && $this->end_date != null) {
$this->ob = 0;
$financial_year_start_date = $this->getFinancialYearStartDate();
$financial_year_end_date = $this->getFinancialYearEndDate();
$balance = 0;
$cash = CashType::where('cash_type_name', $this->cash_type)->latest()->first();
if ($cash != null) {
$opening = GlOpeningBalance::where('opening_balance_date', Carbon::parse($financial_year_start_date)->format('Y-m-d'))->where('ledger_head_id', $cash['ledger_head_id'])->where('status', 1)->latest()->first();
$opening_balance = 0;
$opening_balance += $opening != null ? $opening['amount'] : 0;
$this->ob = $opening_balance;
$balance = $opening_balance;
$financial_transaction_old = FinancialTransactions::where('cash_type_id', $cash['id'])->whereDate('voucher_date', '>=', Carbon::parse($financial_year_start_date)->format('Y-m-d'))->whereDate('voucher_date', '<=', Carbon::parse($this->start_date)->format('Y-m-d'))->where('voucher_status', 2)->orderBy('voucher_date', 'ASC')->get();
if (sizeof($financial_transaction_old) > 0) {
$opening_balance = 0;
foreach ($financial_transaction_old as $key1 => $ft_old) {
$financial_details = FinancialTransactionsLedgerHead::where('finance_transaction_id', $ft_old['id'])->where('ledger_head_id', '!=', $cash['ledger_head_id'])->get();
if ($ft_old['total_credit_amount'] != null && $ft_old['total_credit_amount'] > 0) {
$opening_balance += $ft_old['total_credit_amount'];
} elseif ($ft_old['total_debit_amount'] != null && $ft_old['total_debit_amount'] > 0) {
$opening_balance -= $ft_old['total_debit_amount'];
}
if (sizeof($financial_details) > 0) {
$financial_transaction_old[$key1]['details'] = $financial_details->toArray();
}
}
/*$opening_balance += $opening != null ? $opening['amount'] : 0;
$this->ob = $opening_balance;
$balance = $opening_balance;*/
}
$financial_transaction = FinancialTransactions::where('cash_type_id', $cash['id'])->whereDate('voucher_date', '>=', Carbon::parse($this->start_date)->format('Y-m-d'))->whereDate('voucher_date', '<=', Carbon::parse($this->end_date)->format('Y-m-d'))->where('voucher_status', 2)->orderBy('voucher_date', 'ASC')->get();
if (sizeof($financial_transaction) > 0) {
foreach ($financial_transaction as $key => $ft) {
$selected_financial_details = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*', 'ledger_heads.ledger_head_name')->join('ledger_heads', ' financial_transactions_ledger_heads.ledger_head_id', '=', 'ledger_heads.id')->where('finance_transaction_id', $ft['id'])->where('ledger_head_id', '!=', $cash['ledger_head_id'])->get();
if (sizeof($selected_financial_details) > 0) {
$selected_financial_details = $selected_financial_details->toArray();
foreach ($selected_financial_details as $sfd_key => $sfd) {
if ($ft['total_credit_amount'] != null && $ft['total_credit_amount'] > 0) {
$balance += abs($sfd['amount']);
$selected_financial_details[$sfd_key]['balance_amt'] = $balance;
} elseif ($ft['total_debit_amount'] != null && $ft['total_debit_amount'] > 0) {
$balance -= abs($sfd['amount']);
$selected_financial_details[$sfd_key]['balance_amt'] = $balance;
}
}
$financial_transaction[$key]['details'] = $selected_financial_details;
}
}
$this->book_data = $financial_transaction;
}
}
}
}
public function render()
{
$banks = Bank::where('status', 1)->get();
$cash = CashType::where('status', 1)->get();
//$ftledgerhead = FinancialTransactionsLedgerHead::where('status', 1)->get(); //dd($hello); // $finacialTransaticon = FinancialTransactions::where('status', 1)->get(); // dd($finacialTransaticon);
return view('livewire.report.bank-book-livewire',compact('banks', 'cash')); }
}