public override bool UpdateData(int id, ref DTO.ShowroomReceiptMng.ShowroomReceipt dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (ShowroomReceiptMngEntities context = CreateContext()) { ShowroomReceipt dbItem = null; if (id == 0) { dbItem = new ShowroomReceipt(); context.ShowroomReceipt.Add(dbItem); } else { dbItem = context.ShowroomReceipt.FirstOrDefault(o => o.ShowroomReceiptID == id); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { // check concurrency //if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoItem.ConcurrencyFlag_String))) //{ // throw new Exception(DALBase.Helper.TEXT_CONCURRENCY_CONFLICT); //} if (dtoItem.ReceiptTypeID == 2) // 2: EXPORT { DAL.Support.DataFactory support_factory = new Support.DataFactory(); Hashtable support_filters = new Hashtable(); foreach (var item in dtoItem.ShowroomReceiptDetails) { if (item.ShowroomReceiptDetailID > 0) { foreach (var area in item.ShowroomReceiptAreaDetails) { if (area.ShowroomReceiptAreaDetailID > 0) { var physicalByArea = support_factory.QuickSearchShowroomAreaByPhysicalQnt(support_filters, out notification).Where(o => o.ShowroomItemID == item.ShowroomItemID && o.ShowroomAreaID == area.ShowroomAreaID).FirstOrDefault(); var currentArea = context.ShowroomReceiptAreaDetail.Where(o => o.ShowroomReceiptAreaDetailID == area.ShowroomReceiptAreaDetailID).FirstOrDefault(); if (physicalByArea == null) { throw new Exception("Could not find this product in area. You should select another area"); } else if (area.Quantity - currentArea.Quantity > physicalByArea.Quantity) { throw new Exception("Quantity must be less than current physical quantity"); } } else { var physicalByArea = support_factory.QuickSearchShowroomAreaByPhysicalQnt(support_filters, out notification).Where(o => o.ShowroomItemID == item.ShowroomItemID && o.ShowroomAreaID == area.ShowroomAreaID).FirstOrDefault(); if (physicalByArea == null) { throw new Exception("Could not find this product in area. You should select another area"); } else if (area.Quantity > physicalByArea.Quantity) { throw new Exception("Quantity must be less than current physical quantity"); } } } } else { foreach (var area in item.ShowroomReceiptAreaDetails) { var physicalByArea = support_factory.QuickSearchShowroomAreaByPhysicalQnt(support_filters, out notification).Where(o => o.ShowroomItemID == item.ShowroomItemID && o.ShowroomAreaID == area.ShowroomAreaID).FirstOrDefault(); if (physicalByArea == null) { throw new Exception("Could not find this product in area. You should select another area"); } else if (area.Quantity > physicalByArea.Quantity) { throw new Exception("Quantity must be less than current physical quantity"); } } } } } //convert dto to db converter.DTO2DB_ShowroomReceipt(dtoItem, ref dbItem); //reove orphan context.ShowroomReceiptAreaDetail.Local.Where(o => o.ShowroomReceiptDetail == null).ToList().ForEach(o => context.ShowroomReceiptAreaDetail.Remove(o)); context.ShowroomReceiptDetail.Local.Where(o => o.ShowroomReceipt == null).ToList().ForEach(o => context.ShowroomReceiptDetail.Remove(o)); //save data context.SaveChanges(); //update receipt no context.ShowroomReceiptMng_function_GenerateReceipNo(dbItem.ShowroomReceiptID, dbItem.Season, dbItem.ReceiptTypeID); //get return data dtoItem = GetData(dbItem.ShowroomReceiptID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }