OrderModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_flag)
  29. VALUES (:table_id, :user_id, :company_id, :order_name, :order_phone, :status_id, :order_created_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. ]);
  42. return $executed ? (int)$this->pdo->lastInsertId() : false;
  43. } catch (\PDOException $e) {
  44. error_log("PDO Exception during order creation: " . $e->getMessage());
  45. return false;
  46. }
  47. }
  48. public function updateOrderStatus(int $orderId, int $companyId, int $statusId): bool
  49. {
  50. $stmt = $this->pdo->prepare("
  51. UPDATE `order`
  52. SET status_id = :status_id
  53. WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'
  54. ");
  55. $executed = $stmt->execute([
  56. 'status_id' => $statusId,
  57. 'order_id' => $orderId,
  58. 'company_id' => $companyId
  59. ]);
  60. return $executed && $stmt->rowCount() > 0;
  61. }
  62. public function deleteOrder(int $orderId, int $companyId): bool
  63. {
  64. $stmt = $this->pdo->prepare("
  65. UPDATE `order`
  66. SET order_flag = 'd'
  67. WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'
  68. ");
  69. $executed = $stmt->execute([
  70. 'order_id' => $orderId,
  71. 'company_id' => $companyId
  72. ]);
  73. return $executed && $stmt->rowCount() > 0;
  74. }
  75. public function getOrders(int $companyId, ?int $statusId = null): array
  76. {
  77. $sql = "SELECT * FROM `order` WHERE company_id = :company_id AND order_flag = 'a'";
  78. $params = ['company_id' => $companyId];
  79. if ($statusId !== null) {
  80. $sql .= " AND status_id = :status_id";
  81. $params['status_id'] = $statusId;
  82. }
  83. $stmt = $this->pdo->prepare($sql);
  84. $stmt->execute($params);
  85. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  86. }
  87. public function getOrdersByTable(int $tableId, int $companyId, ?int $statusId = null): array
  88. {
  89. if (!$this->tableExists($tableId, $companyId)) {
  90. error_log("Tentativa de obter pedidos de mesa inválida: table_id={$tableId}, company_id={$companyId}");
  91. return [];
  92. }
  93. $sql = "SELECT * FROM `order` WHERE table_id = :table_id AND company_id = :company_id AND order_flag = 'a'";
  94. $params = [
  95. 'table_id' => $tableId,
  96. 'company_id' => $companyId
  97. ];
  98. if ($statusId !== null) {
  99. $sql .= " AND status_id = :status_id";
  100. $params['status_id'] = $statusId;
  101. }
  102. $stmt = $this->pdo->prepare($sql);
  103. $stmt->execute($params);
  104. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  105. }
  106. public function getOrderById(int $orderId, int $companyId): ?array
  107. {
  108. $stmt = $this->pdo->prepare("SELECT * FROM `order` WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'");
  109. $stmt->execute(['order_id' => $orderId, 'company_id' => $companyId]);
  110. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  111. return $result ?: null;
  112. }
  113. public function getStatusNameById(int $statusId): ?string
  114. {
  115. $stmt = $this->pdo->prepare("SELECT status_status FROM status WHERE status_id = :status_id");
  116. $stmt->execute(['status_id' => $statusId]);
  117. $result = $stmt->fetchColumn();
  118. return $result ?: null;
  119. }
  120. public function getStatusIdByName(string $statusName): ?int
  121. {
  122. $stmt = $this->pdo->prepare("SELECT status_id FROM status WHERE status_status = :status_name");
  123. $stmt->execute(['status_name' => $statusName]);
  124. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  125. return $result ? (int)$result['status_id'] : null;
  126. }
  127. private function companyExists(int $companyId): bool
  128. {
  129. $stmt = $this->pdo->prepare("SELECT 1 FROM company WHERE company_id = :id AND company_flag = 'a'");
  130. $stmt->execute(['id' => $companyId]);
  131. return (bool) $stmt->fetch();
  132. }
  133. private function tableExists(int $tableId, int $companyId): bool
  134. {
  135. $stmt = $this->pdo->prepare("SELECT 1 FROM `table` WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'");
  136. $stmt->execute(['table_id' => $tableId, 'company_id' => $companyId]);
  137. return (bool) $stmt->fetch();
  138. }
  139. private function userExists(int $userId, int $companyId): bool
  140. {
  141. $stmt = $this->pdo->prepare("SELECT 1 FROM user WHERE user_id = :user_id AND company_id = :company_id AND user_flag = 'a'");
  142. $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  143. return (bool) $stmt->fetch();
  144. }
  145. private function statusExists(int $statusId): bool
  146. {
  147. $stmt = $this->pdo->prepare("SELECT 1 FROM status WHERE status_id = :id");
  148. $stmt->execute(['id' => $statusId]);
  149. return (bool) $stmt->fetch();
  150. }
  151. }