/// <summary> /// Delete an entity. /// </summary> /// <param name="model"></param> public void Delete(DepreciationRateViewModel model) { var entity = model.ToEntity(); this._DepreciationRatesRepository.Delete(entity); #region Commit Changes this._unitOfWork.Commit(); #endregion }
/// <summary> /// Throw an exception if name is exist. /// </summary> /// <param name="model">DepreciationRate view model</param> public void ThrowExceptionIfExist(DepreciationRateViewModel model) { ConditionFilter <DepreciationRate, long> condition = new ConditionFilter <DepreciationRate, long> { Query = (entity => entity.Name == model.Name && entity.Id != model.Id) }; var existEntity = this._DepreciationRatesRepository.Get(condition).FirstOrDefault(); if (existEntity != null) { throw new ItemAlreadyExistException(); } }
/// <summary> /// Update an entity. /// </summary> /// <param name="model"></param> /// <returns></returns> public DepreciationRateViewModel Update(DepreciationRateViewModel model) { this.ThrowExceptionIfExist(model); var entity = model.ToEntity(); entity = this._DepreciationRatesRepository.Update(entity); #region Commit Changes this._unitOfWork.Commit(); #endregion model = entity.ToModel(); return(model); }
public ActionResult CreateDepreciationRate(DepreciationRateViewModel model) { List <string> errors = new List <string>(); try { if (DuplicateDepreciationRate(model.Id, model.ItemGroupId, model.FiscalYearId) != null) { var ficsalyearname = db.FiscalYearRepo.GetById(model.FiscalYearId); ModelState.AddModelError("FiscalYearId", $"Depreciation Rate For {ficsalyearname.Name} already exists."); } if (ModelState.IsValid) { if (model.Id == 0) { var itemD = db.DepreciationRateRepo.Table(); model.Id = db.DepreciationRateRepo.Create(AutomapperConfig.Mapper.Map <DepreciationRate>(model)); } else { var oldmodel = db.DepreciationRateRepo.GetById(model.Id); if (oldmodel == null) { Response.StatusCode = (int)HttpStatusCode.OK; return(Json(new { redirecturl = "/error/badrequest" })); } oldmodel.Rate = model.Rate; } db.SaveChanges(); TempData["Success"] = "Depreciation Rate Successfully Saved"; return(RedirectToAction("DepreciationRateDetail", new { ItemGroupId = model.ItemGroupId })); } foreach (var item in ModelState.Where(x => x.Value.Errors.Any())) { errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage); } } catch (Exception ex) { errors.Add(ex.GetExceptionMessages()); } Response.StatusCode = (int)HttpStatusCode.SeeOther; return(View(model)); }
public PartialViewResult CreateDepreciationRate(int itemGroupId, int id = 0) { var model = new DepreciationRateViewModel() { ItemGroupId = itemGroupId }; if (id > 0) { model = AutomapperConfig.Mapper.Map <DepreciationRateViewModel>(db.DepreciationRateRepo.GetById(id)); } if (model == null) { Response.StatusCode = (int)HttpStatusCode.OK; return(PartialView(BadRequestView)); } return(PartialView(model)); }
public IList <DepreciationViewModel> Add(IEnumerable <DepreciationViewModel> collection) { Language lang = this._languageService.CurrentLanguage; IList <Asset> FailedToUpdate = new List <Asset>(); IList <Asset> assetsToUpdate = new List <Asset>(); List <Depreciation> depreciations = new List <Depreciation>(); IList <DepreciationViewModel> DepreciationViewModels = collection.ToList(); DateTime now = DateTime.Now; //DateTime depreciationDate = DateTime.Now; foreach (var model in collection) { Asset asset = this._AssetsRepository.Get(model.AssetId); DateTime startDepreciationDate; if (asset.StartDepreciationDate.HasValue) { startDepreciationDate = asset.StartDepreciationDate.Value; } else { startDepreciationDate = asset.PurchaseDate.Value; } switch (asset.DepreciationRate.DepreciationRateCode) { case DepreciationRateCodeEnum.Day: { if (asset.LastDepreciationDate == null) { model.NextDepreciationDate = startDepreciationDate; } else { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddDays(1); } } break; case DepreciationRateCodeEnum.Month: { if (asset.LastDepreciationDate == null) { model.NextDepreciationDate = startDepreciationDate.AddMonths(1); } else { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddMonths(1); } } break; case DepreciationRateCodeEnum.Year: { if (asset.LastDepreciationDate == null) { model.NextDepreciationDate = startDepreciationDate.AddYears(1); } else { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddYears(1); } } break; default: break; } //switch (asset.DepreciationRate.DepreciationRateCode) //{ // case MersalAccountingService.Common.Enums.DepreciationRateCodeEnum.Day: // depreciationDate = DateTime.Now.AddDays(1); // break; // case MersalAccountingService.Common.Enums.DepreciationRateCodeEnum.Month: // depreciationDate = DateTime.Now.AddMonths(1); // break; // case MersalAccountingService.Common.Enums.DepreciationRateCodeEnum.Year: // depreciationDate = DateTime.Now.AddYears(1); // break; // default: // break; //} if (model.NextDepreciationDate > now) //if (asset.LastDepreciationDate <= depreciationDate) { FailedToUpdate.Add(asset); DepreciationViewModel depreciationViewModel = DepreciationViewModels.FirstOrDefault(x => x.AssetId == asset.Id); DepreciationViewModels.Remove(depreciationViewModel); //model.AccountChart = asset.AccountChart.ToModel(); BrandViewModel brand = asset.Brand.ChildTranslatedBrands.FirstOrDefault(c => c.Language == lang).ToModel(); DepreciationRateViewModel depreciation = asset.DepreciationRate.ToModel(); if (lang == Language.Arabic) { model.BrandName = brand.NameAr; model.DepreciationRateName = depreciation.NameAr; } else if (lang == Language.English) { model.BrandName = brand.NameEn; model.DepreciationRateName = depreciation.NameEn; } model.CurrentValue = asset.CurrentValue; model.Successed = false; } else { asset.CurrentValue -= asset.DepreciationValue; if (asset.CurrentValue < 0) { asset.CurrentValue = 0; } asset.LastDepreciationDate = now; assetsToUpdate.Add(asset); depreciations.Add(new Depreciation { AssetId = asset.Id }); //model.AccountChart = asset.AccountChart.ToModel(); BrandViewModel brand = asset.Brand.ChildTranslatedBrands.FirstOrDefault(c => c.Language == lang).ToModel(); DepreciationRateViewModel depreciation = asset.DepreciationRate.ToModel(); if (lang == Language.Arabic) { model.BrandName = brand.NameAr; model.DepreciationRateName = depreciation.NameAr; } else if (lang == Language.English) { model.BrandName = brand.NameEn; model.DepreciationRateName = depreciation.NameEn; } model.CurrentValue = asset.CurrentValue; model.Successed = true; switch (asset.DepreciationRate.DepreciationRateCode) { case DepreciationRateCodeEnum.Day: { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddDays(1); } break; case DepreciationRateCodeEnum.Month: { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddMonths(1); } break; case DepreciationRateCodeEnum.Year: { model.NextDepreciationDate = asset.LastDepreciationDate.Value.AddYears(1); } break; default: break; } } //if (asset.CurrentValue <= 0) //{ //} this.ThrowExceptionIfExist(model); } var entityCollection = DepreciationViewModels.Select(model => model.ToEntity()); entityCollection = this._DepreciationRepository.Add(depreciations).ToList(); this._AssetsRepository.Update(assetsToUpdate); #region Commit Changes this._unitOfWork.Commit(); #endregion //var modelCollection = entityCollection.Select(entity => entity.ToModel()).ToList(); return(collection.ToList()); }