Exemplo n.º 1
0
        //adaugare concediu
        public void AddNewLeave(Leave leave)
        {
            try
            {
                //verifica daca intervalele se intersecteaza
                bool overlap = false;
                foreach (var l in leaveList)
                {
                    if (leave.StartingDate.Add(new TimeSpan(leave.Duration, 0, 0, 0)) > l.StartingDate &&
                        leave.StartingDate < l.StartingDate.Add(new TimeSpan(l.Duration, 0, 0, 0)))
                    {
                        overlap = true;
                    }
                }

                if (overlap == false)
                {
                    //daca concediul are zile in doi ani diferiti
                    if (leave.StartingDate.Year != leave.StartingDate.Add(new TimeSpan(leave.Duration, 0, 0, 0)).Year)
                    {
                        //aflam cate zile sunt in primul an
                        var days1 = (new DateTime(leave.StartingDate.Year + 1, 1, 1)).AddDays(-leave.StartingDate.Day).Day;
                        if (availableDaysOff[leave.StartingDate.Year] >= days1)
                        {
                            SubtractDays(days1, leave.StartingDate.Year);
                        }
                        else
                        {
                            throw (new NegativeLeaveException("Numarul de zile disponibile nu poate fi mai mic decat durata concediului"));
                        }
                        //aflam cate zile sunt in al doilea an
                        var days2 = leave.Duration - days1;
                        if (availableDaysOff[leave.StartingDate.Year + 1] >= days2)
                        {
                            SubtractDays(days2, leave.StartingDate.Year + 1);
                        }
                        else
                        {
                            throw (new NegativeLeaveException("Numarul de zile disponibile nu poate fi mai mic decat durata concediului"));
                        }
                        leave.Employee = this;
                        leaveList.Add(leave);
                    }
                    else
                    if (availableDaysOff[leave.StartingDate.Year] >= leave.Duration)
                    {
                        SubtractDays(leave.Duration, leave.StartingDate.Year);
                        leave.Employee = this;
                        leaveList.Add(leave);
                    }
                    else
                    {
                        throw (new NegativeLeaveException("Numarul de zile disponibile nu poate fi mai mic decat durata concediului"));
                    }
                }
                else
                {
                    Console.WriteLine("Concediul introdus se suprapune cu altul deja existent");
                }
            }
            catch (NegativeLeaveException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }