commit13
This commit is contained in:
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Brick\Math\RoundingMode;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class QuotationPricing
|
||||
{
|
||||
@@ -13,22 +12,10 @@ class QuotationPricing
|
||||
*/
|
||||
public function unit(string $price, string $type = 'none', string $value = '0'): array
|
||||
{
|
||||
$gross = BigDecimal::of($price)->multipliedBy(100)->toInt();
|
||||
$discount = BigDecimal::of($value);
|
||||
if ($gross < 1 || $discount->isNegative() || ! in_array($type, ['none', 'percent', 'amount'], true) ||
|
||||
($type === 'none' && ! $discount->isZero()) || ($type === 'percent' && $discount->isGreaterThan(100))) {
|
||||
throw ValidationException::withMessages(['items' => 'Indique um desconto unitário válido: percentagem ou valor em MZN, antes do IVA.']);
|
||||
try {
|
||||
return (new \Tempo\Purchasing\Economic\QuotationPricing)->unit($price, $type, $value);
|
||||
} catch (InvalidArgumentException $error) {
|
||||
throw ValidationException::withMessages(['items' => $error->getMessage()]);
|
||||
}
|
||||
$reduction = match ($type) {
|
||||
'percent' => BigDecimal::of($gross)->multipliedBy($discount)->dividedBy(100, 4),
|
||||
'amount' => $discount->multipliedBy(100),
|
||||
default => BigDecimal::zero(),
|
||||
};
|
||||
$net = BigDecimal::of($gross)->minus($reduction)->toScale(0, RoundingMode::HalfUp)->toInt();
|
||||
if ($net < 1) {
|
||||
throw ValidationException::withMessages(['items' => 'O preço unitário após desconto deve ser pelo menos 0,01 MZN.']);
|
||||
}
|
||||
|
||||
return ['gross_unit_minor' => $gross, 'discount_type' => $type, 'discount_value' => (string) $discount->toScale(2), 'net_unit_minor' => $net];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user