/// <summary> /// The post action from add view. Save the operation data /// </summary> /// <param name="operationVm">Operation view model object</param> /// <returns>string "true" or "false + error message"</returns> public ActionResult AddEditOperation(OperationVm operationVm) { int operId; string isSaved = OperationHelper.AddEditOperation(operationVm, out operId); return(Json(new { isSaved = isSaved, operId = operId })); }
public async Task <PartialViewResult> Detail(int id) { var retVal = new OperationVm { Success = false, Message = "" }; if (id <= 0) { retVal.Message = "Please provide an id."; return(PartialView("Partial/_operationDetailsPartial", retVal)); } var result = await _operationService.FindAsync(id); if (result != null) { retVal.Success = true; if (Request.IsAjaxRequest()) { retVal.Operation = result.ConvertToViewModel(); } } else { retVal.Message = "Item with requested Id was not found."; } return(PartialView("Partial/_operationDetailsPartial", retVal)); }
public PartialViewResult Edit(int id) { var retVal = new OperationVm { Success = false, Message = "" }; if (id <= 0) { retVal.Message = "Please provide an id."; return(PartialView("Partial/_operationEditPartial", retVal)); } var result = _operationService.Find(id); if (result != null) { retVal.Success = true; retVal.Operation = result.ConvertToViewModel(); } else { retVal.Message = "Item with requested Id was not found."; } return(PartialView("Partial/_operationEditPartial", retVal)); }
public static string AddEditOperation(OperationVm operationVm, out int operId) { string isSaved = "true"; int operationId = operationVm.OperationId; OperationsEntities db = new OperationsEntities(); Operation operationDb; List <OperationContainer> operationContListDb; // notifications NotificationMsgVm notifi = new NotificationMsgVm(); if (operationId == 0) { operationDb = new Operation(); operationContListDb = new List <OperationContainer>(); } else { operationDb = db.Operations.Include("OperationContainers") .Where(x => x.OperationId == operationId).FirstOrDefault(); if (operationDb.StatusId > 2) { operId = operationDb.OperationId; isSaved = "false .. Cann't update operation is not open "; return(isSaved); } operationContListDb = operationDb.OperationContainers.ToList(); //Get quotContainers Ids sent from the screen List <long> containerVmIds = operationVm.OperationContainers.Select(x => x.OperConId).ToList(); var containerDel = operationContListDb.Where(x => !containerVmIds.Contains(x.OperConId)).ToList(); foreach (var item in containerDel) { db.OperationContainers.Remove(item); } } Mapper.CreateMap <OperationVm, Operation>().IgnoreAllNonExisting(); // .ForMember(x => x.OperationContainers, v=> v.Ignore()); Mapper.CreateMap <OperationContainerVm, OperationContainer>().IgnoreAllNonExisting(); Mapper.Map(operationVm, operationDb); bool updateHB = false; if (operationId == 0) { //Generate code at save event if (operationDb.OrderFrom == 1) { operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Export, true); } else { operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Import, true); } db.Operations.Add(operationDb); //update quotation status if any int?quoteId = operationVm.QuoteId; if (quoteId != null) { //status = 2 -- opened Quotation quoteToUpdate = db.Quotations.Where(x => x.QuoteId == quoteId).FirstOrDefault(); quoteToUpdate.StatusId = 2; } notifi.NotificationTypeID = (operationDb.OrderFrom == 1) ? 1 : 3; notifi.ObjectID = -1; } else { List <HouseBillListVm> hbList = HouseBillHelper.GetHBList(operationId, operationDb.OrderFrom); if (hbList.Count == 1) { if (!operationDb.IsConsolidation) { HouseBill hb = db.HouseBills.Where(x => x.OperationId == operationDb.OperationId).FirstOrDefault(); hb.CBM = operationDb.CBM; hb.GoodsDescription = operationDb.GoodsDescription; hb.GrossWeight = operationDb.GrossWeight; hb.NetWeight = operationDb.NetWeight; hb.NumberOfPackages = operationDb.NumberOfPackages; hb.CollectedFreightCost = operationDb.FreightCostAmount; hb.CollectedThcCost = operationDb.ThcCostAmount; // hb.ShipperId = operationDb.ShipperId; // hb.ConsigneeId = operationDb.ConsigneeId; // hb.NotifierId = operationDb.NotifierId; // hb.NotifierAsConsignee = operationDb.NotifierAsConsignee; // hb.AgentId = operationDb.AgentId; hb.FromPortId = operationDb.FromPortId; hb.ToPortId = operationDb.ToPortId; } } } operId = 0; try { db.SaveChanges(); operId = operationDb.OperationId; if (notifi.ObjectID == -1) { notifi.ObjectID = operId; notifi.NotificationMsg = " New " + (operationDb.OrderFrom == 1 ? "Export " : "Import ") + " Operation Code: " + operationDb.OperationCode; NotificationHelper.Create_Notification(notifi); } //update hb if (updateHB) { } } catch (DbEntityValidationException e) { isSaved = "false " + e.Message; } catch (Exception e) { isSaved = "false " + e.Message; } return(isSaved); }
public static OperationVm GetOperationInfo(int id = 0, byte orderFrom = 1, int quoteId = 0) { OperationVm operationVm = new OperationVm(orderFrom, quoteId); OperationsEntities db = new OperationsEntities(); if (quoteId == 0 && id == 0) //add new with no quotation { OperationContainerVm operContVm = new OperationContainerVm(); operationVm.OperationContainers.Add(operContVm); return(operationVm); } if (quoteId != 0 && id == 0) { //check if has record in operation table Operation operationDb = db.Operations.Where(x => x.QuoteId == quoteId).FirstOrDefault(); if (operationDb != null) { id = operationDb.OperationId; } } if (id != 0) // has record in operation table { var operationDb = db.Operations.Include("OperationContainers") .Where(x => x.OperationId == id).FirstOrDefault(); Mapper.CreateMap <Operation, OperationVm>().IgnoreAllNonExisting(); Mapper.CreateMap <OperationContainer, OperationContainerVm>().IgnoreAllNonExisting(); Mapper.Map(operationDb, operationVm); if (operationVm.OperationContainers.Count == 0) { OperationContainerVm operContVm = new OperationContainerVm(); operationVm.OperationContainers.Add(operContVm); } return(operationVm); } else // No has record in operation table .. and has quote id { //Will fill operationVm from quotation data var quotationDb = db.Quotations.Include("QuotationContainers") .Where(x => x.QuoteId == quoteId).FirstOrDefault(); var quotationContListDb = quotationDb.QuotationContainers.ToList(); Mapper.CreateMap <Quotation, OperationVm>().IgnoreAllNonExisting() .ForMember(x => x.OperationContainers, v => v.Ignore()) .ForMember(x => x.CreateDate, y => y.Ignore()) .ForMember(x => x.CreateBy, y => y.Ignore()); Mapper.Map(quotationDb, operationVm); OperationContainerVm operContVm; if (quotationContListDb.Count == 0) { operContVm = new OperationContainerVm(); operationVm.OperationContainers.Add(operContVm); } else { foreach (var item in quotationContListDb) { for (int i = 0; i < item.NumberOfContainers; i++) { operContVm = new OperationContainerVm(); operContVm.ContainerTypeId = item.ContainerTypeId; operationVm.OperationContainers.Add(operContVm); } } } } return(operationVm); }
/// <summary> /// Add export or import MBL operation /// </summary> /// <param name="orderFrom"> export --- import</param> /// <param name="id">operation id to load edit mode view</param> /// <param name="quoteId">quotation id in case of processing an exsiting quotation</param> /// <returns>the add operation view</returns> public ActionResult Add(string orderFrom = "export", int id = 0, int quoteId = 0) { #region Check Rights bool hasRights; if (orderFrom.ToLower() == "export") //Check export rights { if (quoteId != 0) //process quotation check { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ExportQuotation, ActionEnum.ProcessToMBL); } else { if (id == 0) { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ExportMBL, ActionEnum.Add); } else { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ExportMBL, ActionEnum.Edit); } } } else { if (quoteId != 0) //process quotation check { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ImportQuotation, ActionEnum.ProcessToMBL); } else { if (id == 0) { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ImportMBL, ActionEnum.Add); } else { hasRights = AdminHelper.CheckUserAction(ScreenEnum.ImportMBL, ActionEnum.Edit); } } } if (!hasRights) { return(RedirectToAction("UnAuthorized", "Home")); } #endregion byte orderFromInt = 0; if (orderFrom.ToLower() == "export") { ViewBag.OperationType = "Export"; orderFromInt = 1; } else if (orderFrom.ToLower() == "import") { ViewBag.OperationType = "Import"; orderFromInt = 2; } else { //Error Page } OperationVm operationVm = OperationHelper.GetOperationInfo(id, orderFromInt, quoteId); ViewBag.CarrierList = ListCommonHelper.GetCarrierList(); ViewBag.ShipperList = ListCommonHelper.GetShipperList(); ViewBag.ConsigneeList = ListCommonHelper.GetConsigneeList(); ViewBag.NotifierList = ListCommonHelper.GetNotifierList(0); ViewBag.IncotermLib = ListCommonHelper.GetIncotermLibList(); ViewBag.Containers = ListCommonHelper.GetContainerList(); ViewBag.PackageList = ListCommonHelper.GetPackageTypeList(); ViewData["PortList"] = ListCommonHelper.GetPortsGrouped(); ViewData["CurrencyList"] = ListCommonHelper.GetCurrencyList(); ViewBag.AgentList = ListCommonHelper.GetAgentList(); return(View(operationVm)); }
public async Task <ActionResult> Edit([Bind(Include = "Id, Name, Description, Active, ActiveFrom, ActiveTo, RowVersion")] Operation operation) { // https://stackoverflow.com/questions/39533599/mvc-5-with-bootstrap-modal-from-partial-view-validation-not-working // https://stackoverflow.com/questions/2845852/asp-net-mvc-how-to-convert-modelstate-errors-to-json var retVal = new OperationVm { Success = false, Message = "" }; if (ModelState.IsValid) { var repo = _unitOfWorkAsync.RepositoryAsync <Operation>(); var valid = operation.Validate(); try { var existingEntity = await repo.FindAsync(operation.Id); if (existingEntity == null) { ModelState.AddModelError(string.Empty, @"Unable to update entity. The entity was deleted by another user."); } existingEntity.Name = operation.Name; existingEntity.Description = operation.Description; if (existingEntity.Active != operation.Active) { // the item has been deactivated... if (existingEntity.Active && !operation.Active) { // set the date of deactivation to current date, dont touch the created date existingEntity.ActiveTo = DateTime.Now; } // the item has been reactivated... if (!existingEntity.Active && operation.Active) { // audit log? existingEntity.Active = true; existingEntity.ActiveFrom = DateTime.Now; existingEntity.ActiveTo = operation.ActiveTo; } } else { // extending the activation validity date... existingEntity.ActiveTo = operation.ActiveTo; } existingEntity.Active = operation.Active; // you are not allowed to change the value of active from, you naughty naughty person! existingEntity.TrackingState = TrackingState.Modified; existingEntity.RowVersion = operation.RowVersion; // Warning: concurrency issues (row versions) must be resolved here... repo.Update(existingEntity); var result = await _unitOfWorkAsync.SaveChangesAsync(); if (result > 0) { retVal.Success = true; } } catch (Exception e) { Console.WriteLine(e); Debug.WriteLine(e); retVal.Message = e.Message; // if we stumbled upon an optimistic concurrency error, emit proper error to the user, and have the view reloaded with the new values taken from the database if (e.Message.Contains("The record you attempted to edit was modified by another user after you got the original value. The edit operation was canceled and the current values in the database have been displayed.")) { return(Json(new { Success = false, OptimisticConcurrencyError = true, OptimisticConcurrencyErrorMsg = "The record you attempted to edit was modified by another user after you got the original value. The edit operation was canceled and the current values in the database have been displayed." })); } } } else { // the model aint valid, we need to return the user to the view to enable him to fix the entry... retVal.Success = false; var errorModel = from x in ModelState.Keys where ModelState[x].Errors.Count > 0 select new { key = x.First().ToString().ToUpper() + string.Join("", x.Skip(1)), errors = ModelState[x].Errors. Select(y => y.ErrorMessage). ToArray() }; return(Json(new { Success = false, FormErrors = errorModel })); } return(Json(new { Success = true })); }