ProductModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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): int|false
  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. $executed = $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. return $executed ? (int)$this->pdo->lastInsertId() : false;
  39. }
  40. public function updateProduct(int $productId, int $companyId, ?string $productName = null, ?float $productPrice = null, ?bool $productIsKitchen = null): bool
  41. {
  42. $sql = "UPDATE product SET ";
  43. $params = [];
  44. $updates = [];
  45. if ($productName !== null) {
  46. $updates[] = "product_name = :product_name";
  47. $params['product_name'] = $productName;
  48. }
  49. if ($productPrice !== null) {
  50. $updates[] = "product_price = :product_price";
  51. $params['product_price'] = $productPrice;
  52. }
  53. if ($productIsKitchen !== null) {
  54. $updates[] = "product_is_kitchen = :product_is_kitchen";
  55. $params['product_is_kitchen'] = $productIsKitchen ? 1 : 0;
  56. }
  57. if (empty($updates)) {
  58. return false; // nada para atualizar
  59. }
  60. $sql .= implode(', ', $updates);
  61. $sql .= " WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'";
  62. $params['product_id'] = $productId;
  63. $params['company_id'] = $companyId;
  64. $stmt = $this->pdo->prepare($sql);
  65. return $stmt->execute($params);
  66. }
  67. public function deleteProduct(int $productId, int $companyId): bool
  68. {
  69. $stmt = $this->pdo->prepare("UPDATE product SET product_flag = 'd'
  70. WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'");
  71. return $stmt->execute(['product_id' => $productId, 'company_id' => $companyId]);
  72. }
  73. public function deleteProductByName(string $productName, int $companyId): bool
  74. {
  75. $stmt = $this->pdo->prepare("UPDATE product SET product_flag = 'd'
  76. WHERE product_name = :product_name AND company_id = :company_id AND product_flag = 'a'");
  77. return $stmt->execute(['product_name' => $productName, 'company_id' => $companyId]);
  78. }
  79. public function getProductById(int $productId, int $companyId): ?array
  80. {
  81. $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'");
  82. $stmt->execute(['product_id' => $productId, 'company_id' => $companyId]);
  83. $product = $stmt->fetch(\PDO::FETCH_ASSOC);
  84. if ($product) {
  85. $product['product_is_kitchen'] = (bool) $product['product_is_kitchen'];
  86. }
  87. return $product ?: null;
  88. }
  89. }