/// <summary> /// add Test to the DataBase /// </summary> /// <param name="test"></param> public void AddTest(BE.Test test) { if (test.Address == null || test.Address == "" || test.TraineeID == null || test.TraineeID == "") { throw new Exception("חובה למלא את כל השדות"); } BE.Trainee trainee = IDAL.GetTraineeCopy(test.TraineeID); if (trainee == null) { throw new KeyNotFoundException("לא נמצא תלמיד שמספרו " + test.TraineeID); } if (trainee.NumOfDrivingLessons < BE.Configuration.MinimumDrivingLessons) { throw new Exception("אין אפשרות להוסיף מבחן לתלמיד שעשה פחות מ-." + BE.Configuration.MinimumDrivingLessons + " שיעורים."); } BE.Test LastPreviusTest = null, FirstNextTest = null; foreach (var item in IDAL.GetAllTests(t => t.TraineeID == test.TraineeID)) { if (item.Time < test.Time && (LastPreviusTest == null || LastPreviusTest.Time < item.Time)) { LastPreviusTest = item; } else if (item.Time >= test.Time && (FirstNextTest == null || LastPreviusTest.Time < item.Time)) { FirstNextTest = item; } } if (LastPreviusTest != null && (test.Time - LastPreviusTest.Time).Days < BE.Configuration.MinimumDaysBetweenTests || FirstNextTest != null && (FirstNextTest.Time - test.Time).Days < BE.Configuration.MinimumDaysBetweenTests) { throw new Exception("לתלמיד זה קיים מבחן בהפרש של פחות משבעה ימים."); } if (test.Time < DateTime.Now) { throw new Exception("מועד הטסט חלף"); } if (test.Time != NextWorkTime(test.Time)) { throw new Exception("מועד הטסט מחוץ לשעות העבודה. \nשעות העבודה בימי השבוע הם: " + BE.Configuration.WorkStartHour + " עד " + BE.Configuration.WorkEndHour); } BE.Tester tester = (from item in GetAllTesters(test.Time) where item.Vehicle == trainee.Vehicle && BE.Tools.Maps_DrivingDistance(item.Address, test.Address) < item.MaxDistanceInMeters && (!trainee.OnlyMyGender || item.Gender == trainee.Gender) && item.GearBoxType == trainee.GearBoxType && NumOfTestsInWeek(item, test.Time) < item.MaxTestsInWeek select item).FirstOrDefault(); DateTime time = test.Time; if (tester == null) { time = time.AddMinutes(-time.Minute); while (!(from item in GetAllTesters(time) where item.Vehicle == trainee.Vehicle && (!trainee.OnlyMyGender || item.Gender == trainee.Gender) && item.GearBoxType == trainee.GearBoxType && BE.Tools.Maps_DrivingDistance(item.Address, test.Address) < item.MaxDistanceInMeters select item).Any() && time.Subtract(DateTime.Now).TotalDays < 30) { time += new TimeSpan(0, 30, 0); time = NextWorkTime(time); } if (time.Subtract(DateTime.Now).TotalDays >= 30) { throw new Exception("הזמן המבוקש תפוס. לא קיים זמן פנוי בשלושת החודשים הקרובים."); } else { throw new Exception("הזמן המבוקש תפוס, אבל יש לנו זמן אחר להציע לך: " + time.Day + '/' + time.Month + '/' + time.Year + ' ' + time.Hour + ':' + time.Minute); } } test.TesterID = tester.ID; IDAL.AddTest(test); }
/// <summary> /// הוספת מבחן חדש לרשימת המבחנים, תוך בדיקת מקרי קיצון שונים /// </summary> /// <param name="mytest"></param> public void AddTest(Test mytest) { //בדיקה שהמורה קיים במערכת Trainee mytrainee = dal.SearchTrainee(mytest.TraineeId, mytest.TestTypeOfCar); if (mytrainee == null) { throw new Exception("ERROR - The trainee does not exist"); } //בדיקת תקינות של בוחן Tester mytester = SearchTester(mytest.TesterId); if (mytester == null) { throw new Exception("ERROR - The tester does not exist"); } //מציאת טסט אחרון של נבחן Test lastTest = FindLastTest(mytest.TraineeId, mytest.TestTypeOfCar); //אם נמצא מבחן אחרון על סוג רכב זה יש לוודא שעברו מספיק ימים בין המבחן האחרון למבחן החדש if (lastTest != null) { //תקינות התלמיד TimeSpan seven = mytest.DateTimeOfTest - lastTest.DateTimeOfTest; if (seven.TotalDays < 7) { throw new Exception("Not enough days has passed between the last test and the current test"); } // אם הטסט הקודם ללא תוצאה יש לעדכן את תוצאת הטסט האחרון if (lastTest.TestResult == PassOrFail.Nun) { throw new Exception("You need to update the result of the last test"); } //בדיקה האם התלמיד כבר עבר טסט על סוג רכב זה וישנה טעות במערכת if (lastTest.TestResult == PassOrFail.Pass) { throw new Exception("The trainee has already passed a test"); } } //בדיקת מספר שיעורי התלמיד - יש לוודא שאכן עשה לפחות את המינימום הנדרש if (mytrainee.TraineeNumOfDrivingLessons < Configuration.MINNumberOfLessons) { throw new Exception("The trainee did not do enough driving lessons to attend the test"); } //בדיקה האם התלמיד עבר מבחן נהיגה פנימי - טסט פנימי if (mytrainee.IfTraineePassedAnInternalTest == false) { throw new Exception("The trainee did not pass an Internal Test"); } // בדיקה האם סוג הרכב שעליו התלמיד למד וכעת נבחן זהים או לא if (mytrainee.TraineeLearingCar != mytest.TestTypeOfCar) { throw new Exception("The trainee and the type of car in test dont fit"); } //בדיקת תקינות תאריך הטסט DateTime date = mytest.DateTimeOfTest.Date; DateTime temp = DateTime.Now; TimeSpan cheakDate = date - temp; if (cheakDate.TotalDays <= 0) { throw new Exception("The date that you enterd already pass"); } if ((date.Day.ToString() == "Friday") || (date.Day.ToString() == "Saturday")) { throw new Exception("The date that you enterd is worng"); } bool flag = true; while (flag == true) { //מאגר בוחנים שעונים על 5 התנאים //התמחות, מקסימום טסטים, עובדים ופנויים IEnumerable <Tester> searchForATester = GetAllTesters(t => t.isActive == true && t.TesterSpecialization == mytest.TestTypeOfCar); List <Tester> mylist = searchForATester.ToList(); searchForATester = mylist.FindAll(t => cheakAll(mytester, mytrainee, mytest)); //בדיקה האם סוג התמחות הבוחן מתאים לסוג הרכב שעליו התלמיד נבחן bool specialization = mytester.TesterSpecialization == mytrainee.TraineeLearingCar; //הרשימה לא ריקה if (searchForATester.Count() != 0) { //בדיקה האם הטסטר שהוכנס למבחן עומד בכל התנאים ופנוי לטסט IEnumerable <Tester> testers = searchForATester.Where <Tester>(t => t.TesterId == mytest.TesterId); // הטסטר שלי לא ברשימה- והרשימה לא ריקה אז אחליף לתלמיד טסטר if (testers.Count() == 0) { //נחפש טסטר אחר למבחן בתאריך הנל - אם הרשימה שלי לא ריקה ניקח את הראשון Tester Atester = searchForATester.First <Tester>(); mytest.TesterId = Atester.TesterId; try { dal.AddTest(mytest); } catch (Exception e) { throw (e); } try { UpdateTesteravailability(mytest, Atester, mytest.DateTimeOfTest, mytest.DateTimeOfTest.Hour, true); } catch (Exception e) { throw (e); } flag = false; return; } //הטסטר הנוכחי נמצא פנוי וכו - ניתן להכניס את המבחן לרשימת המבחנים הכל תקין if ((testers.Count() == 1) && (testers.First() != null)) { try { dal.AddTest(mytest); } catch (Exception ex) { throw (ex); } try { UpdateTesteravailability(mytest, mytester, mytest.DateTimeOfTest, mytest.DateTimeOfTest.Hour, true); } catch (Exception e) { throw (e); } flag = false; return; } if (testers.Count() > 1) { throw new Exception("ERROR - The tester was put on the list more then once"); } } //אם רשימת הטסטרים שעונים על 4 התנאים - ריקה else { // בדיקה על מה הטסטר שלי נפל // אם התחום התמחות והמרחק בסדר אז אחפש לטסטר זה תאריך חדש if ((specialization == true) && (Distance(mytester, mytest.TestExitAddress) < mytester.MaxiDistanceFromAddress)) { // נחפש עבור טסטר נוכחי תאריך אחר למבחן // נחפש שעה אחרת ליום נוכחי, אם לא נמצא נחפש יום חדש לגמרי DateTime update = AnotherDateForTheTest(mytester, mytest, mytest); throw new Exception(update.ToString()); } // אם הרשימה ריקה וגם הטססטר שלי נפל על מרחק או התמחות מה שאומר שלעולם לא יוכל לבחון את התלמיד הנל // יש לבצע את כל התהליך מחדש אך הפעם לשנות את התאריך - או את שעתו או את יומו בהתאם למצב הנוכחי // ואת התאריך החדש לבדוק על שאר הטסטרים DateTime newDate = mytest.DateTimeOfTest.AddHours(1); if (newDate.Hour > 14) { if ((newDate.DayOfWeek.ToString() != "Friday") && (newDate.DayOfWeek.ToString() != "Saturday")) { newDate = new DateTime(mytest.DateTimeOfTest.Year, mytest.DateTimeOfTest.Month, mytest.DateTimeOfTest.Day + 1, 10, 00, 00); } if (newDate.DayOfWeek.ToString() == "Friday") { newDate = new DateTime(mytest.DateTimeOfTest.Year, mytest.DateTimeOfTest.Month, mytest.DateTimeOfTest.Day + 2, 10, 00, 00); } if (newDate.DayOfWeek.ToString() == "Saturday") { newDate = new DateTime(mytest.DateTimeOfTest.Year, mytest.DateTimeOfTest.Month, mytest.DateTimeOfTest.Day + 3, 10, 00, 00); } } else { newDate = new DateTime(mytest.DateTimeOfTest.Year, mytest.DateTimeOfTest.Month, mytest.DateTimeOfTest.Day, mytest.DateTimeOfTest.Hour + 1, 00, 00); } mytest.DateTimeOfTest = newDate; } } }