ProductModel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Models;
  3. class ProductModel
  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 getProducts(int $companyId, bool $showDescription = false): array
  14. {
  15. if ($showDescription === false) {
  16. $stmt = $this->pdo->prepare("SELECT * FROM product WHERE company_id = :company_id AND product_flag = 'a'");
  17. } else {
  18. $stmt = $this->pdo->prepare("SELECT * FROM product NATURAL JOIN `description` WHERE company_id = :company_id AND product_flag = 'a'");
  19. }
  20. $stmt->execute(['company_id' => $companyId]);
  21. $products = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  22. foreach ($products as &$product) {
  23. $product['product_is_kitchen'] = (bool) $product['product_is_kitchen'];
  24. }
  25. return $products;
  26. }
  27. public function createProduct(string $name, float $price, int $categoryId, int $companyId, bool $productIsKitchen): bool
  28. {
  29. $stmt = $this->pdo->prepare("INSERT INTO product (product_name, product_price, category_id, company_id, product_flag, product_is_kitchen)
  30. VALUES (:name, :price, :category_id, :company_id, 'a', :product_is_kitchen)");
  31. return $stmt->execute([
  32. 'name' => $name,
  33. 'price' => $price,
  34. 'category_id' => $categoryId,
  35. 'company_id' => $companyId,
  36. 'product_is_kitchen' => $productIsKitchen ? 1 : 0
  37. ]);
  38. }
  39. public function updateProduct(int $productId, int $companyId, ?string $productName = null, ?float $productPrice = null, ?bool $productIsKitchen = null): bool
  40. {
  41. $sql = "UPDATE product SET ";
  42. $params = [];
  43. $updates = [];
  44. if ($productName !== null) {
  45. $updates[] = "product_name = :product_name";
  46. $params['product_name'] = $productName;
  47. }
  48. if ($productPrice !== null) {
  49. $updates[] = "product_price = :product_price";
  50. $params['product_price'] = $productPrice;
  51. }
  52. if ($productIsKitchen !== null) {
  53. $updates[] = "product_is_kitchen = :product_is_kitchen";
  54. $params['product_is_kitchen'] = $productIsKitchen ? 1 : 0;
  55. }
  56. if (empty($updates)) {
  57. return false; // nada para atualizar
  58. }
  59. $sql .= implode(', ', $updates);
  60. $sql .= " WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'";
  61. $params['product_id'] = $productId;
  62. $params['company_id'] = $companyId;
  63. $stmt = $this->pdo->prepare($sql);
  64. return $stmt->execute($params);
  65. }
  66. public function deleteProduct(int $productId, int $companyId): bool
  67. {
  68. $stmt = $this->pdo->prepare("UPDATE product SET product_flag = 'd'
  69. WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'");
  70. return $stmt->execute(['product_id' => $productId, 'company_id' => $companyId]);
  71. }
  72. public function deleteProductByName(string $productName, int $companyId): bool
  73. {
  74. $stmt = $this->pdo->prepare("UPDATE product SET product_flag = 'd'
  75. WHERE product_name = :product_name AND company_id = :company_id AND product_flag = 'a'");
  76. return $stmt->execute(['product_name' => $productName, 'company_id' => $companyId]);
  77. }
  78. public function getProductById(int $productId, int $companyId): ?array
  79. {
  80. $stmt = $this->pdo->prepare("SELECT product_id, company_id, category_id, product_is_kitchen, product_name, product_price FROM product WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'");
  81. $stmt->execute(['product_id' => $productId, 'company_id' => $companyId]);
  82. $product = $stmt->fetch(\PDO::FETCH_ASSOC);
  83. if ($product) {
  84. $product['product_is_kitchen'] = (bool) $product['product_is_kitchen'];
  85. }
  86. return $product ?: null;
  87. }
  88. }