/// <summary>
        /// Save
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            EmployeeStandardCostsSetupTDS employeeStandardCostsSetupChanges = (EmployeeStandardCostsSetupTDS)Data.GetChanges();

            if (employeeStandardCostsSetupChanges.StandardCostsSetup.Rows.Count > 0)
            {
                EmployeeStandardCostsSetupGateway employeeStandardCostsSetupGateway = new EmployeeStandardCostsSetupGateway(employeeStandardCostsSetupChanges);

                // Update employee
                foreach (EmployeeStandardCostsSetupTDS.StandardCostsSetupRow standardCostsSetupRow in (EmployeeStandardCostsSetupTDS.StandardCostsSetupDataTable)employeeStandardCostsSetupChanges.StandardCostsSetup)
                {
                    // Unchanged values
                    int employeeId = standardCostsSetupRow.EmployeeID;
                    int? loginId = null; if(!standardCostsSetupRow.IsLoginIDNull()) loginId =  standardCostsSetupRow.LoginID;
                    int? contactsId = null; if (!standardCostsSetupRow.IsContactsIDNull()) contactsId = standardCostsSetupRow.ContactsID;
                    string fullName = standardCostsSetupRow.FullName;
                    string firstName = standardCostsSetupRow.FirstName;
                    string middleInitial = ""; if (!standardCostsSetupRow.IsMiddleInitialNull()) middleInitial = standardCostsSetupRow.MiddleInitial;
                    string lastName = standardCostsSetupRow.LastName;
                    string type = standardCostsSetupRow.Type;
                    string state = standardCostsSetupRow.State;
                    bool isSalessman = standardCostsSetupRow.IsSalesman;
                    bool requestProjectTime = standardCostsSetupRow.RequestProjectTime;
                    bool deleted = standardCostsSetupRow.Deleted;
                    bool salaried = standardCostsSetupRow.Salaried;
                    string eMail = ""; if (!standardCostsSetupRow.IseMailNull()) eMail = standardCostsSetupRow.eMail;
                    bool assignableSrs = standardCostsSetupRow.AssignableSRS;
                    string jobClassType = ""; if (!standardCostsSetupRow.IsJobClassTypeNull()) jobClassType = standardCostsSetupRow.JobClassType;
                    string category = standardCostsSetupRow.Category;
                    string personalAgencyName = ""; if (!standardCostsSetupRow.IsPersonalAgencyNameNull()) personalAgencyName = standardCostsSetupRow.PersonalAgencyName;
                    bool isVacationsManager = standardCostsSetupRow.IsVacationsManager;
                    bool approveTimesheets = standardCostsSetupRow.ApproveTimesheets;

                    // Original values
                    decimal? originalBourdenFactor = employeeStandardCostsSetupGateway.GetBourdenFactorOriginal(employeeId);
                    decimal? originalUSHealthBenefitFactor = employeeStandardCostsSetupGateway.GetUSHealthBenefitFactorOriginal(employeeId);
                    decimal? originalBenefitFactorCad = employeeStandardCostsSetupGateway.GetBenefitFactorCadOriginal(employeeId);
                    decimal? originalBenefitFactorUsd = employeeStandardCostsSetupGateway.GetBenefitFactorUsdOriginal(employeeId);

                    // New variables
                    decimal? newBourdenFactor = employeeStandardCostsSetupGateway.GetBourdenFactor(employeeId);
                    decimal? newUSHealthBenefitFactor = employeeStandardCostsSetupGateway.GetUSHealthBenefitFactor(employeeId);
                    decimal? newBenefitFactorCad = employeeStandardCostsSetupGateway.GetBenefitFactorCad(employeeId);
                    decimal? newBenefitFactorUsd = employeeStandardCostsSetupGateway.GetBenefitFactorUsd(employeeId);
                    string crew = ""; if (!standardCostsSetupRow.IsCrewNull()) crew = standardCostsSetupRow.Crew;

                    // ... Update employee
                    Employee employee = new Employee(null);
                    employee.UpdateDirect(employeeId, loginId, contactsId, fullName, firstName, middleInitial, lastName, type, state, isSalessman, requestProjectTime, deleted, salaried, eMail, assignableSrs, jobClassType, category, personalAgencyName, isVacationsManager, approveTimesheets, originalBourdenFactor, originalUSHealthBenefitFactor, originalBenefitFactorCad, originalBenefitFactorUsd, crew, fullName, firstName, middleInitial, lastName, type, state, isSalessman, requestProjectTime, deleted, salaried, eMail, assignableSrs, jobClassType, category, personalAgencyName, isVacationsManager, approveTimesheets, newBourdenFactor, newUSHealthBenefitFactor, newBenefitFactorCad, newBenefitFactorUsd, crew);

                    // ... Update cost history table
                    // ... ... if there are new values
                    if ((newBourdenFactor.HasValue) && (newUSHealthBenefitFactor.HasValue) && (newBenefitFactorCad.HasValue) && (newBenefitFactorUsd.HasValue))
                    {
                        // ... ... verify if the new values are different from original
                        if ((originalBourdenFactor.HasValue) && (originalUSHealthBenefitFactor.HasValue) && (originalBenefitFactorCad.HasValue) && (originalBenefitFactorUsd.HasValue))
                        {
                            if (((decimal)originalBourdenFactor != (decimal)newBourdenFactor) || ((decimal)originalUSHealthBenefitFactor != (decimal)newUSHealthBenefitFactor) || ((decimal)originalBenefitFactorCad != (decimal)newBenefitFactorCad) || ((decimal)originalBenefitFactorUsd != (decimal)newBenefitFactorUsd))
                            {
                                EmployeeInformationCostInformationGateway employeeInformationCostInformationGateway = new EmployeeInformationCostInformationGateway();
                                employeeInformationCostInformationGateway.LoadLastCostByEmployeeId(employeeId, companyId);
                                if (employeeInformationCostInformationGateway.Table.Rows.Count > 0)
                                {
                                    DateTime date = DateTime.Now;
                                    int lastCostId = employeeInformationCostInformationGateway.GetCostId(employeeId);
                                    string unitOfMeasurement = employeeInformationCostInformationGateway.GetUnitOfMeasurement(lastCostId, employeeId);
                                    decimal payRateCad = employeeInformationCostInformationGateway.GetPayRateCad(lastCostId, employeeId);
                                    decimal payRateUsd = employeeInformationCostInformationGateway.GetPayRateUsd(lastCostId, employeeId);
                                    decimal benefitFactorUsd = employeeInformationCostInformationGateway.GetBenefitFactorUsd(lastCostId, employeeId);

                                    decimal boudenFactor = (decimal)newBourdenFactor / 100;

                                    decimal burdenRateCad = Decimal.Round(payRateCad * boudenFactor, 2);
                                    decimal totalCostCad = payRateCad + burdenRateCad;
                                    totalCostCad = Decimal.Round(totalCostCad, 2);

                                    decimal burdenRateUsd = Decimal.Round(payRateUsd * boudenFactor, 2);
                                    decimal totalCostUsd = payRateUsd + burdenRateUsd;
                                    totalCostUsd = Decimal.Round(totalCostUsd, 2);

                                    decimal healthBenefitFactorUsd = (decimal)newUSHealthBenefitFactor / 100;
                                    decimal healthBenefitUsd = benefitFactorUsd * healthBenefitFactorUsd;

                                    int newCostId = lastCostId + 1;

                                    EmployeeCostHistory employeeCostHistory = new EmployeeCostHistory(null);
                                    employeeCostHistory.InsertDirect(newCostId, employeeId, date, unitOfMeasurement, payRateCad, burdenRateCad, totalCostCad, payRateUsd, burdenRateUsd, totalCostUsd, false, companyId, (decimal)newBenefitFactorCad, (decimal)newBenefitFactorUsd, healthBenefitUsd);
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Save all costs to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            EmployeeInformationTDS costInformationChanges = (EmployeeInformationTDS)Data.GetChanges();

            if (costInformationChanges.CostInformation.Rows.Count > 0)
            {
                EmployeeInformationCostInformationGateway employeeInformationCostInformationGateway = new EmployeeInformationCostInformationGateway(costInformationChanges);

                foreach (EmployeeInformationTDS.CostInformationRow row in (EmployeeInformationTDS.CostInformationDataTable)costInformationChanges.CostInformation)
                {
                    // Insert new costs
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        EmployeeCostHistory employeeCost = new EmployeeCostHistory(null);
                        employeeCost.InsertDirect(row.CostID, row.EmployeeID, row.Date, row.UnitOfMeasurement, row.PayRateCad, row.BurdenRateCad, row.TotalCostCad, row.PayRateUsd, row.BurdenRateUsd, row.TotalCostUsd, row.Deleted, row.COMPANY_ID, row.BenefitFactorCad, row.BenefitFactorUsd, row.HealthBenefitUsd);
                    }

                    // Update costs
                    if ((!row.Deleted) && (row.InDatabase))
                    {
                        int costId = row.CostID;
                        int employeeId = row.EmployeeID;
                        bool originalDeleted = row.Deleted;
                        int originalCompanyId = companyId;

                        // original values
                        DateTime originalDate = employeeInformationCostInformationGateway.GetDateOriginal(costId, employeeId);
                        string originalUnitOfMeasurement = employeeInformationCostInformationGateway.GetUnitOfMeasurementOriginal(costId, employeeId);
                        decimal originalPayRateCad = employeeInformationCostInformationGateway.GetPayRateCadOriginal(costId, employeeId);
                        decimal originalBurdenRateCad = employeeInformationCostInformationGateway.GetBurdenRateCadOriginal(costId, employeeId);
                        decimal originalTotalCostCad = employeeInformationCostInformationGateway.GetTotalCostCadOriginal(costId, employeeId);
                        decimal originalPayRateUsd = employeeInformationCostInformationGateway.GetPayRateUsdOriginal(costId, employeeId);
                        decimal originalBurdenRateUsd = employeeInformationCostInformationGateway.GetBurdenRateUsdOriginal(costId, employeeId);
                        decimal originalTotalCostUsd = employeeInformationCostInformationGateway.GetTotalCostUsdOriginal(costId, employeeId);
                        decimal originalBenefitFactorCad = employeeInformationCostInformationGateway.GetBenefitFactorCadOriginal(costId, employeeId);
                        decimal originalBenefitFactorUsd = employeeInformationCostInformationGateway.GetBenefitFactorUsdOriginal(costId, employeeId);
                        decimal originalHealthBenefitUsd = employeeInformationCostInformationGateway.GetHealthBenefitUsdOriginal(costId, employeeId);

                        // new values
                        DateTime newDate = employeeInformationCostInformationGateway.GetDate(costId, employeeId);
                        string newUnitOfMeasurement = employeeInformationCostInformationGateway.GetUnitOfMeasurement(costId, employeeId);
                        decimal newPayRateCad = employeeInformationCostInformationGateway.GetPayRateCad(costId, employeeId);
                        decimal newBurdenRateCad = employeeInformationCostInformationGateway.GetBurdenRateCad(costId, employeeId);
                        decimal newTotalCostCad = employeeInformationCostInformationGateway.GetTotalCostCad(costId, employeeId);
                        decimal newPayRateUsd = employeeInformationCostInformationGateway.GetPayRateUsd(costId, employeeId);
                        decimal newBurdenRateUsd = employeeInformationCostInformationGateway.GetBurdenRateUsd(costId, employeeId);
                        decimal newTotalCostUsd = employeeInformationCostInformationGateway.GetTotalCostUsd(costId, employeeId);
                        decimal newBenefitFactorCad = employeeInformationCostInformationGateway.GetBenefitFactorCad(costId, employeeId);
                        decimal newBenefitFactorUsd = employeeInformationCostInformationGateway.GetBenefitFactorUsd(costId, employeeId);
                        decimal newHealthBenefitUsd = employeeInformationCostInformationGateway.GetHealthBenefitUsd(costId, employeeId);

                        EmployeeCostHistory employeeCost = new EmployeeCostHistory(null);
                        employeeCost.UpdateDirect(costId, employeeId, originalDate, originalUnitOfMeasurement, originalPayRateCad, originalBurdenRateCad, originalTotalCostCad, originalPayRateUsd, originalBurdenRateUsd, originalTotalCostUsd, originalDeleted, originalCompanyId, originalBenefitFactorCad, originalBenefitFactorUsd, originalHealthBenefitUsd, costId, employeeId, newDate, newUnitOfMeasurement, newPayRateCad, newBurdenRateCad, newTotalCostCad, newPayRateUsd, newBurdenRateUsd, newTotalCostUsd, originalDeleted, originalCompanyId, newBenefitFactorCad, newBenefitFactorUsd, newHealthBenefitUsd);
                    }

                    // Deleted costs
                    if ((row.Deleted) && (row.InDatabase))
                    {
                        EmployeeCostHistory employeeCost = new EmployeeCostHistory(null);
                        employeeCost.DeleteDirect(row.CostID, row.EmployeeID, row.COMPANY_ID);
                    }
                }
            }
        }
        protected void grdCosts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Edit items
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);
                int costId = Int32.Parse(((Label)e.Row.FindControl("lblCostIDEdit")).Text.Trim());

                EmployeeInformationCostInformationGateway employeeInformationCostInformationGateway = new EmployeeInformationCostInformationGateway(employeeInformationTDS);
                string unitOfMeasurement = employeeInformationCostInformationGateway.GetUnitOfMeasurement(costId, employeeId);
                ((DropDownList)e.Row.FindControl("ddlUnitOfMeasurementEdit")).SelectedValue = unitOfMeasurement;
            }

            // Control value for footer employees
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                // For benefit factors
                ((TextBox)e.Row.FindControl("tbxBenefitFactorCadNew")).Text = tbxBenefitFactorCad.Text;
                ((TextBox)e.Row.FindControl("tbxBenefitFactorUsdNew")).Text = tbxBenefitFactorUsd.Text;

                // Validation to add new rows
                if ((tbxBourdenFactor.Text == "") || (tbxUSHealthBenefitFactor.Text == "") || (tbxBenefitFactorCad.Text == "") || (tbxBenefitFactorUsd.Text == ""))
                {
                    ((ImageButton)e.Row.FindControl("ibtnAdd")).Visible = false;
                }
            }

            // Normal rows
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == (DataControlRowState.Normal | DataControlRowState.Alternate))))
            {
                // Validation to edit rows
                if ((tbxBourdenFactor.Text == "") || (tbxUSHealthBenefitFactor.Text == "") || (tbxBenefitFactorCad.Text == "") || (tbxBenefitFactorUsd.Text == ""))
                {
                    ((ImageButton)e.Row.FindControl("ibtnEdit")).Visible = false;
                }
                else
                {
                    if (((Label)e.Row.FindControl("lblInDataBase")).Text == "1")
                    {
                        ((ImageButton)e.Row.FindControl("ibtnEdit")).Visible = false;
                    }
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadAllByEmployeeId
 /// </summary>
 /// <param name="employeeId">employeeId</param>              
 /// <param name="companyId">companyId</param>
 public void LoadAllByEmployeeId(int employeeId, int companyId)
 {
     EmployeeInformationCostInformationGateway employeeInformationCostInformationGateway = new EmployeeInformationCostInformationGateway(Data);
     employeeInformationCostInformationGateway.LoadAllByEmployeeId(employeeId, companyId);
 }