$definitions * @return list> */ public function definitions(array $definitions): array { $data = Validator::make(['charges' => $definitions], [ 'charges' => 'present|array|list|max:20', 'charges.*' => 'required|array:key,kind,label,method,rate_percent,specific_minor,quantity_unit,basis_reference', 'charges.*.key' => 'required|string|max:100|distinct', 'charges.*.label' => 'required|string|min:2|max:120', 'charges.*.kind' => 'required|in:duty,ice,vat,other', 'charges.*.method' => 'required|in:unknown,not_applicable,ad_valorem,specific,maximum', 'charges.*.rate_percent' => ['present', 'nullable', 'regex:/^\d{1,4}(\.\d{1,6})?$/', 'numeric', 'min:0', 'max:10000'], 'charges.*.specific_minor' => 'present|nullable|integer|min:0|max:100000000000000', 'charges.*.quantity_unit' => 'present|nullable|string|max:40', 'charges.*.basis_reference' => 'required|string|min:3|max:4000', ])->validate(); $result = []; foreach ($data['charges'] as $index => $charge) { $percent = in_array($charge['method'], ['ad_valorem', 'maximum'], true); $specific = in_array($charge['method'], ['specific', 'maximum'], true); if (($charge['rate_percent'] !== null) !== $percent || ($charge['specific_minor'] !== null) !== $specific || ($specific && trim($charge['quantity_unit'] ?? '') === '')) { throw ValidationException::withMessages(['charges.'.$index => 'Indique exactamente as taxas e a unidade do método documentado; desconhecido ou não aplicável não presume uma taxa.']); } $result[] = [...$charge, 'rate_percent' => $percent ? (string) BigDecimal::of($charge['rate_percent'])->toScale(6) : null, 'specific_minor' => $specific ? (int) $charge['specific_minor'] : null]; } return $result; } /** @param list> $definitions * @param array> $bases * @return array */ public function calculate(array $definitions, array $bases): array { $definitions = $this->definitions($definitions); $bases = $this->bases($bases); if (array_diff(array_keys($bases), array_column($definitions, 'key')) !== []) { throw ValidationException::withMessages(['bases' => 'As bases devem corresponder às imposições da versão seleccionada.']); } $lines = []; $known = 0; $complete = $definitions !== []; foreach ($definitions as $charge) { $base = $bases[$charge['key']] ?? null; $value = $base['value_minor'] ?? null; $quantity = $base['quantity'] ?? null; if (($value !== null || $quantity !== null) && mb_strlen(trim($base['source'] ?? '')) < 3) { throw ValidationException::withMessages(['bases.'.$charge['key'].'.source' => 'Indique a origem do valor ou da quantidade efectivamente usados.']); } $percent = in_array($charge['method'], ['ad_valorem', 'maximum'], true); $specific = in_array($charge['method'], ['specific', 'maximum'], true); if ($specific && $quantity !== null && ($base['quantity_unit'] ?? null) !== $charge['quantity_unit']) { throw ValidationException::withMessages(['bases.'.$charge['key'] => 'A unidade da quantidade deve coincidir com a taxa específica; não foi presumida uma conversão.']); } $amount = null; if ($charge['method'] === 'not_applicable') { $amount = mb_strlen(trim($base['source'] ?? '')) >= 3 ? 0 : null; } elseif ($charge['method'] !== 'unknown' && (! $percent || $value !== null) && (! $specific || $quantity !== null)) { $adValorem = $percent ? BigDecimal::of($value)->multipliedBy($charge['rate_percent'])->dividedBy(100, 8, RoundingMode::Unnecessary) : BigDecimal::zero(); $perUnit = $specific ? BigDecimal::of($quantity)->multipliedBy($charge['specific_minor']) : BigDecimal::zero(); $calculated = ($charge['method'] === 'specific' || ($charge['method'] === 'maximum' && $perUnit->isGreaterThan($adValorem))) ? $perUnit : $adValorem; $rounded = $calculated->toScale(0, RoundingMode::HalfUp); if ($rounded->isGreaterThan(100000000000000) || $rounded->plus($known)->isGreaterThan(100000000000000)) { throw ValidationException::withMessages(['bases' => 'A estimativa excede o limite monetário suportado. Reveja as bases e as unidades.']); } $amount = $rounded->toInt(); } $complete = $complete && $amount !== null; $known += $amount ?? 0; $lines[] = ['key' => $charge['key'], 'kind' => $charge['kind'], 'label' => $charge['label'], 'method' => $charge['method'], 'amount_minor' => $amount, 'basis_reference' => $charge['basis_reference'], 'input' => $base]; } return ['currency' => 'MZN', 'lines' => $lines, 'complete' => $complete, 'known_subtotal_minor' => $known, 'total_minor' => $complete ? $known : null, 'financial_effects' => false, 'rounding' => 'HALF_UP_PER_CHARGE']; } /** @param array> $bases * @return array> */ public function bases(array $bases): array { Validator::make(['bases' => $bases], ['bases' => 'present|array|max:20', 'bases.*' => 'required|array:value_minor,quantity,quantity_unit,source', 'bases.*.value_minor' => 'present|nullable|integer|min:0|max:100000000000000', 'bases.*.quantity' => ['present', 'nullable', 'string', 'regex:/^\d{1,12}(\.\d{1,6})?$/'], 'bases.*.quantity_unit' => 'present|nullable|string|max:40', 'bases.*.source' => 'nullable|string|min:3|max:1000'])->validate(); foreach ($bases as &$base) { $base['value_minor'] = $base['value_minor'] === null ? null : (int) $base['value_minor']; foreach (['quantity_unit', 'source'] as $field) { $base[$field] = trim($base[$field] ?? '') === '' ? null : trim($base[$field]); } } unset($base); return $bases; } }