// ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadAll
 /// </summary>       
 public void LoadAll()
 {
     EmployeeStandardCostsSetupGateway employeeStandardCostsSetupGateway = new EmployeeStandardCostsSetupGateway(Data);
     employeeStandardCostsSetupGateway.LoadAll();
 }
        /// <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);
                                }
                            }
                        }
                    }
                }
            }
        }
        private bool ExistsDataModified()
        {
            foreach (GridViewRow row in grdCostSetup.Rows)
            {
                try
                {
                    EmployeeStandardCostsSetupTDS dataSet = new EmployeeStandardCostsSetupTDS();
                    dataSet.StandardCostsSetup.Merge(standardCostsSetup, true);
                    EmployeeStandardCostsSetupGateway model = new EmployeeStandardCostsSetupGateway(dataSet);

                    // Getting general information
                    int employeeId = Int32.Parse(grdCostSetup.DataKeys[row.RowIndex].Values["EmployeeID"].ToString());

                    // Verify BurdenRate
                    decimal? newBurdenFactor = decimal.Parse(((TextBox)row.FindControl("tbxBourdenFactor")).Text);
                    decimal? originalBurdenFactor = model.GetBourdenFactorOriginal(employeeId);

                    if (newBurdenFactor != originalBurdenFactor)
                    {
                        return true;
                    }
                    else
                    {
                        // Verify USHealthBenefitFactor
                        decimal newUSHealthBenefitFactor = decimal.Parse(((TextBox)row.FindControl("tbxUSHealthBenefitFactor")).Text);
                        decimal? originalUSHealthBenefitFactor = model.GetUSHealthBenefitFactorOriginal(employeeId);

                        if (newUSHealthBenefitFactor != originalUSHealthBenefitFactor)
                        {
                            return true;
                        }
                        else
                        {
                            // Verify BenefitFactorCad
                            decimal newBenefitFactorCad = decimal.Parse(((TextBox)row.FindControl("tbxBenefitFactorCad")).Text);
                            decimal? originalBenefitFactorCad = model.GetBenefitFactorCadOriginal(employeeId);

                            if (newBenefitFactorCad != originalBenefitFactorCad)
                            {
                                return true;
                            }
                            else
                            {
                                // Verify BenefitFactorUsd
                                decimal newBenefitFactorUsd = decimal.Parse(((TextBox)row.FindControl("tbxBenefitFactorUsd")).Text);
                                decimal? originalBenefitFactorUsd = model.GetBenefitFactorUsdOriginal(employeeId);

                                if (newBenefitFactorUsd != originalBenefitFactorUsd)
                                {
                                    return true;
                                }
                            }
                        }
                    }

                }
                catch
                {
                    return true;
                }
            }

            return false;
        }