You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditBudget.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\BudgetResource\Pages;
  3. use App\Filament\Company\Resources\Accounting\BudgetResource;
  4. use App\Models\Accounting\Budget;
  5. use App\Models\Accounting\BudgetItem;
  6. use Filament\Actions;
  7. use Filament\Resources\Pages\EditRecord;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Support\Carbon;
  10. class EditBudget extends EditRecord
  11. {
  12. protected static string $resource = BudgetResource::class;
  13. protected function getHeaderActions(): array
  14. {
  15. return [
  16. Actions\ViewAction::make(),
  17. Actions\DeleteAction::make(),
  18. ];
  19. }
  20. protected function mutateFormDataBeforeFill(array $data): array
  21. {
  22. /** @var Budget $budget */
  23. $budget = $this->record;
  24. $data['budgetItems'] = $budget->budgetItems->map(function ($budgetItem) {
  25. return [
  26. 'id' => $budgetItem->id,
  27. 'account_id' => $budgetItem->account_id,
  28. 'total_amount' => $budgetItem->allocations->sum('amount'), // Calculate total dynamically
  29. 'amounts' => $budgetItem->allocations->mapWithKeys(function ($allocation) {
  30. return [$allocation->period => $allocation->amount]; // Use the correct period label
  31. })->toArray(),
  32. ];
  33. })->toArray();
  34. return $data;
  35. }
  36. protected function handleRecordUpdate(Model $record, array $data): Model
  37. {
  38. /** @var Budget $budget */
  39. $budget = $record;
  40. $budget->update([
  41. 'name' => $data['name'],
  42. 'interval_type' => $data['interval_type'],
  43. 'start_date' => $data['start_date'],
  44. 'end_date' => $data['end_date'],
  45. 'notes' => $data['notes'] ?? null,
  46. ]);
  47. $budgetItemIds = [];
  48. foreach ($data['budgetItems'] as $itemData) {
  49. /** @var BudgetItem $budgetItem */
  50. $budgetItem = $budget->budgetItems()->updateOrCreate(
  51. ['id' => $itemData['id'] ?? null],
  52. ['account_id' => $itemData['account_id']]
  53. );
  54. $budgetItemIds[] = $budgetItem->id;
  55. $budgetItem->allocations()->delete();
  56. $allocationStart = Carbon::parse($data['start_date']);
  57. foreach ($itemData['amounts'] as $periodLabel => $amount) {
  58. $allocationEnd = self::calculateEndDate($allocationStart, $data['interval_type']);
  59. // Recreate allocations
  60. $budgetItem->allocations()->create([
  61. 'period' => $periodLabel,
  62. 'interval_type' => $data['interval_type'],
  63. 'start_date' => $allocationStart->toDateString(),
  64. 'end_date' => $allocationEnd->toDateString(),
  65. 'amount' => $amount,
  66. ]);
  67. $allocationStart = $allocationEnd->addDay();
  68. }
  69. }
  70. $budget->budgetItems()->whereNotIn('id', $budgetItemIds)->delete();
  71. return $budget;
  72. }
  73. private static function calculateEndDate(Carbon $startDate, string $intervalType): Carbon
  74. {
  75. return match ($intervalType) {
  76. 'quarter' => $startDate->copy()->addMonths(2)->endOfMonth(),
  77. 'year' => $startDate->copy()->endOfYear(),
  78. default => $startDate->copy()->endOfMonth(),
  79. };
  80. }
  81. }