public ActionResult TermCreate(CompanyTermVM model) { var user = User as MPrincipal; var login = user.UserDetails.Login; ViewBag.UserName = user.UserDetails.Login; ViewBag.UserRole = 4; if (ModelState.IsValid) { if (model.DateStop < model.DateStart) { TempData["alertMessage"] = "Data końca nie może być wcześniejsza od daty początku"; return(RedirectToAction("CompanyTermList")); } using (ApplicationDbContext db = new ApplicationDbContext()) { var company = db.Companies.SingleOrDefault(x => x.Login == login); if (company == null) { return(View(model)); } //sprawdzanie czy ten dzień nie ma już terminu IEnumerable <DateTime> dniTworzone = DateRangeToArray(model.DateStart, model.DateStop).ToList(); var termList = db.Terms.Where(t => t.CompanyId == company.Id).ToList(); foreach (var item in termList) { IEnumerable <DateTime> dateTimesZajete = DateRangeToArray(item.DateStart, item.DateStop).ToList(); foreach (var zajte in dateTimesZajete) { foreach (var tworzone in dniTworzone) { if (tworzone == zajte) { TempData["alertMessage"] = "W tworzonym terminie występują zaplanowane wcześniej dni, sprawdź inne terminy"; return(RedirectToAction("CompanyTermList")); } } } } var companyTerms = new Term() { CompanyId = company.Id, DateStart = model.DateStart, DateStop = model.DateStop, }; company.CompanyTerms.Add(companyTerms); db.SaveChanges(); } return(RedirectToAction("CompanyTermList")); } return(View(model)); }
public static List <CompanyTermVM> CompanyTermToList(int companyID) { List <CompanyTermVM> compTermsList = new List <CompanyTermVM>(); using (ApplicationDbContext db = new ApplicationDbContext()) { var company = db.Companies.SingleOrDefault(x => x.Id == companyID); var termList = db.Terms.Where(t => t.CompanyId == company.Id && t.IsDelete == false).OrderBy(x => x.DateStart).ToList(); foreach (var item in termList) { bool actual = false; if (item.DateStart > DateTime.Now || (item.DateStart < DateTime.Now && item.DateStop > DateTime.Now)) { actual = true; } var rez = db.Reservations.FirstOrDefault(x => x.TermId == item.Id); int reservationId = 0; bool acceptedRez = false; if (rez != null) { reservationId = rez.Id; acceptedRez = rez.CompanyAccept; } CompanyTermVM TermVM = new CompanyTermVM { ID = item.Id, DateStart = item.DateStart, DateStop = item.DateStop, Start = item.DateStart.ToShortDateString(), Stop = item.DateStop.ToShortDateString(), CustomerID = item.CustomerId, Actual = actual, ReservationId = reservationId, Accepted = acceptedRez }; compTermsList.Add(TermVM); } } return(compTermsList); }
public static CompanyInfoVM CompanyInfo(int companyId) { using (ApplicationDbContext db = new ApplicationDbContext()) { var company = db.Companies.SingleOrDefault(x => x.Id == companyId); List <Tag> tagList = new List <Tag>(); tagList = db.Tags.Where(t => t.CompanyTag.Any(c => c.CompanyId == company.Id)).ToList(); bool tagExist = false; if (tagList != null) { tagExist = true; } List <CompanyTermVM> compTermList = Controllers.TermController.CompanyTermToList(company.Id).Where(c => c.Actual == true && c.CustomerID == null).ToList(); CompanyTermVM nearestTerm = compTermList.FirstOrDefault(t => t.DateStart > DateTime.Now); string nearestFreeTerm = "Brak wolnych terminów"; if (nearestTerm != null) { nearestFreeTerm = nearestTerm.DateStart.ToShortDateString(); } List <Image> imageList = new List <Image>(); imageList = db.Images.Where(t => t.CompanyId == company.Id && t.IsDelete == false).ToList(); Image imageMain = new Image(); imageMain = db.Images.FirstOrDefault(i => i.CompanyId == company.Id && i.MainPicture == true); if (imageMain == null) { imageMain = new Image() { Link = "~/Content/images/brakZdj.jpg" }; } var opinionList = db.Opinions.Where(o => o.CompanyId == company.Id && o.IsDelete == false && o.AdminAccept == true).ToList(); List <OpinionVM> opinionListVM = new List <OpinionVM>(); List <int> gradeValues = new List <int>(); for (int i = 0; i < opinionList.Count(); i++) { var customName = opinionList[i].Reservation.Term.Customer.Name + " " + opinionList[i].Reservation.Term.Customer.Surname; OpinionVM opinionVM = new OpinionVM { Description = opinionList[i].Description, CustomerName = customName, GradeValue = opinionList[i].GradeValue }; opinionListVM.Add(opinionVM); gradeValues.Add(opinionList[i].GradeValue); } double average = 0; if (gradeValues.Count > 0) { average = gradeValues.Average(); } CompanyInfoVM companyInfoVM = new CompanyInfoVM() { IdCompany = company.Id, CityName = company.City.Name, CompanyName = company.CompanyName, Speciality = company.Speciality, Services = company.Services, Pricing = company.Pricing, PhoneNumer = company.PhoneNumer, CompanyTagList = tagList, CompanyTermVMList = compTermList, CompanyImageList = imageList, TagExist = tagExist, NearestFreeDate = nearestFreeTerm, CompanyMainImage = imageMain, CompanyOpinionList = opinionListVM, AverageRating = average, OpinionsCount = opinionListVM.Count() }; return(companyInfoVM); }; }