Implement discount calculations that apply to the piece (sold unit) for products invoiced by weight.
131 lines
4.9 KiB
PHP
131 lines
4.9 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;
|
|
|
|
/**
|
|
* Calculates all pricing values (base, discounted, per-kg, and discount rate) for a product.
|
|
*
|
|
* Business rules:
|
|
* - When a product is invoiced by the kilo (InvoicingUnit::KGM) and sold by the piece (SellUnit::PCE),
|
|
* $product->price is the reference price per kg HT, while $basePriceHt is the price of the piece HT.
|
|
* - All discounts (percentage or fixed amount) always apply to the piece (sold unit).
|
|
*/
|
|
public function calculateForProduct(Product $product): ProductPricing
|
|
{
|
|
$basePriceHt = $this->priceHt($product);
|
|
$basePriceTtc = $this->applyTax($basePriceHt, $product->tax_rate);
|
|
|
|
// * All discounts apply to the piece ($basePriceHt), capped at the piece price to prevent negative prices
|
|
$discountAmountHt = $product->validDiscount?->calculateDiscountAmount($basePriceHt)
|
|
?? new Money(0, $basePriceHt->getCurrency());
|
|
|
|
if ($discountAmountHt->greaterThan($basePriceHt)) {
|
|
$discountAmountHt = $basePriceHt;
|
|
}
|
|
|
|
$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 x 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
|
|
{
|
|
if ($product->invoicing_unit === InvoicingUnit::KGM) {
|
|
// * The product price is already a price per kg: it is the reference for the kg price
|
|
$discountAmountKgHt = $product->validDiscount?->calculateDiscountAmount($product->price)
|
|
?? new Money(0, $product->price->getCurrency());
|
|
|
|
$grams = $product->product_weight?->toGrams() ?? 0;
|
|
|
|
if (
|
|
$product->validDiscount !== null
|
|
&& ! $product->validDiscount->isPercentage()
|
|
&& $product->sell_unit === SellUnit::PCE
|
|
&& $grams > 0
|
|
) {
|
|
// * A fixed amount applies to the piece: bring it back to a per-kg amount
|
|
$discountAmountKgHt = $discountAmountKgHt
|
|
->multiply(self::GRAMS_PER_KG)
|
|
->divide($grams, self::ROUNDING);
|
|
}
|
|
|
|
$discountedKgPriceHt = $product->price->subtract($discountAmountKgHt);
|
|
|
|
if ($discountedKgPriceHt->isNegative()) {
|
|
$discountedKgPriceHt = new Money(0, $product->price->getCurrency());
|
|
}
|
|
|
|
return $this->applyTax($discountedKgPriceHt, $product->tax_rate);
|
|
}
|
|
|
|
$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 x 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);
|
|
}
|
|
}
|