@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', $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;
$pastPayments = \App\Models\Payment::with([
'lease',
'electricPayments',
'waterPayments',
'newPayments',
])
->where('lease_id', $entry->lease_id)
->where('payment_date', '<', $entry->payment_date) // Only past
->get();
// Add current entry manually
$pastPayments->push($entry);
$pastPaymentsTotal = $pastPayments->sum(
callback: function ($payment) use ($client) {
$total = 0;
if ($payment instanceof \App\Models\Payment) {
$rentAmount = $payment->Amount ;
$electricAmount = optional(
$payment->electricPayments,
)->sum('Amount');
$waterAmount = optional(
$payment->waterPayments,
)->sum('Amount');
$totalRent = $rentAmount;
$totalElectric = $electricAmount;
$totalWater = $waterAmount;
$paymentDate = Carbon\Carbon::parse(
$payment->payment_date,
);
$penaltyPerMonth = 0.02;
$penaltyRent = 0;
$penaltyElectric = 0;
$penaltyWater = 0;
if ($client->penalty === 'yes') {
// Filter newPayments specific to this payment
$relatedNewPayments = $payment->newPayments;
// Penalty end dates for rent and utilities
$penaltyEndDateRaw =
$relatedNewPayments->max('check_date') ??
$relatedNewPayments->max('date_of_payment');
$penaltyEndDate = $penaltyEndDateRaw
? Carbon\Carbon::parse($penaltyEndDateRaw)
: Carbon\Carbon::now();
$penaltyUtilityEndDateRaw = $relatedNewPayments->max(
'date_of_payment',
);
$penaltyUtilityEndDate = $penaltyUtilityEndDateRaw
? Carbon\Carbon::parse(
$penaltyUtilityEndDateRaw,
)
: Carbon\Carbon::now();
// R E N T P E N A L T Y
$dueDate = $paymentDate
->copy()
->addMonthsNoOverflow(1);
if (
$payment->type == 'electric' ||
$payment->type == 'water'
) {
if (
$penaltyEndDate->greaterThan(
$paymentDate,
)
) {
$monthsutilityOverdue = 0;
$penaltyStart = $paymentDate
->copy()
->addMonths(1)
->day(8);
if ($paymentDate->day <= 8) {
$penaltyStart = $paymentDate
->copy()
->addMonths(1)
->day(8);
} else {
// If the payment date is after the 8th, we need to wait for the next 8th
$penaltyStart = $paymentDate
->copy()
->addMonths(1)
->day(8);
}
// Count how many 8th days have passed
while (
$penaltyStart->lessThanOrEqualTo(
$penaltyEndDate,
)
) {
$monthsutilityOverdue++;
$penaltyStart->addMonth(); // Move to the next 8th of the month
}
if (
$monthsutilityOverdue > 0 &&
$totalRent > 0
) {
$penaltyRent =
$totalRent *
$penaltyPerMonth *
$monthsutilityOverdue;
}
}
} else {
if (
$penaltyEndDate->greaterThan($dueDate)
) {
$monthsOverdue = 0;
$currentDueDate = $dueDate->copy();
while (
$penaltyEndDate->greaterThan(
date: $currentDueDate,
)
) {
$monthsOverdue++;
$currentDueDate->addMonthsNoOverflow(
1,
);
}
if (
$monthsOverdue > 0 &&
$totalRent > 0
) {
$penaltyRent =
$totalRent *
$penaltyPerMonth *
$monthsOverdue;
}
}
}
}
$total =
$totalRent +
$totalElectric +
$totalWater +
$penaltyRent +
$penaltyElectric +
$penaltyWater;
}
return $total;
},
);
$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;
});
$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) }}
|