35 lines
1.6 KiB
PHP
35 lines
1.6 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Shared\Context\CompanyContext;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tempo\Purchasing\Internal\InternalRequisitions;
|
|
use Tempo\Purchasing\SgfCandidate\Port;
|
|
|
|
require getenv('IR_SGF_HOST').'/vendor/autoload.php';
|
|
$app = require getenv('IR_SGF_HOST').'/bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
require getenv('IR_CANDIDATE_ROOT').'/integrations/sgf/internal-requisitions/bootstrap.php';
|
|
|
|
$input = json_decode(file_get_contents($argv[1]), true, 64, JSON_THROW_ON_ERROR);
|
|
$actor = User::findOrFail($input['actor']);
|
|
auth()->guard()->setUser($actor);
|
|
app(CompanyContext::class)->selectFor($actor->id, $input['scope']['tenant_id'], $input['scope']['company_id'], $input['scope']['unit_id']);
|
|
request()->headers->set('X-Purchasing-Context', 'procurement.internal');
|
|
try {
|
|
$result = DB::transaction(function () use ($input, $actor) {
|
|
if (($input['pause'] ?? false)) {
|
|
DB::table('companies')->where('id', $input['scope']['company_id'])->lockForUpdate()->firstOrFail();
|
|
echo "LOCK_HELD\n";
|
|
flush();
|
|
usleep(1200000);
|
|
}
|
|
|
|
return (new InternalRequisitions(new Port($actor, $input['key'])))->command($input['action'], $input['id'], $input['version'], $input['key'], $input['data']);
|
|
});
|
|
echo json_encode(['result' => 'ok', 'id' => $result['id'], 'version' => $result['version'], 'replayed' => $result['replayed'] ?? false], JSON_THROW_ON_ERROR)."\n";
|
|
} catch (Throwable $e) {
|
|
echo json_encode(['result' => 'rejected', 'class' => $e::class], JSON_THROW_ON_ERROR)."\n";
|
|
}
|