TableModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Models;
  3. class TableModel
  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. // Prevenção de erros de concorrência
  13. $this->pdo->exec('PRAGMA journal_mode = WAL;');
  14. $this->pdo->exec('PRAGMA busy_timeout = 5000;');
  15. }
  16. // Retorna todas as mesas ativas de uma empresa
  17. public function getTables(int $companyId): array
  18. {
  19. $stmt = $this->pdo->prepare("
  20. SELECT table_id, table_number, status_id
  21. FROM `table`
  22. WHERE company_id = :company_id AND table_flag = 'a'
  23. ");
  24. $stmt->execute(['company_id' => $companyId]);
  25. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  26. }
  27. // Cria uma nova mesa
  28. public function createTable(int $companyId, string $tableNumber, int $statusId): bool
  29. {
  30. $stmt = $this->pdo->prepare("
  31. INSERT INTO `table` (company_id, table_number, status_id, table_flag)
  32. VALUES (:company_id, :table_number, :status_id, 'a')
  33. ");
  34. return $stmt->execute([
  35. 'company_id' => $companyId,
  36. 'table_number' => $tableNumber,
  37. 'status_id' => $statusId
  38. ]);
  39. }
  40. // Atualiza o status de uma mesa
  41. public function updateTable(int $tableId, int $companyId, int $statusId): bool
  42. {
  43. $stmt = $this->pdo->prepare("
  44. UPDATE `table`
  45. SET status_id = :status_id
  46. WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'
  47. ");
  48. return $stmt->execute([
  49. 'status_id' => $statusId,
  50. 'table_id' => $tableId,
  51. 'company_id' => $companyId
  52. ]);
  53. }
  54. // Soft delete de uma mesa POR table_id (mantido para compatibilidade, opcional)
  55. public function deleteTable(int $tableId, int $companyId): bool
  56. {
  57. $stmt = $this->pdo->prepare("
  58. UPDATE `table`
  59. SET table_flag = 'd'
  60. WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'
  61. ");
  62. return $stmt->execute([
  63. 'table_id' => $tableId,
  64. 'company_id' => $companyId
  65. ]);
  66. }
  67. // Soft delete de uma mesa POR table_number (novo método)
  68. public function deleteTableByNumber(string $tableNumber, int $companyId): bool
  69. {
  70. $stmt = $this->pdo->prepare("
  71. UPDATE `table`
  72. SET table_flag = 'd'
  73. WHERE table_number = :table_number AND company_id = :company_id AND table_flag = 'a'
  74. ");
  75. return $stmt->execute([
  76. 'table_number' => $tableNumber,
  77. 'company_id' => $companyId
  78. ]);
  79. }
  80. // Verifica se uma empresa existe e está ativa
  81. public function companyExists(int $companyId): bool
  82. {
  83. $stmt = $this->pdo->prepare("SELECT 1 FROM company WHERE company_id = :id AND company_flag = 'a'");
  84. $stmt->execute(['id' => $companyId]);
  85. return (bool) $stmt->fetch();
  86. }
  87. // Verifica se um status existe
  88. public function statusExists(int $statusId): bool
  89. {
  90. $stmt = $this->pdo->prepare("SELECT 1 FROM status WHERE status_id = :id");
  91. $stmt->execute(['id' => $statusId]);
  92. return (bool) $stmt->fetch();
  93. }
  94. public function getStatusIdByName(string $statusStatus): ?int
  95. {
  96. $stmt = $this->pdo->prepare("SELECT status_id FROM status WHERE status_status = :name");
  97. $stmt->execute(['name' => $statusStatus]);
  98. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  99. return $result ? (int)$result['status_id'] : null;
  100. }
  101. public function updateTableByNumber(string $tableNumber, int $companyId, int $statusId): bool
  102. {
  103. $stmt = $this->pdo->prepare("
  104. UPDATE `table`
  105. SET status_id = :status_id
  106. WHERE table_number = :table_number AND company_id = :company_id AND table_flag = 'a'
  107. ");
  108. return $stmt->execute([
  109. 'status_id' => $statusId,
  110. 'table_number' => $tableNumber,
  111. 'company_id' => $companyId
  112. ]);
  113. }
  114. }