public static bool Insert(MaterialDeliveryModel model)
        {
            var @ProductNo    = new SqlParameter("@ProductNo", model.ProductNo);
            var @SupplierId   = new SqlParameter("@SupplierId", model.SupplierId);
            var @SizeNo       = new SqlParameter("@SizeNo", model.SizeNo);
            var @Quantity     = new SqlParameter("@Quantity", model.Quantity);
            var @Reject       = new SqlParameter("@Reject", model.Reject);
            var @RejectSewing = new SqlParameter("@RejectSewing", model.RejectSewing);

            using (var db = new SaovietMasterScheduleEntities())
            {
                if (db.ExecuteStoreCommand("EXEC spm_InsertMaterialDelivery_1 @ProductNo, @SupplierId, @SizeNo, @Quantity, @Reject, @RejectSewing",
                                           @ProductNo, @SupplierId, @SizeNo, @Quantity, @Reject, @RejectSewing) > 0)
                {
                    return(true);
                }
                return(false);
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (dgDeliveryInfo.Items == null)
            {
                return;
            }
            if (account.MaterialDelivery == false)
            {
                MessageBox.Show("User does not have permission excute data", this.Title, MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            // Get Data From Datatable
            List <MaterialDeliveryModel> matsFromGridList = new List <MaterialDeliveryModel>();
            var actualDate = dtDefault;
            var etd        = dtDefault;

            for (int r = 0; r < dtDelivery.Rows.Count; r++)
            {
                var dr         = dtDelivery.Rows[r];
                var status     = dr["Status"].ToString();
                var supplierId = dr["SupplierId"].ToString();
                if (status == RowQuantity)
                {
                    actualDate = (DateTime)dr["ActualDateDate"];
                    etd        = (DateTime)dr["ETDDate"];
                }

                foreach (var sizeRun in sizeRunList)
                {
                    string sizeBinding = sizeRun.SizeNo.Contains(".") ? sizeRun.SizeNo.Replace(".", "@") : sizeRun.SizeNo;
                    int    qtyAtCell   = 0;
                    Int32.TryParse(dr[String.Format("Column{0}", sizeBinding)].ToString(), out qtyAtCell);
                    var materialDeliveryAddModel = new MaterialDeliveryModel
                    {
                        ProductNo          = productNo,
                        SupplierId         = int.Parse(supplierId),
                        ActualDeliveryDate = actualDate,
                        ETD    = etd,
                        SizeNo = sizeRun.SizeNo
                    };
                    if (status == RowQuantity)
                    {
                        materialDeliveryAddModel.Quantity = qtyAtCell;
                    }
                    else if (status == RowReject)
                    {
                        materialDeliveryAddModel.Reject = qtyAtCell;
                    }
                    else
                    {
                        materialDeliveryAddModel.RejectSewing = qtyAtCell;
                    }

                    matsFromGridList.Add(materialDeliveryAddModel);
                }
            }

            if (bwUpload.IsBusy == false)
            {
                this.Cursor       = Cursors.Wait;
                eAction           = EExcute.AddNew;
                btnSave.IsEnabled = false;

                // Created Add List
                var supplierIdList      = matsFromGridList.Select(s => s.SupplierId).Distinct().ToList();
                var matsDeliveryAddList = new List <MaterialDeliveryModel>();
                var matsPlanUpdateList  = new List <MaterialPlanModel>();
                foreach (var supplierId in supplierIdList)
                {
                    var matsBySuppList = matsFromGridList.Where(w => w.SupplierId == supplierId).ToList();
                    var matsFisrt      = matsBySuppList.FirstOrDefault();
                    matsPlanUpdateList.Add(new MaterialPlanModel
                    {
                        ProductNo          = matsFisrt.ProductNo,
                        SupplierId         = matsFisrt.SupplierId,
                        ActualDeliveryDate = matsFisrt.ActualDeliveryDate,
                        ETD                   = matsFisrt.ETD,
                        BalancePO             = sizeRunList.Sum(s => s.Quantity) - matsBySuppList.Sum(s => s.Quantity),
                        RejectPO              = matsBySuppList.Sum(s => s.Reject),
                        TotalDeliveryQuantity = matsBySuppList.Sum(s => s.Quantity)
                    });
                    foreach (var sizeRun in sizeRunList)
                    {
                        matsDeliveryAddList.Add(new MaterialDeliveryModel
                        {
                            ProductNo          = matsFisrt.ProductNo,
                            SupplierId         = matsFisrt.SupplierId,
                            SizeNo             = sizeRun.SizeNo,
                            ActualDeliveryDate = matsFisrt.ActualDeliveryDate,
                            Quantity           = matsBySuppList.Where(w => w.SizeNo == sizeRun.SizeNo).Sum(s => s.Quantity),
                            Reject             = matsBySuppList.Where(w => w.SizeNo == sizeRun.SizeNo).Sum(s => s.Reject),
                            RejectSewing       = matsBySuppList.Where(w => w.SizeNo == sizeRun.SizeNo).Sum(s => s.RejectSewing),
                        });
                    }
                }
                object[] par = new object[] { matsDeliveryAddList, matsPlanUpdateList };
                bwUpload.RunWorkerAsync(par);
            }
        }