void setStatus(ScheduleEntryBase entry, ScheduleEntryState status)
        {
            Championship championship = entry as Championship;

            if (championship != null)
            {
                championshipStatusDoneImplementation(championship);
            }
            entry.State = status;
        }
        protected ScheduleEntryReservation CreateReservation(ScheduleEntryBase entry, Customer customer)
        {
            ScheduleEntryReservation reservation = new ScheduleEntryReservation();

            reservation.Name          = "test";
            reservation.ScheduleEntry = entry;
            reservation.Profile       = customer.Profile;
            entry.Reservations.Add(reservation);
            reservation.Customer = customer;
            insertToDatabase(reservation);
            return(reservation);
        }
        private void makeGroupReservation(ReservationsOperationParam param, ScheduleEntryBase dbEntry, Profile dbEmployee)
        {
            var dbGroup = Session.Get <CustomerGroup>(param.CustomerId.Value);

            if (dbGroup.Profile != dbEmployee || (dbEntry.Profile != dbEmployee))
            {
                throw new CrossProfileOperationException("CustomerGroup or ScheduleEntry belongs to another user");
            }

            foreach (var dbCustomer in dbGroup.Customers)
            {
                //get existing reservation for this customer. If he is on the list then skip this customer
                var dbReservation = dbEntry.Reservations.SingleOrDefault(x => x.Customer == dbCustomer && x.LeaveDateTime == null);
                if (dbReservation == null)
                {
                    dbReservation = makeReservation(dbEmployee, dbEntry, dbCustomer, dbReservation);
                    calculatePayement(dbReservation, false);
                }
            }
        }
        private static void undoReservation(ScheduleEntryReservation dbReservation, ScheduleEntryBase dbEntry)
        {
            if (dbReservation == null)
            {
                throw new AlreadyOccupiedException("Selected customer isn't on the reservation list");
            }
            if (dbEntry.State != ScheduleEntryState.Planned)
            {
                throw new InvalidOperationException("Cannot undo reservation for entries with other statuses than Planned");
            }
            dbReservation.ScheduleEntry = null;
            dbEntry.Reservations.Remove(dbReservation);

            //Championship championship = dbEntry as Championship;
            //if (championship != null)
            //{
            //    var championshipCustomer=championship.Customers.Where(x => x.Customer.GlobalId == dbReservation.Customer.GlobalId).SingleOrDefault();
            //    if (championshipCustomer != null)
            //    {
            //        championship.Customers.Remove(championshipCustomer);
            //    }
            //    championship.Entries.
            //}
        }
        public IList <ScheduleEntryBaseDTO> SaveScheduleEntryRange(SaveScheduleEntryRangeParam saveScheduleEntryRangeParam)
        {
            DateTime startDate = saveScheduleEntryRangeParam.StartDay;
            DateTime endDate   = saveScheduleEntryRangeParam.EndDay.AddDays(1);

            Log.WriteWarning("SaveScheduleEntriesRange:Username={0},Start={1},End={2}", SecurityInfo.SessionData.Profile.UserName, saveScheduleEntryRangeParam.StartDay, saveScheduleEntryRangeParam.EndDay);

            if (!SecurityInfo.Licence.IsInstructor)
            {
                throw new LicenceException("This feature is allowed for Instructor account");
            }

            if (saveScheduleEntryRangeParam.Entries.Where(x => x.StartTime < startDate).Count() > 0 || saveScheduleEntryRangeParam.Entries.Where(x => x.EndTime > endDate).Count() > 0)
            {
                throw new ArgumentOutOfRangeException("Entries are out of range StartDay and EndDay");
            }



            //determine if we should copy instead of save
            bool copy = saveScheduleEntryRangeParam.CopyStart != null && saveScheduleEntryRangeParam.CopyEnd != null;

            var newDb = saveScheduleEntryRangeParam.Entries.Map <IList <ScheduleEntryBase> >();

            using (var trans = Session.BeginSaveTransaction())
            {
                var dbProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId);
                IList <ScheduleEntryBase> itemsToSave;
                if (copy)
                {
                    //if((saveScheduleEntryRangeParam.CopyStart.Value-saveScheduleEntryRangeParam.StartDay).TotalDays<7 ||
                    //    (saveScheduleEntryRangeParam.CopyEnd.Value - saveScheduleEntryRangeParam.EndDay).TotalDays < 7)
                    //{
                    //    throw new ArgumentException("Copy start and end range must be at least one week after source range");
                    //}
                    //var count =Session.QueryOver<ScheduleEntry>().Where(x =>x.StartTime >= saveScheduleEntryRangeParam.CopyStart.Value && x.EndTime <= saveScheduleEntryRangeParam.CopyEnd.Value && x.Profile == dbProfile).RowCount();
                    //if (count>0)
                    //{
                    //    throw new AlreadyOccupiedException("Destination days are occupied already");
                    //}
                    var count = Session.QueryOver <ScheduleEntry>().Where(x => x.StartTime >= saveScheduleEntryRangeParam.CopyStart.Value && x.EndTime <= saveScheduleEntryRangeParam.CopyEnd.Value && x.Profile == dbProfile).List();
                    if (count.Count > 0)
                    {
                        throw new AlreadyOccupiedException("Destination days are occupied already");
                    }
                    itemsToSave = newDb;
                }
                else
                {
                    var db       = Session.QueryOver <ScheduleEntryBase>().Where(x => x.StartTime >= startDate && x.EndTime < endDate && x.Profile == dbProfile).List();
                    var comparer = new CollectionComparer <ScheduleEntryBase>(newDb, db);
                    foreach (var entry in comparer.RemovedItems)
                    {
                        if (entry.IsLocked)
                        {
                            throw new DeleteConstraintException("Cannot delete schedule entry");
                        }
                        Session.Delete(entry);
                    }
                    itemsToSave = comparer.GetForSaveItems();
                }

                for (int index = 0; index < itemsToSave.Count; index++)
                {
                    var entry = itemsToSave[index];
                    ScheduleEntryBase dbOldEntry = null;
                    if (entry.GlobalId != Guid.Empty)
                    {
                        dbOldEntry         = Session.QueryOver <ScheduleEntryBase>().Fetch(x => x.Reminder).Eager.Where(x => x.GlobalId == entry.GlobalId).SingleOrDefault();
                        entry.Reservations = dbOldEntry.Reservations;
                        entry.Reminder     = dbOldEntry.Reminder;
                        if (!copy && entry.IsLocked)
                        {//if we are in saving mode and this entry is locked then skip to the next one
                            itemsToSave[index] = dbOldEntry;
                            continue;
                        }
                    }
                    if (dbOldEntry == null)
                    {
                        entry.Profile = dbProfile;
                        dbOldEntry    = entry;
                    }


                    var scheduleEntry = entry as ScheduleEntry;

                    if (dbOldEntry.Profile != dbProfile)
                    {
                        throw new CrossProfileOperationException();
                    }

                    if (scheduleEntry != null && scheduleEntry.CustomerGroup != null && scheduleEntry.CustomerGroup.Profile != dbProfile)
                    {
                        throw new CrossProfileOperationException("Group not belong to this user");
                    }
                    if (scheduleEntry != null && scheduleEntry.Activity.Profile != dbProfile)
                    {
                        throw new CrossProfileOperationException("Activity is not belong to this user");
                    }

                    if (itemsToSave.Where(x => x != entry && (
                                              x.StartTime <= entry.StartTime &&
                                              x.EndTime > entry.StartTime ||
                                              x.StartTime < entry.EndTime &&
                                              x.EndTime >= entry.EndTime))
                        .Count() > 0)
                    {
                        throw new AlreadyOccupiedException();
                    }

                    var dto = saveScheduleEntryRangeParam.Entries[newDb.IndexOf(entry)];
                    //if (dto.RemindBefore!=null)
                    //{
                    //    if(entry.Reminder==null)
                    //    {
                    //        entry.Reminder=new ReminderItem();
                    //    }
                    //    entry.Reminder.Profile = dbProfile;
                    //    entry.Reminder.DateTime = entry.StartTime;
                    //    entry.Reminder.RemindBefore = dto.RemindBefore==TimeSpan.Zero?(TimeSpan?) null:dto.RemindBefore.Value;
                    //    entry.Reminder.Name = string.Format("{0}: {1}", entry.Activity.Name,entry.StartTime);
                    //    entry.Reminder.Type = ReminderType.ScheduleEntry;
                    //}
                    if (dto.RemindBefore != null)
                    {
                        ReminderService.CreateReminder <ScheduleEntryDTO>(Session, entry, dto.RemindBefore, dbProfile,
                                                                          entry.StartTime, ReminderType.ScheduleEntry);
                    }
                    entry.Profile = dbProfile;
                    if (dto.MyPlaceId == null)
                    {
                        entry.MyPlace = Session.QueryOver <MyPlace>().Where(x => x.Profile == dbProfile && x.IsDefault).SingleOrDefault();
                    }
                    if (entry.MyPlace.Profile != dbProfile)
                    {
                        throw new CrossProfileOperationException("MyPlace not belong to this user");
                    }
                    if (!copy)
                    {
                        //save only when we are in saving mode. in copy mode this loop is only for checking and preparing
                        itemsToSave[index] = Session.Merge(entry);

                        if (dto.RemindBefore != null)
                        {
                            itemsToSave[index].Reminder.ConnectedObject = string.Format("ScheduleEntryDTO:{0}", itemsToSave[index].GlobalId);
                            Session.Update(itemsToSave[index].Reminder);
                        }
                        else if (dbOldEntry.Reminder != null)
                        {
                            Session.Delete(dbOldEntry.Reminder);
                            dbOldEntry.Reminder             = null;
                            dbProfile.DataInfo.ReminderHash = Guid.NewGuid();
                        }
                    }
                }
                if (copy)
                {
                    itemsToSave = copyScheduleEntries(startDate, endDate, saveScheduleEntryRangeParam.CopyStart.Value, saveScheduleEntryRangeParam.CopyEnd.Value, itemsToSave, saveScheduleEntryRangeParam.Mode);
                }
                dbProfile.DataInfo.ScheduleEntryHash = Guid.NewGuid();
                trans.Commit();
                return(itemsToSave.Map <IList <ScheduleEntryBaseDTO> >());
            }
        }
        private ScheduleEntryReservation makeReservation(Profile dbProfile, ScheduleEntryBase dbEntry, Customer dbCustomer, ScheduleEntryReservation res)
        {
            //except anonymous cusomers
            if (res != null && !dbCustomer.IsVirtual)
            {
                throw new AlreadyOccupiedException("Selected customer is already in the reservation list");
            }
            Championship championship = dbEntry as Championship;

            if (championship != null && dbCustomer.IsVirtual)
            {
                throw new InvalidOperationException("Cannot make reservation to championship for virtual customer");
            }
            ScheduleEntryReservation reservation = null;

            if (dbEntry.State != ScheduleEntryState.Planned)
            {
                throw new InvalidOperationException("Cannot make reservations for entries with Done state");
            }
            if (!dbCustomer.IsVirtual)
            {
                //check if this customer has reservation for another acitivity at the same time
                int count = Session.QueryOver <ScheduleEntry>().JoinAlias(x => x.Reservations, () => reservation).Where(
                    x => x.GlobalId != dbEntry.GlobalId && (x.StartTime <= dbEntry.StartTime && x.EndTime > dbEntry.StartTime ||
                                                            x.StartTime < dbEntry.EndTime && x.EndTime >= dbEntry.EndTime) &&
                    x.Profile == dbProfile && reservation.Customer == dbCustomer).RowCount();

                if (count > 0)
                {
                    throw new AlreadyOccupiedException("Selected customer is already in the reservation list for another activity");
                }
            }
            reservation          = new ScheduleEntryReservation();
            reservation.DateTime = Configuration.TimerService.UtcNow;
            reservation.Customer = dbCustomer;
            reservation.Profile  = dbProfile;
            //if (dbEntry != null)
            //{
            reservation.ScheduleEntry = dbEntry;
            dbEntry.Reservations.Add(reservation);
            //}
            //else
            //{
            //    reservation.Room = dbRoom;
            //    reservation.Usluga = dbRoom.FreeEnterUsluga;
            //}
            ScheduleEntry activityEntry = dbEntry as ScheduleEntry;
            Championship  championEntry = dbEntry as Championship;

            if (activityEntry != null)
            {
                reservation.Name = string.Format("{0}:{1}", activityEntry.Activity.Name, dbEntry.StartTime);
            }
            else
            {
                //championship entry
                //TODO:Translate
                reservation.Name = championEntry.ChampionshipType.ToString();
            }

            //Championship championship = dbEntry as Championship;
            //if (championship != null)
            //{
            //    createChampionshipEntryForCustomer(championship,reservation);
            //}
            res = reservation;
            Session.Save(res);
            return(res);
        }