Andrew Wallo 10 месяцев назад
Родитель
Сommit
98707e816e

+ 3
- 1
app/Filament/Company/Resources/Sales/InvoiceResource/Widgets/InvoiceOverview.php Просмотреть файл

45
 
45
 
46
         $totalValidInvoiceCount = $validInvoices->count();
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
         $averagePaymentTime = $this->getPageTableQuery()
52
         $averagePaymentTime = $this->getPageTableQuery()
51
             ->whereNotNull('paid_at')
53
             ->whereNotNull('paid_at')

+ 5
- 0
app/Models/Accounting/Account.php Просмотреть файл

133
         return self::where('name', 'Accounts Payable')->firstOrFail();
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
     protected static function newFactory(): Factory
141
     protected static function newFactory(): Factory
137
     {
142
     {
138
         return AccountFactory::new();
143
         return AccountFactory::new();

+ 29
- 1
app/Models/Accounting/Invoice.php Просмотреть файл

14
 use App\Filament\Company\Resources\Sales\InvoiceResource;
14
 use App\Filament\Company\Resources\Sales\InvoiceResource;
15
 use App\Models\Common\Client;
15
 use App\Models\Common\Client;
16
 use App\Observers\InvoiceObserver;
16
 use App\Observers\InvoiceObserver;
17
+use App\Utilities\Currency\CurrencyConverter;
17
 use Filament\Actions\Action;
18
 use Filament\Actions\Action;
18
 use Filament\Actions\MountableAction;
19
 use Filament\Actions\MountableAction;
19
 use Filament\Actions\ReplicateAction;
20
 use Filament\Actions\ReplicateAction;
267
             'description' => $baseDescription,
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
             $lineItemDescription = "{$baseDescription} › {$lineItem->offering->name}";
276
             $lineItemDescription = "{$baseDescription} › {$lineItem->offering->name}";
272
 
277
 
273
             $transaction->journalEntries()->create([
278
             $transaction->journalEntries()->create([
287
                     'description' => $lineItemDescription,
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
 

Загрузка…
Отмена
Сохранить