Ejemplo n.º 1
0
        public async Task <List <ReceiveAfterSubmit> > SubmitData(List <ReceivingMaterialMainModel> data, string updateBy)
        {
            var receiveAfterSubmit = new List <ReceiveAfterSubmit>();

            foreach (var itemData in data)
            {
                // Update ở bảng WMSB_Material_Purchase
                var materialPurchaseByPurchase = await _repoPurchase.FindAll(x => x.Biz_Tflag.Trim() != "D" &&
                                                                             (x.MO_Qty != 0 || x.Purchase_Qty != 0) &&
                                                                             x.Purchase_No.Trim() == itemData.Purchase_No.Trim() &&
                                                                             x.MO_No.Trim() == itemData.MO_No.Trim() &&
                                                                             x.Material_ID.Trim() == itemData.Material_ID.Trim()).ToListAsync();

                materialPurchaseByPurchase.ForEach(item => {
                    item.Status            = "Y";
                    item.Accumlated_In_Qty = item.Purchase_Qty;
                });

                // Tính toán có bao nhiêu batch
                var countBath = materialPurchaseByPurchase.Select(x => x.MO_Seq).ToList();
                countBath = countBath.Distinct().ToList();
                if (countBath.Count > 0)
                {
                    foreach (var itemBatch in countBath)
                    {
                        //Thêm ở bảng PackingList
                        var packingListModel = new WMSB_Packing_List();
                        packingListModel.Sheet_Type       = "B";
                        packingListModel.Missing_No       = "";
                        packingListModel.Delivery_No      = "";
                        packingListModel.Supplier_ID      = itemData.Supplier_ID;
                        packingListModel.Supplier_Name    = itemData.Supplier_Name;
                        packingListModel.Receive_Date     = DateTime.Now;
                        packingListModel.MO_No            = itemData.MO_No;
                        packingListModel.Purchase_No      = itemData.Purchase_No;
                        packingListModel.MO_Seq           = itemBatch;
                        packingListModel.Receive_No       = CodeUtility.RandomReceiveNo("RW", 2);
                        packingListModel.Material_ID      = itemData.Material_ID;
                        packingListModel.Material_Name    = itemData.Material_Name;
                        packingListModel.Article          = itemData.Article;
                        packingListModel.Model_No         = itemData.Model_No;
                        packingListModel.Model_Name       = itemData.Model_Name;
                        packingListModel.Subcon_ID        = itemData.Subcon_No;
                        packingListModel.Subcon_Name      = itemData.Subcon_Name;
                        packingListModel.T3_Supplier      = itemData.T3_Supplier;
                        packingListModel.T3_Supplier_Name = itemData.T3_Supplier_Name;
                        packingListModel.Generated_QRCode = "N";
                        packingListModel.Updated_Time     = DateTime.Now;
                        packingListModel.Updated_By       = updateBy;
                        _repoPackingList.Add(packingListModel);

                        // Tạo ra record để show ngoài màn hình sau khi submit
                        var receiveAfterSubmitItem = new ReceiveAfterSubmit();
                        var sumAccumlatedQty       = materialPurchaseByPurchase
                                                     .Where(x => x.MO_Seq == itemBatch).Sum(x => x.Purchase_Qty);
                        receiveAfterSubmitItem.MO_No         = packingListModel.MO_No;
                        receiveAfterSubmitItem.Purchase_No   = packingListModel.Purchase_No;
                        receiveAfterSubmitItem.Receive_No    = packingListModel.Receive_No;
                        receiveAfterSubmitItem.MO_Seq        = itemBatch;
                        receiveAfterSubmitItem.Material_ID   = packingListModel.Material_ID;
                        receiveAfterSubmitItem.Material_Name = packingListModel.Material_Name;
                        receiveAfterSubmitItem.Receive_Time  = packingListModel.Receive_Date;
                        receiveAfterSubmitItem.Purchase_Qty  = sumAccumlatedQty;
                        receiveAfterSubmitItem.Type          = "R";
                        receiveAfterSubmitItem.Update_By     = packingListModel.Updated_By;
                        receiveAfterSubmit.Add(receiveAfterSubmitItem);

                        // Thêm ở bảng PackingListDetail
                        var materialPurchaseForBatchs = materialPurchaseByPurchase.Where(x => x.MO_Seq == itemBatch).ToList();
                        foreach (var materialForBatchItem in materialPurchaseForBatchs)
                        {
                            var packingListDetailModel = new WMSB_PackingList_Detail();
                            packingListDetailModel.Receive_No   = packingListModel.Receive_No;
                            packingListDetailModel.Order_Size   = materialForBatchItem.Order_Size;
                            packingListDetailModel.Model_Size   = materialForBatchItem.Model_Size;
                            packingListDetailModel.Tool_Size    = materialForBatchItem.Tool_Size;
                            packingListDetailModel.Spec_Size    = materialForBatchItem.Spec_Size;
                            packingListDetailModel.MO_Qty       = materialForBatchItem.MO_Qty;
                            packingListDetailModel.Purchase_Qty = materialForBatchItem.Purchase_Qty;
                            packingListDetailModel.Received_Qty = materialForBatchItem.Purchase_Qty;
                            packingListDetailModel.Updated_Time = DateTime.Now;
                            packingListDetailModel.Updated_By   = updateBy;
                            _repoPackingListDetail.Add(packingListDetailModel);
                        }
                    }
                }
            }
            await _repoPackingListDetail.SaveAll();

            return(receiveAfterSubmit);
        }
Ejemplo n.º 2
0
        public async Task <List <ReceiveNoMain> > UpdateMaterial(List <OrderSizeByBatch> data, string updateBy)
        {
            foreach (var item in data)
            {
                item.Purchase_Qty = item.Purchase_Qty.Where(x => x.Tool_Size != null).ToList();
            }
            var Purchase_No = data[0].Purchase_No.Trim();
            var MO_No       = data[0].MO_No.Trim();

            // ------------------------------------------------------------------------------------------------------//
            if (data[0].Missing_No == "")
            {
                // --------Update lại Accumlated_In_Qty theo Purchase_No,Order_Size và Mo_Seq ở bảng Material_Purchase----//
                var purchaseMaterialAll = await _repoPurchase.FindAll(x => x.Biz_Tflag.Trim() != "D" &&
                                                                      (x.MO_Qty != 0 || x.Purchase_Qty != 0) &&
                                                                      x.Purchase_No.Trim() == Purchase_No.Trim()).ToListAsync();

                foreach (var item in data)
                {
                    foreach (var item1 in item.Purchase_Qty)
                    {
                        var materialItem = purchaseMaterialAll
                                           .Where(x => x.MO_No.Trim() == MO_No &&
                                                  x.Order_Size == item1.Order_Size &&
                                                  x.MO_Seq == item.MO_Seq).FirstOrDefault();
                        // materialItem.Accumlated_In_Qty = item1.Accumlated_In_Qty;
                        // Số lượng bằng số lượng nhận hiện tại + số lượng vừa mới nhận vào.
                        materialItem.Accumlated_In_Qty = materialItem.Accumlated_In_Qty + item1.Received_Qty;
                    }
                }

                //------------------------- Update giá trị Status--------------------------------------------//
                foreach (var item in data)
                {
                    await this.UpdateStatusMaterialSubmit(item.Purchase_No, item.MO_No, item.MO_Seq, item.Missing_No);
                }
            }
            else
            {
                // Update lại Accumlated_In_Qty theo Purchase_No,Order_Size và Mo_Seq ở bảng Material_Missing
                var MaterialMissingAll = await _repoMissing.FindAll(x => x.Purchase_No.Trim() == Purchase_No.Trim()).ToListAsync();

                foreach (var item in data)
                {
                    foreach (var item1 in item.Purchase_Qty)
                    {
                        var materialItem = MaterialMissingAll
                                           .Where(x => x.MO_No.Trim() == MO_No &&
                                                  x.Order_Size == item1.Order_Size &&
                                                  x.MO_Seq == item.MO_Seq).FirstOrDefault();
                        materialItem.Accumlated_In_Qty = item1.Accumlated_In_Qty;
                    }
                }

                // Update lại Status
                foreach (var item in data)
                {
                    await this.UpdateStatusMaterialSubmit(item.Purchase_No, item.MO_No, item.MO_Seq, item.Missing_No);
                }
            }

            //------------------------Thêm vào 2 bảng Packing_List và Packing_List_Detail------------------//
            var ReceiveNoMain = new List <ReceiveNoMain>();

            foreach (var item in data)
            {
                // Check xem có tiến hành thêm hay ko
                var checkAdd = false;
                foreach (var item1 in item.Purchase_Qty)
                {
                    // Kiểm tra nếu tồn tại Received_Qty lớn hơn 0
                    //,có nghĩa là tồn tại 1 Order_Size trong batch đó có nhận hàng
                    if (item1.Received_Qty > 0)
                    {
                        checkAdd = true;
                        break;
                    }
                }
                // Tiến hành thêm vào bảng Packing_List và Packing_List_Detail
                if (checkAdd == true)
                {
                    var packing_List = new Packing_List_Dto();
                    if (item.Missing_No == string.Empty)
                    {
                        packing_List.Sheet_Type = "R";
                    }
                    else
                    {
                        packing_List.Sheet_Type = "M";
                    }
                    packing_List.Missing_No       = item.Missing_No;
                    packing_List.Supplier_ID      = item.Supplier_ID;
                    packing_List.Supplier_Name    = item.Supplier_Name;
                    packing_List.MO_No            = item.MO_No;
                    packing_List.Purchase_No      = item.Purchase_No;
                    packing_List.MO_Seq           = item.MO_Seq;
                    packing_List.Delivery_No      = item.Delivery_No;
                    packing_List.Material_ID      = item.Material_ID;
                    packing_List.Material_Name    = item.Material_Name;
                    packing_List.Model_No         = item.Model_No;
                    packing_List.Model_Name       = item.Model_Name;
                    packing_List.Article          = item.Article;
                    packing_List.Subcon_ID        = item.Subcon_No;
                    packing_List.Subcon_Name      = item.Subcon_Name;
                    packing_List.T3_Supplier      = item.T3_Supplier;
                    packing_List.T3_Supplier_Name = item.T3_Supplier_Name;
                    packing_List.Generated_QRCode = "N";
                    packing_List.Receive_Date     = DateTime.Now;
                    packing_List.Updated_By       = updateBy;
                    packing_List.Receive_No       = CodeUtility.RandomReceiveNo("RW", 2);

                    var packing_ListAdd = _mapper.Map <WMSB_Packing_List>(packing_List);
                    _repoPackingList.AddModel(packing_ListAdd);

                    foreach (var item2 in item.Purchase_Qty)
                    {
                        var packing_List_detail = new Packing_List_Detail_Dto();
                        packing_List_detail.Receive_No   = packing_List.Receive_No;
                        packing_List_detail.Order_Size   = item2.Order_Size;
                        packing_List_detail.Model_Size   = item2.Model_Size;
                        packing_List_detail.Tool_Size    = item2.Tool_Size;
                        packing_List_detail.Spec_Size    = item2.Spec_Size;
                        packing_List_detail.MO_Qty       = item2.MO_Qty;
                        packing_List_detail.Purchase_Qty = item2.Purchase_Qty_Const;
                        packing_List_detail.Received_Qty = item2.Received_Qty;
                        packing_List_detail.Updated_Time = DateTime.Now;
                        packing_List_detail.Updated_By   = updateBy;
                        var packingDetailAdd = _mapper.Map <WMSB_PackingList_Detail>(packing_List_detail);
                        _repoPackingListDetail.AddModel(packingDetailAdd);
                    }
                }
            }
            await _repoPackingList.SaveAll();

            return(ReceiveNoMain);
        }