public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification { Type = Library.DTO.NotificationType.Success }; try { using (FactoryProductionStatusEntities context = CreateContext()) { var dbItem = context.FactoryProductionStatus.Where(o => o.FactoryProductionStatusID == id).FirstOrDefault(); context.FactoryProductionStatus.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.FactoryProductionStatusDTO dtoFactoryProductionStatus = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryProductionStatusDTO>(); try { using (FactoryProductionStatusEntities context = CreateContext()) { FactoryProductionStatus dbItem = null; if (id > 0) { dbItem = context.FactoryProductionStatus.Where(o => o.FactoryProductionStatusID == id).FirstOrDefault(); if (dbItem.WeekNo != dtoFactoryProductionStatus.WeekNo) { throw new Exception("You can not change week"); } } else { dbItem = new FactoryProductionStatus(); context.FactoryProductionStatus.Add(dbItem); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //calculate Produced Cont for every order decimal totalCont = 0; foreach (var item in dtoFactoryProductionStatus.FactoryProductionStatusDetailDTOs) { totalCont = 0; foreach (var sItem in item.FactoryProductionStatusOrderDetailDTOs) { if (sItem.Qnt40HC.HasValue && sItem.Qnt40HC != 0 && sItem.ProducedQnt.HasValue) { sItem.ProducedCont = Math.Round(Convert.ToDecimal(sItem.ProducedQnt.Value) / Convert.ToDecimal(sItem.Qnt40HC), 2); totalCont += Math.Round(Convert.ToDecimal(sItem.ProducedQnt.Value) / Convert.ToDecimal(sItem.Qnt40HC), 2); } } item.ProducedContainerQnt = totalCont; } //convert dto to db converter.DTO2DB_FactoryProductionStatus(dtoFactoryProductionStatus, ref dbItem); dbItem.UpdatedBy = userId; //check permission on factory if (fwFactory.CheckFactoryPermission(userId, dbItem.FactoryID.Value) == 0) { throw new Exception("You do not have access permission on this factory"); } //check week //int curentWeek = Library.Helper.GetCurrentWeek(); //if (curentWeek - dbItem.WeekNo.Value >= 2) //{ // throw new Exception("You can not update because this week is over 2 weeks compare with current week (" + curentWeek.ToString() + " )"); //} //remove orphan context.FactoryProductionStatusDetail.Local.Where(o => o.FactoryProductionStatus == null).ToList().ForEach(o => context.FactoryProductionStatusDetail.Remove(o)); //save data context.SaveChanges(); //get return data dtoItem = GetData(dbItem.FactoryProductionStatusID, out notification).Data; //auto create production item context.FactoryStockReceiptMng_function_CreateProductionItem(); //get total output procduce all week //List<int> factoryOrderDetailIDs = new List<int>(); //foreach (var item in dbItem.FactoryProductionStatusDetail.ToList()) //{ // foreach (var sItem in item.FactoryProductionStatusOrderDetail.Where(o =>o.FactoryOrderDetailID.HasValue).ToList()) // { // factoryOrderDetailIDs.Add(sItem.FactoryOrderDetailID.Value); // } //} //var factoryOrderDetails = from item in context.FactoryProductionStatusOrderDetail.Where(o =>factoryOrderDetailIDs.Contains(o.FactoryOrderDetailID.Value)).ToList() // group item by new { item.FactoryOrderDetailID } into g // select new { g.Key.FactoryOrderDetailID, TotalOutputProduced = g.Sum(o => o.OutputProducedQnt) }; ////var factoryStock = context.FactoryStockReceiptDetail.Where(o => o.FactoryOrderDetailID.HasValue && factoryOrderDetails.Select(s => s.FactoryOrderDetailID).Contains(o.FactoryOrderDetailID)); //foreach (var item in context.FactoryStockReceiptDetail.Where(o => o.FactoryOrderDetailID.HasValue && factoryOrderDetailIDs.Contains(o.FactoryOrderDetailID.Value))) //{ // var x = factoryOrderDetails.Where(o => o.FactoryOrderDetailID == item.FactoryOrderDetailID).FirstOrDefault(); // if (x != null) // { // item.ProducedQnt = x.TotalOutputProduced; // } //} //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); } }