payfrit-api/api/tasks/callServer.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

94 lines
3.1 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Customer calls server to their table
* POST: { BusinessID, ServicePointID, OrderID?, Message?, UserID?, TaskTypeID? }
*/
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? 0);
$servicePointID = (int) ($data['ServicePointID'] ?? 0);
$orderID = (int) ($data['OrderID'] ?? 0);
$message = trim($data['Message'] ?? '');
$userID = (int) ($data['UserID'] ?? 0);
$taskTypeID = (int) ($data['TaskTypeID'] ?? 0);
if ($businessID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required']);
}
try {
// If servicePointID not provided but orderID is, look it up from the order
if ($servicePointID <= 0 && $orderID > 0) {
$qOrderSP = queryOne("SELECT ServicePointID FROM Orders WHERE ID = ?", [$orderID]);
if ($qOrderSP && (int) $qOrderSP['ServicePointID'] > 0) {
$servicePointID = (int) $qOrderSP['ServicePointID'];
}
}
if ($servicePointID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'ServicePointID is required']);
}
// Get service point info
$spQuery = queryOne("SELECT Name FROM ServicePoints WHERE ID = ?", [$servicePointID]);
$tableName = $spQuery ? $spQuery['Name'] : "Table #$servicePointID";
// Get user name if available
$userName = '';
if ($userID > 0) {
$userQuery = queryOne("SELECT FirstName FROM Users WHERE ID = ?", [$userID]);
if ($userQuery && !empty(trim($userQuery['FirstName']))) {
$userName = $userQuery['FirstName'];
}
}
// Get task type name if TaskTypeID provided
$taskTypeName = '';
if ($taskTypeID > 0) {
$typeQuery = queryOne("SELECT Name FROM tt_TaskTypes WHERE ID = ?", [$taskTypeID]);
if ($typeQuery && !empty(trim($typeQuery['Name']))) {
$taskTypeName = $typeQuery['Name'];
}
}
// Create task title and details
$taskTitle = !empty($taskTypeName)
? "$taskTypeName - $tableName"
: "Service Request - $tableName";
$taskDetails = '';
if (!empty($taskTypeName)) $taskDetails .= "Task: $taskTypeName\n";
if (!empty($userName)) $taskDetails .= "Customer: $userName\n";
$taskDetails .= "Location: $tableName\n";
$taskDetails .= !empty($message) ? "Request: $message" : "Customer is requesting assistance";
// Insert task
queryTimed("
INSERT INTO Tasks (
BusinessID, ServicePointID, UserID, OrderID, TaskTypeID,
Title, Details, ClaimedByUserID, CreatedOn
) VALUES (?, ?, ?, ?, ?, ?, ?, 0, NOW())
", [
$businessID,
$servicePointID,
$userID > 0 ? $userID : null,
$orderID > 0 ? $orderID : null,
$taskTypeID,
$taskTitle,
$taskDetails,
]);
$taskID = lastInsertId();
jsonResponse([
'OK' => true,
'TASK_ID' => (int) $taskID,
'MESSAGE' => 'Staff has been notified',
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}