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);
            }
        }
Exemplo n.º 2
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);
            }
        }