KitchenModel.php 2.1 KB

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