Esempio n. 1
0
        public void ExchangeWorkGroup(decimal workGroup1, decimal workGroup2, string date1, string date2)
        {
            UIValidationExceptions exception = new UIValidationExceptions();

            if (workGroup1 == 0 || workGroup2 == 0)
            {
                exception.Add(ExceptionResourceKeys.ExceptionShiftWorkGroupIdRequierd, "شناسه گروه کاری مشخص نشده است", ExceptionSrc);
            }

            if (exception.Count > 0)
            {
                throw exception;
            }
            using (NHibernateSessionManager.Instance.BeginTransactionOn())
            {
                try
                {
                    DateTime dayDate1, dayDate2;
                    if (BLanguage.CurrentSystemLanguage == LanguagesName.Parsi)
                    {
                        dayDate1 = Utility.ToMildiDate(date1);
                        dayDate2 = Utility.ToMildiDate(date2);
                    }
                    else
                    {
                        dayDate1 = Utility.ToMildiDateTime(date1);
                        dayDate2 = Utility.ToMildiDateTime(date2);
                    }

                    ISearchPerson            searchTool = new BPerson();
                    PersonAdvanceSearchProxy proxy      = new PersonAdvanceSearchProxy();
                    proxy.WorkGroupId       = workGroup1;
                    proxy.WorkGroupFromDate = Utility.ToString(dayDate1);
                    int            count    = searchTool.GetPersonInAdvanceSearchCount(proxy);
                    IList <Person> persons1 = searchTool.GetPersonInAdvanceSearch(proxy, 0, count);

                    proxy                   = new PersonAdvanceSearchProxy();
                    proxy.WorkGroupId       = workGroup2;
                    proxy.WorkGroupFromDate = Utility.ToString(dayDate2);
                    count                   = searchTool.GetPersonInAdvanceSearchCount(proxy);
                    IList <Person> persons2 = searchTool.GetPersonInAdvanceSearch(proxy, 0, count);

                    ShiftRepository shiftRep = new ShiftRepository(false);
                    decimal?        shift1   = shiftRep.GetShiftIdByWorkGroupId(workGroup1, dayDate1);
                    decimal?        shift2   = shiftRep.GetShiftIdByWorkGroupId(workGroup2, dayDate2);

                    foreach (Person prs in persons2)
                    {
                        decimal personId2 = prs.ID;
                        objectRep.DeleteExceptionShift(personId2, dayDate2, dayDate2);
                        objectRep.DeleteExceptionShift(personId2, dayDate1, dayDate1);

                        ShiftException exShift = new ShiftException();
                        exShift.Date   = dayDate1;
                        exShift.Person = new Person()
                        {
                            ID = personId2
                        };
                        exShift.RegistrationDate = DateTime.Now;
                        if (shift1 != null && shift1 > 0)
                        {
                            exShift.Shift = new Shift()
                            {
                                ID = (decimal)shift1
                            };
                        }
                        else
                        {
                            exShift.Shift = null;
                        }
                        base.SaveChanges(exShift, UIActionType.ADD);
                    }

                    foreach (Person prs in persons1)
                    {
                        decimal personId1 = prs.ID;
                        objectRep.DeleteExceptionShift(personId1, dayDate2, dayDate2);
                        objectRep.DeleteExceptionShift(personId1, dayDate1, dayDate1);

                        ShiftException exShift = new ShiftException();
                        exShift.Date   = dayDate2;
                        exShift.Person = new Person()
                        {
                            ID = personId1
                        };
                        exShift.RegistrationDate = DateTime.Now;
                        if (shift2 != null && shift2 > 0)
                        {
                            exShift.Shift = new Shift()
                            {
                                ID = (decimal)shift2
                            };
                        }
                        else
                        {
                            exShift.Shift = null;
                        }
                        base.SaveChanges(exShift, UIActionType.ADD);
                    }
                    NHibernateSessionManager.Instance.CommitTransactionOn();
                }
                catch (Exception ex)
                {
                    NHibernateSessionManager.Instance.RollbackTransactionOn();
                    LogException(ex, "BExceptionShift", "ExchangeWorkGroup");
                    throw ex;
                }
            }
        }