Пример #1
0
        public Discount AddDiscountToProduct(string productName, DateTime startDate, DateTime endDate,
                                             int discountAmount, string discountType, bool presenteges)
        {
            try
            {
                MarketLog.Log("StoreCenter", "trying to add discount to product in store");
                MarketLog.Log("StoreCenter", "check if store exists");
                CheckIfStoreExistsAndActiveDiscount();
                MarketLog.Log("StoreCenter", " store exists");
                MarketLog.Log("StoreCenter", " check if has premmision to edit products");
                _storeManager.CanDeclareDiscountPolicy();
                MarketLog.Log("StoreCenter", " has premmission");
                MarketLog.Log("StoreCenter", " check if product name exists in the store " + _storeName);
                Product product = DataLayerInstance.GetProductByNameFromStore(_storeName, productName);
                CheckifProductExistsDiscount(product);
                MarketLog.Log("StoreCenter", "check if dates are OK");
                CheckIfDatesOK(startDate, endDate);
                MarketLog.Log("StoreCenter", "check that discount amount is OK");
                CheckPresentegesAndAmountOK(discountAmount, presenteges, product);
                StockListItem stockListItem = DataLayerInstance.GetProductFromStore(_storeName, product.Name);
                MarketLog.Log("StoreCenter", "check that the product don't have another discount");
                CheckHasNoExistsDiscount(stockListItem);
                Discount discount = new Discount(EnumStringConverter.GetdiscountTypeEnumString(discountType), startDate,
                                                 endDate, discountAmount, presenteges);
                stockListItem.Discount = discount;
                DataLayerInstance.AddDiscount(discount);
                DataLayerInstance.EditStockInDatabase(stockListItem);
                MarketLog.Log("StoreCenter", "discount added successfully");
                string[] coupon = { discount.discountCode };
                answer = new StoreAnswer(DiscountStatus.Success, "discount added successfully", coupon);
                return(discount);
            }
            catch (StoreException exe)
            {
                answer = new StoreAnswer((StoreEnum)exe.Status, exe.GetErrorMessage());
            }
            catch (DataException e)
            {
                answer = new StoreAnswer((StoreEnum)e.Status, e.GetErrorMessage());
            }
            catch (MarketException)
            {
                answer = new StoreAnswer(StoreEnum.NoPermission, "you have no premmision to do that");
            }

            return(null);
        }