private void GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Stores_PartGrid.DataContext = null;
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                        new Action(() =>
            {
                PickList_PartComboBox.Text         = "";
                PickList_PartComboBox.SelectedItem = null;
                PickList_QtyTextBox.Clear();
                PickList_SUNTextBox.Clear();

                Line_PartComboBox.Text         = "";
                Line_PartComboBox.SelectedItem = null;
                Line_ReleaseQtyTextBox.Clear();

                Line_TrackingCodeTextBox.Clear();
                Line_ReturnQtyTextBox.Clear();

                Scrap_PartNoCombobox.Text         = "";
                Scrap_PartNoCombobox.SelectedItem = null;
                Scrap_QtyTextBox.Clear();
                Scrap_ReasonTextBox.Clear();
            }));
        }
        private void GenerateTrackingCodeButton_Click(object sender, RoutedEventArgs e)
        {
            CurrentPart = (Part)Line_PartComboBox.SelectedItem;
            using (var db = new MTSDB())
            {
                var parts = from p in db.Parts
                            where p.PartNo == CurrentPart.PartNo
                            select p;

                Part P = parts.SingleOrDefault();


                if (P == null)
                {
                    MessageBox.Show("Part Not Found !! Please Verify", "Database Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                int qty = 0;
                if (int.TryParse(Line_ReleaseQtyTextBox.Text, out qty) == false || (qty == 0))
                {
                    MessageBox.Show("Please Verify Quantity", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                CurrentPart = P;

                if (P.Quantity < qty)
                {
                    MessageBox.Show("Quantity Unavailable", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                ToLine tl = new ToLine();

                foreach (FromStores fs in P.FromStoresRecords)
                {
                    if (qty > 0)
                    {
                        if (fs.Balance <= 0)
                        {
                            continue;
                        }
                        if (fs.Balance >= qty)
                        {
                            var FromStoresToLines = new FromStoresToLines();
                            FromStoresToLines.FromStores        = fs;
                            FromStoresToLines.ToLine            = tl;
                            FromStoresToLines.FromStoresBalance = fs.Balance.Value;

                            fs.Balance -= qty;
                            P.Quantity -= qty;

                            tl.Quantity += qty;
                            tl.Timestamp = DateTime.Now;
                            tl.Part      = P;
                            if ((tl.SMN == String.Empty) || tl.SMN == null)
                            {
                                DateTime ts     = DateTime.Now;
                                int      offset = 0;
                                if (ts.Hour >= 22 && ts.Hour < 6)
                                {
                                    offset = 1;
                                }
                                P.ToLineReference++;
                                tl.SMN = P.PartID.ToString("D4") + P.ToLineReference.Value.ToString("D4")
                                         + (ts.DayOfYear - offset).ToString("D3") + (ts.Year - 2000).ToString("D2");
                            }
                            tl.SUN = fs.SUN;

                            FromStoresToLines.ToLineBalance = qty;
                            FromStoresToLines.Part          = P;
                            db.FromStoresToLines.Add(FromStoresToLines);
                            break;
                        }
                        else
                        {
                            var FromStoresToLines = new FromStoresToLines();
                            FromStoresToLines.FromStores        = fs;
                            FromStoresToLines.ToLine            = tl;
                            FromStoresToLines.FromStoresBalance = fs.Balance.Value;

                            tl.Quantity += fs.Balance;
                            tl.Timestamp = DateTime.Now;
                            tl.Part      = P;


                            if ((tl.SMN == String.Empty) || tl.SMN == null)
                            {
                                DateTime ts     = DateTime.Now;
                                int      offset = 0;
                                if (ts.Hour >= 22 && ts.Hour < 6)
                                {
                                    offset = 1;
                                }

                                P.ToLineReference++;
                                tl.SMN = P.PartID.ToString("D4") + P.ToLineReference.Value.ToString("D4")
                                         + (ts.DayOfYear - offset).ToString("D3") + (ts.Year - 2000).ToString("D2");
                            }

                            tl.SUN = fs.SUN;

                            qty -= fs.Balance.Value;
                            FromStoresToLines.ToLineBalance = fs.Balance.Value;
                            P.Quantity            -= fs.Balance.Value;
                            fs.Balance             = 0;
                            FromStoresToLines.Part = P;
                            db.FromStoresToLines.Add(FromStoresToLines);
                        }
                    }
                }
                tl.Balance   = tl.Quantity;
                tl.Timestamp = DateTime.Now;

                P.ToLineRecords.Add(tl);
                P.LastUpdated  = DateTime.Now;
                P.LastActivity = "To Line Entry";
                db.SaveChanges();

                //Code for generation of Tracking Code


                // PrinterManager.PrintSMN(PrinterName, tl.SMN, templatePath);
                MessageBox.Show("SMN Generated - " + tl.SMN, "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                            new Action(() =>
                {
                    Line_PartComboBox.Text         = "";
                    Line_PartComboBox.SelectedItem = null;
                    Line_ReleaseQtyTextBox.Clear();

                    DisplayParts.Clear();
                }));
            }
        }