Andrew Wallo 10 miesięcy temu
rodzic
commit
3d13f8a28d

+ 3
- 2
app/Console/Commands/UpdateOverdueInvoices.php Wyświetl plik

@@ -2,6 +2,7 @@
2 2
 
3 3
 namespace App\Console\Commands;
4 4
 
5
+use App\Jobs\ProcessOverdueInvoices;
5 6
 use Illuminate\Console\Command;
6 7
 
7 8
 class UpdateOverdueInvoices extends Command
@@ -23,8 +24,8 @@ class UpdateOverdueInvoices extends Command
23 24
     /**
24 25
      * Execute the console command.
25 26
      */
26
-    public function handle()
27
+    public function handle(): void
27 28
     {
28
-        $this->info('Processing overdue invoices...');
29
+        ProcessOverdueInvoices::dispatch();
29 30
     }
30 31
 }

+ 9
- 0
app/Enums/Accounting/InvoiceStatus.php Wyświetl plik

@@ -36,4 +36,13 @@ enum InvoiceStatus: string implements HasColor, HasLabel
36 36
             self::Overdue => 'danger',
37 37
         };
38 38
     }
39
+
40
+    public static function canBeOverdue(): array
41
+    {
42
+        return [
43
+            self::Partial,
44
+            self::Sent,
45
+            self::Unsent,
46
+        ];
47
+    }
39 48
 }

+ 12
- 9
app/Jobs/ProcessOverdueInvoices.php Wyświetl plik

@@ -2,26 +2,29 @@
2 2
 
3 3
 namespace App\Jobs;
4 4
 
5
+use App\Enums\Accounting\InvoiceStatus;
6
+use App\Models\Accounting\Invoice;
5 7
 use Illuminate\Contracts\Queue\ShouldQueue;
8
+use Illuminate\Foundation\Bus\Dispatchable;
6 9
 use Illuminate\Foundation\Queue\Queueable;
10
+use Illuminate\Queue\InteractsWithQueue;
11
+use Illuminate\Queue\SerializesModels;
7 12
 
8 13
 class ProcessOverdueInvoices implements ShouldQueue
9 14
 {
15
+    use Dispatchable;
16
+    use InteractsWithQueue;
10 17
     use Queueable;
11
-
12
-    /**
13
-     * Create a new job instance.
14
-     */
15
-    public function __construct()
16
-    {
17
-        //
18
-    }
18
+    use SerializesModels;
19 19
 
20 20
     /**
21 21
      * Execute the job.
22 22
      */
23 23
     public function handle(): void
24 24
     {
25
-        //
25
+        Invoice::query()
26
+            ->whereIn('status', InvoiceStatus::canBeOverdue())
27
+            ->where('due_date', '<', today())
28
+            ->update(['status' => InvoiceStatus::Overdue]);
26 29
     }
27 30
 }

+ 5
- 0
app/Models/Accounting/Invoice.php Wyświetl plik

@@ -124,6 +124,11 @@ class Invoice extends Model
124 124
         ]);
125 125
     }
126 126
 
127
+    public function canBeOverdue(): bool
128
+    {
129
+        return in_array($this->status, InvoiceStatus::canBeOverdue());
130
+    }
131
+
127 132
     public static function getNextDocumentNumber(): string
128 133
     {
129 134
         $company = auth()->user()->currentCompany;

+ 1
- 1
config/queue.php Wyświetl plik

@@ -38,7 +38,7 @@ return [
38 38
             'driver' => 'database',
39 39
             'connection' => env('DB_QUEUE_CONNECTION', null),
40 40
             'table' => env('DB_QUEUE_TABLE', 'jobs'),
41
-            'queue' => env('DB_QUEUE', 'translations'),
41
+            'queue' => env('DB_QUEUE', 'default'),
42 42
             'retry_after' => env('DB_QUEUE_RETRY_AFTER', 90),
43 43
             'after_commit' => false,
44 44
         ],

+ 2
- 2
database/factories/Accounting/InvoiceFactory.php Wyświetl plik

@@ -36,7 +36,7 @@ class InvoiceFactory extends Factory
36 36
             'invoice_number' => $this->faker->unique()->numerify('INV-#####'),
37 37
             'order_number' => $this->faker->unique()->numerify('ORD-#####'),
38 38
             'date' => $this->faker->dateTimeBetween('-1 year'),
39
-            'due_date' => $this->faker->dateTimeBetween('now', '+2 months'),
39
+            'due_date' => $this->faker->dateTimeBetween('-2 months', '+2 months'),
40 40
             'status' => InvoiceStatus::Draft,
41 41
             'currency_code' => 'USD',
42 42
             'terms' => $this->faker->sentence,
@@ -125,7 +125,7 @@ class InvoiceFactory extends Factory
125 125
 
126 126
             $this->recalculateTotals($invoice);
127 127
 
128
-            if ($invoice->due_date->isPast() && ! in_array($invoice->status, [InvoiceStatus::Draft, InvoiceStatus::Paid, InvoiceStatus::Void, InvoiceStatus::Overpaid])) {
128
+            if ($invoice->due_date->isPast() && $invoice->canBeOverdue()) {
129 129
                 $invoice->updateQuietly([
130 130
                     'status' => InvoiceStatus::Overdue,
131 131
                 ]);

+ 3
- 5
routes/console.php Wyświetl plik

@@ -1,8 +1,6 @@
1 1
 <?php
2 2
 
3
-use Illuminate\Foundation\Inspiring;
4
-use Illuminate\Support\Facades\Artisan;
3
+use App\Console\Commands\UpdateOverdueInvoices;
4
+use Illuminate\Support\Facades\Schedule;
5 5
 
6
-Artisan::command('inspire', function () {
7
-    $this->comment(Inspiring::quote());
8
-})->purpose('Display an inspiring quote')->hourly();
6
+Schedule::command(UpdateOverdueInvoices::class)->everyFiveMinutes();

Ładowanie…
Anuluj
Zapisz