public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryProformaInvoice dtoFactoryProformaInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryProformaInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryProformaInvoice2MngEntities context = CreateContext()) { // check if user can update this information if (fwFactory.CheckFactoryPermission(userId, dtoFactoryProformaInvoice.FactoryID.Value) == 0) { throw new Exception("Current user don't have access permission for the selected factory data"); } if (id > 0 && fwFactory.CheckFactoryProformaInvoicePermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected proforma invoice data"); } FactoryProformaInvoice2 dbItem = null; if (id == 0) { dbItem = new FactoryProformaInvoice2(); context.FactoryProformaInvoice2.Add(dbItem); } else { dbItem = context.FactoryProformaInvoice2.FirstOrDefault(o => o.FactoryProformaInvoiceID == id); } if (dbItem == null) { notification.Message = "Factory proforma invoice not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryProformaInvoice.ConcurrencyFlag))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } // check if item already in other proforma invoice or if item belong to the filter: client, factory, season int itemID; int clientID = dtoFactoryProformaInvoice.ClientID.Value; int factoryID = dtoFactoryProformaInvoice.FactoryID.Value; string season = dtoFactoryProformaInvoice.Season; List <DTO.FactoryOrderItemSearchResult> checkList = converter.DB2DTO_FactoryOrderItemSearchResultList(context.FactoryProformaInvoice2Mng_FactoryOrderItemSearchResult_View.Where(o => o.ClientID == clientID && o.FactoryID == factoryID && o.Season == season).ToList()); foreach (DTO.FactoryProformaInvoiceDetail dtoDetail in dtoFactoryProformaInvoice.FactoryProformaInvoiceDetails) { if (dtoDetail.FactoryOrderDetailID.HasValue) { itemID = dtoDetail.FactoryOrderDetailID.Value; if (checkList.FirstOrDefault(o => o.FactoryOrderDetailID == itemID) == null) { throw new Exception(dtoDetail.Description + " is invalid"); } if (context.FactoryProformaInvoiceDetail2.FirstOrDefault(o => o.FactoryOrderDetailID == itemID && o.FactoryProformaInvoiceID != id) != null) { throw new Exception(dtoDetail.Description + "(" + dtoDetail.FactoryOrderUD + " - " + dtoDetail.LDS + ")" + " is already exists in other proforma invoice"); } } if (dtoDetail.FactoryOrderSparepartDetailID.HasValue) { itemID = dtoDetail.FactoryOrderSparepartDetailID.Value; if (checkList.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == itemID) == null) { throw new Exception(dtoDetail.Description + " is invalid"); } if (context.FactoryProformaInvoiceDetail2.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == itemID && o.FactoryProformaInvoiceID != id) != null) { throw new Exception(dtoDetail.Description + "(" + dtoDetail.FactoryOrderUD + " - " + dtoDetail.LDS + ")" + " is already exists in other proforma invoice"); } } } if ((dbItem.IsFurnindoConfirmed.HasValue && dbItem.IsFurnindoConfirmed.Value) || (dbItem.IsFactoryConfirmed.HasValue && dbItem.IsFactoryConfirmed.Value)) { converter.DTO2DB(dtoFactoryProformaInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", true); } else { converter.DTO2DB(dtoFactoryProformaInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", false); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // remove orphan context.FactoryProformaInvoiceDetail2.Local.Where(o => o.FactoryProformaInvoice2 == null).ToList().ForEach(o => context.FactoryProformaInvoiceDetail2.Remove(o)); // generate p/i number if (id <= 0) { using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM FactoryProformaInvoice2 WITH (TABLOCKX, HOLDLOCK)"); context.Database.ExecuteSqlCommand("SELECT * FROM FactoryProformaInvoiceDetail2 WITH (TABLOCKX, HOLDLOCK)"); try { dbItem.ProformaInvoiceNo = context.FactoryProformaInvoice2Mng_function_GeneratePINumber(dbItem.FactoryID, dbItem.ClientID, dbItem.Season).FirstOrDefault(); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } else { context.SaveChanges(); } dtoItem = GetData(userId, dbItem.FactoryProformaInvoiceID, -1, -1, string.Empty, out notification).Data; return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }