public SavingResult UpdateRateUplift(int ProductID, decimal RateUplift) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { tblProduct SaveModel = db.tblProducts.Find(ProductID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Product not found"; return(res); } SaveModel = db.tblProducts.FirstOrDefault(r => r.ProductID == ProductID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find product details. Please retry."; return(res); } if (SaveModel.RateUpliftPerc == RateUplift) // if quantity has not changed then don't update quantity. { return(new SavingResult() { ExecutionResult = eExecutionResult.NotExecutedYet }); } SaveModel.RateUpliftPerc = RateUplift; SaveModel.redt = DateTime.Now; //SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblProducts.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.ProductID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult ApproveUser(int UserID) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { tblUser SaveModel = null; SaveModel = db.tblUsers.FirstOrDefault(r => r.UserID == UserID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.IsApproved = true; SaveModel.redt = DateTime.Now; db.tblUsers.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.UserID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { while (ex.Message == "An error occurred while updating the entries. See the inner exception for details.") { ex = ex.InnerException; } res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult UpdateRateUpliftAllProducts(decimal RateUplift) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { //if (SaveModel.RateUpliftPerc == RateUplift) // if quantity has not changed then don't update quantity. //{ // return new SavingResult() // { // ExecutionResult = eExecutionResult.NotExecutedYet // }; //} foreach (tblProduct SaveModel in db.tblProducts) { SaveModel.RateUpliftPerc = RateUplift; SaveModel.redt = DateTime.Now; //SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblProducts.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } //-- try { db.SaveChanges(); res.PrimeKeyValue = 0;// SaveModel.ProductID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(CompanyViewModel ViewModel) { SavingResult res = new SavingResult(); if (String.IsNullOrWhiteSpace(ViewModel.CompanyName)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter company name"; return(res); } using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { tblCompany SaveModel = null; if (ViewModel.CompanyID == 0) { SaveModel = new tblCompany() { rcdt = DateTime.Now }; db.tblCompanies.Add(SaveModel); } else { SaveModel = db.tblCompanies.FirstOrDefault(r => r.CompanyID == ViewModel.CompanyID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; db.tblCompanies.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.CompanyName = ViewModel.CompanyName; SaveModel.Address = ViewModel.Address; SaveModel.PhoneNo = ViewModel.PhoneNo; SaveModel.FaxNo = ViewModel.FaxNo; SaveModel.EMailID = ViewModel.EMailID; SaveModel.Website = ViewModel.Website; SaveModel.Slogan = ViewModel.Slogan; //SaveModel.Logo = ViewModel.Logo; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.CompanyID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { if (ex.GetType() == typeof(DbEntityValidationException)) { string err = ""; DbEntityValidationException dbVErr = (DbEntityValidationException)ex; foreach (var x in dbVErr.EntityValidationErrors) { foreach (var e in x.ValidationErrors) { err = err + "\r\n" + e.PropertyName + " = " + e.ErrorMessage + "."; } } ex = new Exception(err); } else { while (ex.Message == "An error occurred while updating the entries. See the inner exception for details.") { ex = ex.InnerException; } } res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(SaleOrderViewModel ViewModel) { SavingResult res = new SavingResult(); //if (String.IsNullOrWhiteSpace(ViewModel.SaleOrderName)) //{ // res.ExecutionResult = eExecutionResult.ValidationError; // res.ValidationError = "Please enter Cultivation Type name"; // return res; //} using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (ViewModel.SaleOrderID != 0 && CheckDuplicate(ViewModel.SaleOrderID, ViewModel.SONo)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Can not accept duplicate values. The Cultivation Type name is already exists."; return(res); } tblSaleOrder SaveModel = null; if (ViewModel.SaleOrderID == 0) { SaveModel = new tblSaleOrder() { SONo = GenerateNextSONo(db), rcdt = DateTime.Now, rcuid = Common.Props.LoginUser.UserID }; db.tblSaleOrders.Add(SaveModel); } else { SaveModel = db.tblSaleOrders.FirstOrDefault(r => r.SaleOrderID == ViewModel.SaleOrderID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblSaleOrders.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; db.tblSaleOrderProductDetails.RemoveRange(db.tblSaleOrderProductDetails.Where(r => r.SaleOrderID == ViewModel.SaleOrderID)); } SaveModel.SODate = ViewModel.SODate; SaveModel.CustomerID = ViewModel.CustomerID; SaveModel.BusinessName = ViewModel.BusinessName; SaveModel.ContactName = ViewModel.ContactName; SaveModel.Address = ViewModel.Address; SaveModel.City = ViewModel.City; SaveModel.Postcode = ViewModel.Postcode; SaveModel.Country = ViewModel.Country; SaveModel.IntPhoneNo = ViewModel.IntPhoneNo; SaveModel.AirportDestCity = ViewModel.AirportDestCity; SaveModel.EstDelDate = ViewModel.EstDelDate; SaveModel.TotalQuan = (ViewModel.Products.Sum(r => (decimal?)r.OrderQty) ?? 0); SaveModel.TotalAmt = (ViewModel.Products.Sum(r => (decimal?)(r.OrderQty * r.Rate)) ?? 0); foreach (SaleOrderProducDetailViewModel rp in ViewModel.Products.Where(r => r.OrderQty != 0)) { SaveModel.tblSaleOrderProductDetails.Add(new tblSaleOrderProductDetail() { tblSaleOrder = SaveModel, ProductID = rp.ProductID, Rate = rp.Rate, Quan = rp.OrderQty, Amt = rp.Rate * rp.OrderQty }); } //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.SaleOrderID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(ProductMasterViewModel ViewModel) { SavingResult res = new SavingResult(); if (ViewModel.ProductCode == 0) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Product code is required"; return(res); } using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (CheckDuplicate(ViewModel.ProductID, ViewModel.ScientificNameID, ViewModel.CommonNameID, ViewModel.Descr, ViewModel.SizeID, ViewModel.CultivationTypeID)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Can not accept duplicate values. The product is already exists."; return(res); } tblProduct SaveModel = null; if (ViewModel.ProductID == 0) { SaveModel = new tblProduct() { rcdt = DateTime.Now, rcuid = Common.Props.LoginUser.UserID }; db.tblProducts.Add(SaveModel); } else { SaveModel = db.tblProducts.FirstOrDefault(r => r.ProductID == ViewModel.ProductID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblProducts.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.ProductCode = ViewModel.ProductCode; SaveModel.ScientificNameID = ViewModel.ScientificNameID; SaveModel.CommonNameID = ViewModel.CommonNameID; SaveModel.Descr = ViewModel.Descr; SaveModel.SizeID = ViewModel.SizeID; SaveModel.CultivationTypeID = ViewModel.CultivationTypeID; SaveModel.Rate = ViewModel.Rate; SaveModel.RateUpliftPerc = ViewModel.RateUplift; SaveModel.UpliftedRate = ViewModel.Rate + Math.Round((ViewModel.Rate * ViewModel.RateUplift) / 100, 2); SaveModel.CurrentStock = ViewModel.CurrentStock; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.ProductID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(ProductScientificNameViewModel ViewModel) { SavingResult res = new SavingResult(); if (String.IsNullOrWhiteSpace(ViewModel.ProductScientificName)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter scientific name"; return(res); } using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (CheckDuplicate(ViewModel.ProductScientificNameID, ViewModel.ProductScientificName)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Can not accept duplicate values. Scientific name is already exists."; return(res); } tblProductScientificName SaveModel = null; if (ViewModel.ProductScientificNameID == 0) { SaveModel = new tblProductScientificName() { rcdt = DateTime.Now, rcuid = Common.Props.LoginUser.UserID }; db.tblProductScientificNames.Add(SaveModel); } else { SaveModel = db.tblProductScientificNames.FirstOrDefault(r => r.ProductScientificNameID == ViewModel.ProductScientificNameID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblProductScientificNames.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.ProductScientificName = ViewModel.ProductScientificName; SaveModel.IsAlive = ViewModel.IsAlive; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.ProductScientificNameID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(UserRegistrationViewModel ViewModel) { SavingResult res = new SavingResult(); if (String.IsNullOrWhiteSpace(ViewModel.FullName)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter full name"; return(res); } else if (String.IsNullOrWhiteSpace(ViewModel.EMailID)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter email id"; return(res); } else if (String.IsNullOrWhiteSpace(ViewModel.EMailID)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter password"; return(res); } using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (CheckDuplicate(ViewModel.UserID, ViewModel.EMailID, db)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Email already exists"; return(res); } tblUser SaveModel = null; if (ViewModel.UserID == 0) { SaveModel = new tblUser() { UserRoleID = (int)eUserRoleID.Admin, IsApproved = true, rcdt = DateTime.Now }; db.tblUsers.Add(SaveModel); } else { SaveModel = db.tblUsers.FirstOrDefault(r => r.UserID == ViewModel.UserID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; db.tblUsers.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.FullName = ViewModel.FullName; SaveModel.EmailID = ViewModel.EMailID; SaveModel.Password = ViewModel.Password; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.UserID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { while (ex.Message == "An error occurred while updating the entries. See the inner exception for details.") { ex = ex.InnerException; } res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(CustomerViewModel ViewModel) { SavingResult res = new SavingResult(); if (ViewModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "information not passed to save."; return(res); } if (ViewModel.EMailID == "") { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Email id is required"; return(res); } else if (ViewModel.BusinessName == "") { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Business Name is required"; return(res); } using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (CheckDuplicateEmailID(ViewModel.UserID, ViewModel.EMailID, db)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "entered email id is already registered."; return(res); } else if (CheckDuplicate(ViewModel.UserID, ViewModel.BusinessName, db)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Can not accept duplicate values. The business name is already exists."; return(res); } tblUser SaveModel = null; if (ViewModel.UserID == 0) { SaveModel = new tblUser() { Password = ViewModel.Password ?? "", UserRoleID = (int)Models.Users.eUserRoleID.Customer, rcdt = DateTime.Now }; if (Common.Props.LoginUser != null) { SaveModel.rcuid = Common.Props.LoginUser.UserID; } db.tblUsers.Add(SaveModel); } else { SaveModel = db.tblUsers.FirstOrDefault(r => r.UserID == ViewModel.UserID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblUsers.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.BusinessName = ViewModel.BusinessName; SaveModel.ContactName = ViewModel.ContactName ?? ""; SaveModel.EmailID = ViewModel.EMailID; SaveModel.Address = ViewModel.Address ?? ""; SaveModel.City = ViewModel.City ?? ""; SaveModel.PostCode = ViewModel.Postcode ?? ""; SaveModel.Country = ViewModel.Country ?? ""; SaveModel.IntPhoneNo = ViewModel.IntPhoneNo ?? ""; SaveModel.AirportDestCity = ViewModel.AirportDestCity ?? ""; //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.UserID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(BoxListViewModel ViewModel) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { //if (ViewModel.SaleInvoiceID != 0 && CheckDuplicate(ViewModel.SaleInvoiceID, ViewModel.InvoiceNo)) //{ // res.ExecutionResult = eExecutionResult.ValidationError; // res.ValidationError = "Can not accept duplicate values. The Cultivation Type name is already exists."; // return res; //} //else //{ // //var x = from p in db.tblProducts // // join ps in db.tblProductSizes on p.SizeID equals ps.ProductSizeID // // join vm in ViewModel.Products on p.ProductID equals vm.ProductID // // where (ps.QuanReq ?? false) == true && vm.Qty == 0 // // select new { count = 1 }; // if (ViewModel.Products.Count(r => r.IsQtyReq && r.Qty == 0) > 0) // { // res.ExecutionResult = eExecutionResult.ValidationError; // res.ValidationError = "Please enter quantity in per plyp and per head size products."; // return res; // } //} tblBoxList SaveModel = null; if (ViewModel.SaleInvoiceID == 0) { SaveModel = new tblBoxList() { rcdt = DateTime.Now, rcuid = Common.Props.LoginUser.UserID }; db.tblBoxLists.Add(SaveModel); } else { SaveModel = db.tblBoxLists.FirstOrDefault(r => r.BoxListID == ViewModel.BoxListID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblBoxLists.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } SaveModel.SaleInvoiceID = ViewModel.SaleInvoiceID; foreach (BoxListBoxDetailViewModel bd in ViewModel.BoxListDetails) { SaveModel.tblBoxListBoxDetails.Add(new tblBoxListBoxDetail() { BoxNo = bd.BoxNo, TotalQuan = bd.TotalQuan, tblBoxListProductDetails = bd.Products.Select <BoxListProductDetailViewModel, tblBoxListProductDetail>(bp => new tblBoxListProductDetail() { ProductScientificNameID = bp.ScientificNameID, Quan = bp.Quan }).ToList() }); } //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.SaleInvoiceID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult DeleteRecord(int ID) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { BeforeDeleteValidationResult BeforeDeleteValres = ValidateBeforeDelete(ID, db); if (!BeforeDeleteValres.IsValidForDelete) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Following errors occured while deleting. " + BeforeDeleteValres.ValidationMessage; return(res); } var RecordToDelete = db.tblSaleInvoices.Find(ID); if (RecordToDelete == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected record is already deleted or changed over network. Record not found."; return(res); } foreach (tblSaleInvoiceProductDetail SIPD in RecordToDelete.tblSaleInvoiceProductDetails) { tblProduct Product = db.tblProducts.Find(SIPD.ProductID); if (Product != null) { Product.CurrentStock += SIPD.Quan; db.tblProducts.Attach(Product); db.Entry(Product).State = System.Data.Entity.EntityState.Modified; } } db.tblSaleInvoiceProductDetails.RemoveRange(db.tblSaleInvoiceProductDetails.Where(r => r.SaleInvoiceID == ID)); //if (RecordToDelete.SaleOrderID != null && RecordToDelete.SaleOrderID != 0) //{ // tblSaleOrder SaleOrder = db.tblSaleOrders.Find(RecordToDelete.SaleOrderID); // SaleOrder.tblSaleInvoice = null; // SaleOrder.SaleInvoiceID = null; // db.tblSaleOrders.Attach(SaleOrder); // db.Entry(SaleOrder).State = System.Data.Entity.EntityState.Modified; //} db.tblSaleInvoices.Remove(RecordToDelete); //-- try { db.SaveChanges(); res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }
public SavingResult SaveRecord(SaleInvoiceViewModel ViewModel) { SavingResult res = new SavingResult(); using (dbUltraCoralEntities db = new dbUltraCoralEntities()) { if (ViewModel.SaleInvoiceID != 0 && CheckDuplicate(ViewModel.SaleInvoiceID, ViewModel.InvoiceNo)) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Can not accept duplicate values. The Cultivation Type name is already exists."; return(res); } else { //var x = from p in db.tblProducts // join ps in db.tblProductSizes on p.SizeID equals ps.ProductSizeID // join vm in ViewModel.Products on p.ProductID equals vm.ProductID // where (ps.QuanReq ?? false) == true && vm.Qty == 0 // select new { count = 1 }; if (ViewModel.Products.Count(r => r.IsQtyReq && r.Qty == 0) > 0) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Please enter quantity in per plyp and per head size products."; return(res); } } tblSaleInvoice SaveModel = null; if (ViewModel.SaleInvoiceID == 0) { SaveModel = new tblSaleInvoice() { InvoiceNo = GenerateNextInvoiceNo(db), rcdt = DateTime.Now, rcuid = Common.Props.LoginUser.UserID }; db.tblSaleInvoices.Add(SaveModel); //if((ViewModel.SaleOrderID ?? 0) != 0) //{ // tblSaleOrder SaleOrder = db.tblSaleOrders.Find(ViewModel.SaleOrderID); // SaleOrder.tblSaleInvoice = SaveModel; // db.tblSaleOrders.Attach(SaleOrder); // db.Entry(SaleOrder).State = System.Data.Entity.EntityState.Modified; //} } else { SaveModel = db.tblSaleInvoices.FirstOrDefault(r => r.SaleInvoiceID == ViewModel.SaleInvoiceID); if (SaveModel == null) { res.ExecutionResult = eExecutionResult.ValidationError; res.ValidationError = "Selected user has been deleted over network. Can not find user's details. Please retry."; return(res); } SaveModel.redt = DateTime.Now; SaveModel.reuid = Common.Props.LoginUser.UserID; db.tblSaleInvoices.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; foreach (tblSaleInvoiceProductDetail SIPD in SaveModel.tblSaleInvoiceProductDetails) { tblProduct Product = db.tblProducts.Find(SIPD.ProductID); if (Product != null) { Product.CurrentStock += SIPD.Quan; db.tblProducts.Attach(Product); db.Entry(Product).State = System.Data.Entity.EntityState.Modified; } } db.tblSaleInvoiceProductDetails.RemoveRange(db.tblSaleInvoiceProductDetails.Where(r => r.SaleInvoiceID == ViewModel.SaleInvoiceID)); } //SaveModel.SaleOrderID = ViewModel.SaleOrderID; SaveModel.InvoiceDate = ViewModel.InvoiceDate; SaveModel.InvoiceNo = ViewModel.InvoiceNo; SaveModel.SaleInvoiceStatusID = ViewModel.SaleInvoiceStatusID; SaveModel.CustomerID = ViewModel.CustomerID; SaveModel.BusinessName = ViewModel.BusinessName; SaveModel.ContactName = ViewModel.ContactName; SaveModel.Address = ViewModel.Address; SaveModel.City = ViewModel.City; SaveModel.Postcode = ViewModel.Postcode; SaveModel.Country = ViewModel.Country; SaveModel.IntPhoneNo = ViewModel.IntPhoneNo; SaveModel.EmailContact = ViewModel.EMailContact; SaveModel.AirportDestCity = ViewModel.AirportDestCity; SaveModel.ShippingDate = ViewModel.ShippingDate; SaveModel.ArrivalDate = ViewModel.ArrivalDate; SaveModel.DomesticFlight = ViewModel.DomesticFlight; SaveModel.InternationalFlight = ViewModel.InternationalFlight; //SaveModel.EstDelDate = ViewModel.EstDelDate; //SaveModel.FreightAWBNo = ViewModel.AWBNo; //SaveModel.FreightAWBNo = ViewModel.AWBNo ?? ""; //SaveModel.FreightFlight1 = ViewModel.Flight1 ?? ""; //SaveModel.FreightFlight2 = ViewModel.Flight2 ?? ""; SaveModel.TotalQuan = (ViewModel.Products.Sum(r => (decimal?)r.Qty) ?? 0); SaveModel.TotalGAmt = (ViewModel.Products.Sum(r => (decimal?)r.Amt) ?? 0); SaveModel.EstBoxes = ViewModel.EstBoxes; SaveModel.BoxCharges = ViewModel.BoxCharges; SaveModel.DomesticFreightCharges = ViewModel.DomesticFreightCharges; SaveModel.IntFreightCharges = ViewModel.IntFreightCharges; SaveModel.TTFee = ViewModel.TTFee; SaveModel.TotalFreight = ViewModel.TotalFreight; SaveModel.PreviousCredit = ViewModel.PreviousCredit; SaveModel.TotalPayableAmt = ViewModel.TotalPayableAmt; foreach (SaleInvoiceProducDetailViewModel rp in ViewModel.Products.Where(r => r.Qty != 0)) { SaveModel.tblSaleInvoiceProductDetails.Add(new tblSaleInvoiceProductDetail() { //tblSaleInvoice = SaveModel, ProductID = rp.ProductID, Rate = rp.Rate, Quan = rp.Qty, Amt = rp.Amt }); tblProduct Product = db.tblProducts.Find(rp.ProductID); if (Product != null) { Product.CurrentStock -= rp.Qty; db.tblProducts.Attach(Product); db.Entry(Product).State = System.Data.Entity.EntityState.Modified; } } //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.SaleInvoiceID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { ex = Common.Functions.FindFinalError(ex); res.ExecutionResult = eExecutionResult.ErrorWhileExecuting; res.Exception = ex; } } return(res); }