90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace Domain\Services;
|
||
|
||
use Domain\Enums\SellUnit;
|
||
use Domain\Enums\TaxRate;
|
||
use Domain\Enums\Units\InvoicingUnit;
|
||
use Domain\Models\Entities\Product;
|
||
use Domain\Models\ValueObjects\DiscountRate;
|
||
use Domain\Models\ValueObjects\ProductPricing;
|
||
use Money\Money;
|
||
|
||
class PricingService
|
||
{
|
||
public const ROUNDING = Money::ROUND_UP;
|
||
|
||
private const GRAMS_PER_KG = 1000;
|
||
|
||
public function calculateForProduct(Product $product): ProductPricing
|
||
{
|
||
$basePriceHt = $this->priceHt($product);
|
||
$basePriceTtc = $this->applyTax($basePriceHt, $product->tax_rate);
|
||
|
||
$discountAmountHt = $product->validDiscount?->calculateDiscountAmount($basePriceHt)
|
||
?? new Money(0, $basePriceHt->getCurrency());
|
||
|
||
$discountedPriceHt = $basePriceHt->subtract($discountAmountHt);
|
||
$discountedPriceTtc = $this->applyTax($discountedPriceHt, $product->tax_rate);
|
||
|
||
$priceKgTtc = $this->calculatePriceKgTtc($discountedPriceHt, $product);
|
||
$discountRate = $this->calculateDiscountRate($basePriceHt, $discountAmountHt, $product);
|
||
|
||
return new ProductPricing(
|
||
basePriceHt: $basePriceHt,
|
||
basePriceTtc: $basePriceTtc,
|
||
discountAmountHt: $discountAmountHt,
|
||
discountedPriceHt: $discountedPriceHt,
|
||
discountedPriceTtc: $discountedPriceTtc,
|
||
priceKgTtc: $priceKgTtc,
|
||
discountRate: $discountRate,
|
||
hasDiscount: $product->validDiscount !== null
|
||
);
|
||
}
|
||
|
||
private function priceHt(Product $product): Money
|
||
{
|
||
if ($product->invoicing_unit === InvoicingUnit::KGM && $product->sell_unit === SellUnit::PCE && $product->product_weight) {
|
||
// * Price per kg => price of the piece: price × grams ÷ 1000 (single rounding, no float)
|
||
return $product->price
|
||
->multiply($product->product_weight->toGrams())
|
||
->divide(self::GRAMS_PER_KG, PricingService::ROUNDING);
|
||
}
|
||
|
||
return $product->price;
|
||
}
|
||
|
||
private function calculatePriceKgTtc(Money $discountedPriceHt, Product $product): Money
|
||
{
|
||
$priceTtc = $this->applyTax($discountedPriceHt, $product->tax_rate);
|
||
|
||
$grams = $product->product_weight?->toGrams() ?? 0;
|
||
|
||
if ($grams > 0 && $product->status && $product->invoicing_unit === InvoicingUnit::PCE) {
|
||
// * Price of the piece => price per kg: price × 1000 ÷ grams (single rounding, no float)
|
||
return $priceTtc
|
||
->multiply(self::GRAMS_PER_KG)
|
||
->divide($grams, self::ROUNDING);
|
||
}
|
||
|
||
return $priceTtc;
|
||
}
|
||
|
||
private function applyTax(Money $price, TaxRate $taxRate): Money
|
||
{
|
||
return $taxRate->applyTo($price);
|
||
}
|
||
|
||
private function calculateDiscountRate(Money $priceHt, Money $discountAmount, Product $product): DiscountRate
|
||
{
|
||
$discount = $product->validDiscount;
|
||
|
||
if ($discount?->isPercentage()) {
|
||
return DiscountRate::fromPercentage($discount->getPercentage());
|
||
}
|
||
|
||
return DiscountRate::fromAmounts($discountAmount, $priceHt);
|
||
}
|
||
}
|