Hoan thien core finance v2
This commit is contained in:
@@ -31,6 +31,12 @@ class PaymentResource extends Resource
|
||||
return PaymentsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
return parent::getEloquentQuery()
|
||||
->with(['scheduleItem.payments']);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -29,9 +29,50 @@ class PaymentsTable
|
||||
Tables\Columns\TextColumn::make('receipt_number')
|
||||
->label('Số phiếu thu')
|
||||
->searchable(),
|
||||
Tables\Columns\TextColumn::make('scheduleItem.type')
|
||||
->label('Loại đợt')
|
||||
->placeholder('Tạm ứng')
|
||||
->formatStateUsing(fn ($state) => $state?->getLabel()),
|
||||
|
||||
Tables\Columns\TextColumn::make('scheduleItem.installment_no')
|
||||
->label('Đợt TT')
|
||||
->placeholder('Tạm ứng'),
|
||||
|
||||
Tables\Columns\TextColumn::make('reconciliation_status')
|
||||
->label('Đối soát')
|
||||
->badge()
|
||||
->color(function ($record) {
|
||||
if (! $record->scheduleItem) {
|
||||
return 'gray';
|
||||
}
|
||||
$remaining = (float) $record->scheduleItem->remaining_amount;
|
||||
if ($remaining == 0) {
|
||||
return 'success';
|
||||
}
|
||||
if ($remaining > 0) {
|
||||
return 'warning';
|
||||
}
|
||||
return 'danger';
|
||||
})
|
||||
->state(function ($record) {
|
||||
if (! $record->scheduleItem) {
|
||||
return 'Tạm ứng';
|
||||
}
|
||||
$remaining = (float) $record->scheduleItem->remaining_amount;
|
||||
if ($remaining == 0) {
|
||||
return 'Đủ';
|
||||
}
|
||||
if ($remaining > 0) {
|
||||
return 'Thiếu';
|
||||
}
|
||||
return 'Thừa';
|
||||
}),
|
||||
|
||||
Tables\Columns\TextColumn::make('scheduleItem.remaining_amount')
|
||||
->label('Còn thiếu')
|
||||
->money('VND')
|
||||
->placeholder('-')
|
||||
->color('danger'),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('method')
|
||||
|
||||
Reference in New Issue
Block a user