public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification { Type = Library.DTO.NotificationType.Success }; try { using (FactoryMaterialReceiptEntities context = CreateContext()) { var dbItem = context.FactoryMaterialReceipt.Where(o => o.FactoryMaterialReceiptID == id).FirstOrDefault(); context.FactoryMaterialReceipt.Remove(dbItem); context.SaveChanges(); } 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); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryMaterialReceipt dtoFactoryMaterialReceipt = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryMaterialReceipt>(); dtoFactoryMaterialReceipt.UpdatedBy = userId; try { using (FactoryMaterialReceiptEntities context = CreateContext()) { FactoryMaterialReceipt dbItem = null; if (id == 0) { dbItem = new FactoryMaterialReceipt(); context.FactoryMaterialReceipt.Add(dbItem); } else { dbItem = context.FactoryMaterialReceipt.Where(o => o.FactoryMaterialReceiptID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { // if (dtoFactoryMaterialReceipt.ReceiptTypeID == 2 && !dtoFactoryMaterialReceipt.FactoryTeamID.HasValue) // 2 : Export { throw new Exception("Team is empty. You should fill-in Team"); } if (dtoFactoryMaterialReceipt.FactoryMaterialReceiptDetails.Where(o => !o.FactoryAreaID.HasValue).Count() > 0) { throw new Exception("Area is empty. All product must be fill-in Area"); } //validate quantity if (dtoFactoryMaterialReceipt.ReceiptTypeID == 1) // import receipt { ValidateQntImport(dtoFactoryMaterialReceipt); } else if (dtoFactoryMaterialReceipt.ReceiptTypeID == 2) // export receipt { ValidateQntExport(dtoFactoryMaterialReceipt); } //convert dto to db converter.DTO2DB_FactoryMaterialReceipt(dtoFactoryMaterialReceipt, ref dbItem); //remove orphan item context.FactoryMaterialReceiptDetail.Local.Where(o => o.FactoryMaterialReceipt == null).ToList().ForEach(o => context.FactoryMaterialReceiptDetail.Remove(o)); //save data context.SaveChanges(); //update receipt no //context.FactoryMaterialReceiptMng_function_GenerateReceipNo(dbItem.FactoryMaterialReceiptID, dbItem.Season, dbItem.ReceiptTypeID); //get return data dtoItem = GetData(dbItem.FactoryMaterialReceiptID, 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); } }