private void bwLoadData_DoWork(object sender, DoWorkEventArgs e)
 {
     insockSupplierList    = InsockSuppliersController.Select();
     insockMaterialList    = InsockMaterialController.Select(productNo);
     sizeRunList           = SizeRunController.Select(productNo);
     insockRawMaterialList = InsockRawMaterialController.SelectPerPO(productNo);
 }
        private void bwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            var insockSupplierList = InsockSuppliersController.Select();

            if (insockSupplierList.Count < 1)
            {
                return;
            }

            List <InsockTemp> insockTempList = new List <InsockTemp>();

            Excel.Application excelApplication = new Excel.Application();
            Excel.Workbook    excelWorkbook    = excelApplication.Workbooks.Open(filePath);
            //excelApplication.Visible = true;
            Excel.Worksheet excelWorksheet;
            Excel.Range     excelRange;
            try
            {
                excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets[1];
                excelRange     = excelWorksheet.UsedRange;
                int columnCheck = 8;
                while ((excelRange.Cells[2, columnCheck] as Excel.Range).Value2 != null)
                {
                    string insockSupplierName    = (excelRange.Cells[2, columnCheck] as Excel.Range).Value2.ToString();
                    var    insockSupplierPerName = insockSupplierList.Where(w => w.InsockSupplierName == insockSupplierName).FirstOrDefault();
                    if (insockSupplierPerName == null)
                    {
                        MessageBox.Show(string.Format("Insock Supplier: {0} doesn't exist!", insockSupplierName), "Infor", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        InsockTemp insockTemp = new InsockTemp();
                        insockTemp.InsockColumn     = columnCheck;
                        insockTemp.InsockSupplierId = insockSupplierPerName.InsockSupplierId;
                        insockTempList.Add(insockTemp);
                    }
                    columnCheck++;
                }

                progressBar.Dispatcher.Invoke((Action)(() => progressBar.Maximum = excelRange.Rows.Count));
                for (int i = 3; i <= excelRange.Rows.Count; i++)
                {
                    var productNoValueCheck = (excelRange.Cells[i, 1] as Excel.Range).Value2;
                    if (productNoValueCheck != null)
                    {
                        string productNo = productNoValueCheck.ToString();
                        for (int j = 8; j <= 8 + insockTempList.Count() - 1; j++)
                        {
                            var etdInCell = (excelRange.Cells[i, j] as Excel.Range).Value2;
                            if (etdInCell != null)
                            {
                                double etd = 0;
                                double.TryParse(etdInCell.ToString(), out etd);
                                var insockPerColumn = insockTempList.Where(w => w.InsockColumn == j).FirstOrDefault();
                                if (insockPerColumn != null && etd != 0)
                                {
                                    insockRawMaterialList.Add(new InsockRawMaterialModel
                                    {
                                        ProductNo          = productNo,
                                        InsockSupplierId   = insockPerColumn.InsockSupplierId,
                                        InsockSupplierName = insockSupplierList.Where(w => w.InsockSupplierId == insockPerColumn.InsockSupplierId).FirstOrDefault().InsockSupplierName,
                                        ETD = DateTime.FromOADate(etd),
                                    });
                                }
                            }
                        }
                    }
                    progressBar.Dispatcher.Invoke((Action)(() => progressBar.Value = i));
                }
            }
            catch { }
        }
        private void bwLoadData_DoWork(object sender, DoWorkEventArgs e)
        {
            insockMaterialList = InsockMaterialController.Select();

            orderList             = OrdersController.SelectByOutsoleMaterial();
            insockSupplierList    = InsockSuppliersController.Select();
            insockRawMaterialList = InsockRawMaterialController.Select();

            List <String> lastCodeList = orderList.Select(o => o.LastCode).Distinct().ToList();

            foreach (string lastCode in lastCodeList)
            {
                var productNoList         = orderList.Where(w => w.LastCode == lastCode).Select(s => s.ProductNo).Distinct().ToList();
                var insockMaterialList_D1 = insockMaterialList.Where(w => productNoList.Contains(w.ProductNo)).ToList();
                var supplierPerLastCode   = insockMaterialList_D1.Select(s => s.InsockSupplierId).Distinct().ToList();
                suppIdShowInGridviewList.AddRange(supplierPerLastCode);
            }

            suppIdShowInGridviewList = suppIdShowInGridviewList.Distinct().OrderBy(s => s).ToList();

            DataTable dt = new DataTable();

            Dispatcher.Invoke(new Action(() => {
                dt.Columns.Add("LastCode", typeof(String));
                DataGridTextColumn columnLastCode = new DataGridTextColumn();
                columnLastCode.Header             = "Last Code";
                columnLastCode.Binding            = new Binding("LastCode");
                columnLastCode.FontWeight         = FontWeights.Bold;
                dgInsockDelivery.Columns.Add(columnLastCode);
                Binding bindingWidth1 = new Binding();
                bindingWidth1.Source  = columnLastCode;
                bindingWidth1.Path    = new PropertyPath("ActualWidth");
                ColumnDefinition cd1  = new ColumnDefinition();
                cd1.SetBinding(ColumnDefinition.WidthProperty, bindingWidth1);
                gridTotal.ColumnDefinitions.Add(cd1);


                dt.Columns.Add("Quantity", typeof(Int32));
                DataGridTextColumn columnQuantity = new DataGridTextColumn();
                columnQuantity.Header             = "Quantity";
                columnQuantity.Binding            = new Binding("Quantity");
                dgInsockDelivery.Columns.Add(columnQuantity);
                Binding bindingWidth2 = new Binding();
                bindingWidth2.Source  = columnQuantity;
                bindingWidth2.Path    = new PropertyPath("ActualWidth");
                ColumnDefinition cd2  = new ColumnDefinition();
                cd2.SetBinding(ColumnDefinition.WidthProperty, bindingWidth2);
                gridTotal.ColumnDefinitions.Add(cd2);

                dt.Columns.Add("Reject", typeof(Int32));
                DataGridTextColumn column4 = new DataGridTextColumn();
                column4.Header             = "Reject";
                column4.Binding            = new Binding("Reject");
                dgInsockDelivery.Columns.Add(column4);
                Binding bindingWidth4 = new Binding();
                bindingWidth4.Source  = column4;
                bindingWidth4.Path    = new PropertyPath("ActualWidth");
                ColumnDefinition cd4  = new ColumnDefinition();
                cd4.SetBinding(ColumnDefinition.WidthProperty, bindingWidth4);
                gridTotal.ColumnDefinitions.Add(cd4);

                dgInsockDelivery.FrozenColumnCount = 3;

                for (int i = 0; i < suppIdShowInGridviewList.Count; i++)
                {
                    var insockSupplier = insockSupplierList.FirstOrDefault(f => f.InsockSupplierId == suppIdShowInGridviewList[i]);
                    if (insockSupplier != null)
                    {
                        dt.Columns.Add(String.Format("Column{0}", suppIdShowInGridviewList[i]), typeof(String));
                        DataGridTextColumn column = new DataGridTextColumn();
                        column.Header             = insockSupplier.InsockSupplierName;
                        column.Binding            = new Binding(String.Format("Column{0}", suppIdShowInGridviewList[i]));
                        column.FontWeight         = FontWeights.Bold;

                        Style style = new Style(typeof(DataGridCell));
                        style.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center));

                        Setter setterBackground   = new Setter();
                        setterBackground.Property = DataGridCell.BackgroundProperty;
                        setterBackground.Value    = new Binding(String.Format("Column{0}Background", suppIdShowInGridviewList[i]));
                        style.Setters.Add(setterBackground);

                        Setter setterToolTip   = new Setter();
                        setterToolTip.Property = DataGridCell.ToolTipProperty;
                        setterToolTip.Value    = new Binding(String.Format("Column{0}ToolTip", suppIdShowInGridviewList[i]));
                        style.Setters.Add(setterToolTip);

                        column.CellStyle = style;

                        dgInsockDelivery.Columns.Add(column);
                        Binding bindingWidth = new Binding();
                        bindingWidth.Source  = column;
                        bindingWidth.Path    = new PropertyPath("ActualWidth");
                        ColumnDefinition cd  = new ColumnDefinition();
                        cd.SetBinding(ColumnDefinition.WidthProperty, bindingWidth);
                        gridTotal.ColumnDefinitions.Add(cd);

                        DataColumn columnBackground   = new DataColumn(String.Format("Column{0}Background", suppIdShowInGridviewList[i]), typeof(SolidColorBrush));
                        DataColumn columnToolTip      = new DataColumn(String.Format("Column{0}ToolTip", suppIdShowInGridviewList[i]), typeof(String));
                        columnBackground.DefaultValue = Brushes.White;
                        dt.Columns.Add(columnBackground);
                        dt.Columns.Add(columnToolTip);
                    }
                }
            }));

            foreach (var lastCode in lastCodeList)
            {
                DataRow dr = dt.NewRow();
                dr["LastCode"] = lastCode;

                var productNoList         = orderList.Where(w => w.LastCode == lastCode).Select(s => s.ProductNo).Distinct().ToList();
                var insockMaterialList_D1 = insockMaterialList.Where(w => productNoList.Contains(w.ProductNo)).ToList();
                var insockSupplierIdList  = insockMaterialList_D1.Select(s => s.InsockSupplierId).Distinct().ToList();
                var qtyTotalList          = new List <Int32>();
                var qtyRejectList         = new List <Int32>();

                foreach (var insockSupplierId in insockSupplierIdList)
                {
                    var outsoleMaterialList_D2 = insockMaterialList_D1.Where(o => o.InsockSupplierId == insockSupplierId).ToList();
                    int qtyTotal       = 0;
                    int qtyRejectTotal = 0;
                    foreach (string productNo in productNoList)
                    {
                        var insockMaterialList_D3 = outsoleMaterialList_D2.Where(w => w.ProductNo == productNo).ToList();
                        var sizeNoList            = insockMaterialList.Where(w => w.ProductNo == productNo).Select(s => s.SizeNo).Distinct().ToList();
                        foreach (var sizeNo in sizeNoList)
                        {
                            int qtyMax    = insockMaterialList_D3.Where(w => w.SizeNo == sizeNo).Sum(s => s.Quantity - s.QuantityReject);
                            int qtyReject = insockMaterialList_D3.Where(w => w.SizeNo == sizeNo).Sum(s => s.QuantityReject);

                            qtyTotal       += qtyMax;
                            qtyRejectTotal += qtyReject;
                        }
                    }
                    dr[String.Format("Column{0}", insockSupplierId)]           = "x";
                    dr[String.Format("Column{0}Background", insockSupplierId)] = Brushes.Green;
                    var suppName = insockSupplierList.FirstOrDefault(f => f.InsockSupplierId == insockSupplierId).InsockSupplierName;
                    if (suppName != null)
                    {
                        dr[String.Format("Column{0}ToolTip", insockSupplierId)] = string.Format("{0}{1}", suppName, qtyRejectTotal > 0 ? String.Format("\nQtyReject: {0}", qtyRejectTotal.ToString()) : "");
                    }
                    qtyTotalList.Add(qtyTotal);
                    qtyRejectList.Add(qtyRejectTotal);
                }

                if (qtyRejectList.Count > 0)
                {
                    dr["Reject"] = qtyRejectList.Max();
                }
                if (qtyTotalList.Count > 0)
                {
                    dr["Quantity"] = qtyTotalList.Max();
                }
                dt.Rows.Add(dr);
            }

            Dispatcher.Invoke(new Action(() =>
            {
                dgInsockDelivery.ItemsSource = dt.AsDataView();
                // Row Tail
                TextBlock lblTotal  = new TextBlock();
                lblTotal.Text       = "TOTAL";
                lblTotal.Margin     = new Thickness(1, 0, 0, 0);
                lblTotal.FontWeight = FontWeights.Bold;
                Border bdrTotal     = new Border();
                Grid.SetColumn(bdrTotal, 2);
                Grid.SetColumnSpan(bdrTotal, 1);
                bdrTotal.BorderThickness = new Thickness(1, 0, 1, 1);
                bdrTotal.BorderBrush     = Brushes.Black;
                bdrTotal.Child           = lblTotal;
                gridTotal.Children.Add(bdrTotal);

                TextBlock lblQuantityTotal  = new TextBlock();
                lblQuantityTotal.Text       = dt.Compute("Sum(Quantity)", "").ToString();
                lblQuantityTotal.Margin     = new Thickness(1, 0, 0, 0);
                lblQuantityTotal.FontWeight = FontWeights.Bold;
                Border bdrQuantityTotal     = new Border();
                Grid.SetColumn(bdrQuantityTotal, 3);
                bdrQuantityTotal.BorderThickness = new Thickness(0, 0, 1, 1);
                bdrQuantityTotal.BorderBrush     = Brushes.Black;
                bdrQuantityTotal.Child           = lblQuantityTotal;
                gridTotal.Children.Add(bdrQuantityTotal);
                dgInsockDelivery.ItemsSource = dt.AsDataView();


                TextBlock lblRejectMaximunTotal  = new TextBlock();
                lblRejectMaximunTotal.Text       = dt.Compute("Sum(Reject)", "").ToString();
                lblRejectMaximunTotal.Margin     = new Thickness(1, 0, 0, 0);
                lblRejectMaximunTotal.FontWeight = FontWeights.Bold;
                Border bdrRejectMaximumTotal     = new Border();
                Grid.SetColumn(bdrRejectMaximumTotal, 4);
                bdrRejectMaximumTotal.BorderThickness = new Thickness(0, 0, 1, 1);
                bdrRejectMaximumTotal.BorderBrush     = Brushes.Black;
                bdrRejectMaximumTotal.Child           = lblRejectMaximunTotal;
                gridTotal.Children.Add(bdrRejectMaximumTotal);
                dgInsockDelivery.ItemsSource = dt.AsDataView();
            }));
        }