public List <Proprietario> salvar(List <Proprietario> proprietario, string mes)
        {
            DateTime            data                   = Convert.ToDateTime(mes);
            Inadimplentes       inadimplentes          = new Inadimplentes();
            List <Proprietario> listaPropInadimplentes = new List <Proprietario>();

            // listaInadimplentes = db.Inadimplentes.ToList();

            foreach (var p1 in proprietario)
            {
                InadimplentesController inadimplenteController = new InadimplentesController();
                if (inadimplenteController.buscarInadimplente(p1, mes) == null)
                {
                    inadimplentes.Proprietarioid = p1.UsuarioId;
                    inadimplentes.data           = data;
                    if (ModelState.IsValid)
                    {
                        listaPropInadimplentes.Add(p1);
                        db.Inadimplentes.Add(inadimplentes);
                        db.SaveChanges();
                    }
                }
            }
            return(listaPropInadimplentes);
        }
Exemplo n.º 2
0
        public ActionResult Import(HttpPostedFileBase excelfile, string mes, string ano)
        {
            double subtotal = 0.0;
            string mes1     = mes;
            MesAnoPagosController   MesAnoPago              = new MesAnoPagosController();
            ContasController        contaController         = new ContasController();
            InadimplentesController inadimplentesController = new InadimplentesController();
            List <Inadimplentes>    listaInadimplentes      = new List <Inadimplentes>();
            List <Contas>           contasDoMes             = new List <Contas>();
            ProprietarioController  proprietarioControler   = new ProprietarioController();
            Proprietario            prop = new Proprietario();
            List <Proprietario>     listaproprietario = new List <Proprietario>();


            int contador = 0;

            if (MesAnoPago.verificaMes(mes1))
            {
                if (excelfile == null || excelfile.ContentLength == 0)
                {
                    ViewBag.Error = "Por favor insira um arquivo .pdf<br>";
                    return(View("Index"));
                }
                else
                {
                    if (excelfile.FileName.EndsWith("xls") || (excelfile.FileName.EndsWith("xlsx")))
                    {
                        string path = Server.MapPath("~/Content/" + excelfile.FileName);
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }
                        excelfile.SaveAs(path);
                        Excel.Application application = new Excel.Application();
                        Excel.Workbook    workbook    = application.Workbooks.Open(path);
                        Excel.Worksheet   worksheet   = workbook.ActiveSheet;
                        Excel.Range       range       = worksheet.UsedRange;
                        List <Contas>     listaContas = new List <Contas>();

                        for (int row = 1; row < range.Rows.Count + 1; row++)
                        {
                            Inadimplentes inadimplente = new Inadimplentes();
                            Contas        c            = new Contas();
                            string        nome         = ((Excel.Range)range.Cells[row, 1]).Text;


                            if (nome != null)
                            {
                                Proprietario p = new Proprietario();
                                p = (Proprietario)proprietarioControler.BuscarPorNome(nome);
                                if (p != null)
                                {
                                    c.proprietarioid = p.UsuarioId;
                                    c.datatitulo     = Convert.ToDateTime(((Excel.Range)range.Cells[row, 2]).Text);
                                    c.datapagamento  = Convert.ToDateTime(((Excel.Range)range.Cells[row, 3]).Text);
                                    c.valor          = Convert.ToDouble(((Excel.Range)range.Cells[row, 4]).Text);
                                    c.fonte          = ((Excel.Range)range.Cells[row, 5]).Text;
                                    c.codigo         = ((Excel.Range)range.Cells[row, 6]).Text;
                                    c.lote           = ((Excel.Range)range.Cells[row, 7]).Text;
                                    c.pago           = ((Excel.Range)range.Cells[row, 8]).Text;
                                    // if (buscarContas(p, c) == null)
                                    // {



                                    if (ModelState.IsValid)
                                    {
                                        listaContas.Add(c);
                                        subtotal = subtotal + c.valor;
                                        listaproprietario.Add(p);
                                        inadimplentesController.deletarInadimplentes(c);
                                        db.Contas.Add(c);
                                        db.SaveChanges();
                                        contador++;
                                    }
                                    //}
                                }
                            }
                        }

                        List <Proprietario> listaProprietarios = new List <Proprietario>();

                        listaProprietarios = proprietarioControler.listarTodosOsProprietarios();

                        List <Proprietario> ListaDeInadimplentes = new List <Proprietario>();

                        // List<Proprietario> ListaDeProprietario = new List<Proprietario>();

                        listaProprietarios = listaDeInadimplentes(listaproprietario);

                        ListaDeInadimplentes = inadimplentesController.salvar(listaProprietarios, mes1);


                        ViewBag.subtotal           = subtotal;
                        ViewBag.ListaProprietario  = listaproprietario;
                        ViewBag.contador           = contador;
                        ViewBag.listarContas       = listaContas;
                        ViewBag.listaInadimplentes = ListaDeInadimplentes;
                        MesAnoPago.salvar(mes1);
                        return(View("Success"));
                    }
                    else
                    {
                        ViewBag.Error = "Inserir aquivos apenas EXCEL  <br>";
                        return(View("Index"));
                    }
                }
            }
            ViewBag.Error = "Mês já inserido  <br>";
            return(View("Index"));
        }