commit13
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace Tempo\Purchasing\Internal;
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Brick\Math\RoundingMode;
|
||||
|
||||
/** Same lifecycle in both compositions; hosts cannot replace independent approval or budget ordering. */
|
||||
final class InternalRequisitions
|
||||
{
|
||||
public function __construct(private RequisitionPort $port) {}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $record
|
||||
* @return array<string,mixed>|null
|
||||
*/
|
||||
public static function nextStep(array $record, int $actor): ?array
|
||||
{
|
||||
if ($record['status'] !== 'pending' || $record['requester_id'] === $actor) {
|
||||
return null;
|
||||
}
|
||||
$next = null;
|
||||
foreach ($record['steps'] as $step) {
|
||||
if (($step['approver_id'] ?? null) === $actor) {
|
||||
return null;
|
||||
}
|
||||
if ($next === null && $step['decision'] === 'pending') {
|
||||
$next = $step;
|
||||
}
|
||||
}
|
||||
|
||||
return $next;
|
||||
}
|
||||
|
||||
/** @return array{subtotal_minor:int,tax_minor:int,total_minor:int,unit_price_minor:int} */
|
||||
public static function line(string $quantity, string $price, string $taxRate): array
|
||||
{
|
||||
$minor = BigDecimal::of($price)->multipliedBy(100)->toScale(0)->toInt();
|
||||
$subtotal = BigDecimal::of($quantity)->multipliedBy($minor)->toScale(0, RoundingMode::HalfUp)->toInt();
|
||||
$tax = BigDecimal::of($subtotal)->multipliedBy($taxRate)->dividedBy(100, 0, RoundingMode::HalfUp)->toInt();
|
||||
|
||||
return ['unit_price_minor' => $minor, 'subtotal_minor' => $subtotal, 'tax_minor' => $tax, 'total_minor' => $subtotal + $tax];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $record
|
||||
* @param array<string,mixed> $policy
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function roles(array $record, array $policy): array
|
||||
{
|
||||
$amount = $policy['approval_includes_tax'] ? $record['total_minor'] : $record['subtotal_minor'];
|
||||
$roles = ['department_head'];
|
||||
if ($amount > $policy['first_limit_minor']) {
|
||||
$roles[] = 'procurement';
|
||||
}
|
||||
if ($amount > $policy['second_limit_minor'] || $record['has_equipment']) {
|
||||
$roles[] = 'finance';
|
||||
}
|
||||
|
||||
return $roles;
|
||||
}
|
||||
|
||||
private static function canonical(mixed $value): mixed
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
if (! array_is_list($value)) {
|
||||
ksort($value);
|
||||
}
|
||||
|
||||
return array_map(self::canonical(...), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $input
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function command(string $action, ?int $id, ?int $version, ?string $key, array $input = []): array
|
||||
{
|
||||
if ($key !== null && ! preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/iD', $key)) {
|
||||
throw new RequisitionFailure('Chave de operação inválida.', 422);
|
||||
}
|
||||
$hash = hash('sha256', json_encode(self::canonical([$action, $id, $version, $input]), JSON_THROW_ON_ERROR));
|
||||
|
||||
return $this->port->atomic(function () use ($action, $id, $version, $key, $hash, $input) {
|
||||
$record = $id === null ? null : $this->port->load($id);
|
||||
$this->port->authorize($action, $record);
|
||||
if ($key !== null && ($old = $this->port->replay($key, $hash)) !== null) {
|
||||
$current = $this->port->load($old['id']);
|
||||
$this->port->authorize('view', $current);
|
||||
if (isset($old['operation_role'])) {
|
||||
$this->port->authorize('step:'.$old['operation_role'], $current);
|
||||
}
|
||||
|
||||
return $old + ['replayed' => true];
|
||||
}
|
||||
if ($record !== null && $version !== null && $record['version'] !== $version) {
|
||||
throw new RequisitionFailure('A requisição mudou. Actualize antes de continuar.');
|
||||
}
|
||||
$role = null;
|
||||
if ($action === 'save') {
|
||||
if ($record !== null && ($record['status'] !== 'draft' || $record['requester_id'] !== $this->port->actorId())) {
|
||||
throw new RequisitionFailure('Rascunho não autorizado.', 403);
|
||||
}
|
||||
$result = $this->port->draft($input, $id);
|
||||
} elseif ($action === 'submit') {
|
||||
if ($record === null || $record['status'] !== 'draft' || $record['requester_id'] !== $this->port->actorId()) {
|
||||
throw new RequisitionFailure('Submissão não autorizada.', 403);
|
||||
}
|
||||
$policy = $this->port->policy($record);
|
||||
$this->port->reserve($record);
|
||||
$result = $this->port->submitted($record, $policy, self::roles($record, $policy));
|
||||
} elseif ($action === 'decide') {
|
||||
$actor = $this->port->actorId();
|
||||
if ($record === null || $record['status'] !== 'pending' || $record['requester_id'] === $actor) {
|
||||
throw new RequisitionFailure('A decisão exige outra identidade autorizada.', 403);
|
||||
}
|
||||
$step = self::nextStep($record, $actor);
|
||||
if ($step === null) {
|
||||
throw new RequisitionFailure('A decisão exige outra identidade para cada etapa.', 403);
|
||||
}
|
||||
if (! in_array($input['decision'] ?? '', ['approved', 'rejected'], true)) {
|
||||
throw new RequisitionFailure('Decisão inválida.', 422);
|
||||
}
|
||||
$role = $step['role'];
|
||||
$this->port->authorize('step:'.$role, $record);
|
||||
$status = $input['decision'] === 'rejected' ? 'rejected' : (count(array_filter($record['steps'], fn ($s) => $s['decision'] === 'pending')) > 1 ? 'pending' : 'approved');
|
||||
$result = $this->port->decided($record, $step, $input['decision'], $input['comment'] ?? null, $status);
|
||||
if ($status === 'rejected') {
|
||||
$this->port->release($record);
|
||||
}
|
||||
} else {
|
||||
throw new RequisitionFailure('Operação desconhecida.', 404);
|
||||
}
|
||||
if ($role !== null) {
|
||||
$result['operation_role'] = $role;
|
||||
}
|
||||
if ($key !== null) {
|
||||
$this->port->remember($key, $hash, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function view(int $id): array
|
||||
{
|
||||
return $this->port->atomic(function () use ($id) {
|
||||
$record = $this->port->load($id);
|
||||
$this->port->authorize('view', $record);
|
||||
|
||||
return $record;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user