Files
api/services/nginx/app/objects/selfserve_wash_sessions_o.php
T

228 lines
9.6 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\selfserve;
use classes\selfserve_schema_bootstrap;
use DateTime;
use modules\selfserve\helpers\selfserve_wash_session_status;
use traits\db_object_t;
class selfserve_wash_sessions_o extends db
{
use db_object_t;
public object_property $lane_id;
public object_property $department_id;
public object_property $machine_type_id;
public object_property $customer_number;
public object_property $vehicle_id;
public object_property $vehicle_type_id;
public object_property $reg;
public object_property $status;
public object_property $allowed;
public object_property $machine_relay_enabled;
public object_property $machine_relay_enabled_at;
public object_property $machine_start_triggered;
public object_property $machine_start_triggered_at;
public object_property $wash_started_at;
public object_property $order_id;
public object_property $completed_at;
public object_property $metadata_json;
public object_property $created_at;
public object_property $updated_at;
public object_property $deleted_at;
public function structure(): void
{
selfserve_schema_bootstrap::ensureTables();
$this->setTable('selfserve_wash_sessions');
}
public function add(
int $laneId,
int $departmentId,
?int $machineTypeId,
?int $customerNumber,
string $reg,
?int $vehicleId,
?int $vehicleTypeId,
selfserve_wash_session_status $status,
bool $allowed = false,
?array $metadata = null
): self {
$this->id = self::add_object([
'lane_id' => $laneId,
'department_id' => $departmentId,
'machine_type_id' => $machineTypeId,
'customer_number' => $customerNumber,
'vehicle_id' => $vehicleId,
'vehicle_type_id' => $vehicleTypeId,
'reg' => selfserve::standardize_registration($reg),
'status' => $status->value,
'allowed' => $allowed,
'metadata_json' => $metadata,
]);
$this->getObjectProperties();
$this->objectChanged();
return $this;
}
public function getObjectProperties(): void
{
$this->lane_id = new object_property($this->table, $this->id, 'lane_id', 'int', false);
$this->department_id = new object_property($this->table, $this->id, 'department_id', 'int', false);
$this->machine_type_id = new object_property($this->table, $this->id, 'machine_type_id', 'int', false);
$this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', false);
$this->vehicle_id = new object_property($this->table, $this->id, 'vehicle_id', 'int', false);
$this->vehicle_type_id = new object_property($this->table, $this->id, 'vehicle_type_id', 'int', false);
$this->reg = new object_property($this->table, $this->id, 'reg', 'string', false);
$this->status = new object_property($this->table, $this->id, 'status', 'string', false);
$this->allowed = new object_property($this->table, $this->id, 'allowed', 'bool', false);
$this->machine_relay_enabled = new object_property($this->table, $this->id, 'machine_relay_enabled', 'bool', false);
$this->machine_relay_enabled_at = new object_property($this->table, $this->id, 'machine_relay_enabled_at', 'datetime', false);
$this->machine_start_triggered = new object_property($this->table, $this->id, 'machine_start_triggered', 'bool', false);
$this->machine_start_triggered_at = new object_property($this->table, $this->id, 'machine_start_triggered_at', 'datetime', false);
$this->wash_started_at = new object_property($this->table, $this->id, 'wash_started_at', 'datetime', false);
$this->order_id = new object_property($this->table, $this->id, 'order_id', 'int', false);
$this->completed_at = new object_property($this->table, $this->id, 'completed_at', 'datetime', false);
$this->metadata_json = new object_property($this->table, $this->id, 'metadata_json', 'json', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
public function objectChanged(): void
{
// No dedicated cache invalidation yet.
}
public function updateStatus(selfserve_wash_session_status $status): void
{
$this->status->set($status->value);
}
public function markRelayEnabled(): void
{
$now = date('Y-m-d H:i:s');
$this->machine_relay_enabled->set(true);
$this->machine_relay_enabled_at->set($now);
$this->status->set(selfserve_wash_session_status::MACHINE_RELAY_ENABLED->value);
}
public function markRelayDisabled(): void
{
$this->machine_relay_enabled->set(false);
$this->machine_relay_enabled_at->set(null);
}
public function markMachineStartTriggered(?string $washStartedAt = null): void
{
$now = date('Y-m-d H:i:s');
$resolvedWashStartedAt = $washStartedAt ?? $now;
$this->machine_start_triggered->set(true);
$this->machine_start_triggered_at->set($now);
$this->wash_started_at->set($resolvedWashStartedAt);
if ((bool)$this->machine_relay_enabled->value() !== true) {
$this->machine_relay_enabled->set(true);
$this->machine_relay_enabled_at->set($now);
}
$this->status->set(selfserve_wash_session_status::MACHINE_STARTED->value);
}
public function markCompleted(?int $orderId = null): void
{
$this->completed_at->set(date('Y-m-d H:i:s'));
if ($orderId !== null) {
$this->order_id->set($orderId);
}
$this->status->set(selfserve_wash_session_status::COMPLETED->value);
}
public function selectLatestOpenByLaneAndReg(int $laneId, string $reg, ?int $customerNumber = null): self
{
$filters = [
'lane_id' => $laneId,
'reg' => selfserve::standardize_registration($reg),
'completed_at' => null,
'deleted_at' => null,
];
if ($customerNumber !== null) {
$filters['customer_number'] = $customerNumber;
}
$rows = $this->getFieldsWhere($filters, ['id']);
if ($rows === []) {
return $this;
}
usort($rows, static fn(array $a, array $b): int => (int)$b['id'] <=> (int)$a['id']);
$this->select((int)$rows[0]['id']);
return $this;
}
public function selectLatestByLaneAndReg(int $laneId, string $reg): self
{
$rows = $this->getFieldsWhere([
'lane_id' => $laneId,
'reg' => selfserve::standardize_registration($reg),
'deleted_at' => null,
], ['id']);
if ($rows === []) {
return $this;
}
usort($rows, static fn(array $a, array $b): int => (int)$b['id'] <=> (int)$a['id']);
$this->select((int)$rows[0]['id']);
return $this;
}
public function asArray(): array
{
return [
'id' => (int)$this->id,
'lane_id' => (int)$this->lane_id->value(),
'department_id' => (int)$this->department_id->value(),
'machine_type_id' => $this->machine_type_id->value() === null ? null : (int)$this->machine_type_id->value(),
'customer_number' => $this->customer_number->value() === null ? null : (int)$this->customer_number->value(),
'vehicle_id' => $this->vehicle_id->value() === null ? null : (int)$this->vehicle_id->value(),
'vehicle_type_id' => $this->vehicle_type_id->value() === null ? null : (int)$this->vehicle_type_id->value(),
'reg' => (string)$this->reg->value(),
'status' => (string)$this->status->value(),
'allowed' => (bool)$this->allowed->value(),
'machine_relay_enabled' => (bool)$this->machine_relay_enabled->value(),
'machine_relay_enabled_at' => $this->machine_relay_enabled_at->value() === null ? null : (string)$this->machine_relay_enabled_at->value(),
'machine_start_triggered' => (bool)$this->machine_start_triggered->value(),
'machine_start_triggered_at' => $this->machine_start_triggered_at->value() === null ? null : (string)$this->machine_start_triggered_at->value(),
'wash_started_at' => $this->wash_started_at->value() === null ? null : (string)$this->wash_started_at->value(),
'order_id' => $this->order_id->value() === null ? null : (int)$this->order_id->value(),
'completed_at' => $this->completed_at->value() === null ? null : (string)$this->completed_at->value(),
'metadata' => (array)($this->metadata_json->value() ?? []),
'created_at' => (string)$this->created_at->value(),
'updated_at' => $this->updated_at->value() === null ? null : (string)$this->updated_at->value(),
];
}
public function getElapsedMinutes(): int
{
$startAt = $this->wash_started_at->value() ?? $this->machine_start_triggered_at->value();
if ($startAt === null || trim((string)$startAt) === '') {
return 0;
}
try {
$start = new DateTime((string)$startAt);
$now = new DateTime();
} catch (\Throwable) {
return 0;
}
if ($start > $now) {
return 0;
}
$diff = $start->diff($now);
return (int)(($diff->days * 24 * 60) + ($diff->h * 60) + $diff->i);
}
}