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/LedgerHeadReportLivewire.php

<?php
namespace App\Http\Livewire\Report;

use App\Models\GlOpeningBalance;
use App\Models\SlOpeningBalance;
use App\Models\LedgerHead;
use App\Models\FinancialTransactions;
use App\Models\FinancialTransactionsLedgerHead;
use App\Models\LedgerheadType;
use App\Exports\LedgerReportExport;
use Maatwebsite\Excel\Facades\Excel;
use Carbon\Carbon;
use Livewire\Component;

class LedgerHeadReportLivewire extends Component
{
    public $start_date, $end_date, $ledger_head_name, $lhd_type;
    public $ob = 0; 
    public $ledger_data = []; 
    public $ledger_head = []; 
    public $debit_amount = 0; 
    public $credit_amount = 0; 

    // Validation rules
    public function book_rule()
    {
        return [
            'start_date' => 'required|date|before_or_equal:end_date',
            'end_date' => 'required|date|after_or_equal:start_date',
        ];
    }

    // Custom error messages
    public $messages = [
        'start_date.required' => 'Start date is required.',
        'end_date.required' => 'End date is required.',
        'start_date.before_or_equal' => 'Start date cannot be after the end date.',
        'end_date.after_or_equal' => 'End date cannot be before the start date.',
    ];

    // Component initialization
    public function mount()
    {
        $this->start_date = $this->end_date = now()->format('Y-m-d'); // Default to today's date
    }

    // Get the start date of the financial year
    private function getFinancialYearStartDate()
    {
        $currentDate = Carbon::now();
        $year = $currentDate->year;
        $financialYearStart = Carbon::create($year, 4, 1);

        return $currentDate->lessThan($financialYearStart) ? Carbon::create($year - 1, 4, 1) : $financialYearStart;
    }

    // Get the end date of the financial year
    private function getFinancialYearEndDate()
    {
        $currentDate = Carbon::now();
        $year = $currentDate->year;
        $financialYearEnd = Carbon::create($year, 3, 31, 23, 59, 59);

        return $currentDate->greaterThanOrEqualTo(Carbon::create($year, 4, 1)) 
            ? Carbon::create($year + 1, 3, 31, 23, 59, 59) 
            : $financialYearEnd;
    }

    public function updatedLhdType($dt)
    {
        $this->ledger_head_name = null; // Reset the ledger head name
        $this->ledger_head = $dt 
            ? LedgerHead::where('ledger_type_id', $dt)->where('status', 1)->get() 
            : [];
    }

    public function show()
    {
        $ledger_head = null;
        if ($this->ledger_head_name != null) {
            $ledger_head = LedgerHead::where('ledger_head_name', $this->ledger_head_name)->latest()->first();
            if ($ledger_head == null) {
                $this->ledger_head_name = null;
            }
        }

        $this->validate($this->book_rule(), $this->messages);

        if ($ledger_head != 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;
            $opening_balance = 0;
            $financial_details = [];
            $financial_transaction_old = [];

            // Fetch the opening balance for the ledger head
            $opening = SlOpeningBalance::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();

            // Fetch financial transactions before the start date
            $financial_transaction_old = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*')
                ->join('financial_transactions', 'financial_transactions_ledger_heads.finance_transaction_id', '=', 'financial_transactions.id')
                ->where('financial_transactions_ledger_heads.ledger_head_id', $ledger_head['id'])
                ->whereDate('financial_transactions.voucher_date', '>=', Carbon::parse($financial_year_start_date)->format('Y-m-d'))
                ->whereDate('financial_transactions.voucher_date', '<=', Carbon::parse($this->start_date)->format('Y-m-d'))
                ->where('financial_transactions.voucher_status', 2)
                ->where('financial_transactions_ledger_heads.status', 1)
                ->get();

            $financial_transaction = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*', 'financial_transactions.voucher_no', 'financial_transactions.voucher_particulars', 'ledger_heads.ledger_head_name')
                ->join('financial_transactions', 'financial_transactions_ledger_heads.finance_transaction_id', '=', 'financial_transactions.id')
                ->join('ledger_heads', 'financial_transactions_ledger_heads.ledger_head_id', '=', 'ledger_heads.id')
                ->where('financial_transactions_ledger_heads.ledger_head_id', $ledger_head['id'])
                ->whereDate('financial_transactions.voucher_date', '>=', Carbon::parse($this->start_date)->format('Y-m-d'))
                ->whereDate('financial_transactions.voucher_date', '<=', Carbon::parse($this->end_date)->format('Y-m-d'))
                ->where('financial_transactions.voucher_status', 2)
                ->where('financial_transactions_ledger_heads.status', 1)
                ->get();

            if ($opening != null) {
                $this->ob = $opening['amount'];
            }

            if (sizeof($financial_transaction_old) > 0) {
                foreach ($financial_transaction_old as $fdo) {
                    $opening_balance += $fdo['amount'];
                }
                if ($opening_balance > 0) {
                    $this->ob += $opening_balance;
                }
            }

            $balance += $this->ob;
            if (sizeof($financial_transaction) > 0) {
                foreach ($financial_transaction as $key => $fd) {
                    $balance += $fd['amount'];
                    $financial_transaction[$key]['balance_amt'] = $balance;
                }
                $this->ledger_data = $financial_transaction;
            } else {
                $this->ledger_data = [];
            }
        } else {
            $this->ledger_data = [];
        }
    }

    public function exportLedgerReport()
    {
        $ledgerTypeId = $this->lhd_type;
        $ledgerHeadName = $this->ledger_head_name;
        $startDate = $this->start_date;
        $endDate = $this->end_date;

        if (!$ledgerTypeId || !$ledgerHeadName || !$startDate || !$endDate) {
            session()->flash('error', 'Please fill in all the filters before exporting.');
            return;
        }
        $ledgerHead = LedgerHead::where('ledger_head_name', $ledgerHeadName)->first();
        if (!$ledgerHead) {
            session()->flash('error', 'Ledger Head not found.');
            return;
        }

        $ledgerData = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*', 'financial_transactions.voucher_no', 'financial_transactions.voucher_particulars', 'ledger_heads.ledger_head_name', 'financial_transactions.created_at')
            ->join('financial_transactions', 'financial_transactions_ledger_heads.finance_transaction_id', '=', 'financial_transactions.id')
            ->join('ledger_heads', 'financial_transactions_ledger_heads.ledger_head_id', '=', 'ledger_heads.id')
            ->where('financial_transactions_ledger_heads.ledger_head_id', $ledgerHead->id)
            ->whereDate('financial_transactions.created_at', '>=', Carbon::parse($startDate)->format('Y-m-d'))
            ->whereDate('financial_transactions.created_at', '<=', Carbon::parse($endDate)->format('Y-m-d'))
            ->where('financial_transactions.voucher_status', 2)
            ->where('financial_transactions_ledger_heads.status', 1)
            ->get();

        if ($ledgerData->isEmpty()) {
            session()->flash('error', 'No data found for the given filters.');
            return;
        }
        return Excel::download(new LedgerReportExport($ledgerData, $this->ob, $startDate, $endDate, $ledgerHeadName), 'ledger_report.xlsx');
    }
    
    public function render()
    {
        $ledger_head_types = LedgerHeadType::whereIn('id', [1, 2, 3, 6, 7])
            ->where('status', 1)
            ->get();

        return view('livewire.report.ledger-head-report-livewire', compact('ledger_head_types'));
    }
}
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