-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from andrewdwallo/development-3.x
Development 3.x
- Loading branch information
Showing
41 changed files
with
1,330 additions
and
1,000 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace App\Concerns; | ||
|
||
use App\Enums\Accounting\AdjustmentComputation; | ||
use App\Enums\Accounting\DocumentDiscountMethod; | ||
use App\Models\Accounting\Bill; | ||
use App\Models\Accounting\DocumentLineItem; | ||
use App\Utilities\Currency\CurrencyConverter; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
|
||
trait ManagesLineItems | ||
{ | ||
protected function handleLineItems(Model $record, Collection $lineItems): void | ||
{ | ||
foreach ($lineItems as $itemData) { | ||
$lineItem = isset($itemData['id']) | ||
? $record->lineItems->find($itemData['id']) | ||
: $record->lineItems()->make(); | ||
|
||
$lineItem->fill([ | ||
'offering_id' => $itemData['offering_id'], | ||
'description' => $itemData['description'], | ||
'quantity' => $itemData['quantity'], | ||
'unit_price' => $itemData['unit_price'], | ||
]); | ||
|
||
if (! $lineItem->exists) { | ||
$lineItem->documentable()->associate($record); | ||
} | ||
|
||
$lineItem->save(); | ||
|
||
$this->handleLineItemAdjustments($lineItem, $itemData, $record->discount_method); | ||
$this->updateLineItemTotals($lineItem, $record->discount_method); | ||
} | ||
} | ||
|
||
protected function deleteRemovedLineItems(Model $record, Collection $lineItems): void | ||
{ | ||
$existingLineItemIds = $record->lineItems->pluck('id'); | ||
$updatedLineItemIds = $lineItems->pluck('id')->filter(); | ||
$lineItemsToDelete = $existingLineItemIds->diff($updatedLineItemIds); | ||
|
||
if ($lineItemsToDelete->isNotEmpty()) { | ||
$record | ||
->lineItems() | ||
->whereIn('id', $lineItemsToDelete) | ||
->each(fn (DocumentLineItem $lineItem) => $lineItem->delete()); | ||
} | ||
} | ||
|
||
protected function handleLineItemAdjustments(DocumentLineItem $lineItem, array $itemData, DocumentDiscountMethod $discountMethod): void | ||
{ | ||
$isBill = $lineItem->documentable instanceof Bill; | ||
|
||
$taxType = $isBill ? 'purchaseTaxes' : 'salesTaxes'; | ||
$discountType = $isBill ? 'purchaseDiscounts' : 'salesDiscounts'; | ||
|
||
$adjustmentIds = collect($itemData[$taxType] ?? []) | ||
->merge($discountMethod->isPerLineItem() ? ($itemData[$discountType] ?? []) : []) | ||
->filter() | ||
->unique(); | ||
|
||
$lineItem->adjustments()->sync($adjustmentIds); | ||
$lineItem->refresh(); | ||
} | ||
|
||
protected function updateLineItemTotals(DocumentLineItem $lineItem, DocumentDiscountMethod $discountMethod): void | ||
{ | ||
$lineItem->updateQuietly([ | ||
'tax_total' => $lineItem->calculateTaxTotal()->getAmount(), | ||
'discount_total' => $discountMethod->isPerLineItem() | ||
? $lineItem->calculateDiscountTotal()->getAmount() | ||
: 0, | ||
]); | ||
} | ||
|
||
protected function updateDocumentTotals(Model $record, array $data): array | ||
{ | ||
$subtotalCents = $record->lineItems()->sum('subtotal'); | ||
$taxTotalCents = $record->lineItems()->sum('tax_total'); | ||
$discountTotalCents = $this->calculateDiscountTotal( | ||
DocumentDiscountMethod::parse($data['discount_method']), | ||
AdjustmentComputation::parse($data['discount_computation']), | ||
$data['discount_rate'] ?? null, | ||
$subtotalCents, | ||
$record | ||
); | ||
|
||
$grandTotalCents = $subtotalCents + $taxTotalCents - $discountTotalCents; | ||
|
||
return [ | ||
'subtotal' => CurrencyConverter::convertCentsToFormatSimple($subtotalCents), | ||
'tax_total' => CurrencyConverter::convertCentsToFormatSimple($taxTotalCents), | ||
'discount_total' => CurrencyConverter::convertCentsToFormatSimple($discountTotalCents), | ||
'total' => CurrencyConverter::convertCentsToFormatSimple($grandTotalCents), | ||
]; | ||
} | ||
|
||
protected function calculateDiscountTotal( | ||
DocumentDiscountMethod $discountMethod, | ||
?AdjustmentComputation $discountComputation, | ||
?string $discountRate, | ||
int $subtotalCents, | ||
Model $record | ||
): int { | ||
if ($discountMethod->isPerLineItem()) { | ||
return $record->lineItems()->sum('discount_total'); | ||
} | ||
|
||
if ($discountComputation?->isPercentage()) { | ||
return (int) ($subtotalCents * ((float) $discountRate / 100)); | ||
} | ||
|
||
return CurrencyConverter::convertToCents($discountRate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Enums\Accounting; | ||
|
||
use App\Enums\Concerns\ParsesEnum; | ||
use Filament\Support\Contracts\HasLabel; | ||
|
||
enum DocumentDiscountMethod: string implements HasLabel | ||
{ | ||
use ParsesEnum; | ||
|
||
case PerLineItem = 'per_line_item'; | ||
case PerDocument = 'per_document'; | ||
|
||
public function getLabel(): string | ||
{ | ||
return match ($this) { | ||
self::PerLineItem => 'Per Line Item', | ||
self::PerDocument => 'Per Document', | ||
}; | ||
} | ||
|
||
public function isPerLineItem(): bool | ||
{ | ||
return $this == self::PerLineItem; | ||
} | ||
|
||
public function isPerDocument(): bool | ||
{ | ||
return $this == self::PerDocument; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.