protected RelatorioInatividadeViewModel getInatividades(RelatorioInatividadeViewModel view)
        {
            IQueryable<Apolice> apolices = apolicesRepository.All.Include("entidade").Include("eventoHistorico").Include("veiculo").Include("Tomador")
                         .Where(a => a.dataReporte < view.dataInatividade);

            if (view.entidadeId != null)
                apolices = apolices.Where(a => a.entidadeId == view.entidadeId);

            view.totalNumberOfRecords = apolices.Count();

            if (view.PageSize == 0)
                view.PageSize = view.totalNumberOfRecords;

            IPagedList<Apolice> apolicesToView = null;
            switch (view.SortColumn)
            {
                case "Apolice":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.numeroApolice).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.numeroApolice).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                case "Seguradora":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.entidade.nome).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.entidade.nome).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                case "Matricula":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.veiculo.numeroMatricula).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.veiculo.numeroMatricula).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                case "DataInicio":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.dataInicio).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.dataInicio).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                case "DataFim":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.dataFim).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.dataFim).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                case "DataUltimoReporte":
                    if (view.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesToView = apolices.OrderByDescending(s => s.dataReporte).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    else
                        apolicesToView = apolices.OrderBy(s => s.dataReporte).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
                default:
                    apolicesToView = apolices.OrderBy(s => s.dataReporte).ToPagedList(view.CurrentPageNumber, view.PageSize);
                    break;
            }
            List<RegistoInatividadeView> registosInativos = new List<RegistoInatividadeView>();

            foreach (Apolice a in apolicesToView)
            {
                RegistoInatividadeView registo = new RegistoInatividadeView
                {
                    dataInicio = a.dataInicio,
                    dataFim = a.dataFim,
                    registoId = a.apoliceId,
                    dataUltimoReporte = a.dataReporte,
                    matricula = a.veiculo.numeroMatricula,
                    numeroApolice = a.numeroApolice,
                    operacao = a.eventoHistorico.codigoOperacao.valor,
                    seguradora = a.entidade.nome
                };

                registosInativos.Add(registo);
            }

            view.resultado = registosInativos;

            return view;
        }
        public ActionResult exportInatividadesToCsv(RelatorioInatividadeViewModel viewModel, string sort, string direction, string tabNr)
        {
            if (tabNr == null || tabNr == "")
                tabNr = "1";

            viewModel.SetParameters(int.Parse(tabNr), 1, sort, direction, Request.Params);

            viewModel.PageSize = 0;
            RelatorioInatividadeViewModel result = getInatividades(viewModel);

            List<RegistoInatividadeToCsv> listRegistos = new List<RegistoInatividadeToCsv>();
            foreach(var item in viewModel.resultado)
            {
                listRegistos.Add(new RegistoInatividadeToCsv(){ matricula = item.matricula,
                                                                numeroApolice = item.numeroApolice,
                                                                operacao = item.operacao,
                                                                seguradora = item.seguradora,
                                                                dataUltimoReporte = item.dataUltimoReporte,
                                                                dataInicio = item.dataInicio, 
                                                                dataFim = item.dataFim});
            }

            CsvExport<RegistoInatividadeToCsv> csv = new CsvExport<RegistoInatividadeToCsv>(listRegistos);

            byte[] fileBytes = csv.ExportToBytes();
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "RelatorioIncumprimentos_" + DateTime.Now.ToShortDateString() + ".csv");
        }
        public ActionResult RelatorioInatividade(RelatorioInatividadeViewModel view, string sort, string direction, int? page, string tabNr)
        {
            if (ModelState.IsValid)
            {
                if (tabNr == null || tabNr == "")
                    tabNr = "1";

                view.SetParameters(int.Parse(tabNr), page == null ? 1 : page.Value, sort, direction, Request.Params);


                if (view.dataInatividade != default(DateTime))
                {
                    view = getInatividades(view);
                }
                else
                {
                    if (view.dataInatividade == default(DateTime))
                        view.dataInatividade = new DateTime(DateTime.Now.Year - 1, DateTime.Now.Month, DateTime.Now.Day);
                }
            }

            if (view.dataInatividade == default(DateTime))
                view.dataInatividade = new DateTime(DateTime.Now.Year - 1, DateTime.Now.Month, DateTime.Now.Day);

            if (view.entidadeId > 0)
            {
                this.ViewBag.entidadeId = new SelectList(entidadesRepository.All, "entidadeId", "nome", view.entidadeId);
            }
            else
            {
                this.ViewBag.entidadeId = new SelectList(entidadesRepository.All, "entidadeId", "nome");
            }

            return this.View(view);
        }