public override void Save()
        {
            if (_employeeweeks != null && _employeeweeks.Count > 0)
            {
                //_weekservice.SaveEmployeeWeeks(_employeeweeks);
                //_dayservice.SaveEmployeeWeeks(_employeeweeks);

                long[] ids = EmployeeWeekProcessor.GetEmployeeIds(_employeeweeks);
                SrvEmployeeWeekRecordingList srv_updater_weeks = new SrvEmployeeWeekRecordingList();
                DateTime nextWeekMonday = DateTimeHelper.GetNextMonday (BeginDate );
                srv_updater_weeks.InitList(ids, BeginDate);
                SrvEmployeesRecordingDayList day_list = new SrvEmployeesRecordingDayList(ids, BeginDate);

                foreach (EmployeeWeek e_w in _employeeweeks)
                {
                    //srv_updater_weeks.UpdateSaldoAfterRecording(e_w.EmployeeId, nextWeekMonday, e_w.Saldo);
                    srv_updater_weeks.UpdateEntityAfterRecording(e_w);
                    foreach (EmployeeDay ed in e_w.DaysList)
                        day_list.CompareAndSave(ed);
                }

                EmployeeRecordingWorkingModelHelper wmhelper = new EmployeeRecordingWorkingModelHelper(_timeservice.EmployeeRecordingWorkingModelService);

                wmhelper.SaveEmployeeWorkingModel(_employeeweeks);
            }
        }
        private void CheckAndInit()
        {
            if (AllInManagers == null)
            {
                AllInManagers = new CacheEmployeesAllIn();

                AllInManagers.LoadByEmployee(Employee.ID);
            }

            if (RecordingWeeks == null)
            {
                RecordingWeeks = new SrvEmployeeWeekRecordingList();

                RecordingWeeks.InitList(Employee.ID, DateOfInit);

                RecordingWeeks.CacheAllIn = AllInManagers;
            }
        }
        public static void RecalculateAfterModifiedContractEndDate(long[] emplids)
        {
            ILog Log = LogManager.GetLogger("EmployeeBusinessObject");

            if (emplids == null || emplids.Length == 0)
            {
                Log.Debug("RecalculateAfterModifiedContractEndDate - Count of employees = 0");
                return;
            }

            CacheListEmployeeContracts contracts = new CacheListEmployeeContracts();
            contracts.LoadByEmployees(emplids);

            if (Log.IsDebugEnabled)
                Log.Debug("Load contracts by long[] ids of employees");

            CacheListEmployeeRelations relations = new CacheListEmployeeRelations();
            relations.LoadByEmployees(emplids);

            if (Log.IsDebugEnabled)
                Log.Debug("Load relations by long[] ids of employees");

            #region  debug checking

            #if DEBUG
            {
                //for (int i = 0; i < emplids.Length; i++)
                //{
                //    ListEmployeeContracts contract_list1 = contracts[emplids[i]];
                //    ListEmployeeRelations relation_list1 = relations[emplids[i]];

                //    if (contract_list1 == null && relation_list1 == null)
                //    {
                //        Log.Debug("Employee (" + emplids[i].ToString() + ") don't have contract and relation");
                //    }

                //    if (contract_list1 == null)
                //    {
                //        Log.Debug("Employee (" + emplids[i].ToString() + ") don't have contract");
                //    }
                //    if (relation_list1 == null)
                //    {
                //        Log.Debug("Employee (" + emplids[i].ToString() + ") don't have relation");
                //    }

                //    DateTime lastContractDate = contract_list1.GetLastContractDate();

                //    DateTime lastRelationDate = relation_list1.GetLastRelationDate();

                //    if (lastContractDate != lastRelationDate)
                //    {
                //        Log.Debug(String.Format("Employee({0}) has not equal contract and relation end date: {1} and {2}", emplids[i], lastContractDate.ToShortDateString(), lastRelationDate.ToShortDateString()));
                //    }

                //}
            }
            #endif
            #endregion

            Dictionary<long, DateTime> setStoreWorlds = new Dictionary<long, DateTime>();
            Dictionary<long, DateTime> setStores = new Dictionary<long, DateTime>();
            ListEmployeeContracts contract_list = null;
            DateTime last_contract_date;

            SrvEmployeeWeekRecordingList recording_weeks = new SrvEmployeeWeekRecordingList();
            recording_weeks.InitList(emplids, DateTimeSql.FirstMinMonday);

            SrvEmployeeWeekPlanningList planning_weeks = new SrvEmployeeWeekPlanningList(emplids, DateTimeSql.FirstMinMonday);

            foreach (long employeeid in emplids)
            {
                Employee employee = ServerEnvironment.EmployeeService.FindById(employeeid);

                if (employee == null)
                {
                    Log.Warn(String.Format("Employee({0}) not found !!!", employeeid));
                    continue;
                }
                setStores[employee.MainStoreID] = DateTime.Today;

                if (Log.IsDebugEnabled)
                {
                    String debugMessage = String.Format(" Process {0}({1}) ( {2} )", employee.FullName, employee.PersID, employee.PersNumber);
                    Log.Debug(debugMessage);
                }

                contract_list = contracts[employeeid];
                if (contract_list == null || contract_list.Count == 0)
                {
                    continue;
                }

                last_contract_date = contract_list.GetLastContractDate();

                last_contract_date = last_contract_date.AddDays(1);

                last_contract_date = last_contract_date.Date;

                EmployeePlanningDayListEx planning_days = new EmployeePlanningDayListEx(employeeid, last_contract_date);
                foreach (EmployeeDayStatePlanning day in planning_days)
                {
                    if (last_contract_date <= day.Date)
                    {
                        if (setStoreWorlds.ContainsKey(day.StoreWorldId))
                        {
                            DateTime date = setStoreWorlds[day.StoreWorldId];

                            if (last_contract_date < date)
                                setStoreWorlds[day.StoreWorldId] = last_contract_date;
                        }
                        else
                            setStoreWorlds[day.StoreWorldId] = last_contract_date;
                    }
                }
                EmployeeRecordingDayListEx recording_days = new EmployeeRecordingDayListEx(employeeid, last_contract_date, DateTimeSql.SmallDatetimeMax);
                foreach (EmployeeDayStateRecording day in recording_days)
                {
                    if (last_contract_date <= day.Date)
                    {
                        if (setStoreWorlds.ContainsKey(day.StoreWorldId))
                        {
                            DateTime date = setStoreWorlds[day.StoreWorldId];

                            if (last_contract_date < date)
                                setStoreWorlds[day.StoreWorldId] = last_contract_date;
                        }
                        else
                            setStoreWorlds[day.StoreWorldId] = last_contract_date;
                    }
                }
                recording_weeks.ValidateWeekWithContractEnd(employeeid, last_contract_date);
                planning_weeks.ValidateWeekWithContractEnd(employeeid, last_contract_date);

                if (recording_days.Count > 0)
                {
                    recording_days.RemoveFromDatabase(last_contract_date, DateTimeSql.SmallDatetimeMax);
                }
                if (planning_days.Count > 0)
                {
                    planning_days.RemoveFromDatabase(last_contract_date, DateTimeSql.SmallDatetimeMax);
                }

                ClearEmployeeTimes(employeeid, last_contract_date, DateTimeSql.SmallDatetimeMax);

                if (last_contract_date.DayOfWeek != DayOfWeek.Monday)
                {
                    DateTime monday = DateTimeHelper.GetMonday (last_contract_date);
                    DateTime sunday = DateTimeHelper.GetSunday(monday);
                    planning_days = new EmployeePlanningDayListEx(employeeid, monday, sunday);

                    EmployeeWeekTimePlanning week_planning =
                        planning_weeks.GetByDate(employeeid, monday);

                    if (week_planning != null)
                    {
                        week_planning.PlannedHours = 0;
                        week_planning.WorkingHours = 0;
                        week_planning.AdditionalCharge = 0;
                        int prevSaldo = week_planning.GetPrevSaldo();
                        foreach (EmployeeDayStatePlanning e in planning_days)
                        {
                            week_planning.PlannedHours += e.AllreadyPlannedHours;
                            week_planning.WorkingHours += e.WorkingHours;
                            week_planning.AdditionalCharge += e.SumOfAddHours;

                        }
                        week_planning.CalculateSaldo(prevSaldo);
                        ServerEnvironment.EmployeeWeekTimePlanningService.Update(week_planning);
                    }
                    recording_days = new EmployeeRecordingDayListEx(employeeid, monday, sunday);

                    EmployeeWeekTimeRecording week_recording =
                        recording_weeks.GetByDate(employeeid, monday);

                    if (week_recording != null)
                    {
                        week_recording.PlannedHours = 0;
                        week_recording.WorkingHours = 0;
                        week_recording.AdditionalCharge = 0;
                        int prevSaldo = week_recording.GetPrevSaldo();
                        foreach (EmployeeDayStateRecording e in recording_days)
                        {
                            week_recording.PlannedHours += e.AllreadyPlannedHours;
                            week_recording.WorkingHours += e.WorkingHours;
                            week_recording.AdditionalCharge += e.SumOfAddHours;

                        }
                        week_recording.CalculateSaldo(prevSaldo);
                        ServerEnvironment.EmployeeWeekTimeRecordingService.Update(week_recording);
                    }
                }
            }
            List<StoreToWorld> lst = ServerEnvironment.StoreToWorldService.FindAll();
            Dictionary<long, StoreToWorld> dc = new Dictionary<long, StoreToWorld>();
            if (lst != null)
            {
                foreach (StoreToWorld sw in lst)
                {
                    dc[sw.ID] = sw;
                }
            }
            //setStores.Clear();
            StoreToWorld sw1 = null;
            foreach (long swid in setStoreWorlds.Keys)
            {

                if (dc.TryGetValue (swid, out sw1))
                {
                    setStores[sw1.StoreID] = DateTime.Today;
                }
            }

            foreach (long storeid in setStores.Keys)
            {
                StoreBufferHoursAvailableManager.UpdateWholeYear(storeid, 2008);
            }
        }
        public void RecalculateFrom(DateTime date)
        {
            DateTime monday = DateTimeHelper.GetMonday(date);
            DateTime PrevMonday = monday.AddDays(-7);
            if (Relations == null)
            {
                Relations = new DictionListEmployeeRelations(ServerEnvironment.EmployeeRelationService.GetEmployeeRelations (Employee.ID));
            }
            if (Contracts == null)
            {
                Contracts = new DictionListEmployeesContract(ServerEnvironment.EmployeeContractService.GetEmployeeContracts (Employee.ID));
            }

            //LongAbsences = new EmployeeLongTimeAbsenceIndexer(ServerEnvironment.EmployeeLongTimeAbsenceService, Employee.ID);
            if (AllInManagers == null)
            {
                AllInManagers = new CacheEmployeesAllIn(Employee.ID);
            }
            if (RecordingWeeks == null)
            {
                RecordingWeeks = new SrvEmployeeWeekRecordingList();

                RecordingWeeks.InitList(Employee.ID, PrevMonday);
            }

            if (PlanningWeeks == null)
            {
                PlanningWeeks = new SrvEmployeeWeekPlanningList(Employee.ID, PrevMonday);

            }
            PlanningWeeks.BzEmployee = this;
            PlanningWeeks.AllInManager = AllInManagers;
            PlanningWeeks.Contracts = Contracts;
            PlanningWeeks.RecordingWeeks = RecordingWeeks;

            PlanningWeeks.RecalculateAfterImport(monday);
        }
        public static void Recalculate(long[] ids)
        {
            EmployeeService empl_service = ServerEnvironment.EmployeeService as EmployeeService;

            List<Employee> lst = empl_service.EmployeeDao.GetEmployeeByIds(ids);

            IList list = empl_service.EmployeeDao.GetDifferenceContractsAndRelations();

            if (lst == null) return;
            int i = 0;

            DictionListEmployeeRelations rels = new DictionListEmployeeRelations(ServerEnvironment.EmployeeRelationService.LoadAllSorted());

            CacheListEmployeeContracts contracts = (new CacheListEmployeeContracts()).LoadAll();

            CacheEmployeesAllIn all = (new CacheEmployeesAllIn()).LoadAll();

            DateTime beginDate = DateTime.Today.AddDays(-7);
            beginDate = DateTimeHelper.GetMonday(beginDate);

            SrvEmployeeWeekPlanningList PlanningWeeks = new SrvEmployeeWeekPlanningList();
            PlanningWeeks.InitList(null, beginDate);

            SrvEmployeeWeekRecordingList RecordingWeeks = new SrvEmployeeWeekRecordingList();
            RecordingWeeks.InitList(null, beginDate);

            List<Store> stores = ServerEnvironment.StoreService.FindAll();
            Dictionary<long, Store> diction_stores = new Dictionary<long, Store>();
            foreach (Store s in stores)
                diction_stores[s.ID] = s;

            long austriaid = ServerEnvironment.CountryService.AustriaCountryID;
            //rels.LoadRelations(ServerEnvironment.EmployeeRelationService);
            EmployeeBusinessObject bz = null;

            foreach (Employee e in lst)
            {
                i++;

                if (bz == null)
                    bz = new EmployeeBusinessObject(e, DateTime.Today);
                else
                    bz.Employee = e;

                bz.AustriaEmployee = diction_stores[e.MainStoreID].CountryID == austriaid;

                bz.Relations = rels;
                bz.Contracts = contracts;
                bz.AllInManagers = all;

                bz.PlanningWeeks = PlanningWeeks;
                bz.RecordingWeeks = RecordingWeeks;
                bz.RecalculateFrom();

            }
        }