private void btnAddDed_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddDed.Content.ToString() == "Add")
                {
                    grpDed.Visibility = Visibility.Visible;
                    btnAddDed.Content = "Save";
                    btnEdtDed.Content = "Cancel";
                    btnEdtDed.IsEnabled = true;
                    btnDelDed.Visibility = Visibility.Hidden;
                    tbDed.IsEnabled = false;
                    tbInfo.IsEnabled = false;
                    tbPen.IsEnabled = false;
                    tbReq.IsEnabled = false;
                }
                else if (btnAddDed.Content.ToString() == "Save")
                {
                    if (txtDedName.Text == "" || txtDedPerc.Text == "" || txtDedName.Text == "?" || txtDedPerc.Text == "?")
                    {
                        System.Windows.MessageBox.Show("Please enter the correct format and/or complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            var ctr = ctx.Deductions.Where(x => x.ServiceID == sId).Count() + 1;
                            Deduction td = new Deduction { ServiceID = sId, DeductionNum = ctr, Name = txtDedName.Text, Percentage = Convert.ToDouble(txtDedPerc.Text) };
                            ctx.Deductions.Add(td);
                            ctx.SaveChanges();

                            var deds = from dd in ctx.Deductions
                                       where dd.ServiceID == sId
                                       select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                            dgDed.ItemsSource = deds.ToList();
                        }
                        reset();
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        var ctr = ctx.TempoDeductions.Count() + 1;
                        TempoDeduction td = new TempoDeduction { DeductionNum = ctr, Name = txtDedName.Text, Percentage = Convert.ToDouble(txtDedPerc.Text) };
                        ctx.TempoDeductions.Add(td);
                        ctx.SaveChanges();

                        var deds = from dd in ctx.TempoDeductions
                                   select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                        dgDed.ItemsSource = deds.ToList();
                        reset();

                    }

                    reset();
                }
                else //for update
                {
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            int num = Convert.ToInt32(getRow(dgDed, 0));
                            var td = ctx.Deductions.Where(x => x.DeductionNum == num && x.ServiceID == sId).First();
                            td.Name = txtDedName.Text;
                            td.Percentage = Convert.ToDouble(txtDedPerc.Text);
                            ctx.SaveChanges();

                            var deds = from dd in ctx.Deductions
                                       where dd.ServiceID == sId
                                       select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                            dgDed.ItemsSource = deds.ToList();
                        }
                        reset();
                        return;
                    }


                    using (var ctx = new finalContext())
                    {
                        int num = Convert.ToInt32(getRow(dgDed, 0));
                        var td = ctx.TempoDeductions.Where(x => x.DeductionNum == num).First();
                        td.Name = txtDedName.Text;
                        td.Percentage = Convert.ToDouble(txtDedPerc.Text);
                        ctx.SaveChanges();
                        var deds = from dd in ctx.TempoDeductions
                                   select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                        dgDed.ItemsSource = deds.ToList();
                        reset();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cmbDept.Text == "Financing")
                {
                    if (lblName.Content == "?" || lblMinTerm.Content == "?" || lblMaxTerm.Content == "?" || lblMinVal.Content == "?" || lblMaxVal.Content == "?" || lblInterest.Content == "?" || lblDesc.Content == "?" || lblCom.Content == "?" || lblPenAdj.Content == "?" || lblPenCA.Content == "?" || lblPenDaif.Content == "?" || lblPenHolding.Content == "?" || lblPenIn.Content == "?"
                        || String.IsNullOrWhiteSpace(txtCom.Text) || String.IsNullOrWhiteSpace(txtHolding.Text) || String.IsNullOrWhiteSpace(txtDaif.Text) || String.IsNullOrWhiteSpace(txtClosed.Text) || String.IsNullOrWhiteSpace(txtResFee.Text) || String.IsNullOrWhiteSpace(txtResInt.Text) 
                        || String.IsNullOrWhiteSpace(txtName.Text) || String.IsNullOrWhiteSpace(txtMinTerm.Text) || String.IsNullOrWhiteSpace(txtMaxTerm.Text) || String.IsNullOrWhiteSpace(txtMinVal.Text) || String.IsNullOrWhiteSpace(txtMaxVal.Text) || String.IsNullOrWhiteSpace(txtInterest.Text) || String.IsNullOrWhiteSpace(txtDesc.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }
                else
                {
                    if (lblName.Content == "?" || lblMinTerm.Content == "?" || lblMaxTerm.Content == "?" || lblMinVal.Content == "?" || lblMaxVal.Content == "?" || lblInterest.Content == "?" || lblDesc.Content == "?" || lblCom.Content == "?" || lblPenLate.Content == "?"
                        || String.IsNullOrWhiteSpace(txtLtPen.Text) || String.IsNullOrWhiteSpace(txtName.Text) || String.IsNullOrWhiteSpace(txtMinTerm.Text) || String.IsNullOrWhiteSpace(txtMaxTerm.Text) || String.IsNullOrWhiteSpace(txtMinVal.Text) || String.IsNullOrWhiteSpace(txtMaxVal.Text) || String.IsNullOrWhiteSpace(txtInterest.Text) || String.IsNullOrWhiteSpace(txtDesc.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                if (txtName.Text == "" || txtDesc.Text == "" || txtInterest.Text == "" || txtMaxTerm.Text == "" || txtMinVal.Text == "" || txtMinTerm.Text == "" || cmbDept.Text == "" || cmbType.Text == "")
                {
                    return;
                }

                if (Convert.ToDouble(txtMaxVal.Text) < 1 || Convert.ToDouble(txtMinVal.Text) < 1 || Convert.ToDouble(txtMaxTerm.Text) < 1 || Convert.ToDouble(txtMinTerm.Text) < 0)
                {
                    System.Windows.MessageBox.Show("Terms and Values must be greater thant one(1)", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }


                if (status == "Add")
                {


                    using (var ctx = new finalContext())
                    {
                        Service ser = null;
                        if (cmbDept.Text == "Financing")
                        {
                            ser = new Service { Name = txtName.Text, Department = cmbDept.Text, Description = txtDesc.Text, Type = cmbType.Text, Active = true, Interest = Convert.ToDouble(txtInterest.Text), MinTerm = Convert.ToInt32(txtMinTerm.Text), MaxTerm = Convert.ToInt32(txtMaxTerm.Text), MinValue = Convert.ToDouble(txtMinVal.Text), MaxValue = Convert.ToDouble(txtMaxVal.Text), AgentCommission = Convert.ToDouble(txtCom.Text), Holding = Convert.ToDouble(txtHolding.Text), ClosedAccountPenalty = Convert.ToDouble(txtClosed.Text), DaifPenalty = Convert.ToDouble(txtDaif.Text), RestructureFee = Convert.ToDouble(txtResFee.Text), RestructureInterest = Convert.ToDouble(txtResInt.Text), AdjustmentFee = Convert.ToDouble(txtAdjust.Text), LatePaymentPenalty=0 };
                        }
                        else
                        {
                            ser = new Service { Name = txtName.Text, Department = cmbDept.Text, Description = txtDesc.Text, Type = cmbType.Text, Active = true, Interest = Convert.ToDouble(txtInterest.Text), MinTerm = Convert.ToInt32(txtMinTerm.Text), MaxTerm = Convert.ToInt32(txtMaxTerm.Text), MinValue = Convert.ToDouble(txtMinVal.Text), MaxValue = Convert.ToDouble(txtMaxVal.Text), AgentCommission = Convert.ToDouble(txtCom.Text), Holding = 0, ClosedAccountPenalty = 0, DaifPenalty = 0, RestructureFee = 0, RestructureInterest = 0, AdjustmentFee = 0, LatePaymentPenalty=Convert.ToDouble(txtLtPen.Text) };
                        }
                        var deds = from dd in ctx.TempoDeductions
                                   select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                        var rqs = from rq in ctx.TempoRequirements
                                  select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };

                        foreach (var item in deds)
                        {
                            Deduction dd = new Deduction { DeductionNum = item.DedNumber, Name = item.Name, Percentage = item.Percentage };
                            ctx.Deductions.Add(dd);
                        }

                        foreach (var item in rqs)
                        {
                            Requirement rr = new Requirement { RequirementNum = item.ReqNumber, Name = item.Name, Description = item.Description };
                            ctx.Requirements.Add(rr);
                        }

                        ComboBoxItem typeItem = (ComboBoxItem)cmbType.SelectedItem;
                        string value = typeItem.Content.ToString();
                        if (value == "Collateral")
                        {
                            var ci = from c in ctx.TempCollateralInformations
                                     select c;

                            foreach (var itm in ci)
                            {
                                CollateralInformation cl = new CollateralInformation { CollateralInformationNum = itm.TempCollateralInformationNum, Field = itm.Field };
                                ctx.CollateralInformations.Add(cl);
                            }
                        }

                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Service " + txtName.Text };
                        ctx.AuditTrails.Add(at);

                        ctx.Services.Add(ser);
                        ctx.SaveChanges();
                        MessageBox.Show("Service has been successfully added", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
                else
                {
                    using (var ctx = new finalContext())
                    {
                        var ser = ctx.Services.Find(sId);
                        ser.Name = txtName.Text;
                        ser.Department = cmbDept.Text;
                        ser.Description = txtDesc.Text;
                        ser.Type = cmbType.Text;
                        ser.Interest = Convert.ToDouble(txtInterest.Text);
                        ser.MinTerm = Convert.ToInt32(txtMinTerm.Text);
                        ser.MaxTerm = Convert.ToInt32(txtMaxTerm.Text);
                        ser.MinValue = Convert.ToDouble(txtMinVal.Text);
                        ser.MaxValue = Convert.ToDouble(txtMaxVal.Text);
                        if (cmbDept.Text == "Financing")
                        {
                            ser.Holding = Convert.ToDouble(txtHolding.Text);
                            ser.DaifPenalty = Convert.ToDouble(txtDaif.Text);
                            ser.ClosedAccountPenalty = Convert.ToDouble(txtClosed.Text);
                            ser.AgentCommission = Convert.ToDouble(txtCom.Text);
                            ser.RestructureFee = Convert.ToDouble(txtResFee.Text);
                            ser.RestructureInterest = Convert.ToDouble(txtResInt.Text);
                            ser.AdjustmentFee = Convert.ToDouble(txtAdjust.Text);
                            ser.LatePaymentPenalty = 0;
                        }
                        else
                        {
                            ser.Holding = 0;
                            ser.DaifPenalty = 0;
                            ser.ClosedAccountPenalty = 0;
                            ser.AgentCommission = 0;
                            ser.RestructureFee = 0;
                            ser.RestructureInterest = 0;
                            ser.AdjustmentFee = 0;
                            ser.LatePaymentPenalty = Convert.ToDouble(txtLtPen.Text);
                        }

                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Service " + txtName.Text };
                        ctx.AuditTrails.Add(at);
                        ctx.SaveChanges();
                        MessageBox.Show("Service has been successfully updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }