public DiscountDTO CalculateDiscount(List <int> basketProductsIdList) { var discountDTO = new DiscountDTO(); var productDiscountConditions = _productRepository.LoadProductDiscountConditions(); var discountedProductList = _productRepository.LoadProducts(productDiscountConditions.Select(x => x.DiscountedProductId).ToList()); foreach (var productDiscountCondition in productDiscountConditions) { int conditionProductCount = basketProductsIdList.Count(x => x == productDiscountCondition.ConditionProductId); int discountedProductCount = basketProductsIdList.Count(x => x == productDiscountCondition.DiscountedProductId); var discountedProduct = discountedProductList.First(x => x.Id == productDiscountCondition.DiscountedProductId); int amountOfDiscounts = conditionProductCount / productDiscountCondition.ConditionProductsRequired; for (int i = 0; i < amountOfDiscounts; i++) { if (discountedProductCount > 0) { decimal amountToBeDiscounted = discountedProduct.Price * productDiscountCondition.DiscountAmount; discountDTO.DiscountItemDTOList.Add(new DiscountItemDTO(discountedProduct.Name, amountToBeDiscounted)); discountedProductCount--; } } } ; return(discountDTO); }
public async Task <IActionResult> Post(DiscountDTO model) { DiscountModel discount = new DTOMapper <DiscountDTO, DiscountModel>().Serialize(model); var Response = await _discountService.AddDiscounts(discount); return(Ok(Response)); }
public async Task UpdateDiscount_IsSuccess() { var discount = new DiscountDTO() { ProductSKU = "TST_A01", Quantity = 3, Price = 2 }; var postResult = await controller.PostAsync(discount); Assert.IsInstanceOfType(postResult, typeof(CreatedNegotiatedContentResult <DiscountDTO>)); var postResponse = postResult as CreatedNegotiatedContentResult <DiscountDTO>; Assert.IsNotNull(postResponse.Content); var createdDiscount = postResponse.Content as DiscountDTO; createdDiscount.Price = 1; var putResult = await controller.PutAsync(createdDiscount); var putResponse = putResult as OkNegotiatedContentResult <DiscountDTO>; Assert.IsNotNull(putResponse.Content); Assert.IsNotNull(putResponse.Content.Id); Assert.AreEqual(createdDiscount.ProductSKU, putResponse.Content.ProductSKU); Assert.AreEqual(createdDiscount.Quantity, putResponse.Content.Quantity); Assert.AreEqual(createdDiscount.Price, putResponse.Content.Price); }
public async Task AddDiscount(DiscountDTO discount) { if ((await GetDiscountByBookingTypeAsync(discount.BookingType)) != null) { throw new LMEGenericException($"Discount already exist!"); } _discountRepo.Insert(new Discount { BookingType = discount.BookingType, AdultDiscount = discount.AdultDiscount, MinorDiscount = discount.MinorDiscount, MemberDiscount = discount.MemberDiscount, ReturnDiscount = discount.ReturnDiscount, PromoDiscount = discount.PromoDiscount, AppDiscountIos = discount.AppDiscountIos, AppDiscountAndroid = discount.AppDiscountAndroid, AppDiscountWeb = discount.AppDiscountWeb, AppReturnDiscountIos = discount.AppReturnDiscountIos, AppReturnDiscountAndroid = discount.AppReturnDiscountAndroid, AppReturnDiscountWeb = discount.AppReturnDiscountWeb, CustomerDiscount = discount.CustomerDiscount, Active = discount.Active }); await _unitOfWork.SaveChangesAsync(); }
public async Task UpdateDiscount(Guid discountid, DiscountDTO discount) { var existingDiscount = await _discountRepo.GetAsync(discountid); if (discount == null) { throw await _serviceHelper.GetExceptionAsync(ErrorConstants.DISCOUNT_NOT_EXIST); } existingDiscount.MinorDiscount = discount.MinorDiscount; existingDiscount.AdultDiscount = discount.AdultDiscount; existingDiscount.MemberDiscount = discount.MemberDiscount; existingDiscount.AppDiscountIos = discount.AppDiscountIos; existingDiscount.AppDiscountAndroid = discount.AppDiscountAndroid; existingDiscount.AppDiscountWeb = discount.AppDiscountWeb; existingDiscount.AppReturnDiscountIos = discount.AppReturnDiscountIos; existingDiscount.AppReturnDiscountAndroid = discount.AppReturnDiscountAndroid; existingDiscount.AppReturnDiscountWeb = discount.AppReturnDiscountWeb; existingDiscount.ReturnDiscount = discount.ReturnDiscount; existingDiscount.PromoDiscount = discount.PromoDiscount; existingDiscount.Active = discount.Active; existingDiscount.CustomerDiscount = discount.CustomerDiscount; existingDiscount.BookingType = discount.BookingType; await _unitOfWork.SaveChangesAsync(); }
public async Task <ActionResult <Discount> > PostDiscount(DiscountDTO dto) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var discount = await _repo.createDiscount(dto); if (discount != null) { return(Ok(BindOutput(StatusCodes.Status201Created, RequestState.Success, "Retreived Successfuly", discount))); } else { return(NotFound(BindOutput(StatusCodes.Status404NotFound, RequestState.Failed, "Unable to retrieve record"))); } } catch (Exception ex) { return(BadRequest(BindOutput(StatusCodes.Status400BadRequest, RequestState.Error, ex.Message))); } }
public decimal OrderTotalCalculator(ICollection <OrderItemDTO> orderItem, DiscountDTO discount) { var ItemsInDb = _unitOfWork.Item.GetAllAsync(include: o => o.Include(o => o.childItems).ThenInclude(i => i.ChildItem)).AsEnumerable().Where(i => orderItem.Select(o => o.ItemId).Contains(i.ItemId)).Select(x => new { item = x, quantity = orderItem.FirstOrDefault(i => i.ItemId == x.ItemId).Quantity }); Decimal orderTotal = 0M; foreach (var item in ItemsInDb) { orderTotal += item.item.ItemPrice * item.quantity; } if (discount.DiscountType == DiscountType.DOLLER) { orderTotal = orderTotal - discount.DiscountValue; } else { orderTotal = orderTotal - ((discount.DiscountValue * orderTotal) / 100); } return(orderTotal); }
public async Task <IActionResult> PutDiscount(Guid id, DiscountDTO discount) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var updatedDiscount = await _repo.UpdateDiscount(id, discount); if (updatedDiscount != null) { return(Ok(BindOutput(StatusCodes.Status200OK, RequestState.Success, "Updated record successfully", updatedDiscount))); } else { return(NotFound(BindOutput(StatusCodes.Status404NotFound, RequestState.Failed, "Could nnot retrive record"))); } } catch (Exception ex) { return(BadRequest(BindOutput(StatusCodes.Status400BadRequest, RequestState.Error, ex.Message))); } }
public IActionResult setDiscount([FromForm] DiscountDTO discountDTO) { if (discountDTO == null) { return(BadRequest(new JsonCreate() { message = Utils.ConstMessage.BAD_REQUEST, data = false })); } DiscountManager discount = new DiscountManager(); bool judge = discount.insertNewDiscount(discountDTO); if (judge) { return(Ok(new JsonCreate() { message = Utils.ConstMessage.INSERT_SUCCESS, data = judge })); } else { return(Conflict(new JsonCreate() { message = Utils.ConstMessage.CONFILICT, data = false })); } }
public async Task <IServiceResponse <bool> > UpateDiscount(Guid id, DiscountDTO Discount) { return(await HandleApiOperationAsync(async() => { await _DiscountService.UpdateDiscount(id, Discount); return new ServiceResponse <bool>(true); })); }
public async Task <IServiceResponse <bool> > AddDiscount(DiscountDTO Discount) { return(await HandleApiOperationAsync(async() => { await _DiscountService.AddDiscount(Discount); return new ServiceResponse <bool>(true); })); }
public ActionResult Edit(long Id) { DiscountDTO discountDto = _discountService.GetDiscount(Id, CurrentLanguage, CurrentCurrency); Mapper.Initialize(c => c.CreateMap <DiscountDTO, DiscountVM>()); DiscountVM discountVM = Mapper.Map <DiscountDTO, DiscountVM>(discountDto); return(View(discountVM)); }
private void CheckValidProductSKU(DiscountDTO discountDTO) { var foundProduct = Products.Get(discountDTO.ProductSKU); if (foundProduct == null) { throw new Exception(string.Format(ERROR_INVALID_PRODUCT_SKU, discountDTO.ProductSKU)); } }
public async Task <IHttpActionResult> PostDiscount(DiscountDTO discountItem) { if (discountItem.percentage >= 0 && discountItem.percentage <= 100) { return(Json(await booksManager.Add <discount, DiscountDTO>(discountItem, (db, dto) => dto.id = db.id))); } return(Json(false)); }
public async Task <IHttpActionResult> PutDiscount(DiscountDTO discountItem) { if (discountItem.percentage >= 0 && discountItem.percentage <= 100) { await booksManager.Update <discount, DiscountDTO>(discountItem, d => d.id); return(Ok()); } return(Json(false)); }
public async Task <ActionResult> Post([FromBody] DiscountDTO discount) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var record = await _discountService.CreateDiscount(discount); return(CreatedAtAction(nameof(GetById), new { id = record.Id }, record)); }
public Task <DiscountDTO> AddAsync(DiscountDTO discountDTO) { CheckValidProductSKU(discountDTO); var discount = discountDTO.ToModel(); discount.Id = Guid.NewGuid(); Discounts.AddOrUpdate(discount); return(Task.FromResult <DiscountDTO>(DiscountDTO.FromModel(discount))); }
public OperationDetails AddDiscount(DiscountDTO dto) { Discount discount = new Discount(); discount.DateAdded = DateTime.Now; discount.DateModefied = DateTime.Now; if (!String.IsNullOrWhiteSpace(dto.StartDate)) { discount.DateStart = DateTime.ParseExact(dto.StartDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else { discount.DateStart = DateTime.Now; } if (!String.IsNullOrWhiteSpace(dto.EndDate)) { discount.DateEnd = DateTime.ParseExact(dto.EndDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else { discount.DateEnd = DateTime.Now; } discount.IsPercentage = dto.IsPercentage; discount.Value = dto.Value; long arabicLang = (long)Langs.Arabic; long englishLang = (long)Langs.English; discount.Descriptions = new List <DiscountDescription> { new DiscountDescription { DateAdded = DateTime.Now, DateModefied = DateTime.Now, LanguageId = arabicLang, Name = dto.ArabicName }, new DiscountDescription { DateAdded = DateTime.Now, DateModefied = DateTime.Now, LanguageId = englishLang, Name = dto.EnglishName } }; discount = _uitOfWork.DiscountRepository.Insert(discount); _uitOfWork.Save(); return(new OperationDetails(true, "تمت عملية الإضافة بنجاح", discount.Id.ToString())); }
public static IDiscountPrice getDiscount(DiscountDTO discount, int totalAmount) { for (int i = 0; i < discountList.Count; i++) { var bank = discountList[i]; if (bank.checkDiscount(discount, totalAmount)) { return(bank); } } return(null); }
public IActionResult Update([FromBody] DiscountDTO discountDTO) { try { _bizLogic.DiscountCommand.Update(discountDTO); _bizLogic.SaveChanges(); return(Ok()); } catch (Exception ex) { return(NotFound(ex.Message)); } }
public async Task <Discount> UpdateDiscount(Guid id, DiscountDTO Discount) { var discountExist = await _context.Discount.Where(c => c.Id == id).FirstOrDefaultAsync(); discountExist.Key = Discount.Key; discountExist.Value = Discount.Value; discountExist.Description = Discount.Description; discountExist.IsPercent = Discount.IsPercent; discountExist.IsFixed = Discount.IsFixed; discountExist.CustomerTypeId = Discount.CustomerTypeId; return(await saveChanges(discountExist, SaveChangesCodes.Update)); }
public IActionResult NewMenuItem([FromBody] DiscountDTO discountDTO) { try { _bizLogic.DiscountCommand.Add(discountDTO); _bizLogic.SaveChanges(); return(Ok()); } catch (Exception ex) { return(NotFound("Failed to add new discount")); } }
//建立一条新的优惠信息 public bool insertNewDiscount(DiscountDTO discountDTO) { try { Db.Ado.BeginTran(); DISCOUNT discount = new DISCOUNT { DISCOUNT_PRICE = discountDTO._amount, START_TIME = discountDTO._start_time, END_TIME = discountDTO._end_time, CONTEXT = discountDTO._context }; var existId = Db.Queryable <SET_DISCOUNT>().Where(it => it.MEDICINE_ID == discountDTO._medicine_id && it.BATCH_ID == discountDTO._batch_id).Select(it => it.DISCOUNT_ID).ToList(); var cId = 0; if (existId.Count == 0) { Db.Insertable <DISCOUNT>(discount). IgnoreColumns(it => new { it.DISCOUNT_ID }). ExecuteCommandIdentityIntoEntity(); var id = Db.Ado.SqlQuery <int>("select ISEQ$$_75598.currval from dual"); cId = id[0]; SET_DISCOUNT setDiscount = new SET_DISCOUNT { BATCH_ID = discountDTO._batch_id, MEDICINE_ID = discountDTO._medicine_id, DISCOUNT_ID = cId }; Db.Insertable(setDiscount).ExecuteCommand(); } else { cId = existId[0]; discount.DISCOUNT_ID = cId; Db.Updateable(discount).ExecuteCommand(); } Db.Ado.CommitTran(); return(true); }catch (Exception ex) { Db.Ado.RollbackTran(); return(false); } }
public async Task <Discount> createDiscount(DiscountDTO discount) { var newDiscount = new Discount() { CustomerTypeId = discount.CustomerTypeId, Key = discount.Key, Value = discount.Value, Description = discount.Description, IsPercent = discount.IsPercent, IsFixed = discount.IsFixed }; return(await saveChanges(newDiscount, SaveChangesCodes.Add)); }
public Task <DiscountDTO> UpdateAsync(DiscountDTO discountDTO) { var existingDiscount = Discounts.Get(discountDTO.Id); if (existingDiscount == null) { throw new Exception(string.Format(ERROR_DISCOUNT_NOT_FOUND, discountDTO.Id)); } CheckValidProductSKU(discountDTO); Discounts.AddOrUpdate(discountDTO.ToModel()); return(Task.FromResult <DiscountDTO>(discountDTO)); }
public async Task <IActionResult> PostDiscount([FromBody] DiscountDTO discountDTO) { var discount = _mapper.Map <Discount>(discountDTO); var validation = _validator.Validate(discount); if (!validation.IsValid) { return(BadRequest(validation.Errors.Select(x => x.ErrorMessage).ToList())); } await _mediator.Send(new CreateDiscountCommand(discount)); return(Ok()); }
public ActionResult Add(DiscountVM discount) { if (ModelState.IsValid) { Mapper.Initialize(c => c.CreateMap <DiscountVM, DiscountDTO>()); DiscountDTO dto = Mapper.Map <DiscountVM, DiscountDTO>(discount); OperationDetails op = _discountService.AddDiscount(dto); return(Json(new { Succedeed = op.Succedeed, message = op.Message, prop = op.Property })); } else { return(View(discount)); } }
public async Task <IHttpActionResult> GetDiscount(int id) { using (var dbcontext = unitOfWorkFactory.CreateUnitOfWork()) { Discount discount = await Task.Run(() => dbcontext.Discounts.Find(id)); if (discount == null) { return(NotFound()); } DiscountDTO discountDTO = Mapper.Map <Discount, DiscountDTO>(discount); return(Ok(discountDTO)); } }
public async Task <DiscountDTO> CreateDiscount(DiscountDTO discount) { var newDiscount = new Discount { DiscountAppliesTo = discount.DiscountAppliesTo, DiscountValue = discount.DiscountValue, DiscountValueType = discount.DiscountValueType, Units = discount.Units }; await _unitOfWork.DiscountRepository.Insert(newDiscount); _unitOfWork.Commit(); discount.Id = newDiscount.Id; return(discount); }
public void DiscountDTO_MissingSKU_IsInvalid() { var discount = new DiscountDTO() { Quantity = 3, Price = 2 }; var context = new ValidationContext(discount, null, null); var results = new List <ValidationResult>(); var isModelStateValid = Validator.TryValidateObject(discount, context, results, true); // Assert here Assert.IsFalse(isModelStateValid); }