public HttpResponseMessage GetSchedule([FromUri] ScheduleParamsModel parameters, bool _search, string nd, int rows, int page, string sidx, string sord, string filters = "")
        {
            var items        = new List <ScheduleItemModel>();
            var totalRecords = 0;
            var linesIdsList = parameters.LinesIds != null
                ? parameters.LinesIds.Split(',').Select(int.Parse)
                : null;

            var dateFromDt = DateHelper.StringToDate(parameters.DateFrom);
            var dateToDt   = DateHelper.StringToDate(parameters.DateTo);

            using (var logic = new tblScheduleLogic())
            {
                items = logic.GetPaged(linesIdsList, dateFromDt, dateToDt, _search, rows, page, sidx, sord, filters)
                        .Select(z => new ScheduleItemModel(z)).ToList();
                totalRecords = logic.Schedule.Count();
            }
            return(Request.CreateResponse(
                       HttpStatusCode.OK,
                       new
            {
                total = (totalRecords + rows - 1) / rows,
                page,
                records = totalRecords,
                rows = items
            }));
        }
        public bool SaveGeneratedShcedule(IEnumerable <tblSchedule> schedule, DateTime dateFrom, DateTime dateTo)
        {
            using (var logic = new tblScheduleLogic())
            {
                var scheduleArr = schedule != null?schedule.ToArray() : new tblSchedule[0];

                var linesIds = scheduleArr
                               .Where(x => x.LineId.HasValue)
                               .Select(x => x.LineId.Value)
                               .Distinct();

                var itemsToDelete = logic.Schedule
                                    .Where(x => x.LineId.HasValue)
                                    .Where(x => linesIds.Any(id => id == x.LineId.Value) && x.Date >= dateFrom && x.Date <= dateTo);
                logic.DeleteItems(itemsToDelete);

                foreach (var item in scheduleArr)
                {
                    item.Line = null;
                    item.Bus  = null;
                    logic.SaveItem(item);
                }
            }
            return(true);
        }
 public JsonResult AutoCorrectLineWeekSchedules(int lineID, DateTime weekStart)
 {
     using (var l = new tblScheduleLogic()) {
         l.AutoCorrectLineSchedules(lineID, weekStart, weekStart.AddDays(7));
     }
     return(new JsonResult {
         Data = true
     });
 }
        public JsonResult EditItem(ScheduleItemModel model)
        {
            using (var logic = new tblScheduleLogic())
            {
                switch ((GridOperation)Enum.Parse(typeof(GridOperation), model.Oper, true))
                {
                case GridOperation.add:
                    logic.SaveItem(model.ToDbModel());
                    break;

                case GridOperation.edit:
                    logic.Update(model.ToDbModel());
                    break;

                case GridOperation.del:
                    logic.DeleteItem(model.Id);
                    break;
                }
            }
            return(new JsonResult {
                Data = true
            });
        }
        public JsonResult GetAvailableBuses(int?lineId = null, int?scheduleId = null)
        {
            var  buses        = new List <SelectItemModel>();
            Line scheduleLine = null;

            buses.Add(new SelectItemModel {
                Value = "0", Text = string.Empty, Title = string.Empty
            });
            if (scheduleId.HasValue)
            {
                using (var logic = new tblScheduleLogic())
                {
                    var scheduleItem = logic.GetItem(scheduleId.Value);
                    if (scheduleItem != null)
                    {
                        scheduleLine = scheduleItem.Line;
                    }
                }
            }
            var lineIdRes = scheduleLine != null ? scheduleLine.Id : lineId ?? 0;

            using (var logic = new LineLogic())
            {
                buses.AddRange(logic.GetAvailableBuses(lineIdRes)
                               .Select(z => new SelectItemModel
                {
                    Value    = z.Id.ToString(),
                    Text     = string.Format("{0} ({1} - {2})", z.Id, z.BusId, z.PlateNumber),
                    Title    = string.Format("{0} ({1} - {2} - {3} - {4})", z.Id, z.BusId, z.PlateNumber, z.BusCompany != null ? z.BusCompany.companyName : string.Empty, z.seats.HasValue ? z.seats.Value.ToString() : string.Empty),
                    Selected = z.BusesToLines.Any() && z.BusesToLines.First().LineId == lineIdRes
                }).ToList());
            }

            return(new JsonResult {
                Data = buses
            });
        }
        public JsonResult EditLine(GridLineModel model)
        {
            int LineIdToAutoCorrect = -1;

            using (var logic = new LineLogic()) {
                switch ((GridOperation)Enum.Parse(typeof(GridOperation), model.Oper, true))
                {
                case GridOperation.edit:
                    var existingLine = logic.GetLine(model.Id);
                    if (existingLine != null)
                    {
                        model.UpdateDbModel(existingLine);
                        logic.SaveChanges();
                        using (var busesToLinesLogic = new BusToLineLogic()) {
                            busesToLinesLogic.UpdateBusToLine(model.Id, model.Bus);
                        }
                        LineIdToAutoCorrect = existingLine.Id;
                    }
                    break;

                case GridOperation.del:
                    logic.DeleteLine(model.Id);
                    break;
                }
            }
            if (LineIdToAutoCorrect != -1)
            {
                var weekStart = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
                using (var l = new tblScheduleLogic()) {
                    l.AutoCorrectLineSchedules(LineIdToAutoCorrect, weekStart, weekStart.AddDays(7));
                }
            }
            return(new JsonResult {
                Data = true
            });
        }