87 lines
4.4 KiB
PHP
87 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\GoodsReceiptItem;
|
|
use App\Models\PurchaseOrderItem;
|
|
use App\Models\StockItem;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Tempo\Inventory\QuantityConversion;
|
|
|
|
/** Host basis frozen on first accepted receipt of this order line/article. */
|
|
final class ReceiptQuantityConversion
|
|
{
|
|
/** @param array<string,mixed>|null $input
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
public function freeze(User $actor, PurchaseOrderItem $line, StockItem $stock, int $accepted, ?array $input): ?array
|
|
{
|
|
$same = mb_strtolower(trim($stock->unit)) === mb_strtolower(trim($line->unit));
|
|
foreach (GoodsReceiptItem::query()->where('purchase_order_item_id', $line->id)->where('accepted_milli', '>', 0)->get() as $previous) {
|
|
$frozen = $previous->snapshot['quantity_conversion'] ?? null;
|
|
if (is_array($frozen) && ($same || $frozen['stock_item_id'] !== $stock->id || $frozen['stock_unit'] !== $stock->unit || $frozen['document_unit'] !== $line->unit)) {
|
|
throw ValidationException::withMessages(['items' => 'O artigo e as unidades já estão congelados nesta linha.']);
|
|
}
|
|
}
|
|
if ($same) {
|
|
if ($input !== null) {
|
|
throw ValidationException::withMessages(['items' => 'Unidades coincidentes não exigem um factor de conversão.']);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
if (config('inventory_conversion.qa_enabled') !== true || ! app()->environment('testing')) {
|
|
throw ValidationException::withMessages(['items' => 'A conversão de unidades está preparada apenas para QA.']);
|
|
}
|
|
$v = Validator::make($input ?? [], [
|
|
'numerator' => ['required', 'string', 'regex:/^[1-9]\d{0,8}$/D'],
|
|
'denominator' => ['required', 'string', 'regex:/^[1-9]\d{0,8}$/D'],
|
|
'source' => 'required|string|max:500', 'version' => 'required|string|max:100',
|
|
])->validate();
|
|
$basis = ['schema' => 'inventory.quantity.v1', 'stock_item_id' => $stock->id, 'document_unit' => $line->unit, 'stock_unit' => $stock->unit,
|
|
'numerator' => $v['numerator'], 'denominator' => $v['denominator'], 'source' => trim($v['source']), 'version' => trim($v['version'])];
|
|
if ($basis['source'] === '' || $basis['version'] === '') {
|
|
throw ValidationException::withMessages(['items' => 'Indique fonte e versão da conversão.']);
|
|
}
|
|
foreach (GoodsReceiptItem::query()->where('purchase_order_item_id', $line->id)->where('accepted_milli', '>', 0)->get() as $previous) {
|
|
$frozen = $previous->snapshot['quantity_conversion'] ?? null;
|
|
if (! is_array($frozen) || array_intersect_key($frozen, $basis) !== $basis) {
|
|
throw ValidationException::withMessages(['items' => 'Conserve artigo, unidades, factor, fonte e versão já congelados nesta linha.']);
|
|
}
|
|
}
|
|
// Load and validate the same writer artifact before using its arithmetic; never a second implementation.
|
|
app(ReceiptStockMovement::class)->for($actor);
|
|
try {
|
|
$physical = QuantityConversion::toBase((string) $accepted, $basis['numerator'], $basis['denominator']);
|
|
} catch (\InvalidArgumentException $e) {
|
|
throw ValidationException::withMessages(['items' => $e->getMessage()]);
|
|
}
|
|
|
|
return [...$basis, 'accepted_document_milli' => (string) $accepted, 'accepted_stock_milli' => $physical];
|
|
}
|
|
|
|
public function physicalAccepted(GoodsReceiptItem $line): int
|
|
{
|
|
$basis = $line->snapshot['quantity_conversion'] ?? null;
|
|
|
|
return $basis === null ? $line->accepted_milli : (int) $basis['accepted_stock_milli'];
|
|
}
|
|
|
|
public function documentary(GoodsReceiptItem $line, int $physical): int
|
|
{
|
|
$basis = $line->snapshot['quantity_conversion'] ?? null;
|
|
if ($basis === null) {
|
|
return $physical;
|
|
}
|
|
abort_unless(config('inventory_conversion.qa_enabled') === true, 503, 'Convers?o ainda n?o instalada.');
|
|
app(ReceiptStockMovement::class)->load();
|
|
try {
|
|
return (int) QuantityConversion::fromBase((string) $physical, $basis['numerator'], $basis['denominator']);
|
|
} catch (\InvalidArgumentException $e) {
|
|
throw ValidationException::withMessages(['data.quantity' => $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|