private void bwInsert_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (OutsoleRawMaterialController.IsFull(sizeRunList, outsoleRawMaterialList, outsoleMaterialToInsertList) == true)
            {
                if (outsoleRawMaterialToInsertList.Count() > 0)
                {
                    rawMaterial.ActualDate = outsoleRawMaterialToInsertList.Select(o => o.ActualDate).Max();
                }
                else
                {
                    rawMaterial.ActualDate = outsoleRawMaterialList.Select(o => o.ActualDate).Max();
                }
                int balance = sizeRunList.Sum(s => s.Quantity) - sizeRunList.Select(s => outsoleMaterialToInsertList.Where(o => o.SizeNo == s.SizeNo).Min(o => (o.Quantity - o.QuantityReject))).Sum();
                if (balance > 0)
                {
                    rawMaterial.ActualDate = dtDefault;
                    rawMaterial.Remarks    = balance.ToString();
                }
            }
            else
            {
                rawMaterial.ActualDate = dtDefault;
                rawMaterial.Remarks    = (sizeRunList.Sum(s => s.Quantity) - sizeRunList.Select(s => outsoleMaterialToInsertList.Where(o => o.SizeNo == s.SizeNo).Min(o => (o.Quantity - o.QuantityReject))).Sum()).ToString();
            }

            // Get total reject assembly.
            totalRejectAssemblyRespone = outsoleMaterialToInsertList.Sum(s => s.RejectAssembly);

            if (threadUpdateRawMaterial.IsBusy == false)
            {
                threadUpdateRawMaterial.RunWorkerAsync();
            }
        }
        private void bwReport_DoWork(object sender, DoWorkEventArgs e)
        {
            outsoleRawMaterialList = OutsoleRawMaterialController.Select();
            outsoleMaterialList    = OutsoleMaterialController.SelectByOutsoleRawMaterial();
            sizeRunList            = SizeRunController.SelectByOutsoleRawMaterial();
            outsoleSupplierList    = OutsoleSuppliersController.Select();
            ordersList             = OrdersController.SelectByOutsoleRawMaterial();

            DataTable dt = new OutsoleDeliveryDataSet().Tables["OutsoleDeliveryTable"];

            List <String> productNoList = outsoleRawMaterialList.Select(r => r.ProductNo).Distinct().ToList();

            foreach (string productNo in productNoList)
            {
                OrdersModel order = ordersList.Where(o => o.ProductNo == productNo).FirstOrDefault();
                List <OutsoleRawMaterialModel> outsoleRawMaterialOfProductNoList = outsoleRawMaterialList.Where(o => o.ProductNo == productNo).ToList();
                List <SizeRunModel>            sizeRunOfProductNoList            = sizeRunList.Where(s => s.ProductNo == productNo).ToList();
                List <OutsoleMaterialModel>    outsoleMaterialOfProductNoList    = outsoleMaterialList.Where(o => o.ProductNo == productNo).ToList();
                foreach (OutsoleRawMaterialModel outsoleRawMaterial in outsoleRawMaterialOfProductNoList)
                {
                    DateTime etd    = outsoleRawMaterial.ETD.Date;
                    bool     isFull = OutsoleRawMaterialController.IsFull(sizeRunOfProductNoList, new List <OutsoleRawMaterialModel>()
                    {
                        outsoleRawMaterial,
                    }, outsoleMaterialOfProductNoList);
                    if (etd != new DateTime(2000, 1, 1) && etd == dateSearch.Date && isFull == false)
                    {
                        DataRow dr = dt.NewRow();
                        dr["ProductNo"] = productNo;
                        if (order != null)
                        {
                            dr["ArticleNo"]   = order.ArticleNo;
                            dr["OutsoleCode"] = order.OutsoleCode;
                            dr["Quantity"]    = order.Quantity;
                            dr["ETD"]         = order.ETD;
                        }
                        dr["SupplierETD"] = etd;
                        dr["Remarks"]     = sizeRunOfProductNoList.Sum(s => (s.Quantity - outsoleMaterialOfProductNoList.Where(o => o.OutsoleSupplierId == outsoleRawMaterial.OutsoleSupplierId && o.SizeNo == s.SizeNo).Sum(o => (o.Quantity - o.QuantityReject)))).ToString();
                        OutsoleSuppliersModel outsoleSupplier = outsoleSupplierList.Where(o => o.OutsoleSupplierId == outsoleRawMaterial.OutsoleSupplierId).FirstOrDefault();
                        if (outsoleSupplier != null)
                        {
                            dr["Supplier"] = outsoleSupplier.Name;
                        }

                        dt.Rows.Add(dr);
                    }
                }
            }
            e.Result = dt;
        }