OrderModel.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Models;
  3. class OrderModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. $dbFile = $_ENV['DB_FILE'];
  9. $dbPath = __DIR__ . '/../' . $dbFile;
  10. $this->pdo = new \PDO("sqlite:" . $dbPath);
  11. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  12. $this->pdo->exec('PRAGMA journal_mode = WAL;');
  13. $this->pdo->exec('PRAGMA busy_timeout = 5000;');
  14. }
  15. public function createOrder(
  16. int $tableId,
  17. int $userId,
  18. int $companyId,
  19. string $orderName,
  20. string $orderPhone,
  21. int $statusId
  22. ): int|false {
  23. if (!$this->tableExists($tableId, $companyId) || !$this->userExists($userId, $companyId) || !$this->companyExists($companyId) || !$this->statusExists($statusId)) {
  24. error_log("Tentativa de criar pedido com IDs inválidos: table_id={$tableId}, user_id={$userId}, company_id={$companyId}, status_id={$statusId}");
  25. return false;
  26. }
  27. $stmt = $this->pdo->prepare("
  28. INSERT INTO `order` (table_id, user_id, company_id, order_name, order_phone, status_id, order_created_at, order_finished_at, order_flag)
  29. VALUES (:table_id, :user_id, :company_id, :order_name, :order_phone, :status_id, :order_created_at, :order_finished_at, 'a')
  30. ");
  31. $currentTime = date('Y-m-d H:i:s');
  32. try {
  33. $executed = $stmt->execute([
  34. 'table_id' => $tableId,
  35. 'user_id' => $userId,
  36. 'company_id' => $companyId,
  37. 'order_name' => $orderName,
  38. 'order_phone' => $orderPhone,
  39. 'status_id' => $statusId,
  40. 'order_created_at' => $currentTime,
  41. 'order_finished_at' => ' '
  42. ]);
  43. return $executed ? (int)$this->pdo->lastInsertId() : false;
  44. } catch (\PDOException $e) {
  45. error_log("PDO Exception during order creation: " . $e->getMessage());
  46. return false;
  47. }
  48. }
  49. public function updateOrderStatus(int $orderId, int $companyId, int $statusId, ?string $orderFlag = null): bool|string
  50. {
  51. $stmtStatus = $this->pdo->prepare("
  52. SELECT status_status
  53. FROM status
  54. WHERE status_id = :status_id
  55. ");
  56. $stmtStatus->execute(['status_id' => $statusId]);
  57. $statusName = $stmtStatus->fetchColumn();
  58. if (strtolower($statusName) === 'finalizada') {
  59. $stmtCheck = $this->pdo->prepare("
  60. SELECT COUNT(*)
  61. FROM order_item
  62. NATURAL JOIN product
  63. NATURAL JOIN 'order'
  64. WHERE order_id = :order_id
  65. AND company_id = :company_id
  66. AND product_is_kitchen = 1
  67. AND order_flag = 'a';
  68. ");
  69. $stmtCheck->execute([
  70. 'order_id' => $orderId,
  71. 'company_id' => $companyId
  72. ]);
  73. $pendingKitchen = (int)$stmtCheck->fetchColumn();
  74. if ($pendingKitchen > 0) {
  75. return "Comanda possui itens de cozinha não finalizados.";
  76. }
  77. }
  78. $sql = "
  79. UPDATE `order`
  80. SET status_id = :status_id
  81. ";
  82. if (strtolower($statusName) === 'finalizada') {
  83. $sql .= ", order_finished_at = :order_finished_at";
  84. }
  85. if ($orderFlag !== null) {
  86. $sql .= ", order_flag = :order_flag";
  87. }
  88. $sql .= "
  89. WHERE order_id = :order_id
  90. AND company_id = :company_id
  91. ";
  92. $stmt = $this->pdo->prepare($sql);
  93. $params = [
  94. 'status_id' => $statusId,
  95. 'order_id' => $orderId,
  96. 'company_id' => $companyId
  97. ];
  98. if (strtolower($statusName) === 'finalizada') {
  99. $params['order_finished_at'] = date('Y-m-d H:i:s');
  100. }
  101. if ($orderFlag !== null) {
  102. $params['order_flag'] = $orderFlag;
  103. }
  104. $executed = $stmt->execute($params);
  105. return $executed && $stmt->rowCount() > 0;
  106. }
  107. public function deleteOrder(int $orderId, int $companyId, bool $hardDelete = false): bool
  108. {
  109. if ($hardDelete) {
  110. $stmtItems = $this->pdo->prepare("DELETE FROM order_item WHERE order_id = :order_id");
  111. $stmtItems->execute(['order_id' => $orderId]);
  112. $stmtOrder = $this->pdo->prepare("DELETE FROM `order` WHERE order_id = :order_id AND company_id = :company_id");
  113. $stmtOrder->execute(['order_id' => $orderId, 'company_id' => $companyId]);
  114. return $stmtOrder->rowCount() > 0;
  115. } else {
  116. $currentTime = date('Y-m-d H:i:s');
  117. $stmt = $this->pdo->prepare("
  118. UPDATE `order`
  119. SET order_flag = 'd', order_finished_at = :order_finished_at
  120. WHERE order_id = :order_id AND company_id = :company_id AND order_flag IN ('a', 'p')
  121. ");
  122. $executed = $stmt->execute([
  123. 'order_finished_at' => $currentTime,
  124. 'order_id' => $orderId,
  125. 'company_id' => $companyId
  126. ]);
  127. return $executed && $stmt->rowCount() > 0;
  128. }
  129. }
  130. public function getOrders(int $companyId, ?int $statusId = null): array
  131. {
  132. $sql = "SELECT *
  133. FROM `order`
  134. WHERE company_id = :company_id
  135. AND order_flag IN ('a', 'p')";
  136. $params = ['company_id' => $companyId];
  137. if ($statusId !== null) {
  138. $sql .= " AND status_id = :status_id";
  139. $params['status_id'] = $statusId;
  140. }
  141. $stmt = $this->pdo->prepare($sql);
  142. $stmt->execute($params);
  143. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  144. }
  145. public function getOrdersByTable(int $tableId, int $companyId, ?int $statusId = null): array
  146. {
  147. if (!$this->tableExists($tableId, $companyId)) {
  148. error_log("Tentativa de obter pedidos de mesa inválida: table_id={$tableId}, company_id={$companyId}");
  149. return [];
  150. }
  151. $sql = "SELECT *
  152. FROM `order`
  153. WHERE table_id = :table_id
  154. AND company_id = :company_id
  155. AND order_flag IN ('a', 'p')";
  156. $params = [
  157. 'table_id' => $tableId,
  158. 'company_id' => $companyId
  159. ];
  160. if ($statusId !== null) {
  161. $sql .= " AND status_id = :status_id";
  162. $params['status_id'] = $statusId;
  163. }
  164. $stmt = $this->pdo->prepare($sql);
  165. $stmt->execute($params);
  166. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  167. }
  168. public function getOrderById(int $orderId, int $companyId, bool $onlyActive = true): ?array
  169. {
  170. $sql = "SELECT * FROM `order` WHERE order_id = :order_id AND company_id = :company_id";
  171. if ($onlyActive) {
  172. $sql .= " AND order_flag IN ('a', 'p')";
  173. }
  174. $stmt = $this->pdo->prepare($sql);
  175. $stmt->execute(['order_id' => $orderId, 'company_id' => $companyId]);
  176. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  177. return $result ?: null;
  178. }
  179. public function getStatusNameById(int $statusId): ?string
  180. {
  181. $stmt = $this->pdo->prepare("SELECT status_status FROM status WHERE status_id = :status_id");
  182. $stmt->execute(['status_id' => $statusId]);
  183. $result = $stmt->fetchColumn();
  184. return $result ?: null;
  185. }
  186. public function getStatusIdByName(string $statusName): ?int
  187. {
  188. $stmt = $this->pdo->prepare("SELECT status_id FROM status WHERE status_status = :status_name");
  189. $stmt->execute(['status_name' => $statusName]);
  190. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  191. return $result ? (int)$result['status_id'] : null;
  192. }
  193. private function companyExists(int $companyId): bool
  194. {
  195. $stmt = $this->pdo->prepare("SELECT 1 FROM company WHERE company_id = :id AND company_flag = 'a'");
  196. $stmt->execute(['id' => $companyId]);
  197. return (bool) $stmt->fetch();
  198. }
  199. private function tableExists(int $tableId, int $companyId): bool
  200. {
  201. $stmt = $this->pdo->prepare("SELECT 1 FROM `table` WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'");
  202. $stmt->execute(['table_id' => $tableId, 'company_id' => $companyId]);
  203. return (bool) $stmt->fetch();
  204. }
  205. private function userExists(int $userId, int $companyId): bool
  206. {
  207. $stmt = $this->pdo->prepare("SELECT 1 FROM user WHERE user_id = :user_id AND company_id = :company_id AND user_flag = 'a'");
  208. $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  209. return (bool) $stmt->fetch();
  210. }
  211. private function statusExists(int $statusId): bool
  212. {
  213. $stmt = $this->pdo->prepare("SELECT 1 FROM status WHERE status_id = :id");
  214. $stmt->execute(['id' => $statusId]);
  215. return (bool) $stmt->fetch();
  216. }
  217. }