com.demo.impl.PagoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.demo.impl.PagoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demo.impl;

import com.demo.dao.PagosDao;
import com.demo.dao.QuincenasDao;
import com.demo.model.CatQuincenas;
import com.demo.model.Pago;
import com.demo.model.TablaCuentasGestion;
import com.demo.util.HibernateUtil;
import com.demo.util.LogSistema;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ivan
 */
public class PagoImpl implements PagosDao, Serializable {

    @Override
    public List<Pago> pagosQuincena() {
        try {
            QuincenasDao dao = new QuincenasImpl();
            CatQuincenas catQuincenas = dao.ultimaQuincena();
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            List<Pago> pagos;
            pagos = session.createQuery("from Pago p where p.aprobado='Pendiente' or p.aprobado='En revision'")
                    .list();
            tx.commit();
            return pagos;
        } catch (HibernateException j) {
            LogSistema
                    .guardarlog(this.getClass().getName() + " Method:pagoQuincena , Exception: " + j.getMessage());
            return new ArrayList<>();
        }
    }

    @Override
    public boolean actupago(Pago p) {
        Session sess = null;
        String sql = "";
        try {
            sess = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = sess.beginTransaction();
            //String sql = "update pago p set p.`RevCantidad`=" + p.getRevCantidad() + ", p.`Aprobado_Corp`=" + p.getAprobadoCorp() + ",  p.`Aprobado`='" + p.getRevAprovado() + "', p.`Rev_Aprovado`='" + p.getRevAprovado() + "',  p.`Rev_Observaciones`='" + p.getRevObservaciones() + "' , p.`RevFecha`='"+dateToMySQLDate(p.getRevFecha())+"'  , p.`Cat_Quincenas_Quincena_Clv`="+p.getCatQuincenas().getQuincenaClv()+"  where p.`idPago`=" + p.getIdPago() + ";";            
            if (p.getRevFecha() != null)
                sql = "UPDATE pago SET `Aprobado`='" + p.getAprobado() + "', `RevCantidad`='" + p.getRevCantidad()
                        + "', `RevFecha`='" + dateToMySQLDate(p.getRevFecha()) + "', `Rev_Observaciones`='"
                        + p.getRevObservaciones() + "', `Rev_Aprovado`='" + p.getRevAprovado()
                        + "', `Cat_Quincenas_Quincena_Clv`='" + p.getCatQuincenas().getQuincenaClv()
                        + "', `Aprobado_Corp`='" + p.getAprobadoCorp() + "', `Motivo`='" + p.getMotivo()
                        + "' WHERE `idPago`='" + p.getIdPago() + "';";
            else
                sql = "UPDATE pago SET `Aprobado`='" + p.getAprobado() + "', `RevCantidad`='" + p.getRevCantidad()
                        + "', `Rev_Observaciones`='" + p.getRevObservaciones() + "', `Rev_Aprovado`='"
                        + p.getRevAprovado() + "', `Cat_Quincenas_Quincena_Clv`='"
                        + p.getCatQuincenas().getQuincenaClv() + "', `Aprobado_Corp`='" + p.getAprobadoCorp()
                        + "', `Motivo`='" + p.getMotivo() + "' WHERE `idPago`='" + p.getIdPago() + "';";

            System.out.println("CONSULTA UPDATE PAGO" + sql);
            System.out.println("PAGO ID = " + p.getIdPago());
            System.out.println("FORMA PAGO =" + p.getFormaPago());
            //sess.update(p);
            int resultado = sess.createSQLQuery(sql).executeUpdate();
            System.out.println("RESULTADO " + resultado);
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }

            return true;
        } catch (HibernateException he) {
            LogSistema.guardarlog(this.getClass().getName() + " Method:actuPago , Exception: " + he.getMessage());
            return false;
        }
    }

    @Override
    public int numeroDePagos(TablaCuentasGestion credito) {
        int numPagos = 0;
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();

        try {

            switch (credito.getTipoCredito()) {
            case 1:
                String consulta1 = "select count(*) from Pago a join Convenio b where a.Convenio_idConvenio1 = b.idConvenio and b.idConvenio and b.Credito_Auto_CreditoAut = '"
                        + credito.getFolio() + "';";
                numPagos = Integer.parseInt(sess.createSQLQuery(consulta1).uniqueResult().toString());

                break;
            case 2:
                String consulta2 = "select count(*) from Pago a join Convenio b where a.Convenio_idConvenio1 = b.idConvenio and b.Credito_Fianzas_NumContratoFZ = '"
                        + credito.getFolio() + "';";
                numPagos = Integer.parseInt(sess.createSQLQuery(consulta2).uniqueResult().toString());

                break;
            case 3:
                String consulta3 = "select count(*) from Pago a join Convenio b where a.Convenio_idConvenio1 = b.idConvenio and b.Credito_Hipotecario_CreditoHip = '"
                        + credito.getFolio() + "';";
                numPagos = Integer.parseInt(sess.createSQLQuery(consulta3).uniqueResult().toString());

                break;
            case 4:
                String consulta4 = "select count(*) from Pago a join Convenio b where a.Convenio_idConvenio1 = b.idConvenio and b.Credito_SF_LT_NT_CT_CreditosII = '"
                        + credito.getFolio() + "';";
                numPagos = Integer.parseInt(sess.createSQLQuery(consulta4).uniqueResult().toString());

                break;
            }

            tx.commit();
            return numPagos;

        } catch (HibernateException he) {
            tx.rollback();
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method:numeroDePagos , Exception: " + he.getMessage());
            return numPagos;
        }
    }

    @Override
    public List<Pago> pagosQuincena(int cvlQuincena, int clvGestor) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            List<Pago> pagos;
            String consulta;
            if (clvGestor != -1) {
                consulta = "from Pago p where p.catQuincenas.quincenaClv=" + cvlQuincena
                        + " and p.catGestores.catGestorClv=" + clvGestor + "";
            } else {
                consulta = "from Pago p where p.catQuincenas.quincenaClv=" + cvlQuincena;
            }

            pagos = session.createQuery(consulta).list();
            tx.commit();
            return pagos;
        } catch (HibernateException j) {
            LogSistema
                    .guardarlog(this.getClass().getName() + " Method:pagosQuincena , Exception: " + j.getMessage());
            return new ArrayList<>();
        }

    }

    @Override
    public List<Pago> pagosFiltroProductos(Date inicio, Date Final, String Producto, int Gestor) {

        String Finicio = dateToMySQLDate(inicio);
        String Ffinal = dateToMySQLDate(Final);
        Session session;

        List<Pago> pagos = new ArrayList<>();
        String consulta = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();

            if (Producto.equals("Todos") && Gestor != 0) {
                consulta = "select p.* from Pago p, Convenio c, Credito_SF_LT_NT_CT cl where \n"
                        + "p.`RevFecha` between '" + Finicio + "' and  '" + Ffinal + "'  and \n"
                        + "p.Convenio_idConvenio1=c.idConvenio and  c.Credito_SF_LT_NT_CT_CreditosII=cl.CreditosII and \n"
                        + " cl.Cat_Gestores_Cat_Gestor_clv=" + Gestor
                        + " and (p.Aprobado='Aprobado' or p.Aprobado='Pendiente' or p.Aprobado='En revision');";
            } else if (Producto.equals("Todos") && Gestor == 0) {
                consulta = "select p.* from Pago p, Convenio c, Credito_SF_LT_NT_CT cl where \n"
                        + "p.`RevFecha` between '" + Finicio + "' and  '" + Ffinal + "'  and \n"
                        + "p.Convenio_idConvenio1=c.idConvenio and  c.Credito_SF_LT_NT_CT_CreditosII=cl.CreditosII and (p.Aprobado='Aprobado' or p.Aprobado='Pendiente' or p.Aprobado='En revision');";
            } else if (!Producto.equals("Todos") && Gestor != 0) {
                consulta = "select p.* from Pago p, Convenio c, Credito_SF_LT_NT_CT cl where \n"
                        + "p.`RevFecha` between '" + Finicio + "' and  '" + Ffinal + "'  and \n"
                        + "p.Convenio_idConvenio1=c.idConvenio and  c.Credito_SF_LT_NT_CT_CreditosII=cl.CreditosII and \n"
                        + "cl.Producto='" + Producto + "' and cl.Cat_Gestores_Cat_Gestor_clv=" + Gestor
                        + " and (p.Aprobado='Aprobado' or p.Aprobado='Pendiente' or p.Aprobado='En revision');";
            } else if (!Producto.equals("Todos") && Gestor == 0) {
                consulta = "select p.* from Pago p, Convenio c, Credito_SF_LT_NT_CT cl where \n"
                        + "p.`RevFecha` between '" + Finicio + "' and  '" + Ffinal + "'  and \n"
                        + "p.Convenio_idConvenio1=c.idConvenio and  c.Credito_SF_LT_NT_CT_CreditosII=cl.CreditosII and \n"
                        + "cl.Producto='" + Producto
                        + "' and (p.Aprobado='Aprobado' or p.Aprobado='Pendiente' or p.Aprobado='En revision');";
            }

            pagos = session.createSQLQuery(consulta).addEntity(Pago.class).list();
            tx.commit();

            return pagos;
        } catch (HibernateException j) {
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method:pagosFiltroProducto , Exception: " + j.getMessage());
            return pagos;
        }

    }

    public String dateToMySQLDate(Date fecha) {
        try {
            System.out.println("fecha revision metodo dateToMySQLDate" + fecha);
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("lo que devuelve " + sdf.format(fecha));
            return sdf.format(fecha);
        } catch (NullPointerException npe) {
            System.out.println("problemas al cnvertor fecha");
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method:dateToMySQLDate , Exception: " + npe.getMessage());
            return sdf.format(new Date());
        }
    }

    @Override
    public boolean eliminarPago(Pago p) {
        Session sess = null;
        try {
            sess = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = sess.beginTransaction();
            sess.delete(p);
            tx.commit();
            return true;
        } catch (HibernateException he) {
            LogSistema
                    .guardarlog(this.getClass().getName() + " Method:eliminarPago , Exception: " + he.getMessage());
            return false;
        }
    }

    @Override
    public List<Pago> pagosQuincenaGestor(int clvGestor) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            List<Pago> pagos;
            String consulta;
            consulta = "select p.* from pago p, convenio c where \n"
                    + "p.Cat_Quincenas_Quincena_Clv=(select MAX(cq.Quincena_Clv) FROM Cat_Quincenas cq)\n"
                    + "and (p.`Rev_Aprovado`=\"Aprobado\" or p.`Aprobado`=\"Aprobado\")\n"
                    + "and p.`Convenio_idConvenio1`=c.`idConvenio`\n" + "and c.`Cat_Gestores_Cat_Gestor_clv`="
                    + clvGestor + ";";

            pagos = session.createSQLQuery(consulta).addEntity(Pago.class).list();
            tx.commit();
            return pagos;
        } catch (HibernateException j) {
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method:pagosQuincenaGestor , Exception: " + j.getMessage());
            return new ArrayList<>();
        }
    }
}