Andrew Wallo 10 months ago
parent
commit
1e7bd101ab

+ 9
- 3
app/Filament/Company/Resources/Purchases/BillResource.php View File

54
                             Forms\Components\Group::make([
54
                             Forms\Components\Group::make([
55
                                 Forms\Components\TextInput::make('bill_number')
55
                                 Forms\Components\TextInput::make('bill_number')
56
                                     ->label('Bill Number')
56
                                     ->label('Bill Number')
57
-                                    ->default(fn () => Bill::getNextDocumentNumber()),
57
+                                    ->default(fn () => Bill::getNextDocumentNumber())
58
+                                    ->required(),
58
                                 Forms\Components\TextInput::make('order_number')
59
                                 Forms\Components\TextInput::make('order_number')
59
                                     ->label('P.O/S.O Number'),
60
                                     ->label('P.O/S.O Number'),
60
                                 Forms\Components\DatePicker::make('date')
61
                                 Forms\Components\DatePicker::make('date')
61
                                     ->label('Bill Date')
62
                                     ->label('Bill Date')
62
-                                    ->default(now()),
63
+                                    ->default(now())
64
+                                    ->disabled(function (?Bill $record) {
65
+                                        return $record?->hasPayments();
66
+                                    })
67
+                                    ->required(),
63
                                 Forms\Components\DatePicker::make('due_date')
68
                                 Forms\Components\DatePicker::make('due_date')
64
                                     ->label('Due Date')
69
                                     ->label('Due Date')
65
                                     ->default(function () use ($company) {
70
                                     ->default(function () use ($company) {
66
                                         return now()->addDays($company->defaultBill->payment_terms->getDays());
71
                                         return now()->addDays($company->defaultBill->payment_terms->getDays());
67
-                                    }),
72
+                                    })
73
+                                    ->required(),
68
                             ])->grow(true),
74
                             ])->grow(true),
69
                         ])->from('md'),
75
                         ])->from('md'),
70
                         TableRepeater::make('lineItems')
76
                         TableRepeater::make('lineItems')

+ 3
- 0
app/Filament/Company/Resources/Sales/InvoiceResource.php View File

109
                                     ->label('Invoice Date')
109
                                     ->label('Invoice Date')
110
                                     ->live()
110
                                     ->live()
111
                                     ->default(now())
111
                                     ->default(now())
112
+                                    ->disabled(function (?Invoice $record) {
113
+                                        return $record?->hasPayments();
114
+                                    })
112
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
115
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
113
                                         $date = $state;
116
                                         $date = $state;
114
                                         $dueDate = $get('due_date');
117
                                         $dueDate = $get('due_date');

+ 5
- 0
app/Models/Accounting/Bill.php View File

121
         ]);
121
         ]);
122
     }
122
     }
123
 
123
 
124
+    public function hasPayments(): bool
125
+    {
126
+        return $this->payments->isNotEmpty();
127
+    }
128
+
124
     public static function getNextDocumentNumber(): string
129
     public static function getNextDocumentNumber(): string
125
     {
130
     {
126
         $company = auth()->user()->currentCompany;
131
         $company = auth()->user()->currentCompany;

+ 5
- 0
app/Models/Accounting/Invoice.php View File

159
         return in_array($this->status, InvoiceStatus::canBeOverdue());
159
         return in_array($this->status, InvoiceStatus::canBeOverdue());
160
     }
160
     }
161
 
161
 
162
+    public function hasPayments(): bool
163
+    {
164
+        return $this->payments->isNotEmpty();
165
+    }
166
+
162
     public static function getNextDocumentNumber(): string
167
     public static function getNextDocumentNumber(): string
163
     {
168
     {
164
         $company = auth()->user()->currentCompany;
169
         $company = auth()->user()->currentCompany;

+ 11
- 3
app/Observers/TransactionObserver.php View File

35
     {
35
     {
36
         $this->transactionService->createJournalEntries($transaction);
36
         $this->transactionService->createJournalEntries($transaction);
37
 
37
 
38
-        if (! $transaction->is_payment) {
38
+        if (! $transaction->transactionable) {
39
             return;
39
             return;
40
         }
40
         }
41
 
41
 
57
 
57
 
58
         $this->transactionService->updateJournalEntries($transaction);
58
         $this->transactionService->updateJournalEntries($transaction);
59
 
59
 
60
-        if (! $transaction->is_payment) {
60
+        if (! $transaction->transactionable) {
61
             return;
61
             return;
62
         }
62
         }
63
 
63
 
78
         DB::transaction(function () use ($transaction) {
78
         DB::transaction(function () use ($transaction) {
79
             $this->transactionService->deleteJournalEntries($transaction);
79
             $this->transactionService->deleteJournalEntries($transaction);
80
 
80
 
81
-            if (! $transaction->is_payment) {
81
+            if (! $transaction->transactionable) {
82
                 return;
82
                 return;
83
             }
83
             }
84
 
84
 
103
 
103
 
104
     protected function updateInvoiceTotals(Invoice $invoice, ?Transaction $excludedTransaction = null): void
104
     protected function updateInvoiceTotals(Invoice $invoice, ?Transaction $excludedTransaction = null): void
105
     {
105
     {
106
+        if (! $invoice->hasPayments()) {
107
+            return;
108
+        }
109
+
106
         $depositTotal = (int) $invoice->deposits()
110
         $depositTotal = (int) $invoice->deposits()
107
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
111
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
108
             ->sum('amount');
112
             ->sum('amount');
138
 
142
 
139
     protected function updateBillTotals(Bill $bill, ?Transaction $excludedTransaction = null): void
143
     protected function updateBillTotals(Bill $bill, ?Transaction $excludedTransaction = null): void
140
     {
144
     {
145
+        if (! $bill->hasPayments()) {
146
+            return;
147
+        }
148
+
141
         $withdrawalTotal = (int) $bill->withdrawals()
149
         $withdrawalTotal = (int) $bill->withdrawals()
142
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
150
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
143
             ->sum('amount');
151
             ->sum('amount');

Loading…
Cancel
Save