Beispiel #1
0
 private void BwLoad_DoWork(object sender, DoWorkEventArgs e)
 {
     materialPlanList = MaterialPlanController.GetMaterialPlanByPO(productNo);
     materialPlanList.ForEach(t => t.ActualDateString = t.ActualDate != dtDefault ? String.Format("{0:MM/dd}", t.ActualDate) : "");
     supplierAccessoriesList = SupplierController.GetSuppliersAccessories();
     sizeRunList             = SizeRunController.Select(productNo);
     matsInspectionList      = MaterialInspectController.GetMaterialInspectByPO(productNo);
     matsDeliveryList        = MaterialDeliveryController.GetMaterialDeliveryByPO(productNo);
 }
        private void BwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                sizeRunList          = SizeRunController.Select(productNo);
                matsDeliveryByPOList = MaterialDeliveryController.GetMaterialDeliveryByPO(productNo);
                matsPlanByPOList     = MaterialPlanController.GetMaterialPlanByPO(productNo);
                supplierList         = SupplierController.GetSuppliersAccessories();
                Dispatcher.Invoke(new Action(() =>
                {
                    // Create Column Datagrid
                    dgDeliveryInfo.Columns.Clear();
                    dtDelivery = new DataTable();

                    dtDelivery.Columns.Add("Status", typeof(String));
                    dtDelivery.Columns.Add("Name", typeof(String));
                    dtDelivery.Columns.Add("SupplierId", typeof(String));
                    DataGridTemplateColumn colSuppName = new DataGridTemplateColumn();
                    colSuppName.Header                  = String.Format("Supplier");
                    DataTemplate templateSuppName       = new DataTemplate();
                    FrameworkElementFactory tblSuppName = new FrameworkElementFactory(typeof(TextBlock));
                    templateSuppName.VisualTree         = tblSuppName;
                    tblSuppName.SetBinding(TextBlock.TextProperty, new Binding(String.Format("Name")));
                    tblSuppName.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
                    tblSuppName.SetValue(TextBlock.PaddingProperty, new Thickness(3, 0, 3, 0));
                    colSuppName.CellTemplate            = templateSuppName;
                    colSuppName.ClipboardContentBinding = new Binding(String.Format("Name"));
                    dgDeliveryInfo.Columns.Add(colSuppName);

                    //Column ETD
                    dtDelivery.Columns.Add("ETD", typeof(String));
                    dtDelivery.Columns.Add("ETDDate", typeof(DateTime));
                    DataGridTemplateColumn colETD = new DataGridTemplateColumn();
                    colETD.Header                  = String.Format("EFD");
                    colETD.MinWidth                = 60;
                    DataTemplate templateETD       = new DataTemplate();
                    FrameworkElementFactory tblETD = new FrameworkElementFactory(typeof(TextBlock));
                    templateETD.VisualTree         = tblETD;
                    tblETD.SetBinding(TextBlock.TextProperty, new Binding(String.Format("ETD")));
                    tblETD.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
                    tblETD.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                    tblETD.SetValue(TextBlock.PaddingProperty, new Thickness(3, 0, 3, 0));
                    colETD.CellTemplate            = templateETD;
                    colETD.ClipboardContentBinding = new Binding(String.Format("ETD"));
                    dgDeliveryInfo.Columns.Add(colETD);

                    //Column Actual Date
                    dtDelivery.Columns.Add("ActualDate", typeof(String));
                    dtDelivery.Columns.Add("ActualDateDate", typeof(DateTime));
                    DataGridTemplateColumn colActualDate = new DataGridTemplateColumn();
                    colActualDate.Header                  = String.Format("{0}\n{1}\n{2}", "Order Size", "Qty", "Actual Date");
                    DataTemplate templateActualDate       = new DataTemplate();
                    FrameworkElementFactory tblActualDate = new FrameworkElementFactory(typeof(TextBlock));
                    templateActualDate.VisualTree         = tblActualDate;
                    tblActualDate.SetBinding(TextBlock.TextProperty, new Binding(String.Format("ActualDate")));
                    tblActualDate.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
                    tblActualDate.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                    tblActualDate.SetValue(TextBlock.PaddingProperty, new Thickness(3, 0, 3, 0));
                    colActualDate.CellTemplate            = templateActualDate;
                    colActualDate.ClipboardContentBinding = new Binding(String.Format("ActualDate"));
                    dgDeliveryInfo.Columns.Add(colActualDate);

                    var regex = new Regex("[a-z]|[A-Z]");
                    if (sizeRunList.Count() > 0)
                    {
                        sizeRunList = sizeRunList.OrderBy(s => regex.IsMatch(s.SizeNo) ? Double.Parse(regex.Replace(s.SizeNo, "100")) : Double.Parse(s.SizeNo)).ToList();
                    }
                    foreach (var sizeRun in sizeRunList)
                    {
                        string sizeBinding = sizeRun.SizeNo.Contains(".") ? sizeRun.SizeNo.Replace(".", "@") : sizeRun.SizeNo;

                        dtDelivery.Columns.Add(String.Format("Column{0}", sizeBinding), typeof(String));
                        dtDelivery.Columns.Add(String.Format("Column{0}Foreground", sizeBinding), typeof(SolidColorBrush));
                        //dtDelDetail.Columns.Add(String.Format("Column{0}ToolTip", sizeBinding), typeof(String));
                        DataGridTextColumn column = new DataGridTextColumn();
                        column.SetValue(TagProperty, sizeRun.SizeNo);
                        column.Header   = string.Format("{0}\n\n{1}", sizeRun.SizeNo, sizeRun.Quantity);
                        column.MinWidth = 40;
                        column.MaxWidth = 200;
                        column.Binding  = new Binding(String.Format("Column{0}", sizeBinding));

                        Style styleColumn              = new Style();
                        Setter setterColumnForecolor   = new Setter();
                        setterColumnForecolor.Property = DataGridCell.ForegroundProperty;
                        setterColumnForecolor.Value    = new Binding(String.Format("Column{0}Foreground", sizeBinding));

                        styleColumn.Setters.Add(setterColumnForecolor);
                        column.CellStyle = styleColumn;

                        dgDeliveryInfo.Columns.Add(column);
                    }

                    //Column Total
                    dtDelivery.Columns.Add("Balance", typeof(String));
                    DataGridTemplateColumn colBalance = new DataGridTemplateColumn();
                    colBalance.Header                  = String.Format("Total\n\n{0}", sizeRunList.Sum(s => s.Quantity));
                    colBalance.MinWidth                = 80;
                    colBalance.MaxWidth                = 80;
                    DataTemplate templateBalance       = new DataTemplate();
                    FrameworkElementFactory tblBalance = new FrameworkElementFactory(typeof(TextBlock));
                    templateBalance.VisualTree         = tblBalance;
                    tblBalance.SetBinding(TextBlock.TextProperty, new Binding(String.Format("Balance")));
                    tblBalance.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
                    tblBalance.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                    tblBalance.SetValue(TextBlock.PaddingProperty, new Thickness(3, 0, 3, 0));
                    colBalance.CellTemplate            = templateBalance;
                    colBalance.ClipboardContentBinding = new Binding(String.Format("Balance"));
                    dgDeliveryInfo.Columns.Add(colBalance);

                    DataGridTemplateColumn colButtonOK = new DataGridTemplateColumn();
                    colButtonOK.MinWidth                = 40;
                    colButtonOK.MaxWidth                = 40;
                    DataTemplate templateButtonOK       = new DataTemplate();
                    FrameworkElementFactory fefButtonOK = new FrameworkElementFactory(typeof(Button));
                    templateButtonOK.VisualTree         = fefButtonOK;
                    fefButtonOK.SetValue(Button.ContentProperty, "Ok");
                    fefButtonOK.AddHandler(Button.ClickEvent, new RoutedEventHandler(BtnOK_Click));
                    colButtonOK.CellTemplate = templateButtonOK;
                    dgDeliveryInfo.Columns.Add(colButtonOK);

                    // Binding Data
                    if (matsDeliveryByPOList.Count() == 0)
                    {
                        foreach (var matsPlan in matsPlanByPOList)
                        {
                            var supplierByPlan = supplierList.FirstOrDefault(f => f.SupplierId == matsPlan.SupplierId);
                            foreach (var sizeRun in sizeRunList)
                            {
                                matsDeliveryByPOList.Add(new MaterialDeliveryModel
                                {
                                    SupplierId          = matsPlan.SupplierId,
                                    SupplierNameDisplay = supplierByPlan != null ? String.Format("{0} - {1}", supplierByPlan.Name, supplierByPlan.ProvideAccessories) : "",
                                    ProductNo           = matsPlan.ProductNo,
                                    ETD = matsPlan.ETD,
                                    ActualDeliveryDate = dtDefault,
                                    SizeNo             = sizeRun.SizeNo,
                                    Quantity           = 0,
                                    Reject             = 0,
                                    RejectSewing       = 0
                                });
                            }
                        }
                    }
                    else
                    {
                        // Put Supplier Name - Accessories Name
                        matsDeliveryByPOList.ForEach(f => f.SupplierNameDisplay = String.Format("{0} - {1}",
                                                                                                supplierList.FirstOrDefault(w => w.SupplierId == f.SupplierId).Name,
                                                                                                supplierList.FirstOrDefault(w => w.SupplierId == f.SupplierId).ProvideAccessories));
                    }

                    var supplierIdList = matsDeliveryByPOList.Select(s => s.SupplierId).Distinct().ToList();
                    foreach (var supplierId in supplierIdList)
                    {
                        var deliveryListBySupp = matsDeliveryByPOList.Where(w => w.SupplierId == supplierId).ToList();
                        var materialInfoBySupp = deliveryListBySupp.FirstOrDefault();

                        // Row Quantity, Reject, RejectSewing
                        DataRow drQuantity     = dtDelivery.NewRow();
                        DataRow drReject       = dtDelivery.NewRow();
                        DataRow drRejectSewing = dtDelivery.NewRow();

                        drQuantity["Status"]     = RowQuantity;
                        drReject["Status"]       = RowReject;
                        drRejectSewing["Status"] = RowRejectSewing;

                        drQuantity["Name"]     = materialInfoBySupp.SupplierNameDisplay;
                        drReject["Name"]       = RowReject;
                        drRejectSewing["Name"] = RowRejectSewing;

                        drQuantity["SupplierId"]     = materialInfoBySupp.SupplierId;
                        drReject["SupplierId"]       = materialInfoBySupp.SupplierId;
                        drRejectSewing["SupplierId"] = materialInfoBySupp.SupplierId;

                        drQuantity["ETD"] = string.Format("{0:MM/dd}", materialInfoBySupp.ETD);
                        if (materialInfoBySupp.ActualDeliveryDate != dtDefault)
                        {
                            drQuantity["ActualDate"] = string.Format("{0:MM/dd}", materialInfoBySupp.ActualDeliveryDate);
                        }

                        drQuantity["ActualDateDate"] = materialInfoBySupp.ActualDeliveryDate;
                        drQuantity["ETDDate"]        = materialInfoBySupp.ETD;

                        foreach (var sizeRun in sizeRunList)
                        {
                            string sizeBinding = sizeRun.SizeNo.Contains(".") ? sizeRun.SizeNo.Replace(".", "@") : sizeRun.SizeNo;

                            var deliveryBySizeNo = deliveryListBySupp.FirstOrDefault(f => f.SizeNo == sizeRun.SizeNo);
                            if (deliveryBySizeNo != null)
                            {
                                if (deliveryBySizeNo.Quantity > 0)
                                {
                                    drQuantity[String.Format("Column{0}", sizeBinding)] = deliveryBySizeNo.Quantity;
                                    if (deliveryBySizeNo.Quantity == sizeRun.Quantity)
                                    {
                                        drQuantity[String.Format("Column{0}Foreground", sizeBinding)] = Brushes.Blue;
                                    }
                                }
                                if (deliveryBySizeNo.Reject > 0)
                                {
                                    drReject[String.Format("Column{0}", sizeBinding)]           = deliveryBySizeNo.Reject;
                                    drReject[String.Format("Column{0}Foreground", sizeBinding)] = Brushes.Red;
                                }
                                if (deliveryBySizeNo.RejectSewing > 0)
                                {
                                    drRejectSewing[String.Format("Column{0}", sizeBinding)]           = deliveryBySizeNo.RejectSewing;
                                    drRejectSewing[String.Format("Column{0}Foreground", sizeBinding)] = Brushes.Red;
                                }
                            }
                        }

                        int totalBalanceBySupp = sizeRunList.Sum(s => s.Quantity) - deliveryListBySupp.Sum(s => s.Quantity);
                        int totalReject        = deliveryListBySupp.Sum(s => s.Reject);
                        int totalRejectSewing  = deliveryListBySupp.Sum(s => s.RejectSewing);

                        drQuantity["Balance"]     = totalBalanceBySupp > 0 ? totalBalanceBySupp.ToString() : "";
                        drReject["Balance"]       = totalReject > 0 ? totalReject.ToString() : "";
                        drRejectSewing["Balance"] = totalRejectSewing > 0 ? totalRejectSewing.ToString() : "";

                        dtDelivery.Rows.Add(drQuantity);
                        dtDelivery.Rows.Add(drReject);
                        dtDelivery.Rows.Add(drRejectSewing);
                    }

                    dgDeliveryInfo.ItemsSource = dtDelivery.AsDataView();
                }));
            }
            catch (Exception ex)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    MessageBox.Show(ex.InnerException.Message, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                }));
            }
        }