KitchenModel.php 1.8 KB

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