public function removeDiscountOnCategory($categoryId) { $condition = 'category_id = ' . $categoryId; $categoryPromotion = $this->db->getAllEntitiesWithCondition(self::PROMO_CATEGORIES_TABLENAME, $condition, 'promotedAt'); $discount = $categoryPromotion[0]['discount']; $productsInCategory = $this->db->getAllEntitiesWithCondition(self::PRODUCTS_TABLENAME, "category_id={$categoryId}", 'id'); foreach ($productsInCategory as $product) { $restoredPrice = $product['price'] + $product['price'] * ($discount / 100); $isUpdated = $this->db->updateEntityById(self::PRODUCTS_TABLENAME, array("price" => $restoredPrice), $product['id']); } if ($isUpdated) { $remove = $this->db->deleteEntityById(self::PROMO_CATEGORIES_TABLENAME, $categoryPromotion[0]['id']); } return $remove; }
public function removeProductFromPromotion($id) { $condition = "id={$id}"; $promotion = $this->db->getAllEntitiesWithCondition(self::PRODUCTS_PROMO_TABLENAME, $condition, 'id'); $discount = $promotion[0]['discount']; var_dump($promotion); $product = $this->db->getEntityById(self::PRODUCTS_TABLENAME, $promotion[0]['product_id']); $restoredPrice = $product['price'] + $product['price'] * ($discount / 100); $isUpdated = $this->db->updateEntityById(self::PRODUCTS_TABLENAME, array("price" => $restoredPrice), $product['id']); if (!$isUpdated) { throw new InvalidUserOperationException("Error during remove promo"); } if ($isUpdated) { $isDeleted = $this->db->deleteEntityById(self::PRODUCTS_PROMO_TABLENAME, $id); } return $isDeleted; }