CasperSecurity
<?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 Carbon\Carbon;
use Livewire\Component;
class LedgerHeadReportLivewire extends Component
{
public $start_date, $end_date, $ledger_head_name, $lhd_type;
public $ob = 0; // Opening Balance
public $ledger_data = []; // To hold the ledger data
public $ledger_head = []; // To hold the ledger heads based on selected type
public $debit_amount = 0; // Total Debit
public $credit_amount = 0; // Total Credit
// 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;
}
// Update ledger heads based on the selected ledger type
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()
: [];
}
// Generate the report
public function show()
{
$ledger_head = null;
// Removing vendor, client, and employee logic
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 = [];
// Original conditions involving $vendor, $client, and $employee removed
$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();
$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', 'financial_transactions.voucher_no', '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;
}
}
}
// Render the view
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'));
}
}