public function getAllProducts()
 {
     $statement = $this->db->prepare("\n            SELECT * FROM products\n        ");
     $statement->execute();
     $products = $statement->fetchAll();
     $promoRepo = new PromotionsRepository();
     for ($i = 0; $i < count($products); $i++) {
         $discount = $promoRepo->getTheBiggestPromotion(Session::get('userId'), $products[$i]['id'], $products[$i]['category_id']);
         $products[$i]['original_price'] = $products[$i]['price'];
         $products[$i]['price'] = $products[$i]['price'] - $products[$i]['price'] * $discount / 100;
         $products[$i]['discount'] = $discount;
     }
     return $products;
 }
 public function checkoutCart($userId, $cartId)
 {
     $products = $this->getProductsInCart($cartId);
     $promoRepo = new PromotionsRepository();
     $this->db->beginTransaction();
     $price = 0;
     foreach ($products as $product) {
         if ($product['quantity'] > $product['product_max_quantity']) {
             $this->db->rollBack();
             throw new \Exception("Product quantity");
         }
         $discount = $promoRepo->getTheBiggestPromotion($userId, $product['id'], $product['category_id']);
         $price += $product['price'] * $product['quantity'] - $product['price'] * $product['quantity'] * $discount / 100;
         $statement = $this->db->prepare("\n                UPDATE products\n                SET quantity = quantity - ?\n                WHERE id = ?\n            ");
         $statement->execute([$product['quantity'], $product['id']]);
     }
     $statement = $this->db->prepare("\n            SELECT cash FROM users WHERE id = ?\n        ");
     $statement->execute([$userId]);
     $userCash = $statement->fetch();
     if ($price > $userCash) {
         $this->db->rollBack();
         return false;
     }
     $statement = $this->db->prepare("\n            UPDATE users\n            SET cash = cash - ?\n            WHERE id = ?\n        ");
     $statement->execute([$price, $userId]);
     if ($statement->rowCount() <= 0) {
         $this->db->rollBack();
         return false;
     }
     $statement = $this->db->prepare("\n            DELETE FROM cart_products\n            WHERE cart_id = ?\n        ");
     $statement->execute([$cartId]);
     if ($statement->rowCount() < 0) {
         $this->db->rollBack();
         return false;
     }
     foreach ($products as $product) {
         $statement = $this->db->prepare("\n                INSERT INTO user_products (user_id, name, quantity, details, price)\n                VALUES (?, ?, ?, ?, ?)\n            ");
         $statement->execute([$userId, $product['name'], $product['quantity'], $product['details'], $product['price']]);
         if ($statement->rowCount() < 0) {
             $this->db->rollBack();
             return false;
         }
     }
     $this->db->commit();
     return true;
 }