protected void btnSave_Click(object sender, EventArgs e) { foreach (RepeaterItem repeaterItemRouteByWay in rptRouteByWay.Items) { var routeId = ((HiddenField)repeaterItemRouteByWay.FindControl("hidRouteId")).Value; var routeIdInt = -1; try { routeIdInt = Int32.Parse(routeId); } catch { } var route = TransferRequestByDateBLL.RouteGetById(routeIdInt); if (route == null || route.Id <= 0) { ShowErrors("Route doesn't exist. Please try again"); return; } var rptBusType = (Repeater)repeaterItemRouteByWay.FindControl("rptBusType"); foreach (RepeaterItem repeaterItemBusType in rptBusType.Items) { var busTypeId = ((HiddenField)repeaterItemBusType.FindControl("hidBusTypeId")).Value; var busTypeIdInt = -1; try { busTypeIdInt = Int32.Parse(busTypeId); } catch { } var busType = TransferRequestByDateBLL.BusTypeGetById(busTypeIdInt); if (route == null || route.Id <= 0) { ShowErrors("Bus type doesn't exist. Please try again"); return; } var rptTransportBooking = (Repeater)repeaterItemBusType.FindControl("rptTransportBooking"); //Xóa hết các liên kết BookingBusByDate cũ var listBusByDate = TransferRequestByDateBLL.BusByDateGetAllByCriterion(Date, busType, route, route.Way) .Future().ToList(); ClearOldBookingBusByDate(listBusByDate); //-- foreach (RepeaterItem repeaterItemBooking in rptTransportBooking.Items) { var bookingId = ((HiddenField)repeaterItemBooking.FindControl("hidBookingId")).Value; var bookingIdInt = -1; try { bookingIdInt = Int32.Parse(bookingId); } catch { } var booking = TransferRequestByDateBLL.BookingGetById(bookingIdInt); if (booking == null || booking.Id <= 0) { ShowErrors("Booking doesn't exist. Please try again"); return; } var ddlGroup = (DropDownList)repeaterItemBooking.FindControl("ddlGroup"); var group = -1; try { group = Int32.Parse(ddlGroup.SelectedValue); } catch { } //Tạo các liên kết BookingBusByDate mới if (group != -1) { var busByDate = TransferRequestByDateBLL.BusByDateGetAllByCriterion(Date, busType, route, route.Way, group) .Future().ToList().FirstOrDefault(); var bookingBusByDate = new BookingBusByDate() { Booking = booking, BusByDate = busByDate, }; TransferRequestByDateBLL.BookingBusByDateSaveOrUpdate(bookingBusByDate); } //-- } } } ShowSuccess("Saved successfully"); Session["Redirect"] = true; Response.Redirect(Request.RawUrl); }
public void SaveGroup(Repeater repeater) { foreach (RepeaterItem repeaterItemBooking in repeater.Items) { var bookingId = ((HiddenField)repeaterItemBooking.FindControl("hidBookingId")).Value; var bookingIdInt = -1; try { bookingIdInt = Int32.Parse(bookingId); } catch { } var booking = TransferRequestByDateBLL.BookingGetById(bookingIdInt); if (booking == null || booking.Id <= 0) { ShowErrors("Booking doesn't exist. Please try again"); return; } var ddlGroup = (DropDownList)repeaterItemBooking.FindControl("ddlGroup"); var group = -1; try { group = Int32.Parse(ddlGroup.SelectedValue); } catch { } var listBusByDate = new List <BusByDate>(); if (group != -1) { listBusByDate = TransferRequestByDateBLL.BusByDateGetAllByCriterion(Date, BusType, Route, Way, group) .Future().ToList(); } var listBookingBusByDate = TransferRequestByDateBLL.BookingBusByDateGetAllByCriterion(booking) .Future().ToList() .Where(x => x.BusByDate.BusType.Id == BusType.Id) .Where(x => x.BusByDate.Route.Id == Route.Id && x.BusByDate.Route.Way == Way) .ToList(); if (listBookingBusByDate == null) { listBookingBusByDate = new List <BookingBusByDate>(); } for (int i = 0; i < listBusByDate.Count; i++) { var busByDate = listBusByDate[i]; var bookingBusByDate = new BookingBusByDate(); bookingBusByDate.Booking = booking; bookingBusByDate.BusByDate = busByDate; var addMore = true; for (int j = i; j < listBookingBusByDate.Count; j++) { bookingBusByDate = listBookingBusByDate[j]; bookingBusByDate.Booking = booking; bookingBusByDate.BusByDate = busByDate; TransferRequestByDateBLL.BookingBusByDateSaveOrUpdate(bookingBusByDate); addMore = false; } if (addMore) { TransferRequestByDateBLL.BookingBusByDateSaveOrUpdate(bookingBusByDate); } } var numberOfBookingBusByDateUnnecessary = listBookingBusByDate.Count - listBusByDate.Count; if (numberOfBookingBusByDateUnnecessary > 0) { for (int i = 0; i < numberOfBookingBusByDateUnnecessary; i++) { var bookingBusByDate = listBookingBusByDate.OrderByDescending(x => x.Id).ToList()[i]; TransferRequestByDateBLL.BookingBusByDateDelete(bookingBusByDate); } } } }
private void BusByDateCloneForRouteBackNextDay(BusByDate busByDate) { if (busByDate == null) { return; } var route = busByDate.Route; if (route.Way != "To") { return; } var routeBack = TransferRequestByDateBLL.RouteBackGetByRouteTo(busByDate.Route); var date = busByDate.Date; var tomorrow = date.HasValue ? date.Value.AddDays(1) : date; //Tạo bus cho chiều về vào ngày mai nếu được tạo BusByDate clonedBusByDateRouteBack = null; if (busByDate.Cloned) { clonedBusByDateRouteBack = busByDate.BusByDateRouteBackRef; } else { clonedBusByDateRouteBack = new BusByDate(); } if (clonedBusByDateRouteBack == null) { return; } clonedBusByDateRouteBack.BusType = busByDate.BusType; clonedBusByDateRouteBack.Date = tomorrow; clonedBusByDateRouteBack.Group = busByDate.Group; clonedBusByDateRouteBack.Guide = busByDate.Guide; clonedBusByDateRouteBack.Route = routeBack; //Xóa hết các liên kết bookingbusbydate cũ của busByDateRouteBack var listBookingBusByDate = TransferRequestByDateBLL .BookingBusByDateGetAllByCriterion(clonedBusByDateRouteBack).Future().ToList(); listBookingBusByDate.ForEach(x => { TransferRequestByDateBLL.BookingBusByDateDelete(x); }); //-- TransferRequestByDateBLL.BusByDateSaveOrUpdate(clonedBusByDateRouteBack); busByDate.BusByDateRouteBackRef = clonedBusByDateRouteBack; busByDate.Cloned = true; TransferRequestByDateBLL.BusByDateSaveOrUpdate(busByDate); //Tìm các booking có chiều về vào ngày mai của bus hôm nay var listBookingNeedTransferBackOnTomorrow = busByDate.ListBookingBusByDate .Select(x => x.Booking).Where(x => x.Transfer_DateBack == tomorrow).ToList(); listBookingNeedTransferBackOnTomorrow.ForEach(booking => { //Gắn booking chiều về vào bus chiều về TransferRequestByDateBLL.BookingBusByDateSaveOrUpdate(new BookingBusByDate() { Booking = booking, BusByDate = clonedBusByDateRouteBack, }); //-- }); //-- }