make(Kernel::class)->bootstrap(); if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName())) { throw new LogicException('Apenas base descartável.'); } require_once dirname(__DIR__).'/Support/SuggestionFixture.php'; if (($argv[1] ?? '') === 'prepare') { $f = suggestionFixture(); app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'suspend', $f['policy'])); auth()->guard()->setUser($f['issuer']); $policy = app(SupplyPlanning::class)->save($f['issuer'], 'policy', supplyInput([...supplyPolicy($f), 'purpose' => 'internal', 'agreement_key' => $f['agreement']->record_key])); auth()->guard()->setUser($f['receiver']); $policy = app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'approve', $policy)); app(StockWorkflow::class)->record($f['receiver'], ['kind' => 'issue', 'reference' => 'EMPTY-'.Str::uuid(), 'idempotency_key' => (string) Str::uuid(), 'responsible_id' => $f['issuer']->id, 'warehouse_id' => $f['warehouse']->id, 'stock_item_id' => $f['stock']->id, 'quantity' => '120.000', 'destination' => 'Oficina interna', 'reason' => 'Consumo efectivo antes da configuração de reposição.']); auth()->guard()->setUser($f['issuer']); $rule = app(AutomaticPurchases::class)->save($f['issuer'], ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save', 'reason' => 'Configuração fictícia da prova concorrente.', 'data' => ['policy_key' => $policy->record_key, 'requester_id' => $f['issuer']->id, 'cost_center_id' => $f['r']->cost_center_id, 'unit_price' => null, 'tax_rate' => null, 'needed_days' => 5, 'valid_until' => today()->addMonth()->toDateString()]]); echo json_encode(['user' => $f['receiver']->id, 'tenant' => $f['tenant']->id, 'stock' => $f['stock']->id, 'warehouse' => $f['warehouse']->id, 'input' => ['operation_key' => (string) Str::uuid(), 'record_key' => $rule->record_key, 'version' => 1, 'action' => 'approve', 'reason' => 'Activação concorrente da parametrização testada.', 'data' => []]], JSON_THROW_ON_ERROR); exit; } $data = json_decode($argv[2], true, 32, JSON_THROW_ON_ERROR); $actor = User::query()->withoutGlobalScopes()->findOrFail($data['user']); auth()->guard()->setUser($actor); if (($argv[1] ?? '') === 'hold') { $held = false; DB::listen(function (QueryExecuted $query) use (&$held) { if (! $held && str_contains($query->sql, '"tenants"') && str_contains(strtolower($query->sql), 'for update')) { $held = true; echo "locked\n"; flush(); usleep(2000000); } }); } $rule = app(AutomaticPurchases::class)->save($actor, $data['input']); $need = app(SupplyPlanning::class)->latest('auto_need')->where('stock_item_id', $data['stock'])->where('warehouse_id', $data['warehouse'])->sole(); echo json_encode(['result' => 'saved', 'rule' => $rule->id, 'purchases' => $need->data['purchases']], JSON_THROW_ON_ERROR);