58 lines
3.8 KiB
PHP
58 lines
3.8 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Services\AutomaticPurchases;
|
|
use App\Services\ReplenishmentQueue;
|
|
use App\Services\StockWorkflow;
|
|
use App\Services\SupplyPlanning;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Database\Events\QueryExecuted;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
require dirname(__DIR__, 2).'/vendor/autoload.php';
|
|
$app = require dirname(__DIR__, 2).'/bootstrap/app.php';
|
|
$app->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));
|
|
auth()->guard()->setUser($f['issuer']);
|
|
$rule = app(AutomaticPurchases::class)->save($f['issuer'], ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save', 'reason' => 'Configuração da prova de recuperação isolada.', '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()]]);
|
|
auth()->guard()->setUser($f['receiver']);
|
|
app(AutomaticPurchases::class)->save($f['receiver'], ['operation_key' => (string) Str::uuid(), 'record_key' => $rule->record_key, 'version' => 1, 'action' => 'approve', 'reason' => 'Activação independente da regra de ensaio.', 'data' => []]);
|
|
// Controlled abrupt exit at the start of the post-commit callback. Enqueue remains real.
|
|
app()->instance(ReplenishmentQueue::class, new class extends ReplenishmentQueue
|
|
{
|
|
public function interactive(User $actor, int $id): bool
|
|
{
|
|
$work = DB::table('replenishment_work')->where('id', $id)->sole();
|
|
echo json_encode(['work' => $work->id, 'tenant' => $work->tenant_id, 'stock' => $work->stock_item_id, 'warehouse' => $work->warehouse_id], JSON_THROW_ON_ERROR);
|
|
exit;
|
|
}
|
|
});
|
|
app(StockWorkflow::class)->record($f['receiver'], ['kind' => 'issue', 'reference' => 'CRASH-'.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' => 'Saída confirmada antes da interrupção real do processo.']);
|
|
throw new RuntimeException('A interrupção devia terminar o processo.');
|
|
}
|
|
$data = json_decode($argv[2], true, 32, JSON_THROW_ON_ERROR);
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
$ok = app(ReplenishmentQueue::class)->scheduled($data['work']);
|
|
echo json_encode(['result' => $ok ? 'saved' : 'failed', 'authenticated' => auth()->check(), 'drafts' => DB::table('requisitions')->where('tenant_id', $data['tenant'])->where('status', 'draft')->pluck('id')->all()], JSON_THROW_ON_ERROR);
|