예제 #1
0
 public static bool EditLesson(LessonDTO Lesson)
 {
     using (Gymnastics_Studio_DataEntities context = new Gymnastics_Studio_DataEntities())
     {
         var s = context.Lessons.FirstOrDefault(x => x.Id == Lesson.Id);
         if (s != null)
         {
             s.Name          = Lesson.Name?.TrimStart().TrimEnd();
             s.Day           = Lesson.Day?.TrimStart().TrimEnd();
             s.StartHower    = Lesson.StartHower?.TrimStart().TrimEnd();
             s.FinishHower   = Lesson.FinishHower?.TrimStart().TrimEnd();
             s.MaxStudensNum = Lesson.MaxStudensNum;
             s.LessonKind    = Lesson.LessonKind?.TrimStart().TrimEnd();
             s.TeacherId     = Lesson.TeacherId;
             context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
 public static int AddSubscription(SubscriptionDTO SubscriptionDTO)
 {
     try
     {
         using (Gymnastics_Studio_DataEntities context = new Gymnastics_Studio_DataEntities())
         {
             Subscription Subscription = new Subscription();
             Subscription.Name          = SubscriptionDTO.Name?.TrimStart().TrimEnd();
             Subscription.Price         = SubscriptionDTO.Price;
             Subscription.WeeksNum      = SubscriptionDTO.WeeksNum;
             Subscription.DaysInWeekNum = SubscriptionDTO.DaysInWeekNum;
             Subscription.StudensKind   = SubscriptionDTO.StudensKind?.TrimStart().TrimEnd();
             Subscription.LessonKind    = SubscriptionDTO.LessonKind?.TrimStart().TrimEnd();
             context.Subscriptions.Add(Subscription);
             context.SaveChanges();
             return(context.Subscriptions.Max(o => o.Id));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
예제 #3
0
 public static int GetCurrentWeekNum(int studentId)
 {
     using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
     {
         int num;
         StudentInSubscription CurrentstudentInSubscription = new StudentInSubscription();
         CurrentstudentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentId && x.StartDate <= DateTime.Now && x.FinishDate >= DateTime.Now).FirstOrDefault();
         if (CurrentstudentInSubscription != default && CurrentstudentInSubscription.StartDate != null)
         {
             var d = (DateTime.Now - (DateTime)CurrentstudentInSubscription.StartDate);
             num = (int)d.Days / 7;
             if (d.Days % 7 > 0)
             {
                 num++;
             }
         }
         else
         {
             num = 0;
         }
         return(num);
     }
 }
예제 #4
0
        public static List <List <string> > GetStudentInSubscriptionNamesListByStudentId(int studentId)
        {
            using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
            {
                List <List <StudentInSubscriptionDTO> > studentInSubscriptionListsList = new List <List <StudentInSubscriptionDTO> >();
                List <StudentInSubscription>            studentInSubscriptionList      = new List <StudentInSubscription>();
                List <List <string> > subscriptionNameList = new List <List <string> >();
                List <Payment>        paymentList          = PaymentDTO.ConvertListFromDTO(PaymentFunction.GetStudentPaymentsList(studentId));
                int i = -1;
                foreach (Payment payment in paymentList)
                {
                    i++;
                    studentInSubscriptionList = GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentId && (x.StartDate > payment.StartDate && x.StartDate < payment.FinishDate || x.FinishDate <payment.FinishDate && x.FinishDate> payment.StartDate)).ToList();
                    for (int j = 0; j < studentInSubscriptionList.Count(); j++)

                    {
                        subscriptionNameList[i].Add(GSDE.Subscriptions.FirstOrDefault(x => x.Id == studentInSubscriptionList[j].Id).Name);
                    }
                    studentInSubscriptionListsList.Add(StudentInSubscriptionDTO.ConvertListToDTO(studentInSubscriptionList));
                }
                return(subscriptionNameList);
            }
        }
        public static ExceptionsEnum CreateLessonList(int StudentId, int LessonId, int StudentInSubscreptionId)
        {
            try
            {
                using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
                {
                    // בדיקה אם לתלמידה מספר שיעורים באותו שבוע נמוך ממספר השיעורים שאמור להיות במנוי
                    StudentInSubscription studentInSubscription = new StudentInSubscription();
                    studentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.Id == StudentInSubscreptionId).FirstOrDefault();
                    if (studentInSubscription == default)
                    {
                        return(ExceptionsEnum.StudetInSubscriptionUndefine);
                    }
                    Subscription subscription = new Subscription();
                    subscription = GSDE.Subscriptions.Where(x => x.Id == studentInSubscription.SubscribtionId).FirstOrDefault();
                    if (subscription == default)
                    {
                        return(ExceptionsEnum.SubscriptionUndefine);
                    }
                    List <StudentInLesson> studentInLessonList = new List <StudentInLesson>();
                    studentInLessonList = GSDE.StudentInLessons.Where(x => x.StudentInSubscriptionId == StudentInSubscreptionId).ToList();
                    studentInLessonList = studentInLessonList.Where(x => x.Date >= studentInLessonList[0].Date && x.Date < ((DateTime)studentInLessonList[0].Date).AddDays(7)).ToList();
                    //אם לא החזרת שגיאה מתאימה אחרי יצירת enum
                    if (studentInLessonList.Count >= subscription.DaysInWeekNum)
                    {
                        return(ExceptionsEnum.LessonsNumException);
                    }

                    //אם כן לולאה כמס השבועות במנוי ליצירת שיעור כולל טיפול בתשלום וביצירת שבועות
                    WeekFunction.AddWeeksUpTo((DateTime)studentInSubscription.FinishDate);

                    Lesson lesson = new Lesson();
                    lesson = GSDE.Lessons.Where(x => x.Id == LessonId).FirstOrDefault();
                    if (lesson == default)
                    {
                        return(ExceptionsEnum.LessonUndefine);
                    }
                    if (studentInSubscription.StartDate == default)
                    {
                        return(ExceptionsEnum.StartDateOfStudentInSubscriptionUndefine);
                    }
                    DateTime SundayDate = ((DateTime)studentInSubscription.StartDate).AddDays(0 - (int)((DateTime)studentInSubscription.StartDate).DayOfWeek);
                    int      d          = ConvertHebrewDayNameToNumber(lesson.Day) - (int)SundayDate.DayOfWeek;
                    DateTime Date       = SundayDate.AddDays(d);
                    if (Date < studentInSubscription.StartDate)
                    {
                        Date = Date.AddDays(7);
                    }
                    for (int i = 0; i < subscription.WeeksNum; i++, Date = Date.AddDays(7))
                    {
                        StudentInLesson studentInLesson = new StudentInLesson();
                        studentInLesson.Attendance = true;
                        studentInLesson.Date       = Date;
                        studentInLesson.LessonId   = LessonId;
                        studentInLesson.StudentId  = StudentId;
                        studentInLesson.StudentInSubscriptionId = StudentInSubscreptionId;
                        Week     week  = new Week();
                        DateTime Date2 = Date.AddDays(-7);

                        week = GSDE.Weeks.Where(y => y.Date <= Date && y.Date > Date2).FirstOrDefault();
                        if (week == default)
                        {
                            return(ExceptionsEnum.WeekUndefine);
                        }
                        studentInLesson.WeekId = week.Id;

                        // var x=paymentOfLessson(studentInLesson, subscription, StudentId);
                        // if (x != ExceptionsEnum.True)
                        //   return x;
                        int lessonPayment = (int)(subscription.Price / (subscription.WeeksNum * subscription.DaysInWeekNum));

                        Student student = GSDE.Students.Where(x => x.Id == StudentId).FirstOrDefault();
                        if (student == default)
                        {
                            return(ExceptionsEnum.StudentUndefine);
                        }
                        student.Balance -= lessonPayment;
                        GSDE.SaveChanges();
                        while (lessonPayment > 0)
                        {
                            Payment payment = GSDE.Payments.Where(x => x.StudentId == StudentId && x.FinishDate == null).ToList().OrderBy(o => o.StartDate).FirstOrDefault();
                            switch (payment)
                            {
                            case null:
                                payment               = new Payment();
                                payment.StartDate     = DateTime.Now;
                                payment.StudentId     = StudentId;
                                payment.Sum           = 0 - lessonPayment;
                                payment.FormOfPayment = "חוב";
                                payment.Balance       = 0 - lessonPayment;
                                GSDE.Payments.Add(payment);
                                GSDE.SaveChanges();
                                payment = GSDE.Payments.Where(x => x.StudentId == StudentId && x.FinishDate == default).FirstOrDefault();
                                studentInLesson.PaymentId = payment.Id;
                                break;

                            default:
                                if (payment.Balance > 0 && payment.Balance < lessonPayment)
                                {
                                    lessonPayment      = (int)(lessonPayment - payment.Balance);
                                    payment.Balance    = 0;
                                    payment.FinishDate = DateTime.Now;
                                    GSDE.SaveChanges();
                                }
                                else
                                {
                                    payment.Balance = payment.Balance - lessonPayment;
                                    lessonPayment   = 0;
                                    GSDE.SaveChanges();
                                    studentInLesson.PaymentId = payment.Id;
                                    GSDE.SaveChanges();
                                }
                                break;
                            }
                        }

                        GSDE.StudentInLessons.Add(studentInLesson);
                        GSDE.SaveChanges();
                    }
                }

                return(ExceptionsEnum.True);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
        public static ExceptionsEnum CreateLessonListBySubscriptionIdLessonIdStudentInSubscriptionId(int StudentId, int LessonId, DateTime FromDate, int SubscreptionId, int StudentInSubscreptionId)
        {
            try
            {
                using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
                {
                    Lesson lesson = new Lesson();
                    lesson = GSDE.Lessons.Where(x => x.Id == LessonId).FirstOrDefault();
                    if (lesson == default)
                    {
                        return(ExceptionsEnum.LessonUndefine);
                    }
                    if (FromDate == default)
                    {
                        return(ExceptionsEnum.FromDateUndefine);
                    }
                    DateTime SundayDate = FromDate.AddDays(0 - (int)FromDate.DayOfWeek);
                    int      d          = ConvertHebrewDayNameToNumber(lesson.Day) - (int)SundayDate.DayOfWeek;
                    DateTime Date       = SundayDate.AddDays(d);
                    if (Date < FromDate)
                    {
                        Date = Date.AddDays(7);
                    }
                    Subscription subscription = new Subscription();
                    subscription = GSDE.Subscriptions.Where(x => x.Id == SubscreptionId).FirstOrDefault();
                    if (subscription == default)
                    {
                        return(ExceptionsEnum.SubscriptionUndefine);
                    }
                    for (int i = 0; i < subscription.WeeksNum; i++, Date = Date.AddDays(7))
                    {
                        StudentInLesson studentInLesson = new StudentInLesson();
                        studentInLesson.Attendance = true;
                        studentInLesson.Date       = Date;
                        studentInLesson.LessonId   = LessonId;
                        studentInLesson.StudentId  = StudentId;
                        studentInLesson.StudentInSubscriptionId = StudentInSubscreptionId;
                        Week     week  = new Week();
                        DateTime Date2 = Date.AddDays(-7);
                        week = GSDE.Weeks.Where(x => x.Date <= Date && x.Date > Date2).FirstOrDefault();
                        if (week == default)
                        {
                            return(ExceptionsEnum.WeekUndefine);
                        }
                        studentInLesson.WeekId = week.Id;



                        //טיפול בתשלום השיעור
                        // var l=paymentOfLessson(studentInLesson, subscription, StudentId);
                        int lessonPayment = (int)(subscription.Price / (subscription.WeeksNum * subscription.DaysInWeekNum));

                        Student student = GSDE.Students.Where(x => x.Id == StudentId).FirstOrDefault();
                        if (student == default)
                        {
                            return(ExceptionsEnum.StudentUndefine);
                        }
                        student.Balance -= lessonPayment;
                        GSDE.SaveChanges();
                        while (lessonPayment > 0)
                        {
                            Payment payment = GSDE.Payments.Where(x => x.StudentId == StudentId && x.FinishDate == null).ToList().OrderBy(o => o.StartDate).FirstOrDefault();
                            switch (payment)
                            {
                            case null:
                                payment               = new Payment();
                                payment.StartDate     = DateTime.Now;
                                payment.StudentId     = StudentId;
                                payment.Sum           = 0 - lessonPayment;
                                payment.FormOfPayment = "חוב";
                                payment.Balance       = 0 - lessonPayment;
                                GSDE.Payments.Add(payment);
                                GSDE.SaveChanges();
                                payment = GSDE.Payments.Where(x => x.StudentId == StudentId && x.FinishDate == default).FirstOrDefault();
                                studentInLesson.PaymentId = payment.Id;
                                break;

                            default:
                                if (payment.Balance > 0 && payment.Balance < lessonPayment)
                                {
                                    lessonPayment      = (int)(lessonPayment - payment.Balance);
                                    payment.Balance    = 0;
                                    payment.FinishDate = DateTime.Now;
                                    GSDE.SaveChanges();
                                }
                                else
                                {
                                    payment.Balance = payment.Balance - lessonPayment;
                                    lessonPayment   = 0;
                                    GSDE.SaveChanges();
                                    studentInLesson.PaymentId = payment.Id;
                                    GSDE.SaveChanges();
                                }
                                break;
                            }

                            GSDE.StudentInLessons.Add(studentInLesson);
                            GSDE.SaveChanges();

                            // if (l != ExceptionsEnum.True)
                            // return l;
                        }
                    }
                    return(ExceptionsEnum.True);
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
        //תשלום לשיעור
        public static ExceptionsEnum paymentOfLessson(StudentInLesson studentInLesson, Subscription subscription, int studentId)
        {
            try
            {
                using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
                {
                    int lessonPayment = (int)(subscription.Price / (subscription.WeeksNum * subscription.DaysInWeekNum));

                    Student student = GSDE.Students.Where(x => x.Id == studentId).FirstOrDefault();
                    if (student == default)
                    {
                        return(ExceptionsEnum.StudentUndefine);
                    }
                    student.Balance -= lessonPayment;
                    GSDE.SaveChanges();
                    while (lessonPayment > 0)
                    {
                        Payment payment = GSDE.Payments.Where(x => x.StudentId == studentId && x.FinishDate == null).ToList().OrderBy(o => o.StartDate).FirstOrDefault();
                        switch (payment)
                        {
                        case null:
                            payment               = new Payment();
                            payment.StartDate     = DateTime.Now;
                            payment.StudentId     = studentId;
                            payment.Sum           = 0 - lessonPayment;
                            payment.FormOfPayment = "חוב";
                            payment.Balance       = 0 - lessonPayment;
                            GSDE.Payments.Add(payment);
                            GSDE.SaveChanges();
                            payment = GSDE.Payments.Where(x => x.StudentId == studentId && x.FinishDate == default).FirstOrDefault();
                            studentInLesson.PaymentId = payment.Id;
                            break;

                        default:
                            if (payment.Balance > 0 && payment.Balance < lessonPayment)
                            {
                                lessonPayment      = (int)(lessonPayment - payment.Balance);
                                payment.Balance    = 0;
                                payment.FinishDate = DateTime.Now;
                                GSDE.SaveChanges();
                            }
                            else
                            {
                                payment.Balance = payment.Balance - lessonPayment;
                                lessonPayment   = 0;
                                GSDE.SaveChanges();
                                studentInLesson.PaymentId = payment.Id;
                                GSDE.SaveChanges();
                            }
                            break;
                        }

                        GSDE.StudentInLessons.Add(studentInLesson);
                        GSDE.SaveChanges();
                    }
                }
                return(ExceptionsEnum.True);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
예제 #8
0
        public static List <StudentInSubscriptionDetailsDTO> GetStudentInSubscripyionDetailsByStudentId(int studentId)
        {
            List <StudentInSubscriptionDetailsDTO> StudentInSubscriptionDetailsDTOList = new List <StudentInSubscriptionDetailsDTO>();
            List <Week>            weekList            = new List <Week>();
            List <StudentInLesson> studentInLessonList = new List <StudentInLesson>();

            using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
            {
                weekList = GSDE.Weeks.ToList();

                foreach (Week week in weekList)
                {
                    DateTime finishDate = (Convert.ToDateTime(week.Date)).AddDays(7);
                    studentInLessonList = GSDE.StudentInLessons.Where(x => x.StudentId == studentId && x.Date > week.Date && x.Date < finishDate).ToList();
                    if (studentInLessonList.Count() == 0)
                    {
                        StudentInSubscriptionDetailsDTO studentInSubscriptionDetailsDTO = new StudentInSubscriptionDetailsDTO();
                        studentInSubscriptionDetailsDTO.WeehId         = week.Id;
                        studentInSubscriptionDetailsDTO.WeekStartDate  = week.Date.ToString();
                        studentInSubscriptionDetailsDTO.WeekFinishDate = ((DateTime)week.Date).AddDays(7).ToString();
                        studentInSubscriptionDetailsDTO.WeeklyPortion  = week.WeeklyPortion;


                        StudentInSubscriptionDetailsDTOList.Add(studentInSubscriptionDetailsDTO);
                    }

                    foreach (StudentInLesson studentInLesson in studentInLessonList)
                    {
                        StudentInSubscriptionDetailsDTO studentInSubscriptionDetailsDTO = new StudentInSubscriptionDetailsDTO();
                        studentInSubscriptionDetailsDTO.WeehId            = week.Id;
                        studentInSubscriptionDetailsDTO.WeekStartDate     = week.Date.ToString();
                        studentInSubscriptionDetailsDTO.WeekFinishDate    = ((DateTime)week.Date).AddDays(7).ToString();
                        studentInSubscriptionDetailsDTO.WeeklyPortion     = week.WeeklyPortion;
                        studentInSubscriptionDetailsDTO.StudentInLessonId = (int)studentInLesson.Id;
                        studentInSubscriptionDetailsDTO.StudntId          = (int)studentInLesson.StudentId;
                        studentInSubscriptionDetailsDTO.LessonId          = (int)studentInLesson.LessonId;
                        Lesson lesson = new Lesson();
                        lesson = GSDE.Lessons.Where(x => x.Id == studentInSubscriptionDetailsDTO.LessonId).FirstOrDefault();
                        studentInSubscriptionDetailsDTO.LessonName        = lesson.Name;
                        studentInSubscriptionDetailsDTO.LessonDay         = lesson.Day;
                        studentInSubscriptionDetailsDTO.LessonStartHower  = lesson.StartHower;
                        studentInSubscriptionDetailsDTO.LessonFinishHower = lesson.FinishHower;
                        studentInSubscriptionDetailsDTO.Attendance        = studentInLesson.Attendance;
                        studentInSubscriptionDetailsDTO.SubscriptionId    = (int)GSDE.StudentInSubscriptions.Where(x => x.Id == studentInLesson.StudentInSubscriptionId).First().SubscribtionId;
                        Subscription subscription = new Subscription();
                        subscription = GSDE.Subscriptions.Where(x => x.Id == studentInSubscriptionDetailsDTO.SubscriptionId).First();
                        studentInSubscriptionDetailsDTO.SubscriptionName = subscription.Name;
                        studentInSubscriptionDetailsDTO.PaymentId        = (int)studentInLesson.PaymentId;
                        Payment payment = new Payment();
                        payment = GSDE.Payments.Where(x => x.Id == studentInSubscriptionDetailsDTO.PaymentId).First();
                        studentInSubscriptionDetailsDTO.PaymentStartDate  = payment.StartDate.ToString();
                        studentInSubscriptionDetailsDTO.PaymentFinishDate = payment.FinishDate.ToString();
                        studentInSubscriptionDetailsDTO.PaymentSum        = payment.Sum;
                        Student student = new Student();
                        student = GSDE.Students.Where(x => x.Id == studentId).First();
                        studentInSubscriptionDetailsDTO.Balance = student.Balance;
                        StudentInSubscriptionDetailsDTOList.Add(studentInSubscriptionDetailsDTO);
                    }
                }
            }
            return(StudentInSubscriptionDetailsDTOList);
        }
예제 #9
0
        public static bool EditSubscription(int CurrentStudentInSubscriptionId, int SubscriptionId)
        {
            try
            {
                using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
                {
                    StudentInSubscription lastStudentInSubscription = new StudentInSubscription();
                    lastStudentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.Id == CurrentStudentInSubscriptionId).FirstOrDefault();
                    if (lastStudentInSubscription == default)
                    {
                        return(false);
                    }



                    lastStudentInSubscription.Stop = true;
                    GSDE.SaveChanges();



                    StudentInSubscription newStudentInSubscription = new StudentInSubscription();
                    newStudentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.StartDate == lastStudentInSubscription.FinishDate).FirstOrDefault();


                    if (newStudentInSubscription == default)
                    {
                        return(false);
                    }
                    while (newStudentInSubscription != default)
                    {
                        if (newStudentInSubscription.SubscribtionId != SubscriptionId)
                        {
                            //החזרת תשלום על שיעורים מוחלפים

                            Payment      payment          = new Payment();
                            Subscription LastSubscription = GSDE.Subscriptions.Where(x => x.Id == newStudentInSubscription.SubscribtionId).FirstOrDefault();
                            if (LastSubscription == default)
                            {
                                return(false);
                            }
                            List <StudentInLesson> LeststudentInLessonList = new List <StudentInLesson>();
                            var LessonsToRemove = GSDE.StudentInLessons.Where(x => x.StudentInSubscriptionId == newStudentInSubscription.Id);
                            for (int i = 0; i < LessonsToRemove.Count(); i++)
                            {
                                payment.Balance += LastSubscription.Price / (LastSubscription.WeeksNum * LastSubscription.DaysInWeekNum);
                                GSDE.SaveChanges();
                            }

                            Student student = new Student();
                            student = GSDE.Students.Where(x => x.Id == lastStudentInSubscription.StudentId).FirstOrDefault();
                            if (student == default)
                            {
                                return(false);
                            }
                            student.Balance += LastSubscription.Price;
                            GSDE.SaveChanges();

                            Subscription subscription = new Subscription();
                            subscription = GSDE.Subscriptions.Where(x => x.Id == SubscriptionId).FirstOrDefault();
                            if (subscription == default || subscription.WeeksNum == default)
                            {
                                return(false);
                            }
                            newStudentInSubscription.FinishDate = ((DateTime)newStudentInSubscription.StartDate).AddDays(7 * (int)subscription.WeeksNum);
                            newStudentInSubscription.Stop       = false;
                            GSDE.SaveChanges();

                            //מחיקת שיעורים קודמים

                            GSDE.StudentInLessons.ToList().RemoveAll(x => LessonsToRemove.Contains(x));
                            GSDE.SaveChanges();
                            newStudentInSubscription.SubscribtionId = SubscriptionId;
                            GSDE.SaveChanges();
                        }
                        // else if (newStudentInSubscription.StudentInLessons.Where(x => x.LessonId == LessonId) != default) return true;

                        //יצירת שיעורים למנוי של תלמיד
                        // StudentInLessonFunction.CreateLessonListBySubscriptionIdLessonIdStudentInSubscriptionId((int)newStudentInSubscription.StudentId, LessonId, (DateTime)newStudentInSubscription.StartDate, (int)newStudentInSubscription.SubscribtionId, newStudentInSubscription.Id);


                        newStudentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.StartDate == newStudentInSubscription.FinishDate).FirstOrDefault();
                    }

                    return(true);
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
예제 #10
0
        public static bool AddStudentInSubscriptionUpToNow(int studentId)
        {
            try
            {
                using (Gymnastics_Studio_DataEntities GSDE = new Gymnastics_Studio_DataEntities())
                {
                    StudentInSubscription studentInSubscription = new StudentInSubscription();
                    var i = GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentId && x.FinishDate <= DateTime.Now).Max(t => t.StartDate);
                    if (i == default)
                    {
                        return(false);
                    }
                    studentInSubscription = GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentId && x.StartDate == i).First();


                    WeekFunction.AddWeeksUpTo(DateTime.Now);



                    // הוספת מנוי לתלמידה עד לתאריך נוכחי אלא אם עצרה מנוי או קבעה מנויי אחר
                    DateTime     StartDate    = (DateTime)studentInSubscription.FinishDate;
                    Subscription Subscription = new Subscription();
                    Subscription = GSDE.Subscriptions.Where(x => x.Id == studentInSubscription.SubscribtionId).FirstOrDefault();
                    var      n          = (double)((int)Subscription.WeeksNum * 7);
                    DateTime FinishDate = StartDate.AddDays(n);
                    while (studentInSubscription.Stop == false && studentInSubscription.FinishDate <= DateTime.Now &&
                           (GSDE.StudentInSubscriptions.Where(x => x.StudentId == studentInSubscription.StudentId &&
                                                              ((x.StartDate <= StartDate && x.FinishDate > StartDate) ||
                                                               (x.StartDate > StartDate && x.StartDate < FinishDate)))
                            .FirstOrDefault() == default))
                    {
                        StudentInSubscription newStudentInSubscription = new StudentInSubscription();
                        newStudentInSubscription.StudentId      = studentId;
                        newStudentInSubscription.SubscribtionId = studentInSubscription.SubscribtionId;
                        newStudentInSubscription.StartDate      = studentInSubscription.FinishDate;
                        Subscription subscription = new Subscription();
                        subscription = GSDE.Subscriptions.Where(x => x.Id == newStudentInSubscription.StudentId).First();
                        newStudentInSubscription.FinishDate = ((DateTime)newStudentInSubscription.StartDate).AddDays((double)(7 * (int)subscription.WeeksNum));
                        newStudentInSubscription.Stop       = false;
                        GSDE.StudentInSubscriptions.Add(newStudentInSubscription);
                        GSDE.SaveChanges();
                        newStudentInSubscription = GSDE.StudentInSubscriptions.OrderByDescending(o => o.Id).FirstOrDefault();

                        //הוספת שיעורים לתלמידה עד לתאריך נוכחי
                        List <StudentInLesson> studentInLessonList = new List <StudentInLesson>();
                        studentInLessonList = GSDE.StudentInLessons.Where(x => x.StudentInSubscriptionId == studentInSubscription.Id).ToList()
                                              .OrderByDescending(o => o.Date).Take((int)subscription.DaysInWeekNum).ToList();

                        foreach (StudentInLesson studentInLesson in studentInLessonList)
                        {
                            for (int j = 0; j < subscription.WeeksNum; j++)
                            {
                                StudentInLesson newStudentInLessson = new StudentInLesson();
                                newStudentInLessson.StudentId  = studentId;
                                newStudentInLessson.LessonId   = studentInLesson.LessonId;
                                newStudentInLessson.Attendance = true;
                                newStudentInLessson.Date       = ((DateTime)studentInLesson.Date).AddDays(7 * (j + 1));
                                newStudentInLessson.StudentInSubscriptionId = newStudentInSubscription.Id;
                                var date2 = ((DateTime)newStudentInLessson.Date).AddDays(-7);
                                newStudentInLessson.WeekId = GSDE.Weeks
                                                             .Where(x => x.Date <= newStudentInLessson.Date && x.Date >= date2).First().Id;
                                Student student = new Student();
                                student = GSDE.Students.Where(x => x.Id == studentId).First();


                                //טיפול בתשלום השיעור
                                Payment payment       = new Payment();
                                int     lessonPayment = (int)(subscription.Price / (subscription.WeeksNum * subscription.DaysInWeekNum));
                                student.Balance -= lessonPayment;
                                while (lessonPayment > 0)
                                {
                                    payment = GSDE.Payments.Where(x => x.StudentId == studentId && x.FinishDate == default).ToList().OrderBy(o => o.StartDate).FirstOrDefault();
                                    switch (payment)
                                    {
                                    case null:
                                        payment               = new Payment();
                                        payment.StartDate     = DateTime.Now;
                                        payment.StudentId     = studentId;
                                        payment.Sum           = 0 - lessonPayment;
                                        payment.FormOfPayment = "חוב";
                                        payment.Balance       = 0 - lessonPayment;
                                        GSDE.Payments.Add(payment);
                                        GSDE.SaveChanges();
                                        payment = GSDE.Payments.Where(x => x.StudentId == studentId && x.FinishDate == default).FirstOrDefault();
                                        newStudentInLessson.PaymentId = payment.Id;
                                        break;

                                    default:
                                        if (payment.Balance > 0 && payment.Balance < lessonPayment)
                                        {
                                            lessonPayment      = (int)(lessonPayment - payment.Balance);
                                            payment.Balance    = 0;
                                            payment.FinishDate = DateTime.Now;
                                            GSDE.SaveChanges();
                                        }
                                        else
                                        {
                                            payment.Balance = payment.Balance - lessonPayment;
                                            lessonPayment   = 0;
                                            GSDE.SaveChanges();
                                            newStudentInLessson.PaymentId = payment.Id;
                                        }
                                        break;
                                    }
                                }


                                studentInSubscription = newStudentInSubscription;
                                StartDate             = (DateTime)studentInSubscription.FinishDate;
                                n          = (double)((int)Subscription.WeeksNum * 7);
                                FinishDate = StartDate.AddDays(n);

                                GSDE.StudentInLessons.Add(newStudentInLessson);
                                GSDE.SaveChanges();
                            }
                        }
                    }
                    return(true);
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }