public async Task <bool> ClosePurchase(MaterialMainViewModel model)
        {
            if (model.Missing_No == "")
            {
                var purchaseList = await _repoPurchase.GetAll()
                                   .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync();

                foreach (var item in purchaseList)
                {
                    item.Status = "Y";
                }
                return(await _repoPurchase.SaveAll());
            }
            else
            {
                var purchaseList = await _repoMissing.GetAll()
                                   .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync();

                foreach (var item in purchaseList)
                {
                    item.Status = "Y";
                }
                return(await _repoMissing.SaveAll());
            }
        }
        public async Task <bool> ClosePurchase(MaterialMainViewModel model)
        {
            if (model.Missing_No == "")
            {
                var purchaseList = 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)).ToListAsync();

                foreach (var item in purchaseList)
                {
                    item.Status = "Y";
                }
                return(await _repoPurchase.SaveAll());
            }
            else
            {
                var purchaseList = await _repoMissing.FindAll(x => x.Purchase_No.Trim() == model.Purchase_No.Trim() &&
                                                              x.MO_No.Trim() == model.MO_No.Trim()).ToListAsync();

                foreach (var item in purchaseList)
                {
                    item.Status = "Y";
                }
                return(await _repoMissing.SaveAll());
            }
        }
        public async Task <string> StatusPurchase(MaterialMainViewModel model)
        {
            var packingList = _repoPackingList.GetAll().Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim());
            var material    = new List <Material_Dto>();
            var status      = "ok";

            if (model.Missing_No == "")
            {
                material = await _repoPurchase.GetAll().Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim())
                           .ProjectTo <Material_Dto>(_configMapper).ToListAsync();
            }
            else
            {
                material = await _repoMissing.GetAll().Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim())
                           .ProjectTo <Material_Dto>(_configMapper).ToListAsync();
            }

            // Nếu purchase đó chưa có hàng nhận vào.
            if (packingList.Count() > 0)
            {
                foreach (var item in packingList)
                {
                    // Nếu trong bảng packingList có 1 receiveNo của purchase đó chưa đc sản sinh qrcode thì
                    // chưa cho thêm hàng tiếp
                    if (item.Generated_QRCode.Trim() == "N")
                    {
                        status = "no";
                        break;
                    }
                }
            }
            else
            {
                status = "ok";
            }

            // Nếu tồn tại 1 Status trong bảng Purchase hoặc bảng Missing có status = N thì thêm đc.Còn Y là
            // đã đủ hàng rồi.Ko đc thêm tiếp.
            if (status == "ok")
            {
                var checkmaterial = "enough";
                foreach (var item in material)
                {
                    if (item.Status == "N")
                    {
                        checkmaterial = "not enough";
                        break;
                    }
                }
                if (checkmaterial == "enough")
                {
                    status = "no";
                }
            }
            return(status);
        }
        public async Task <IActionResult> StatusPurchase([FromBody] MaterialMainViewModel model)
        {
            var status = await _service.StatusPurchase(model);

            return(Ok(new { status = status }));
        }
        public async Task <IActionResult> ClosePurchase([FromBody] MaterialMainViewModel model)
        {
            var data = await _service.ClosePurchase(model);

            return(Ok(data));
        }
        public async Task <IActionResult> PurchaseNoDetail([FromBody] MaterialMainViewModel model)
        {
            var data = await _service.ReceiveNoMain(model);

            return(Ok(data));
        }
        public async Task <IActionResult> SearchByPurchase([FromBody] MaterialMainViewModel model)
        {
            var data = await _service.MaterialMerging(model);

            return(Ok(data));
        }
        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 <List <ReceiveNoMain> > ReceiveNoMain(MaterialMainViewModel model)
        {
            var packingList = await _packingListService.GetAllAsync();

            var packingListDetail = await _packingListDetailService.GetAllAsync();

            var materialList = new List <Material_Dto>();

            if (model.Missing_No == "")
            {
                materialList = await _repoPurchase.GetAll().ProjectTo <Material_Dto>(_configMapper)
                               .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync();
            }
            else
            {
                materialList = await _repoMissing.GetAll().ProjectTo <Material_Dto>(_configMapper)
                               .Where(x => x.Purchase_No.Trim() == model.Purchase_No.Trim()).ToListAsync();
            }
            var data = (from a in packingList
                        join b in packingListDetail on a.Receive_No.Trim() equals b.Receive_No.Trim()
                        where a.Purchase_No.Trim() == model.Purchase_No.Trim()
                        select new {
                Status = model.Status,
                Purchase_No = model.Purchase_No,
                Delivery_No = a.Delivery_No,
                Missing_No = model.Missing_No,
                MO_No = model.MO_No,
                Receive_No = a.Receive_No,
                MO_Seq = a.MO_Seq,
                Material_ID = a.Material_ID,
                Material_Name = a.Material_Name,
                Receive_Date = a.Receive_Date,
                Purchase_Qty = b.Purchase_Qty,
                Received_Qty = b.Received_Qty,
                Generated_QRCode = a.Generated_QRCode,
                Sheet_Type = a.Sheet_Type,
                Updated_By = a.Updated_By
            }).GroupBy(x => x.Receive_No).Select(cl => new ReceiveNoMain()
            {
                MO_No            = cl.First().MO_No,
                Missing_No       = cl.First().Missing_No,
                Purchase_No      = cl.First().Purchase_No,
                Delivery_No      = cl.First().Delivery_No,
                Receive_No       = cl.First().Receive_No,
                MO_Seq           = cl.First().MO_Seq,
                Material_ID      = cl.First().Material_ID,
                Material_Name    = cl.First().Material_Name,
                Receive_Date     = cl.First().Receive_Date,
                Purchase_Qty     = cl.Sum(c => c.Purchase_Qty),
                Accumated_Qty    = cl.Sum(c => c.Received_Qty),
                Generated_QRCode = cl.First().Generated_QRCode,
                Sheet_Type       = cl.First().Sheet_Type,
                Updated_By       = cl.First().Updated_By
            }).OrderByDescending(x => x.Receive_Date).ToList();

            foreach (var item1 in data)
            {
                var materialByPurchaseBath = materialList.Where(x => x.MO_Seq == item1.MO_Seq);
                var list1 = materialByPurchaseBath.GroupBy(x => x.Purchase_No).Select(cl => new {
                    Accumated_Qty = cl.Sum(x => x.Accumlated_In_Qty)
                }).ToList();
                item1.Accumated_Qty_All = list1[0].Accumated_Qty;
            }
            return(data);
        }
        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);
        }