Andrew Wallo 9 months ago
parent
commit
3232187ee3

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

426
                         ->failureNotificationTitle('Failed to Record Payments')
426
                         ->failureNotificationTitle('Failed to Record Payments')
427
                         ->deselectRecordsAfterCompletion()
427
                         ->deselectRecordsAfterCompletion()
428
                         ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
428
                         ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
429
-                            $cantRecordPayments = $records->contains(fn (Bill $bill) => ! $bill->canRecordPayment());
429
+                            $isInvalid = $records->contains(fn (Bill $bill) => ! $bill->canRecordPayment());
430
 
430
 
431
-                            if ($cantRecordPayments) {
431
+                            if ($isInvalid) {
432
                                 Notification::make()
432
                                 Notification::make()
433
                                     ->title('Payment Recording Failed')
433
                                     ->title('Payment Recording Failed')
434
                                     ->body('Bills that are either paid, voided, or are in a foreign currency cannot be processed through bulk payments. Please adjust your selection and try again.')
434
                                     ->body('Bills that are either paid, voided, or are in a foreign currency cannot be processed through bulk payments. Please adjust your selection and try again.')

+ 10
- 11
app/Filament/Company/Resources/Sales/EstimateResource.php View File

362
                             'expiration_date',
362
                             'expiration_date',
363
                             'approved_at',
363
                             'approved_at',
364
                             'accepted_at',
364
                             'accepted_at',
365
+                            'converted_at',
365
                             'declined_at',
366
                             'declined_at',
366
                             'last_sent_at',
367
                             'last_sent_at',
367
                             'last_viewed_at',
368
                             'last_viewed_at',
393
                         ->successNotificationTitle('Estimates Approved')
394
                         ->successNotificationTitle('Estimates Approved')
394
                         ->failureNotificationTitle('Failed to Approve Estimates')
395
                         ->failureNotificationTitle('Failed to Approve Estimates')
395
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
396
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
396
-                            $containsNonDrafts = $records->contains(fn (Estimate $record) => ! $record->isDraft());
397
+                            $isInvalid = $records->contains(fn (Estimate $record) => ! $record->canBeApproved());
397
 
398
 
398
-                            if ($containsNonDrafts) {
399
+                            if ($isInvalid) {
399
                                 Notification::make()
400
                                 Notification::make()
400
                                     ->title('Approval Failed')
401
                                     ->title('Approval Failed')
401
                                     ->body('Only draft estimates can be approved. Please adjust your selection and try again.')
402
                                     ->body('Only draft estimates can be approved. Please adjust your selection and try again.')
420
                         ->successNotificationTitle('Estimates Sent')
421
                         ->successNotificationTitle('Estimates Sent')
421
                         ->failureNotificationTitle('Failed to Mark Estimates as Sent')
422
                         ->failureNotificationTitle('Failed to Mark Estimates as Sent')
422
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
423
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
423
-                            $doesntContainUnsent = $records->contains(fn (Estimate $record) => $record->status !== EstimateStatus::Unsent);
424
+                            $isInvalid = $records->contains(fn (Estimate $record) => ! $record->canBeMarkedAsSent());
424
 
425
 
425
-                            if ($doesntContainUnsent) {
426
+                            if ($isInvalid) {
426
                                 Notification::make()
427
                                 Notification::make()
427
                                     ->title('Sending Failed')
428
                                     ->title('Sending Failed')
428
                                     ->body('Only unsent estimates can be marked as sent. Please adjust your selection and try again.')
429
                                     ->body('Only unsent estimates can be marked as sent. Please adjust your selection and try again.')
435
                         })
436
                         })
436
                         ->action(function (Collection $records, Tables\Actions\BulkAction $action) {
437
                         ->action(function (Collection $records, Tables\Actions\BulkAction $action) {
437
                             $records->each(function (Estimate $record) {
438
                             $records->each(function (Estimate $record) {
438
-                                $record->updateQuietly([
439
-                                    'status' => EstimateStatus::Sent,
440
-                                ]);
439
+                                $record->markAsSent();
441
                             });
440
                             });
442
 
441
 
443
                             $action->success();
442
                             $action->success();
449
                         ->successNotificationTitle('Estimates Accepted')
448
                         ->successNotificationTitle('Estimates Accepted')
450
                         ->failureNotificationTitle('Failed to Mark Estimates as Accepted')
449
                         ->failureNotificationTitle('Failed to Mark Estimates as Accepted')
451
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
450
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
452
-                            $doesntContainSent = $records->contains(fn (Estimate $record) => $record->status !== EstimateStatus::Sent || $record->wasAccepted());
451
+                            $isInvalid = $records->contains(fn (Estimate $record) => ! $record->canBeMarkedAsAccepted());
453
 
452
 
454
-                            if ($doesntContainSent) {
453
+                            if ($isInvalid) {
455
                                 Notification::make()
454
                                 Notification::make()
456
                                     ->title('Acceptance Failed')
455
                                     ->title('Acceptance Failed')
457
                                     ->body('Only sent estimates that haven\'t been accepted can be marked as accepted. Please adjust your selection and try again.')
456
                                     ->body('Only sent estimates that haven\'t been accepted can be marked as accepted. Please adjust your selection and try again.')
480
                         ->successNotificationTitle('Estimates Declined')
479
                         ->successNotificationTitle('Estimates Declined')
481
                         ->failureNotificationTitle('Failed to Mark Estimates as Declined')
480
                         ->failureNotificationTitle('Failed to Mark Estimates as Declined')
482
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
481
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
483
-                            $doesntContainSent = $records->contains(fn (Estimate $record) => $record->status !== EstimateStatus::Sent || $record->wasDeclined());
482
+                            $isInvalid = $records->contains(fn (Estimate $record) => ! $record->canBeMarkedAsDeclined());
484
 
483
 
485
-                            if ($doesntContainSent) {
484
+                            if ($isInvalid) {
486
                                 Notification::make()
485
                                 Notification::make()
487
                                     ->title('Declination Failed')
486
                                     ->title('Declination Failed')
488
                                     ->body('Only sent estimates that haven\'t been declined can be marked as declined. Please adjust your selection and try again.')
487
                                     ->body('Only sent estimates that haven\'t been declined can be marked as declined. Please adjust your selection and try again.')

+ 10
- 6
app/Filament/Company/Resources/Sales/InvoiceResource.php View File

468
                             'invoice_number',
468
                             'invoice_number',
469
                             'date',
469
                             'date',
470
                             'due_date',
470
                             'due_date',
471
+                            'approved_at',
472
+                            'paid_at',
473
+                            'last_sent_at',
474
+                            'last_viewed_at',
471
                         ])
475
                         ])
472
                         ->beforeReplicaSaved(function (Invoice $replica) {
476
                         ->beforeReplicaSaved(function (Invoice $replica) {
473
                             $replica->status = InvoiceStatus::Draft;
477
                             $replica->status = InvoiceStatus::Draft;
491
                         ->successNotificationTitle('Invoices Approved')
495
                         ->successNotificationTitle('Invoices Approved')
492
                         ->failureNotificationTitle('Failed to Approve Invoices')
496
                         ->failureNotificationTitle('Failed to Approve Invoices')
493
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
497
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
494
-                            $containsNonDrafts = $records->contains(fn (Invoice $record) => ! $record->isDraft());
498
+                            $isInvalid = $records->contains(fn (Invoice $record) => ! $record->canBeApproved());
495
 
499
 
496
-                            if ($containsNonDrafts) {
500
+                            if ($isInvalid) {
497
                                 Notification::make()
501
                                 Notification::make()
498
                                     ->title('Approval Failed')
502
                                     ->title('Approval Failed')
499
                                     ->body('Only draft invoices can be approved. Please adjust your selection and try again.')
503
                                     ->body('Only draft invoices can be approved. Please adjust your selection and try again.')
518
                         ->successNotificationTitle('Invoices Sent')
522
                         ->successNotificationTitle('Invoices Sent')
519
                         ->failureNotificationTitle('Failed to Mark Invoices as Sent')
523
                         ->failureNotificationTitle('Failed to Mark Invoices as Sent')
520
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
524
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
521
-                            $doesntContainUnsent = $records->contains(fn (Invoice $record) => $record->status !== InvoiceStatus::Unsent);
525
+                            $isInvalid = $records->contains(fn (Invoice $record) => ! $record->canBeMarkedAsSent());
522
 
526
 
523
-                            if ($doesntContainUnsent) {
527
+                            if ($isInvalid) {
524
                                 Notification::make()
528
                                 Notification::make()
525
                                     ->title('Sending Failed')
529
                                     ->title('Sending Failed')
526
                                     ->body('Only unsent invoices can be marked as sent. Please adjust your selection and try again.')
530
                                     ->body('Only unsent invoices can be marked as sent. Please adjust your selection and try again.')
550
                         ->failureNotificationTitle('Failed to Record Payments')
554
                         ->failureNotificationTitle('Failed to Record Payments')
551
                         ->deselectRecordsAfterCompletion()
555
                         ->deselectRecordsAfterCompletion()
552
                         ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
556
                         ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
553
-                            $cantRecordPayments = $records->contains(fn (Invoice $record) => ! $record->canBulkRecordPayment());
557
+                            $isInvalid = $records->contains(fn (Invoice $record) => ! $record->canBulkRecordPayment());
554
 
558
 
555
-                            if ($cantRecordPayments) {
559
+                            if ($isInvalid) {
556
                                 Notification::make()
560
                                 Notification::make()
557
                                     ->title('Payment Recording Failed')
561
                                     ->title('Payment Recording Failed')
558
                                     ->body('Invoices that are either draft, paid, overpaid, voided, or are in a foreign currency cannot be processed through bulk payments. Please adjust your selection and try again.')
562
                                     ->body('Invoices that are either draft, paid, overpaid, voided, or are in a foreign currency cannot be processed through bulk payments. Please adjust your selection and try again.')

+ 23
- 19
app/Models/Accounting/Bill.php View File

394
             })
394
             })
395
             ->databaseTransaction()
395
             ->databaseTransaction()
396
             ->after(function (self $original, self $replica) {
396
             ->after(function (self $original, self $replica) {
397
-                $original->lineItems->each(function (DocumentLineItem $lineItem) use ($replica) {
398
-                    $replicaLineItem = $lineItem->replicate([
399
-                        'documentable_id',
400
-                        'documentable_type',
401
-                        'subtotal',
402
-                        'total',
403
-                        'created_by',
404
-                        'updated_by',
405
-                        'created_at',
406
-                        'updated_at',
407
-                    ]);
408
-
409
-                    $replicaLineItem->documentable_id = $replica->id;
410
-                    $replicaLineItem->documentable_type = $replica->getMorphClass();
411
-
412
-                    $replicaLineItem->save();
413
-
414
-                    $replicaLineItem->adjustments()->sync($lineItem->adjustments->pluck('id'));
415
-                });
397
+                $original->replicateLineItems($replica);
416
             })
398
             })
417
             ->successRedirectUrl(static function (self $replica) {
399
             ->successRedirectUrl(static function (self $replica) {
418
                 return BillResource::getUrl('edit', ['record' => $replica]);
400
                 return BillResource::getUrl('edit', ['record' => $replica]);
419
             });
401
             });
420
     }
402
     }
403
+
404
+    public function replicateLineItems(Model $target): void
405
+    {
406
+        $this->lineItems->each(function (DocumentLineItem $lineItem) use ($target) {
407
+            $replica = $lineItem->replicate([
408
+                'documentable_id',
409
+                'documentable_type',
410
+                'subtotal',
411
+                'total',
412
+                'created_by',
413
+                'updated_by',
414
+                'created_at',
415
+                'updated_at',
416
+            ]);
417
+
418
+            $replica->documentable_id = $target->id;
419
+            $replica->documentable_type = $target->getMorphClass();
420
+            $replica->save();
421
+
422
+            $replica->adjustments()->sync($lineItem->adjustments->pluck('id'));
423
+        });
424
+    }
421
 }
425
 }

+ 9
- 2
app/Models/Accounting/Estimate.php View File

171
 
171
 
172
     public function canBeMarkedAsDeclined(): bool
172
     public function canBeMarkedAsDeclined(): bool
173
     {
173
     {
174
-        return $this->hasBeenSent() && ! $this->wasDeclined();
174
+        return $this->hasBeenSent()
175
+            && ! $this->wasDeclined()
176
+            && ! $this->wasConverted()
177
+            && ! $this->wasAccepted();
175
     }
178
     }
176
 
179
 
177
     public function canBeMarkedAsSent(): bool
180
     public function canBeMarkedAsSent(): bool
181
 
184
 
182
     public function canBeMarkedAsAccepted(): bool
185
     public function canBeMarkedAsAccepted(): bool
183
     {
186
     {
184
-        return $this->hasBeenSent() && ! $this->wasAccepted();
187
+        return $this->hasBeenSent()
188
+            && ! $this->wasAccepted()
189
+            && ! $this->wasDeclined()
190
+            && ! $this->wasConverted();
185
     }
191
     }
186
 
192
 
187
     public function hasLineItems(): bool
193
     public function hasLineItems(): bool
308
                 'expiration_date',
314
                 'expiration_date',
309
                 'approved_at',
315
                 'approved_at',
310
                 'accepted_at',
316
                 'accepted_at',
317
+                'converted_at',
311
                 'declined_at',
318
                 'declined_at',
312
                 'last_sent_at',
319
                 'last_sent_at',
313
                 'last_viewed_at',
320
                 'last_viewed_at',

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

499
                 'approved_at',
499
                 'approved_at',
500
                 'paid_at',
500
                 'paid_at',
501
                 'last_sent_at',
501
                 'last_sent_at',
502
+                'last_viewed_at',
502
             ])
503
             ])
503
             ->modal(false)
504
             ->modal(false)
504
             ->beforeReplicaSaved(function (self $original, self $replica) {
505
             ->beforeReplicaSaved(function (self $original, self $replica) {

Loading…
Cancel
Save