package com.arrebol.taxiservicios.controller.stocktaking;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexInStocktakingView;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
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 Carlos Janitzio Zavala López.
 */
public class KardexInStocktakingController extends ConnectionManager implements Serializable {

    /**
     *
     * @param starDate
     * @param endDate
     * @return
     */
    public List<KardexInStocktakingView> fillDatatable(Date starDate, Date endDate) {
        LOG.info("Searching all kardex In..");

        List<KardexInStocktakingView> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT exp FROM KardexInStocktakingView exp "
                    + "WHERE DATE(exp.fecha) BETWEEN DATE(:starDate) AND DATE(:endDate) "
                    + "ORDER BY DATE(exp.fecha) DESC";

            result = session.createQuery(query)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .getResultList();

            transaction.commit();
            LOG.info("Total of kardex in found for datatable: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            LOG.error("Can not load list of kardex in for datstable ", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method fillDatatable of kardex in", e);
            rollback(transaction);
        }

        return result;
    }

    public List<KardexInStocktakingView> fillDatatableByProvider(Date starDate, Date endDate, String providerId) {
        LOG.info("Searching all kardex In..");

        List<KardexInStocktakingView> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT exp FROM KardexInStocktakingView exp "
                    + "WHERE DATE(exp.fecha) BETWEEN DATE(:starDate) AND DATE(:endDate) AND exp.providerId = :providerId "
                    + "ORDER BY DATE(exp.fecha) DESC";

            result = session.createQuery(query)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .setParameter("providerId", providerId)
                    .getResultList();

            transaction.commit();
            LOG.info("Total of kardex in found for datatable: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            LOG.error("Can not load list of kardex in for datstable ", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method fillDatatableByProvider of kardex in", e);
            rollback(transaction);
        }

        return result;
    }

    public List<KardexInStocktakingView> fillDatatableByProduct(Date starDate, Date endDate, String productId) {
        LOG.info("Searching all kardex In..");

        List<KardexInStocktakingView> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT exp FROM KardexInStocktakingView exp "
                    + "WHERE DATE(exp.fecha) BETWEEN DATE(:starDate) AND DATE(:endDate) AND exp.productId = :productId "
                    + "ORDER BY DATE(exp.fecha) DESC";

            result = session.createQuery(query)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .setParameter("productId", productId)
                    .getResultList();

            transaction.commit();
            LOG.info("Total of kardex in found for datatable: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            LOG.error("Can not load list of kardex in for datstable ", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method fillDatatableByProduct of kardex in", e);
            rollback(transaction);
        }

        return result;
    }

    public List<KardexInStocktakingView> fillDatatableByProductByProvider(Date starDate, Date endDate, String providerId, String productId) {
        LOG.info("Searching all kardex In..");

        List<KardexInStocktakingView> result = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT exp FROM KardexInStocktakingView exp "
                    + "WHERE DATE(exp.fecha) BETWEEN DATE(:starDate) AND DATE(:endDate) AND exp.productId = :productId AND exp.providerId = :providerId "
                    + "ORDER BY DATE(exp.fecha) DESC";

            result = session.createQuery(query)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .setParameter("productId", productId)
                    .setParameter("providerId", providerId)
                    .getResultList();

            transaction.commit();
            LOG.info("Total of kardex in found for datatable: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            LOG.error("Can not load list of kardex in for datstable ", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method fillDatatableByProductByProvider of kardex in", e);
            rollback(transaction);
        }

        return result;
    }

    private static final long serialVersionUID = 3405798902357776578L;

    final Logger LOG = LogManager.getLogger(KardexInStocktakingController.class);

}
