public JsonResult EditPart(PartOperationModel model) { var operationResult = new OperationResult(); if (model.IsProjectPart) { var currentPart = _projectPartRepository.GetProjectPart(model.ProjectPartId); if (currentPart != null) { currentPart = new ProjectPartConverter().ConvertToDomain(model); operationResult = _projectPartRepository.UpdateProjectPart(currentPart); if (operationResult.Success) { var partToEdit = _priceSheetRepository.GetProductionPriceSheetPartByProjectPart(currentPart.ProjectPartId); if (partToEdit != null) { partToEdit.Cost = model.Cost; partToEdit.Price = model.Price; partToEdit.AnnualCost = model.Cost * partToEdit.AnnualUsage; partToEdit.AnnualPrice = model.Price * partToEdit.AnnualUsage; operationResult = _priceSheetRepository.UpdatePriceSheetPart(partToEdit); var totalAnnualCost = _priceSheetRepository.GetPriceSheetParts(partToEdit.PriceSheetId).Select(x => x.AnnualCost).Sum(); var totalAnnualPrice = _priceSheetRepository.GetPriceSheetParts(partToEdit.PriceSheetId).Select(x => x.AnnualPrice).Sum(); var priceSheetToEdit = _priceSheetRepository.GetPriceSheet(partToEdit.PriceSheetId); priceSheetToEdit.AnnualMargin = (totalAnnualPrice - totalAnnualCost) / totalAnnualCost; operationResult = _priceSheetRepository.UpdatePriceSheet(priceSheetToEdit); } } } } else { var existingPart = _partRepository.GetPart(model.PartId); if (existingPart != null) { Part part = new PartConverter().ConvertToUpdatePart(model); operationResult = _partRepository.UpdatePart(part); } } return(Json(operationResult, JsonRequestBehavior.AllowGet)); }
public JsonResult Edit(PriceSheetViewModel priceSheet) { var operationResult = new OperationResult(); PriceSheet priceSheetToUpdate = new PriceSheet(); var priceSheetId = priceSheet.PriceSheetId; priceSheetToUpdate = new PriceSheetConverter().ConvertToDomain(priceSheet); operationResult = _priceSheetRepository.UpdatePriceSheet(priceSheetToUpdate); if (operationResult.Success) { if (priceSheet.CostDetailList != null && priceSheet.CostDetailList.Count > 0) { foreach (var costDetail in priceSheet.CostDetailList) { var priceDetail = priceSheet.PriceDetailList.FirstOrDefault(x => x.ProjectPartId == costDetail.ProjectPartId); var tempPart = _projectPartRepository.GetProjectPart(costDetail.ProjectPartId); { tempPart.Weight = costDetail.Weight; tempPart.AnnualUsage = (int)costDetail.AnnualUsage; tempPart.Cost = costDetail.Cost; tempPart.PatternCost = costDetail.PatternCost; tempPart.FixtureCost = costDetail.FixtureCost; tempPart.PriceSheetId = priceSheetId; tempPart.Price = priceDetail.Price; tempPart.PatternPrice = priceDetail.PatternPrice; tempPart.FixturePrice = priceDetail.FixturePrice; } operationResult = _projectPartRepository.UpdateProjectPart(tempPart); } } if (priceSheetToUpdate.IsProduction) { operationResult = EditPriceSheetParts(priceSheetToUpdate.PriceSheetId); } operationResult.ReferenceId = priceSheetId; } return(Json(operationResult, JsonRequestBehavior.AllowGet)); }