Files
thetool/application/RMLWorkorder/RMLWorkorderModel.php
2025-06-29 20:32:28 +02:00

59 lines
1.9 KiB
PHP

<?php
// RMLWorkorderModel.php
class RMLWorkorderModel extends TTCrudBaseModel {
public int $id;
public int $preorderId;
public ?int $companyId;
public string $status;
public ?int $assignmentDate;
public ?int $deadlineDate;
public ?int $appointmentDate;
public int $create;
public int $createBy;
/**
* Finds work orders that are nearing their deadline or are overdue.
* This can be used for the traffic light system.
*
* @param string $urgency 'red', 'yellow', or 'green'
* @return array
*/
public static function getWorkordersByUrgency(string $urgency, ?int $companyId = null): array {
$db = self::getDB();
$table = self::getFullyQualifiedTable();
$now = time();
$whereClause = "WHERE status IN ('assigned', 'scheduled')";
if ($companyId) {
$whereClause .= " AND companyId = " . intval($companyId);
}
switch ($urgency) {
case 'red': // Less than 1 week left or overdue
$redDate = strtotime('+1 week');
$whereClause .= " AND deadlineDate < $redDate";
break;
case 'yellow': // Between 1 and 3 weeks left
$yellowDateStart = strtotime('+1 week');
$yellowDateEnd = strtotime('+3 weeks');
$whereClause .= " AND deadlineDate BETWEEN $yellowDateStart AND $yellowDateEnd";
break;
case 'green': // More than 3 weeks left
$greenDate = strtotime('+3 weeks');
$whereClause .= " AND deadlineDate > $greenDate";
break;
default:
return [];
}
$sql = "SELECT * FROM $table $whereClause ORDER BY deadlineDate ASC";
$result = $db->query($sql);
$orders = [];
while ($row = $result->fetch_assoc()) {
$orders[] = new self($row);
}
return $orders;
}
}