|
@@ -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
|
|