OrderGetController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\OrderModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as v;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class OrderGetController
  9. {
  10. private OrderModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new OrderModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string)$request->getBody(), true) ?? [];
  18. try {
  19. // company_id é obrigatório para qualquer consulta de pedidos
  20. v::key('company_id', v::intType()->positive())
  21. // order_id, status_status e table_id são opcionais, mas mutuamente exclusivos em alguns cenários
  22. ->key('order_id', v::optional(v::intType()->positive()), false)
  23. ->key('status_status', v::optional(v::stringType()->notEmpty()->in(['Aberta', 'Finalizada', 'Cancelada'])), false)
  24. ->key('table_id', v::optional(v::intType()->positive()), false) // Adicionado table_id como opcional
  25. ->assert($body);
  26. } catch (ValidationException $e) {
  27. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
  28. }
  29. $companyId = (int) $body['company_id'];
  30. $orderId = $body['order_id'] ?? null;
  31. $statusStatus = $body['status_status'] ?? null;
  32. $tableId = $body['table_id'] ?? null; // Pega o table_id do payload
  33. $statusId = null;
  34. // Se status_status for fornecido, converte para status_id
  35. if ($statusStatus !== null) {
  36. $statusId = $this->model->getStatusIdByName($statusStatus);
  37. if ($statusId === null) {
  38. return ResponseLib::sendFail("Invalid status_status Provided: '{$statusStatus}'", [], "E_VALIDATE")->withStatus(400);
  39. }
  40. }
  41. $orders = [];
  42. if ($orderId !== null) {
  43. // Obter um pedido específico por order_id
  44. $order = $this->model->getOrderById($orderId, $companyId);
  45. if ($order) {
  46. $orders[] = $order;
  47. }
  48. } elseif ($tableId !== null) { // NOVO: Lógica para obter pedidos por table_id
  49. $orders = $this->model->getOrdersByTable($tableId, $companyId, $statusId);
  50. } else {
  51. // Obter todos os pedidos da empresa, ou filtrados apenas por status
  52. $orders = $this->model->getOrders($companyId, $statusId);
  53. }
  54. // Formata o retorno para incluir o status_status em vez do status_id
  55. foreach ($orders as &$order) {
  56. if (isset($order['status_id'])) {
  57. $order['status_status'] = $this->model->getStatusNameById($order['status_id']);
  58. unset($order['status_id']); // Remove o ID se não quiser expô-lo
  59. }
  60. }
  61. if (!empty($orders)) {
  62. return ResponseLib::sendOk($orders);
  63. }
  64. return ResponseLib::sendFail("No Orders Found for the Given Criteria", [], "E_DATABASE")->withStatus(204);
  65. }
  66. }