KitchenModel.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Models;
  3. class KitchenModel
  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. }
  13. public function getKitchenOrders(int $companyId): array
  14. {
  15. $sql = "
  16. SELECT
  17. order_id,
  18. table_id,
  19. order_created_at,
  20. order_flag,
  21. order_item_id,
  22. order_item_flag,
  23. product_name,
  24. order_item_kitchen_note
  25. FROM 'order'
  26. NATURAL JOIN order_item
  27. NATURAL JOIN product
  28. WHERE company_id = :company_id
  29. AND order_flag IN ('a', 'p')
  30. AND product_is_kitchen = 1
  31. ORDER BY order_created_at ASC;
  32. ";
  33. $stmt = $this->pdo->prepare($sql);
  34. $stmt->execute(['company_id' => $companyId]);
  35. $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  36. $orders = [];
  37. foreach ($rows as $row) {
  38. $orderId = $row['order_id'];
  39. if (!isset($orders[$orderId])) {
  40. $orders[$orderId] = [
  41. 'order_id' => $row['order_id'],
  42. 'table_id' => $row['table_id'],
  43. 'order_created_at' => $row['order_created_at'],
  44. 'order_flag' => $row['order_flag'],
  45. 'items' => []
  46. ];
  47. }
  48. $orders[$orderId]['items'][] = [
  49. 'order_item_id' => $row['order_item_id'],
  50. 'order_item_flag' => $row['order_item_flag'],
  51. 'product_name' => $row['product_name'],
  52. 'kitchen_note' => $row['order_item_kitchen_note'] ?? ''
  53. ];
  54. }
  55. return array_values($orders);
  56. }
  57. }