private void SerialNoTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (SerialNoTextBox.Text.Length == 12)
            {

                using (PSBContext db = new PSBContext())
                {
                    foreach (Plan p in Plans)
                    {
                        if (p.Reference == ActualReferenceTextBox.Text)
                        {
                            var actual = db.Actuals.Single(a => a.Reference == p.Reference
                                && (a.PlanID == p.PlanID)
                                && (a.EndTimestamp == null) && (a.SerialNo == String.Empty )
                                && (a.StartTimestamp >= CurrentShift.Start) && a.StartTimestamp <= CurrentShift.End);

                            actual.SerialNo = SerialNoTextBox.Text;
                            actual.EndTimestamp = DateTime.Now;

                            Actual newActual = new Actual("", p.Reference, actual.EndTimestamp.Value, p.PlanID);

                            db.Actuals.Add(newActual);
                            db.SaveChanges();

                            break;

                        }
                    }
                }

                UpdatePlansActuals();

                UpdateQuantity();

                SerialNoTextBox.Clear();
                ActualReferenceTextBox.Clear();
            }
        }
        private void ActivateButton_Click(object sender, RoutedEventArgs e)
        {
            if (Plans.Count <= 0)
                return;

            if (PlanGrid.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Plan", "Application Message", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            using (PSBContext DB = new PSBContext())
            {

                int planIndex = PlanGrid.SelectedIndex;

                if (Plans[planIndex].Active == true)
                {

                    MessageBox.Show("Plan Already Activated", "Application Info",
                        MessageBoxButton.OK, MessageBoxImage.Information);
                    return;

                }

                int planid = Plans[planIndex].PlanID;

                var newActivePlan = DB.Plans.Include("Actuals").Single(p => p.PlanID == planid);
                if (ActivePlan != null)
                {
                    ActivePlan = DB.Plans.Include("Actuals").Single(p => p.PlanID == ActivePlan.PlanID);
                    ActivePlan.Active = false;

                    DB.SaveChanges();
                }

                ActivePlan = newActivePlan;

                ActivePlan.Active = true;

                ActivePlan.StartTimestamp = DateTime.Now;

                Actual newActual = new Actual("", ActivePlan.Reference, DateTime.Now, ActivePlan.PlanID);

                DB.Actuals.Add(newActual);

                DB.SaveChanges();

            }

            UpdatePlansActuals();

            UpdateReference();
        }
        private void AddPlanButton_Click(object sender, RoutedEventArgs e)
        {
            String reference = ReferenceTextBox.Text;
            int operators = Convert.ToInt32(OperatorTextBox.Text);
            int quantity = Convert.ToInt32(TargetQtyTextBox.Text);

            using (PSBContext db = new PSBContext())
            {
                var p = db.ProductModels.SingleOrDefault(pm => pm.Reference == ReferenceTextBox.Text);
                if (p == null)
                {
                    MessageBox.Show("Reference Not Found. Please Verify", "Application Message", MessageBoxButton.OK,
                        MessageBoxImage.Exclamation);
                    return;
                }
                Plan newPlan = new Plan(reference, quantity, operators);
                newPlan.CreatedTimestamp = DateTime.Now;
                newPlan.ShiftID = CurrentShift.ShiftID;
                newPlan.DT = p.DT / (0.75 * operators);
                db.Plans.Add(newPlan);
                db.SaveChanges();
                Plans.Add(newPlan);

            }
        }