/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.arrebol.taxiservicios.controller.predio;

import com.arrebol.taxiservicios.controller.admin.GenericPersonController;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.EstatusSolicitud;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import com.arrebol.taxiservicios.model.predio.AbonoLote;
import com.arrebol.taxiservicios.model.predio.DevolucionAbono;
import com.arrebol.taxiservicios.model.predio.DevolucionAbonoEntrada;
import com.arrebol.taxiservicios.model.predio.FoliosCancelados;
import com.arrebol.taxiservicios.model.predio.Lote;
import com.arrebol.taxiservicios.model.predio.SolicitudDevolucionAbono;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pc one
 */
public class DevolucionAbonoController extends GenericPersonController implements Serializable {

    private static final long serialVersionUID = -6265444716269535946L;

    final Logger logger = LogManager.getLogger(getClass().getName());

    public boolean addSolicitudDevolucionAbono(SolicitudDevolucionAbono solicitud, User user, boolean save) {
        logger.info("addSolicitudDevolucionAbono");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                solicitud.setId(UUID.randomUUID().toString());
                solicitud.setCreatedBy(user);
                solicitud.setCreatedOn(new Date());
                session.save(solicitud);
                transaction.commit();
                logger.info("Solicitud devolucion abono saved " + solicitud.getId());
            } else {
                solicitud.setLastUpdatedBy(user);
                solicitud.setLastUpdatedOn(new Date());
                session.update(solicitud);
                transaction.commit();
                logger.info("Solicitud devolucion abono updated " + solicitud.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("Solicitud devolucion abono can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addSolicitudDevolucionAbono() ", e);
            rollback(transaction);
        }
        return success;
    }

    public boolean addDevolucionAbono(DevolucionAbono devolucion, User user, boolean save) {
        logger.info("addDevolucionAbono");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                devolucion.setId(UUID.randomUUID().toString());
                devolucion.setCreatedBy(user);
                devolucion.setCreatedOn(new Date());
                devolucion.setLastUpdatedBy(user);
                devolucion.setLastUpdatedOn(new Date());
                session.save(devolucion);
                transaction.commit();
                logger.info("Devolucion abono saved " + devolucion.getId());
            } else {
                devolucion.setLastUpdatedBy(user);
                devolucion.setLastUpdatedOn(new Date());
                session.update(devolucion);
                transaction.commit();
                logger.info("Devolucion abono updated " + devolucion.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("Devolucion abono can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addDevolucionAbono() ", e);
            rollback(transaction);
        }
        return success;
    }

    public boolean addDevolucionAbonoEntrada(DevolucionAbonoEntrada devolucion, User user, boolean save) {
        logger.info("addDevolucionAbonoEntrada");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                devolucion.setId(UUID.randomUUID().toString());
                devolucion.setCreatedBy(user);
                devolucion.setCreatedOn(new Date());
                devolucion.setLastUpdatedBy(user);
                devolucion.setLastUpdatedOn(new Date());
                session.save(devolucion);
                transaction.commit();
                logger.info("Devolucion abono entrada saved " + devolucion.getId());
            } else {
                devolucion.setLastUpdatedBy(user);
                devolucion.setLastUpdatedOn(new Date());
                session.update(devolucion);
                transaction.commit();
                logger.info("Devolucion abono entrada updated " + devolucion.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("Devolucion abono entrada can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addDevolucionAbonoEntrada() ", e);
            rollback(transaction);
        }
        return success;
    }

    public List<SolicitudDevolucionAbono> fillAllSolicitudDevolucionAbonoByLocation(Location location) {
        logger.info("Searching all solicitud devolucion abono..");
        List<SolicitudDevolucionAbono> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<SolicitudDevolucionAbono> criteria = builder.createQuery(SolicitudDevolucionAbono.class);
            Root<SolicitudDevolucionAbono> root = criteria.from(SolicitudDevolucionAbono.class);

            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioLocation = builder.equal(root.get("location"), location);
            criteria.where(builder.and(criterioActivo, criterioLocation));

            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of solicitud devolucion abono found: " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Unable to load solicitud devolucion abono list ", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillAllSolicitudDevolucionAbono", e);
        }
        return result;
    }

    public List<SolicitudDevolucionAbono> fillAllSolicitudDevolucionAbonoByLocationAndEstatus(Location location, EstatusSolicitud estatus) {
        logger.info("Searching all solicitud devolucion abono..");
        List<SolicitudDevolucionAbono> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<SolicitudDevolucionAbono> criteria = builder.createQuery(SolicitudDevolucionAbono.class);
            Root<SolicitudDevolucionAbono> root = criteria.from(SolicitudDevolucionAbono.class);

            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioLocation = builder.equal(root.get("location"), location);
            Predicate criterioEstatus = builder.equal(root.get("estatusSolicitud"), estatus);
            criteria.where(builder.and(criterioActivo, criterioLocation, criterioEstatus));

            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of solicitud devolucion abono found: " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Unable to load solicitud devolucion abono list ", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillAllSolicitudDevolucionAbono", e);
        }
        return result;
    }
    
        public List<SolicitudDevolucionAbono> fillAllSolicitudDevolucionAbonoByLoteAndEstatus(Lote lote, EstatusSolicitud estatus) {
        logger.info("Searching all solicitud devolucion abono..");
        List<SolicitudDevolucionAbono> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<SolicitudDevolucionAbono> criteria = builder.createQuery(SolicitudDevolucionAbono.class);
            Root<SolicitudDevolucionAbono> root = criteria.from(SolicitudDevolucionAbono.class);

            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioLote = builder.equal(root.get("abonoLote").get("lote").get("id"), lote.getId());
            Predicate criterioEstatus = builder.equal(root.get("estatusSolicitud"), estatus);
            criteria.where(builder.and(criterioActivo, criterioLote, criterioEstatus));

            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of solicitud devolucion abono found: " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Unable to load solicitud devolucion abono list ", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillAllSolicitudDevolucionAbono", e);
        }
        return result;
    }

    public List<SolicitudDevolucionAbono> fillAllSolicitudDevolucionAbonoByLocationAndAbonoLoteAndEstatusSolicitud(Location location, AbonoLote abonoLote, EstatusSolicitud estatus) {
        logger.info("Searching all solicitud devolucion abono..");
        List<SolicitudDevolucionAbono> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<SolicitudDevolucionAbono> criteria = builder.createQuery(SolicitudDevolucionAbono.class);
            Root<SolicitudDevolucionAbono> root = criteria.from(SolicitudDevolucionAbono.class);

            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioLocation = builder.equal(root.get("location"), location);
            Predicate criterioAbonoLote = builder.equal(root.get("abonoLote"), abonoLote);
            Predicate criterioEstatusSolicitud = builder.equal(root.get("estatusSolicitud"), estatus);
            criteria.where(builder.and(criterioActivo, criterioLocation, criterioAbonoLote, criterioEstatusSolicitud));

            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of solicitud devolucion abono found: " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Unable to load solicitud devolucion abono list ", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillAllSolicitudDevolucionAbono", e);
        }
        return result;
    }

    public List<DevolucionAbono> fillAllDevolucionAbonoByLocation(Location location) {
        logger.info("Searching all devolucion abono..");
        List<DevolucionAbono> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<DevolucionAbono> criteria = builder.createQuery(DevolucionAbono.class);
            Root<DevolucionAbono> root = criteria.from(DevolucionAbono.class);

            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioLocation = builder.equal(root.get("location"), location);
            criteria.where(builder.and(criterioActivo, criterioLocation));

            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of devolucion abono found: " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Unable to load devolucion abono list ", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillAllDevolucionAbonoByLocation", e);
        }
        return result;
    }
    
    public boolean addFoliosCancelados(FoliosCancelados foliosCancelado, User user, boolean save) {
        logger.info("addFoliosCancelados");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                foliosCancelado.setId(UUID.randomUUID().toString());
                foliosCancelado.setCreatedBy(user);
                foliosCancelado.setCreatedOn(new Date());
                foliosCancelado.setLastUpdatedBy(user);
                foliosCancelado.setLastUpdatedOn(new Date());
                session.save(foliosCancelado);
                transaction.commit();
                logger.info("Folios cancelado saved " + foliosCancelado.getId());
            } else {
                foliosCancelado.setLastUpdatedBy(user);
                foliosCancelado.setLastUpdatedOn(new Date());
                session.update(foliosCancelado);
                transaction.commit();
                logger.info("Folios cancelado updated " + foliosCancelado.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("Devolucion abono can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addFoliosCancelados() ", e);
            rollback(transaction);
        }
        return success;
    }
    
}
