options = $options; return $this; } public function addParameterChar(string $io, int $size, string $comment = '', string $name = '', mixed $value = null): array { return compact('io', 'size', 'comment', 'name', 'value'); } public function addParameterPackDec(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null): array { return ['type' => 'packed_decimal'] + compact('io', 'length', 'scale', 'comment', 'name', 'value'); } public function addParameterZoned(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null): array { return ['type' => 'zoned_decimal'] + compact('io', 'length', 'scale', 'comment', 'name', 'value'); } public function pgmCall(string $program, ?string $library = null, array $parameters = [], array $returnParameters = [], array $options = []): array|false { return $this->queue('program', $program, $library, [ 'parameters' => $parameters, 'return_parameters' => $returnParameters, 'options' => $options, ]); } public function clCommand(string $command): array|false { return $this->queue('command', $command, null, []); } public function getErrorCode(): string { return $this->errorCode; } public function getErrorMsg(): string { return $this->errorMessage; } private function queue(string $type, string $operation, ?string $library, array $payload): array|false { try { $record = [ 'type' => $type, 'operation' => $operation, 'library' => $library, 'payload' => json_encode($payload, JSON_THROW_ON_ERROR), 'requested_by' => function_exists('m3_user') ? m3_user() : null, 'status' => 'pending', 'created_at' => now(), 'updated_at' => now(), ]; if (Schema::connection(config('m3.connection', 'pgsql'))->hasTable('m3_program_requests')) { $id = DB::connection(config('m3.connection', 'pgsql')) ->table('m3_program_requests') ->insertGetId($record); } else { Log::warning('M3 PostgreSQL program bridge table is not migrated; request logged only.', $record); $id = null; } return ['success' => true, 'queued' => true, 'request_id' => $id, 'io_param' => []]; } catch (\Throwable $e) { $this->errorCode = (string) $e->getCode(); $this->errorMessage = $e->getMessage(); Log::error('Unable to queue M3 program request.', ['exception' => $e]); return false; } } }