public ActionResult CreateUpdateMachinery(Int32 id) { var machineryModel = new MachineryModel(); machineryModel.MachineryGuid = Guid.NewGuid().ToString(); machineryModel.PurchaseDate = DateTime.Now; if (id > 0) { var machinery = FarmManagementEntities.Machineries.Single(x => x.Id == id); machineryModel = machinery.ToType <Machinery, MachineryModel>(); machineryModel.MachineCondition = Convert.ToInt32(machinery.Condition.ParseEnum <Condition>()); GetUploadFile(FileUploadPath.MachineLicenseFile, machinery.Id, machinery.LicenseFile); GetUploadFile(FileUploadPath.MachinePicture, machinery.Id, machinery.Picture); machineryModel.MachineryGuid = machinery.Id.ToString(); } return(PartialView("MachineryPartial", machineryModel)); }
public ActionResult CreateUpdateMachinery(MachineryModel machineryModel) { if (!ModelState.IsValid) { return(ShowErrorMessage(GetModelErrors(ModelState))); } if (!CheckEmployeeHasBalance(machineryModel.Price)) { return(ShowErrorMessage(Constant.InsufficientBalance)); } var machinery = new Machinery(); if (machineryModel.Id > 0) { machinery = FarmManagementEntities.Machineries.Single(x => x.Id == machineryModel.Id); } machinery.FarmId = machineryModel.FarmId; machinery.VendorId = machineryModel.VendorId; machinery.AccountId = machineryModel.AccountId; machinery.Name = machineryModel.Name; machinery.Color = machineryModel.Color; machinery.Type = machineryModel.Type; machinery.RegistrationNo = machineryModel.RegistrationNo; machinery.ModelNo = machineryModel.ModelNo; machinery.CompanyName = machineryModel.CompanyName; machinery.PurchaseDate = machineryModel.PurchaseDate; machinery.OtherDescription = machineryModel.OtherDescription; machinery.Condition = machineryModel.MachineCondition.ToNullIfEmptyEnum <Condition>(); var fileUploadLicense = ClientSession.FileUploads.Single(x => x.FileUploadGuid == machineryModel.MachineryGuid && x.FileUploadName == FileUploadPath.MachineLicenseFile && !x.HasRemoved); machinery.LicenseFile = System.IO.Path.GetFileName(fileUploadLicense.FilePath); var fileUploadPicture = ClientSession.FileUploads.Single(x => x.FileUploadGuid == machineryModel.MachineryGuid && x.FileUploadName == FileUploadPath.MachinePicture && !x.HasRemoved); machinery.Picture = System.IO.Path.GetFileName(fileUploadPicture.FilePath); if (machineryModel.Id == 0) { machinery.Price = machineryModel.Price; machinery.InsertDate = DateTime.Now; machinery.UserId = machineryModel.UserId; FarmManagementEntities.Machineries.Add(machinery); ManageEmployeeBalance(machineryModel.Price); } else { machinery.UpdateDate = DateTime.Now; } FarmManagementEntities.SaveChanges(); UpdateFileUploadPath(FileUploadPath.MachineLicenseFile, fileUploadLicense, machinery.Id); UpdateFileUploadPath(FileUploadPath.MachinePicture, fileUploadPicture, machinery.Id); ClearAllFileUpload(); var message = string.Format(Constant.SuccessMessage, machineryModel.Id > 0 ? "updated" : "added"); return(ShowSuccessMessage(message)); }