Hoan thien core finance v2

This commit is contained in:
2026-04-25 04:04:14 +00:00
parent 86216ef872
commit 002c9a8b99
39 changed files with 1308 additions and 89 deletions

View File

@@ -62,7 +62,74 @@ class PaymentForm
->label('Số tiền thu')
->numeric()
->prefix('VND')
->required(),
->required()
->live(onBlur: true)
->helperText(function ($component) {
$data = $component->getContainer()->getRawState();
$contractId = $data['contract_id'] ?? null;
$scheduleItemId = $data['schedule_item_id'] ?? null;
if (! $contractId) {
return 'Vui lòng chọn hợp đồng trước.';
}
$contract = \App\Models\Contract::find($contractId);
if (! $contract) {
return null;
}
if ($scheduleItemId) {
$item = PaymentScheduleItem::find($scheduleItemId);
if ($item) {
$paid = $contract->payments()
->where('schedule_item_id', $scheduleItemId)
->when($component->getRecord() instanceof \App\Models\Payment, fn ($q, $r) => $q->where('id', '!=', $r->id))
->sum('amount');
$remaining = (float) $item->amount - (float) $paid;
return 'Công nợ đợt này: '.number_format($remaining).' VNĐ';
}
}
return 'Công nợ HĐ còn lại: '.number_format($contract->remaining_amount).' VNĐ';
})
->rules([
function ($component) {
return function (string $attribute, $value, \Closure $fail) use ($component) {
$data = $component->getContainer()->getRawState();
$contractId = $data['contract_id'] ?? null;
$scheduleItemId = $data['schedule_item_id'] ?? null;
if (! $contractId || ! is_numeric($value)) {
return;
}
$contract = \App\Models\Contract::find($contractId);
if (! $contract) {
return;
}
$maxAmount = null;
if ($scheduleItemId) {
$item = PaymentScheduleItem::find($scheduleItemId);
if ($item) {
$paid = $contract->payments()
->where('schedule_item_id', $scheduleItemId)
->when($component->getRecord() instanceof \App\Models\Payment, fn ($q, $r) => $q->where('id', '!=', $r->id))
->sum('amount');
$maxAmount = (float) $item->amount - (float) $paid;
}
} else {
$maxAmount = (float) $contract->remaining_amount;
}
if ($maxAmount !== null && (float) $value > $maxAmount) {
$fail('Số tiền thu không được vượt quá '.number_format($maxAmount).' VNĐ.');
}
};
},
]),
DatePicker::make('paid_date')
->label('Ngày thu')