CasperSecurity

Current Path : /var/www/finance.uiet.co.in/UietFinance/app/Http/Livewire/Report/
Upload File :
Current File : /var/www/finance.uiet.co.in/UietFinance/app/Http/Livewire/Report/BankBookLivewire.php

<?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;
use App\Exports\BankBookExport;
use Maatwebsite\Excel\Facades\Excel;

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;

    // Initialize dates and set default book type to bank_book
    public function mount()
    {
        $this->start_date = $this->end_date = date('Y-m-d');
        $this->book_type = 'bank_book';
    }

    // Validation rules for the form fields based on book type
    public function book_rule()
    {
        $rules = [
            'start_date' => 'required|date',
            'end_date'   => 'required|date|after_or_equal:start_date',
        ];

        if ($this->book_type == 'bank_book') {
            $rules['bank_name'] = 'required';
        } else {
            $rules['cash_type'] = 'required';
        }

        return $rules;
    }

    // Custom validation messages
    public $messages = [
        'start_date.required' => 'Start Date is required.',
        'end_date.required'   => 'End Date is required.',
        'bank_name.required'  => 'Bank Name is required for Bank Book.',
        'cash_type.required'  => 'Cash Type is required for Cash Book.',
    ];

    // Switch between Bank Book and Cash Book tabs
    public function switchTab($type)
    {
        if ($type == 'bb') {
            $this->book_type = 'bank_book';  // Switch to Bank Book tab
        } elseif ($type == 'cb') {
            $this->book_type = 'cash_book';  // Switch to Cash Book tab
        }

        // Clear book data when switching tabs
        $this->book_data = [];
        $this->showData = false;
    }

    // Get Financial Year Start Date
    private function getFinancialYearStartDate()
    {
        $now = Carbon::now();
        $year = $now->year;
        $fyStart = Carbon::create($year, 4, 1);

        if ($now->lessThan($fyStart)) {
            $fyStart->year = $year - 1;
        }

        return $fyStart;
    }

    // Get Financial Year End Date
    private 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;
    }

    // Show the relevant data for Bank Book or Cash Book based on the selected book type
    public function show()
    {
        $this->validate($this->book_rule(), $this->messages);

        // Clear previous data when fetching new data
        $this->book_data = [];
        $this->showData = true;

        // Common initialization
        $this->ob = 0;
        $financial_year_start_date = $this->getFinancialYearStartDate();
        $financial_year_end_date = $this->getFinancialYearEndDate();
        $balance = 0;

        // Fetch data for Bank Book if selected
        if ($this->book_type == 'bank_book' && $this->bank_name != null) {
            $this->processBank($financial_year_start_date, $financial_year_end_date, $balance, $this->bank_name);
        }
        // Fetch data for Cash Book if selected
        elseif ($this->book_type == 'cash_book' && $this->cash_type != null) {
            $this->processCash($financial_year_start_date, $financial_year_end_date, $balance, $this->cash_type);
        }
    }

    // Process Bank data based on bank_name
    private function processBank($financial_year_start_date, $financial_year_end_date, &$balance, $bank_name)
    {
        list($bank_nm, $account_no) = explode('_', $bank_name);
        $bank = Bank::where('bank_name', $bank_nm)->where('bank_account_no', $account_no)->latest()->first();

        if ($bank) {
            $this->ob = $this->getOpeningBalance($financial_year_start_date, $bank['ledger_head_id']);
            $balance = $this->ob;

            // Fetch and process older transactions for opening balance
            $financial_transaction_old = $this->getFinancialTransactions('bank_id', $bank['id'], $financial_year_start_date, $this->start_date);
            $this->processFinancialTransactions($financial_transaction_old, $balance, $bank['ledger_head_id']);

            // Fetch and process current transactions
            $financial_transaction = $this->getFinancialTransactions('bank_id', $bank['id'], $this->start_date, $this->end_date);
            $this->processFinancialTransactions($financial_transaction, $balance, $bank['ledger_head_id']);
        }
    }

    // Process Cash data based on cash_type
    private function processCash($financial_year_start_date, $financial_year_end_date, &$balance, $cash_type)
    {
        $cash = CashType::where('cash_type_name', $cash_type)->latest()->first();

        if ($cash) {
            $this->ob = $this->getOpeningBalance($financial_year_start_date, $cash['ledger_head_id']);
            $balance = $this->ob;

            // Fetch and process older transactions for opening balance
            $financial_transaction_old = $this->getFinancialTransactions('cash_type_id', $cash['id'], $financial_year_start_date, $this->start_date);
            $this->processFinancialTransactions($financial_transaction_old, $balance, $cash['ledger_head_id']);

            // Fetch and process current transactions
            $financial_transaction = $this->getFinancialTransactions('cash_type_id', $cash['id'], $this->start_date, $this->end_date);
            $this->processFinancialTransactions($financial_transaction, $balance, $cash['ledger_head_id']);
        }
    }

    // Get Opening Balance for either Bank or Cash
    private function getOpeningBalance($financial_year_start_date, $ledger_head_id)
    {
        $opening = GlOpeningBalance::where('opening_balance_date', Carbon::parse($financial_year_start_date)->format('Y-m-d'))
            ->where('ledger_head_id', $ledger_head_id)
            ->where('status', 1)
            ->latest()
            ->first();

        return $opening ? $opening['amount'] : 0;
    }

    // Get Financial Transactions based on filter
    private function getFinancialTransactions($key, $id, $start_date, $end_date)
    {
        return FinancialTransactions::where($key, $id)
            ->whereDate('voucher_date', '>=', Carbon::parse($start_date)->format('Y-m-d'))
            ->whereDate('voucher_date', '<=', Carbon::parse($end_date)->format('Y-m-d'))
            ->where('voucher_status', 2)
            ->orderBy('voucher_date', 'ASC')
            ->get();
    }

    // Process Financial Transactions for Debit/Credit balance updates
    private function processFinancialTransactions($transactions, &$balance, $ledger_head_id)
    {
        foreach ($transactions 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', '!=', $ledger_head_id)
                ->get();

            if ($selected_financial_details->isNotEmpty()) {
                foreach ($selected_financial_details as $sfd_key => $sfd) {
                    $amount = abs($sfd['amount']);
                    if ($ft['total_credit_amount'] > 0) {
                        $balance += $amount;
                        $selected_financial_details[$sfd_key]['balance_amt'] = $balance;
                    } elseif ($ft['total_debit_amount'] > 0) {
                        $balance -= $amount;
                        $selected_financial_details[$sfd_key]['balance_amt'] = $balance;
                    }
                }

                $ft['details'] = $selected_financial_details->toArray();
            }
        }

        $this->book_data = $transactions;
    }

    // Excel Export Method
    public function downloadExport()
    {
        $this->validate($this->book_rule(), $this->messages);
        
        if ($this->book_type == 'bank_book') {
            return Excel::download(new BankBookExport($this->start_date, $this->end_date, $this->bank_name, null), 'bank_book_export.xlsx');
        } else {
            return Excel::download(new BankBookExport($this->start_date, $this->end_date, null, $this->cash_type), 'cash_book_export.xlsx');
        }
    }

    // Render the view with the necessary data
    public function render()
    {
        $banks = Bank::where('status', 1)->get();
        $cash = CashType::where('status', 1)->get();
        return view('livewire.report.bank-book-livewire', compact('banks', 'cash'));
    }
}
Hacker Blog, Shell İndir, Sql İnjection, XSS Attacks, LFI Attacks, Social Hacking, Exploit Bot, Proxy Tools, Web Shell, PHP Shell, Alfa Shell İndir, Hacking Training Set, DDoS Script, Denial Of Service, Botnet, RFI Attacks, Encryption
Telegram @BIBIL_0DAY