public async Task <object> MaterialMerging(MaterialMainViewModel model) { var listMaterial = new List <Material_Dto>(); if (model.Missing_No != "") { listMaterial = await _repoMissing.GetAll().ProjectTo <Material_Dto>(_configMapper) .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync(); } else { listMaterial = await _repoPurchase.GetAll().ProjectTo <Material_Dto>(_configMapper) .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync(); } listMaterial.OrderByDescending(x => x.MO_Seq); var listBatch = listMaterial.GroupBy(x => x.MO_Seq).Select(y => new { MO_Seq = y.First().MO_Seq }).ToList(); var list3 = new List <OrderSizeByBatch>(); foreach (var item in listBatch) { var item1 = new OrderSizeByBatch(); item1.MO_Seq = item.MO_Seq; item1.Purchase_No = model.Purchase_No; item1.Missing_No = model.Missing_No; item1.Material_ID = model.Material_ID; item1.Material_Name = model.Material_Name; item1.Model_No = model.Model_No; item1.Model_Name = model.Model_Name; item1.MO_No = model.MO_No; item1.Article = model.Article; item1.Supplier_ID = model.Supplier_ID; item1.Supplier_Name = model.Supplier_Name; item1.Subcon_No = model.Subcon_No; item1.Subcon_Name = model.Subcon_Name; item1.T3_Supplier = model.T3_Supplier; item1.T3_Supplier_Name = model.T3_Supplier_Name; item1.CheckInsert = "1"; foreach (var item4 in listMaterial) { if (item4.MO_Seq == item.MO_Seq) { if (item4.Accumlated_In_Qty != item4.Purchase_Qty) { item1.CheckInsert = "0"; break; } } } var item3 = new List <OrderSizeAccumlate>(); foreach (var item2 in listMaterial) { if (item2.MO_Seq == item.MO_Seq) { var item4 = new OrderSizeAccumlate(); item4.Order_Size = item2.Order_Size; item4.Model_Size = item2.Model_Size; item4.Tool_Size = item2.Tool_Size; item4.Spec_Size = item2.Spec_Size; item4.Purchase_Qty_Const = item2.Purchase_Qty; item4.MO_Qty = item2.MO_Qty; item4.Purchase_Qty = item2.Purchase_Qty - item2.Accumlated_In_Qty; item4.Accumlated_In_Qty = item2.Accumlated_In_Qty; item4.Received_Qty = 0; item3.Add(item4); } ; item1.Purchase_Qty = item3; } list3.Add(item1); } var list1 = listMaterial.GroupBy(l => l.Order_Size) .Select(cl => new { Order_Size = cl.First().Order_Size, Accumlated_In_Qty = cl.Sum(c => c.Accumlated_In_Qty), Delivery_Qty_Batches = cl.Sum(x => x.Accumlated_In_Qty), Purchase_Qty = cl.Sum(c => c.Purchase_Qty), Delivery_Qty = cl.Sum(c => c.Purchase_Qty) - cl.Sum(c => c.Accumlated_In_Qty) }).ToList(); var list2 = listMaterial.GroupBy(x => new { x.Order_Size, x.Purchase_Qty, x.MO_Seq }) .Select(y => new { Order_Size = y.First().Order_Size, Purchase_Qty = y.First().Purchase_Qty, MO_Seq = y.First().MO_Seq, }); var list4 = new List <MaterialMergingViewMode>(); foreach (var item in list1) { var arrayItem = new MaterialMergingViewMode(); arrayItem.Order_Size = item.Order_Size; arrayItem.Purchase_Qty = item.Purchase_Qty; arrayItem.Accumlated_In_Qty = item.Accumlated_In_Qty; arrayItem.Delivery_Qty = item.Delivery_Qty; arrayItem.Delivery_Qty_Batches = item.Delivery_Qty_Batches; var array1 = new List <BatchQtyItem>(); foreach (var item1 in list2) { if (item1.Order_Size.Trim() == item.Order_Size.Trim()) { var item2 = new BatchQtyItem(); item2.MO_Seq = item1.MO_Seq; item2.Purchase_Qty = item1.Purchase_Qty; array1.Add(item2); arrayItem.Purchase_Qty_Item = array1; } } list4.Add(arrayItem); } var result = new { list3, list4 }; return(result); }
public async Task <object> MaterialMerging(MaterialMainViewModel model) { var listMaterial = new List <Material_Dto>(); if (model.Missing_No != "") { listMaterial = await _repoMissing.FindAll(x => x.Purchase_No.Trim() == model.Purchase_No.Trim() && x.Missing_No.Trim() == model.Missing_No.Trim() && x.MO_No.Trim() == model.MO_No.Trim()) .ProjectTo <Material_Dto>(_configMapper) .ToListAsync(); } else { listMaterial = await _repoPurchase.FindAll(x => x.Purchase_No.Trim() == model.Purchase_No.Trim() && x.MO_No.Trim() == model.MO_No.Trim() && x.Biz_Tflag.Trim() != "D" && (x.MO_Qty != 0 || x.Purchase_Qty != 0) ) .ProjectTo <Material_Dto>(_configMapper) .ToListAsync(); } listMaterial.OrderByDescending(x => x.MO_Seq); var OrderSizeGroup = listMaterial.GroupBy(x => x.Order_Size).Select(x => x.Key); OrderSizeGroup = OrderSizeGroup.OrderBy(x => decimal.Parse(x)).ToList(); var listBatch = listMaterial.GroupBy(x => x.MO_Seq).Select(y => new { MO_Seq = y.First().MO_Seq }).ToList(); var list3 = new List <OrderSizeByBatch>(); foreach (var item in listBatch) { var item1 = new OrderSizeByBatch(); item1.MO_Seq = item.MO_Seq; item1.Purchase_No = model.Purchase_No; item1.Missing_No = model.Missing_No; item1.Material_ID = model.Material_ID; item1.Material_Name = model.Material_Name; item1.Model_No = model.Model_No; item1.Model_Name = model.Model_Name; item1.MO_No = model.MO_No; item1.Article = model.Article; item1.Supplier_ID = model.Supplier_ID; item1.Supplier_Name = model.Supplier_Name; item1.Subcon_No = model.Subcon_No; item1.Subcon_Name = model.Subcon_Name; item1.T3_Supplier = model.T3_Supplier; item1.T3_Supplier_Name = model.T3_Supplier_Name; // ---- Start Check xem Batch này đã insert đủ hết chưa.----- item1.CheckInsert = "1"; foreach (var item4 in listMaterial) { if (item4.MO_Seq == item.MO_Seq) { if (item4.Accumlated_In_Qty != item4.Purchase_Qty) { item1.CheckInsert = "0"; break; } } } // -----------------------------End------------------------ var item3 = new List <OrderSizeAccumlate>(); foreach (var orderSize in OrderSizeGroup) { var materialByBathOrderSize = listMaterial .Where(x => x.Order_Size.Trim() == orderSize.Trim() && x.MO_Seq == item.MO_Seq); if (materialByBathOrderSize.Count() > 0) { foreach (var item2 in materialByBathOrderSize) { var item4 = new OrderSizeAccumlate(); item4.Order_Size = item2.Order_Size; item4.Model_Size = item2.Model_Size; item4.Tool_Size = item2.Tool_Size; item4.Spec_Size = item2.Spec_Size; item4.Purchase_Qty_Const = item2.Purchase_Qty; item4.MO_Qty = item2.MO_Qty; item4.Purchase_Qty = item2.Purchase_Qty - item2.Accumlated_In_Qty; item4.Accumlated_In_Qty = item2.Accumlated_In_Qty; item4.Received_Qty = 0; item3.Add(item4); item1.Purchase_Qty = item3; } } else { var item4 = new OrderSizeAccumlate(); item4.Order_Size = orderSize; item3.Add(item4); } } list3.Add(item1); } // -------------------------------------------------------------------------------------------// var list1 = listMaterial.GroupBy(l => l.Order_Size) .Select(cl => new { Tool_Size = cl.First().Tool_Size, Order_Size = cl.First().Order_Size, Accumlated_In_Qty = cl.Sum(c => c.Accumlated_In_Qty), Delivery_Qty_Batches = cl.Sum(x => x.Accumlated_In_Qty), Purchase_Qty = cl.Sum(c => c.Purchase_Qty), Delivery_Qty = cl.Sum(c => c.Purchase_Qty) - cl.Sum(c => c.Accumlated_In_Qty) }).ToList(); var list2 = listMaterial.GroupBy(x => new { x.Order_Size, x.Purchase_Qty, x.MO_Seq }) .Select(y => new { Order_Size = y.First().Order_Size, Purchase_Qty = y.First().Purchase_Qty, MO_Seq = y.First().MO_Seq, }); var list4 = new List <MaterialMergingViewMode>(); foreach (var orderSize in OrderSizeGroup) { var list5 = list1.Where(x => x.Order_Size.Trim() == orderSize.Trim()); foreach (var item in list5) { var arrayItem = new MaterialMergingViewMode(); arrayItem.Tool_Size = item.Tool_Size; arrayItem.Order_Size = item.Order_Size; arrayItem.Purchase_Qty = item.Purchase_Qty; arrayItem.Accumlated_In_Qty = item.Accumlated_In_Qty; arrayItem.Delivery_Qty = item.Delivery_Qty; arrayItem.Delivery_Qty_Batches = item.Delivery_Qty_Batches; var array1 = new List <BatchQtyItem>(); foreach (var item1 in list2) { if (item1.Order_Size.Trim() == item.Order_Size.Trim()) { var item2 = new BatchQtyItem(); item2.MO_Seq = item1.MO_Seq; item2.Purchase_Qty = item1.Purchase_Qty; array1.Add(item2); arrayItem.Purchase_Qty_Item = array1; } } list4.Add(arrayItem); } } var totalByBatch = new List <TotalQtyOfBatch>(); foreach (var item in list3) { var totalQty = item.Purchase_Qty.Sum(x => x.Purchase_Qty); var itemTotalQty = new TotalQtyOfBatch(); itemTotalQty.Total = totalQty; itemTotalQty.MO_Seq = item.MO_Seq; totalByBatch.Add(itemTotalQty); } var listTotalQty = new TotalQty(); listTotalQty.TotalPurchaseQty = list4.Sum(x => x.Purchase_Qty); listTotalQty.TotalAccumated_Qty = list4.Sum(x => x.Accumlated_In_Qty); listTotalQty.TotalDeliveryQty = list4.Sum(x => x.Delivery_Qty); // --------------------------------------End----------------------------------------// var result = new { list3, list4, totalByBatch, listTotalQty }; return(result); }