//method to add fixed overhead detaisl returne true if successfull els false
        public bool addFixedOverHead(FixedOverHeadData fixOh)//this method adds data to fixed overhead db table
        {
            try
            {
                FixedOverhead fixadd = new FixedOverhead();
                fixadd.Year = fixOh.Year;
                fixadd.Month = fixOh.Month;
                fixadd.Electricity = (decimal)fixOh.Elect;
                fixadd.Tax = (decimal)fixOh.Tax;
                fixadd.Water = (decimal)fixOh.Water;
                fixadd.Salary = (decimal)fixOh.Salary;
                fixadd.RentOrMortgage = (decimal)fixOh.Rent;
                fixadd.PhoneAnInternet = (decimal)fixOh.PhInt;
                fixadd.Fuel = (decimal)fixOh.Fule;
                fixadd.Misc = (decimal)fixOh.Mess;
                fixadd.Ot = (decimal)fixOh.Ot;
                fixadd.Other = (decimal)fixOh.Other;

                _context.FixedOverheads.Add(fixadd);
                int chk = _context.SaveChanges();

                if (chk >= 0)
                    return true;
                else
                    return false;
            }
            catch (InvalidOperationException invalidOperationException)
            {
                addException(invalidOperationException,  "addFixedOverHead");
                return false;
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEntityValidationException)
            {
                addException(dbEntityValidationException, "addFixedOverHead()");
                return false;
            }

        }//addFixedOverHead method ends
        }//getmonth methods ends

        //this method updates fixed overhead details and return true if success else return false
        public bool updateFixData(FixedOverHeadData data)
        {
            try
            {
                FixedOverhead fixedEdit = _context.FixedOverheads.FirstOrDefault(i => i.FixID == data.Id);

                fixedEdit.Year = data.Year;
                fixedEdit.Month = data.Month;
                fixedEdit.Electricity = (decimal)data.Elect;
                fixedEdit.Tax = (decimal)data.Tax;
                fixedEdit.Water = (decimal)data.Water;
                fixedEdit.Salary = (decimal)data.Salary;
                fixedEdit.RentOrMortgage = (decimal)data.Rent;
                fixedEdit.PhoneAnInternet = (decimal)data.Rent;
                fixedEdit.Fuel = (decimal)data.Fule;
                fixedEdit.Misc = (decimal)data.Mess;
                fixedEdit.Ot = (decimal)data.Ot;
                fixedEdit.Other = (decimal)data.Other;

                int chk = _context.SaveChanges();

                if (chk >= 0)
                    return true;
                else
                    return false;
            }
            catch (InvalidOperationException invalidOperationException)
            {
                addException(invalidOperationException, "updateFixData()");
                return false;
            }


        }//updateFixData method ends
        private void btnFOHAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (getvalidationsEmptyFields())
                System.Windows.MessageBox.Show("Need to fill all the fields", "Empty Fields", MessageBoxButton.OK, MessageBoxImage.Warning);
            else
            {
                FixedOverHeadDAO fixdao = new FixedOverHeadDAO();
                FixedOverHeadData fixdata = new FixedOverHeadData();
                //check for the values exisit in the tabel
                int mx = 0;
                string fixID = "";
                int mont = cmbFOHmonthComboBox.SelectedIndex + 1;
                int year1 = Convert.ToInt32(cmbFOHForMonthYear.SelectedValue.ToString());
                                                                                       
                fixID = fixdao.getmonth(mont, year1);
                if (fixID != null)
                    mx = 1;
                //end of check
                //parsig values to fixed overhead data objec

                fixdata.Year = Convert.ToInt32(cmbFOHForMonthYear.Text);
                fixdata.Month = cmbFOHmonthComboBox.SelectedIndex + 1;
                fixdata.Elect = Convert.ToDouble(txtFOHAddElectricityCost.Text);
                fixdata.Tax = Convert.ToDouble(txtFOHAddTaxCost.Text);
                fixdata.Water = Convert.ToDouble(txtFOHAddWaterCost.Text);
                fixdata.Salary = Convert.ToDouble(txtFOHAddSalaryCost.Text);
                fixdata.Rent = Convert.ToDouble(txtFOHAddRentMortgageLeaseLoanCost.Text);
                fixdata.PhInt = Convert.ToDouble(txtFOHAddPhoneInternetCost.Text);
                fixdata.Fule = Convert.ToDouble(txtFOHAddFuleCost.Text);
                fixdata.Mess = Convert.ToDouble(txtFOHAddMiscellaneousCost.Text);
                fixdata.Ot = Convert.ToDouble(txtFOHAddOTCost.Text);
                decimal other;

                //other field is allowed be blanked. if field is blank then the value is taken as 0
                if (string.IsNullOrEmpty(txtFOHAddOther.Text) || string.IsNullOrWhiteSpace(txtFOHAddOther.Text))
                    other = 0;
                else
                    other = Convert.ToDecimal(txtFOHAddOther.Text);
                fixdata.Other = (double)other;

                //fixed overhead add
                //
                if (btnFOHAdd.Content.ToString() == "Add")
                {
                    //check whether the fid overhead data for the perticular month is entered
                    if (mx == 0)
                    {
                        //fix overhead data in not available
                        if (fixdao.addFixedOverHead(fixdata))
                        { 
                            this.fixedOverheadDataGrid.Items.Refresh();
                            _context.FixedOverheads.Load();
                            fixDAO.populateYears(cmbFOHEditYear, cmbFOHEditMonth);
                            System.Windows.MessageBox.Show("Succesfully Added", "Done", MessageBoxButton.OK, MessageBoxImage.Information);
                            resetAll();
                        }
                        else
                            System.Windows.MessageBox.Show("Faild to Add", "Faild", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    }
                    else if (mx > 0)
                    {
                        //fix overhead data available 
                        //suggest to edit fix overhead data foe the perticualar month
                        MessageBoxResult msgresul = MessageBox.Show("Fixed Overheads details For this month are alredy exist do you want to update?", "Already exist", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                        //get the confimation by the user to edit data
                        if (msgresul == MessageBoxResult.Yes)
                        {
                            //fill values to the month table
                            fillvalsToTextfohStriing(fixID);
                        }
                    }
                    else
                    {

                    }
                }
                else //fixed overhead edit function
                {
                    
                   
                    fixdata.Id = lbleditFOHID.Content.ToString();
                        
                    
                    try
                    {
                        if (fixdao.updateFixData(fixdata))
                        {
                            this.fixedOverheadDataGrid.Items.Refresh();
                            _context.FixedOverheads.Load();

                            System.Windows.MessageBox.Show("Succesfully updated", "Done", MessageBoxButton.OK, MessageBoxImage.Information);
                           // resetAll();
                        }
                        else
                            System.Windows.MessageBox.Show("Faild to update", "Faild", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "Exception");
                    }
                }
            }
        }