<?php require_once '../libraries/common.php'; include 'ajax_common.php'; const MAX_CHECK_COUNT = 20; $userId = \sessions\getCurrentUserId(); if (is_null($userId)) { notAuthErrorResponse(); return; } $lwTime = intval(getIfExists($_GET, 'lw_time')); $lwParsedOrderId = getParsedOrderId($_GET, 'lw_order_id'); $lwCustomerId = $lwParsedOrderId ? $lwParsedOrderId['customer_id'] : 0; $lwOrderId = $lwParsedOrderId ? $lwParsedOrderId['order_id'] : 0; $orders = \cache\getWaitingOrders($userId, $lwTime, $lwCustomerId, $lwOrderId, 0, 0, 0, MAX_CHECK_COUNT + 1); if (is_null($orders)) { internalErrorResponse(); return; } $newOrdersCount = min(MAX_CHECK_COUNT, sizeof($orders)); $newOrdersHasMore = sizeof($orders) > MAX_CHECK_COUNT; $log = \cache\getDoneOrCanceledLog($userId, $lwTime); if (is_null($log)) { internalErrorResponse(); return; } $presentableLog = array_map(function ($order) { return getCompositeOrderId($order); }, $log); echo jsonEncode(['new_orders_count' => $newOrdersCount, 'new_orders_has_more' => $newOrdersHasMore, 'done_or_canceled' => $presentableLog]);
return; } $role = getIfExists($userInfo, 'role'); if ($role === ROLE_EXECUTOR) { $orders = \storage\getDoneOrdersForExecutor($userId, $lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count + 1); } else { $done = intval(getIfExists($_GET, 'done')); $orders = \storage\getOrdersForCustomer($userId, $done, $lwTime, $lwOrderId, $upTime, $upOrderId, $count + 1); } if (is_null($orders)) { internalErrorResponse(); return; } $list = []; foreach ($orders as $order) { $element = ['order_id' => getCompositeOrderId($order), 'customer_id' => $order['customer_id'], 'description' => $order['description'], 'time' => $order['time']]; $price = getIfExists($order, 'price'); if ($price) { $element['price'] = number_format($price, 2); } $profit = getIfExists($order, 'profit'); if ($profit) { $element['profit'] = $profit; } $doneTime = getIfExists($order, 'done_time'); if ($doneTime) { $element['done_time'] = $doneTime; } if ($role === ROLE_CUSTOMER) { $executorId = getIfExists($order, 'executor_id'); $executorInfo = $executorId ? \storage\getUserInfoById($executorId) : null;
$userId = \sessions\getCurrentUserId(); if (is_null($userId)) { notAuthErrorResponse(); return; } $lwTime = intval(getIfExists($_GET, 'lw_time')); $lwParsedOrderId = getParsedOrderId($_GET, 'lw_order_id'); $lwCustomerId = $lwParsedOrderId ? $lwParsedOrderId['customer_id'] : 0; $lwOrderId = $lwParsedOrderId ? $lwParsedOrderId['order_id'] : 0; $upTime = intval(getIfExists($_GET, 'up_time')); $upParsedOrderId = getParsedOrderId($_GET, 'up_order_id'); $upCustomerId = $upParsedOrderId ? $upParsedOrderId['customer_id'] : 0; $upOrderId = $upParsedOrderId ? $upParsedOrderId['order_id'] : 0; $count = intval(getIfExists($_GET, 'count')); if (!$count) { $count = 1; } $orders = \cache\getWaitingOrders($userId, $lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count + 1); if (is_null($orders)) { internalErrorResponse(); return; } $list = []; foreach ($orders as $order) { array_push($list, ['order_id' => getCompositeOrderId($order), 'customer_id' => $order['customer_id'], 'description' => $order['description'], 'price' => number_format($order['price'], 2), 'time' => $order['time']]); if (sizeof($list) == $count) { break; } } $hasMore = sizeof($orders) > $count; echo jsonEncode(['list' => $list, 'has_more' => $hasMore ? 'true' : 'false']);
if (is_null($userId)) { notAuthErrorResponse(); return; } $description = trim(getIfExists($_POST, 'description')); if ($description == '') { validationErrorResponse(msg('no.description')); return; } $descriptionMaxLength = getCommonConstant('description.max.length'); if (mb_strlen($description) > $descriptionMaxLength) { validationErrorResponse(msg('description.length.error', $descriptionMaxLength)); return; } $price = floatval(getIfExists($_POST, 'price')); if ($price < 1) { validationErrorResponse(msg('min.price.error') . ' 1 ' . msg('currency')); return; } $maxPrice = getCommonConstant('order.max.price'); if ($price > $maxPrice) { validationErrorResponse(msg('max.price.error') . ' ' . $maxPrice . ' ' . msg('currency')); return; } $orderFromDb = \storage\addOrder($userId, $description, $price); if (is_null($orderFromDb)) { internalErrorResponse(); return; } $order = ['order_id' => getCompositeOrderId($orderFromDb), 'customer_id' => $orderFromDb['customer_id'], 'description' => $orderFromDb['description'], 'price' => number_format($orderFromDb['price'], 2), 'time' => $orderFromDb['time']]; echo jsonEncode(['order' => $order]);