Andrew Wallo 10 miesięcy temu
rodzic
commit
98707e816e

+ 3
- 1
app/Filament/Company/Resources/Sales/InvoiceResource/Widgets/InvoiceOverview.php Wyświetl plik

@@ -45,7 +45,9 @@ class InvoiceOverview extends EnhancedStatsOverviewWidget
45 45
 
46 46
         $totalValidInvoiceCount = $validInvoices->count();
47 47
 
48
-        $averageInvoiceTotal = $totalValidInvoiceCount > 0 ? $totalValidInvoiceAmount / $totalValidInvoiceCount : 0;
48
+        $averageInvoiceTotal = $totalValidInvoiceCount > 0
49
+            ? (int) round($totalValidInvoiceAmount / $totalValidInvoiceCount)
50
+            : 0;
49 51
 
50 52
         $averagePaymentTime = $this->getPageTableQuery()
51 53
             ->whereNotNull('paid_at')

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

@@ -133,6 +133,11 @@ class Account extends Model
133 133
         return self::where('name', 'Accounts Payable')->firstOrFail();
134 134
     }
135 135
 
136
+    public static function getSalesDiscountAccount(): self
137
+    {
138
+        return self::where('name', 'Sales Discount')->firstOrFail();
139
+    }
140
+
136 141
     protected static function newFactory(): Factory
137 142
     {
138 143
         return AccountFactory::new();

+ 29
- 1
app/Models/Accounting/Invoice.php Wyświetl plik

@@ -14,6 +14,7 @@ use App\Enums\Accounting\TransactionType;
14 14
 use App\Filament\Company\Resources\Sales\InvoiceResource;
15 15
 use App\Models\Common\Client;
16 16
 use App\Observers\InvoiceObserver;
17
+use App\Utilities\Currency\CurrencyConverter;
17 18
 use Filament\Actions\Action;
18 19
 use Filament\Actions\MountableAction;
19 20
 use Filament\Actions\ReplicateAction;
@@ -267,7 +268,11 @@ class Invoice extends Model
267 268
             'description' => $baseDescription,
268 269
         ]);
269 270
 
270
-        foreach ($this->lineItems as $lineItem) {
271
+        $totalLineItemSubtotal = (int) $this->lineItems()->sum('subtotal');
272
+        $invoiceDiscountTotalCents = (int) $this->getRawOriginal('discount_total');
273
+        $remainingDiscountCents = $invoiceDiscountTotalCents;
274
+
275
+        foreach ($this->lineItems as $index => $lineItem) {
271 276
             $lineItemDescription = "{$baseDescription} › {$lineItem->offering->name}";
272 277
 
273 278
             $transaction->journalEntries()->create([
@@ -287,6 +292,29 @@ class Invoice extends Model
287 292
                     'description' => $lineItemDescription,
288 293
                 ]);
289 294
             }
295
+
296
+            if ($this->discount_method === 'invoice' && $totalLineItemSubtotal > 0) {
297
+                $lineItemSubtotalCents = (int) $lineItem->getRawOriginal('subtotal');
298
+
299
+                if ($index === $this->lineItems->count() - 1) {
300
+                    $lineItemDiscount = $remainingDiscountCents;
301
+                } else {
302
+                    $lineItemDiscount = (int) round(
303
+                        ($lineItemSubtotalCents / $totalLineItemSubtotal) * $invoiceDiscountTotalCents
304
+                    );
305
+                    $remainingDiscountCents -= $lineItemDiscount;
306
+                }
307
+
308
+                if ($lineItemDiscount > 0) {
309
+                    $transaction->journalEntries()->create([
310
+                        'company_id' => $this->company_id,
311
+                        'type' => JournalEntryType::Debit,
312
+                        'account_id' => Account::getSalesDiscountAccount()->id,
313
+                        'amount' => CurrencyConverter::convertCentsToFormatSimple($lineItemDiscount),
314
+                        'description' => "{$lineItemDescription} (Proportional Discount)",
315
+                    ]);
316
+                }
317
+            }
290 318
         }
291 319
     }
292 320
 

Ładowanie…
Anuluj
Zapisz