@php
$deposit = 0;
$noData = true; // Flag to check if there is any data for the filtered month and year
// Get all units for the given property
$units = \App\Models\Lease::with('property', 'client', 'unit')
->where('property_id', request()->input('property_id'))
->whereHas('unit', function ($q) {
$q->orderByRaw('CAST(SUBSTRING(unit_name, -1) AS UNSIGNED) ASC, unit_name'); // Order by the last number in the unit name
})
->get();
@endphp
@forelse ($units as $unit)
@php
// Check if there is a new payment for the current unit
$entry = $newPayments->firstWhere('lease.unit_id', $unit->unit_id);
$totalRent = 0;
@endphp
@if ($entry)
@php
$noData = false;
// Ensure entry->lease exists before accessing its properties
if ($entry->lease) {
$client = \App\Models\Client::find($entry->lease->client_id);
$deposit += $entry->lease->security_deposit + $entry->lease->utilities;
// Calculate total payments with penalty if applicable
$pastPaymentsTotal = \App\Models\Payment::with([
'lease',
'electricPayments',
'waterPayments',
'newPayments',
])
->where('lease_id', $entry->lease_id)
->where('payment_date', '<=', $entry->payment_date)
->get()
->sum(function ($payment) use ($client) {
// Ensure the lease and payment are valid
$totalRent = 0;
// Check if payment is valid before proceeding
if ($payment instanceof \App\Models\Payment) {
// Correctly access the lease associated with this payment
$rentAmount = $payment->Amount;
$rentAmount = $payment->lease->getEscalatedRentPenaltyAmountWithPenalty(
$payment->payment_date,
$payment->lease->escalation ?? 12,
);
$totalRent += $rentAmount;
}
return $totalRent;
});
$pastPaidTotal = \App\Models\Payment::with([
'lease',
'electricPayments',
'newPayments',
'waterPayments',
])
->where('lease_id', $entry->lease_id)
->where('payment_date', '<=', $entry->payment_date)
->get()
->sum(function ($payment) {
$rentalAmount = $payment->newPayments->sum('rental_amount');
$electricBillAmount = $payment->newPayments->sum(
'electricbill_amount',
);
$waterBillAmount = $payment->newPayments->sum(
'waterbill_amount',
);
return $rentalAmount + $electricBillAmount + $waterBillAmount;
});
$totalAmount = $pastPaymentsTotal - $pastPaidTotal;
$totalAmount1 += $totalAmount;
}
@endphp
{{ $entry->lease->unit->unit_name }} |
{{ $entry->lease->client->owners_name }} |
{{ \Carbon\Carbon::parse($entry->payment_date)->addMonth()->format('d-M-y') }}
|
{{ number_format($entry->Amount, 2) }} |
{{ number_format($entry->lease->security_deposit + $entry->lease->utilities, 2) }}
|
{{ $totalAmount >= 0 ? number_format($totalAmount, 2) : '0.00' }} |
@else
{{ $unit->unit->unit_name }} |
{{ $unit->client->owners_name }} |
@php
$month = request()->input('month');
$year = request()->input('year');
$deposit += $unit->security_deposit + $unit->utilities;
@endphp
{{ \Carbon\Carbon::create($year, $month, 1)->addMonth()->format('d-M-y') }}
|
{{ number_format($unit->rent_amount, 2) }} |
{{ number_format($unit->security_deposit + $unit->utilities, 2) }} |
0.00 |
@endif
@empty
@if ($noData)
No data available for the selected
filters |
@endif
@endforelse
Total: |
|
{{ number_format($deposit, 2) }}
|
{{ number_format($totalAmount1, 2) }}
|