OrderModel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. string $kitchenNote = ''
  23. ): int|false {
  24. if (
  25. !$this->tableExists($tableId, $companyId) ||
  26. !$this->userExists($userId, $companyId) ||
  27. !$this->companyExists($companyId) ||
  28. !$this->statusExists($statusId)
  29. ) {
  30. error_log("Tentativa de criar pedido com IDs inválidos: table_id={$tableId}, user_id={$userId}, company_id={$companyId}, status_id={$statusId}");
  31. return false;
  32. }
  33. $stmt = $this->pdo->prepare("
  34. INSERT INTO `order` (
  35. table_id,
  36. user_id,
  37. company_id,
  38. order_name,
  39. order_phone,
  40. status_id,
  41. order_created_at,
  42. order_finished_at,
  43. order_flag,
  44. kitchen_note
  45. ) VALUES (
  46. :table_id,
  47. :user_id,
  48. :company_id,
  49. :order_name,
  50. :order_phone,
  51. :status_id,
  52. :order_created_at,
  53. :order_finished_at,
  54. 'a',
  55. :kitchen_note
  56. )
  57. ");
  58. $currentTime = date('Y-m-d H:i:s');
  59. try {
  60. $executed = $stmt->execute([
  61. 'table_id' => $tableId,
  62. 'user_id' => $userId,
  63. 'company_id' => $companyId,
  64. 'order_name' => $orderName,
  65. 'order_phone' => $orderPhone,
  66. 'status_id' => $statusId,
  67. 'order_created_at' => $currentTime,
  68. 'order_finished_at' => ' ',
  69. 'kitchen_note' => $kitchenNote
  70. ]);
  71. return $executed ? (int)$this->pdo->lastInsertId() : false;
  72. } catch (\PDOException $e) {
  73. error_log("PDO Exception during order creation: " . $e->getMessage());
  74. return false;
  75. }
  76. }
  77. public function updateOrderStatus(int $orderId, int $companyId, int $statusId): bool
  78. {
  79. $stmt = $this->pdo->prepare("
  80. UPDATE `order`
  81. SET status_id = :status_id
  82. WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'
  83. ");
  84. $executed = $stmt->execute([
  85. 'status_id' => $statusId,
  86. 'order_id' => $orderId,
  87. 'company_id' => $companyId
  88. ]);
  89. return $executed && $stmt->rowCount() > 0;
  90. }
  91. public function deleteOrder(int $orderId, int $companyId, bool $hardDelete = false): bool
  92. {
  93. if ($hardDelete) {
  94. $stmtItems = $this->pdo->prepare("DELETE FROM order_item WHERE order_id = :order_id");
  95. $stmtItems->execute(['order_id' => $orderId]);
  96. $stmtOrder = $this->pdo->prepare("DELETE FROM `order` WHERE order_id = :order_id AND company_id = :company_id");
  97. $stmtOrder->execute(['order_id' => $orderId, 'company_id' => $companyId]);
  98. return $stmtOrder->rowCount() > 0;
  99. } else {
  100. $currentTime = date('Y-m-d H:i:s');
  101. $stmt = $this->pdo->prepare("
  102. UPDATE `order`
  103. SET order_flag = 'd', order_finished_at = :order_finished_at
  104. WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'
  105. ");
  106. $executed = $stmt->execute([
  107. 'order_finished_at' => $currentTime,
  108. 'order_id' => $orderId,
  109. 'company_id' => $companyId
  110. ]);
  111. return $executed && $stmt->rowCount() > 0;
  112. }
  113. }
  114. public function getOrders(int $companyId, ?int $statusId = null): array
  115. {
  116. $sql = "SELECT * FROM `order` WHERE company_id = :company_id AND order_flag = 'a'";
  117. $params = ['company_id' => $companyId];
  118. if ($statusId !== null) {
  119. $sql .= " AND status_id = :status_id";
  120. $params['status_id'] = $statusId;
  121. }
  122. $stmt = $this->pdo->prepare($sql);
  123. $stmt->execute($params);
  124. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  125. }
  126. public function getOrdersByTable(int $tableId, int $companyId, ?int $statusId = null): array
  127. {
  128. if (!$this->tableExists($tableId, $companyId)) {
  129. error_log("Tentativa de obter pedidos de mesa inválida: table_id={$tableId}, company_id={$companyId}");
  130. return [];
  131. }
  132. $sql = "SELECT * FROM `order` WHERE table_id = :table_id AND company_id = :company_id AND order_flag = 'a'";
  133. $params = [
  134. 'table_id' => $tableId,
  135. 'company_id' => $companyId
  136. ];
  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 getOrderById(int $orderId, int $companyId): ?array
  146. {
  147. $stmt = $this->pdo->prepare("SELECT * FROM `order` WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'");
  148. $stmt->execute(['order_id' => $orderId, 'company_id' => $companyId]);
  149. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  150. return $result ?: null;
  151. }
  152. public function getStatusNameById(int $statusId): ?string
  153. {
  154. $stmt = $this->pdo->prepare("SELECT status_status FROM status WHERE status_id = :status_id");
  155. $stmt->execute(['status_id' => $statusId]);
  156. $result = $stmt->fetchColumn();
  157. return $result ?: null;
  158. }
  159. public function getStatusIdByName(string $statusName): ?int
  160. {
  161. $stmt = $this->pdo->prepare("SELECT status_id FROM status WHERE status_status = :status_name");
  162. $stmt->execute(['status_name' => $statusName]);
  163. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  164. return $result ? (int)$result['status_id'] : null;
  165. }
  166. private function companyExists(int $companyId): bool
  167. {
  168. $stmt = $this->pdo->prepare("SELECT 1 FROM company WHERE company_id = :id AND company_flag = 'a'");
  169. $stmt->execute(['id' => $companyId]);
  170. return (bool) $stmt->fetch();
  171. }
  172. private function tableExists(int $tableId, int $companyId): bool
  173. {
  174. $stmt = $this->pdo->prepare("SELECT 1 FROM `table` WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'");
  175. $stmt->execute(['table_id' => $tableId, 'company_id' => $companyId]);
  176. return (bool) $stmt->fetch();
  177. }
  178. private function userExists(int $userId, int $companyId): bool
  179. {
  180. $stmt = $this->pdo->prepare("SELECT 1 FROM user WHERE user_id = :user_id AND company_id = :company_id AND user_flag = 'a'");
  181. $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  182. return (bool) $stmt->fetch();
  183. }
  184. private function statusExists(int $statusId): bool
  185. {
  186. $stmt = $this->pdo->prepare("SELECT 1 FROM status WHERE status_id = :id");
  187. $stmt->execute(['id' => $statusId]);
  188. return (bool) $stmt->fetch();
  189. }
  190. }