public void GenerateDyeingOrders(int iserial) { using (var entities = new WorkFlowManagerDBEntities()) { var planHeader = entities.TblDyeingPlanHeaders.SingleOrDefault(x => x.Iserial == iserial); if (planHeader != null) { planHeader.PlanGenerated = true; var dyeingSummaryList = entities.TblDyeingSummaries.Include("DyeingSummaryServices").Where(x => x.DyeingHeader == planHeader.Iserial).ToList(); var vendorList = dyeingSummaryList.Select(x => x.Vendor).Distinct().ToList(); foreach (var item in vendorList) { var dyeingHeaderRow = new TblDyeingOrdersHeader { DocPlan = planHeader.DocNo, Vendor = item, TransactionDate = planHeader.TransactionDate, }; entities.AddToTblDyeingOrdersHeaders(dyeingHeaderRow); entities.SaveChanges(); const int transId = 1; var mainDetails = new TblDyeingOrdersMainDetail { DyeingProductionOrder = dyeingHeaderRow.DyeingProductionOrder, WareHouse = planHeader.Location, TransactionType = 0, TransId = transId, Posted = false }; entities.AddToTblDyeingOrdersMainDetails(mainDetails); entities.SaveChanges(); var summarylist = dyeingSummaryList.Where(x => x.Vendor == item).ToList(); var sub = new ObservableCollection <TblDyeingOrdersDetail>(); foreach (var dyeingObject in summarylist) { var orderDetailsServices = new ObservableCollection <DyeingOrderDetailsService>(); foreach (var service in dyeingObject.DyeingSummaryServices) { orderDetailsServices.Add(new DyeingOrderDetailsService { Notes = service.Notes, ServiceCode = service.ServiceCode, ServiceName = service.ServiceName, Qty = service.Qty }); } var entityCollection = new EntityCollection <DyeingOrderDetailsService>(); foreach (var entity in orderDetailsServices) { entityCollection.Add(entity); } sub.Add(new TblDyeingOrdersDetail { BatchNo = dyeingObject.BatchNo, Color = dyeingObject.Color, DyeingClass = dyeingObject.DyeingClass, DyedFabric = dyeingObject.DyedFabric, Unit = dyeingObject.Unit, CalculatedTotalQty = Convert.ToSingle(dyeingObject.CalculatedTotalQty), FabricCode = dyeingObject.FabricCode, DyeingProductionOrder = dyeingHeaderRow.DyeingProductionOrder, TransactionType = mainDetails.TransactionType, TransId = mainDetails.TransId, DyeingOrderDetailsServices = entityCollection, }); } foreach (var orderDetails in sub) { entities.AddToTblDyeingOrdersDetails(orderDetails); entities.SaveChanges(); } } } } }
public List <TblDyeingOrdersMainDetail> SaveDyeingOrder(TblDyeingOrdersHeader header, List <TblDyeingOrdersMainDetail> Maindetails) { using (var entities = new WorkFlowManagerDBEntities()) { if (header.DyeingProductionOrder != 0) { var h = entities.TblDyeingOrdersHeaders.SingleOrDefault(x => x.DyeingProductionOrder == header.DyeingProductionOrder); if (h != null) { h.DocPlan = header.DocPlan; h.TransactionDate = header.TransactionDate; h.Vendor = header.Vendor; } } else { entities.AddToTblDyeingOrdersHeaders(header); } entities.SaveChanges(); foreach (var item in Maindetails) { if (item.DyeingProductionOrder == 0) { item.DyeingProductionOrder = header.DyeingProductionOrder; entities.AddToTblDyeingOrdersMainDetails(item); } else { var mainDetailRow = (from d in entities.TblDyeingOrdersMainDetails where d.TransId == item.TransId && d.TransactionType == item.TransactionType && d.DyeingProductionOrder == item.DyeingProductionOrder select d).SingleOrDefault(); if (mainDetailRow != null) { GenericUpdate(mainDetailRow, item, entities); } else { entities.AddToTblDyeingOrdersMainDetails(item); } } entities.SaveChanges(); if (item.DyeingProductionOrder != 0) { foreach (var items in item.TblDyeingOrdersDetails.ToList()) { var dyeingOrderDetails = (from d in entities.TblDyeingOrdersDetails where d.Iserial == items.Iserial && d.TransactionType == item.TransactionType select d).SingleOrDefault(); if (dyeingOrderDetails != null) { GenericUpdate(dyeingOrderDetails, items, entities); } else { items.TblDyeingOrdersMainDetail = null; entities.AddToTblDyeingOrdersDetails(items); } entities.SaveChanges(); } } } } return(Maindetails); }