|
@@ -0,0 +1,84 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace Models;
|
|
|
|
|
+
|
|
|
|
|
+use Models\OrderModel;
|
|
|
|
|
+use Models\ProductModel;
|
|
|
|
|
+
|
|
|
|
|
+class OrderItemModel
|
|
|
|
|
+{
|
|
|
|
|
+ private \PDO $pdo;
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ $dbFile = $_ENV['DB_FILE'];
|
|
|
|
|
+ $dbPath = __DIR__ . '/../' . $dbFile;
|
|
|
|
|
+ $this->pdo = new \PDO("sqlite:" . $dbPath);
|
|
|
|
|
+ $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
+ $this->pdo->exec('PRAGMA journal_mode = WAL;');
|
|
|
|
|
+ $this->pdo->exec('PRAGMA busy_timeout = 5000;');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function createOrderItem(int $orderId, int $productId, int $companyId): int|false
|
|
|
|
|
+ {
|
|
|
|
|
+ $orderModel = new OrderModel();
|
|
|
|
|
+ $productModel = new ProductModel();
|
|
|
|
|
+
|
|
|
|
|
+ $order = $orderModel->getOrderById($orderId, $companyId);
|
|
|
|
|
+ if (!$order) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $product = $productModel->getProductById($productId, $companyId);
|
|
|
|
|
+ if (!$product) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $stmt = $this->pdo->prepare("
|
|
|
|
|
+ INSERT INTO order_item (order_id, product_id, company_id)
|
|
|
|
|
+ VALUES (:order_id, :product_id, :company_id)
|
|
|
|
|
+ ");
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $executed = $stmt->execute([
|
|
|
|
|
+ 'order_id' => $orderId,
|
|
|
|
|
+ 'product_id' => $productId,
|
|
|
|
|
+ 'company_id' => $companyId
|
|
|
|
|
+ ]);
|
|
|
|
|
+ return $executed ? (int)$this->pdo->lastInsertId() : false;
|
|
|
|
|
+ } catch (\PDOException $e) {
|
|
|
|
|
+ // Em ambiente de produção, considere logar a exceção de forma mais robusta
|
|
|
|
|
+ // ou retornar uma mensagem de erro genérica mais amigável.
|
|
|
|
|
+ error_log("PDO Exception during order item creation: " . $e->getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function deleteOrderItem(int $orderItemId, int $companyId): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare("
|
|
|
|
|
+ DELETE FROM order_item
|
|
|
|
|
+ WHERE order_item_id = :order_item_id
|
|
|
|
|
+ AND order_id IN (
|
|
|
|
|
+ SELECT order_id FROM `order` WHERE company_id = :company_id AND order_flag = 'a'
|
|
|
|
|
+ )
|
|
|
|
|
+ ");
|
|
|
|
|
+ $executed = $stmt->execute(['order_item_id' => $orderItemId, 'company_id' => $companyId]);
|
|
|
|
|
+ return $executed && $stmt->rowCount() > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function getOrderItemsByOrderId(int $orderId, int $companyId): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $orderModel = new OrderModel();
|
|
|
|
|
+ $order = $orderModel->getOrderById($orderId, $companyId);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$order) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Seleciona explicitamente o 'company_id' para que ele apareça na resposta JSON
|
|
|
|
|
+ $stmt = $this->pdo->prepare("SELECT order_item_id, order_id, product_id, company_id FROM order_item WHERE order_id = :order_id");
|
|
|
|
|
+ $stmt->execute(['order_id' => $orderId]);
|
|
|
|
|
+ return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|