/*
 * 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.stocktaking;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexView;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
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 KardexViewController extends ConnectionManager implements Serializable{
    
    private static final long serialVersionUID = 3405798902357776578L;
    final Logger logger = LogManager.getLogger(ReturnInvoiceController.class);

    public KardexViewController() {
    }
    
    public List<KardexView> findAllKardexView(String economicNumber, Date starDate, Date endDate) {
        logger.info("Searching all records in KardexView");

        List<KardexView> kardex = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            String query = "SELECT kar FROM KardexView kar "
                    + "WHERE DATE(kar.createdOn) BETWEEN DATE(:starDate) AND DATE(:endDate) "
                    + "AND kar.economicNumber LIKE :economicNumber "
                    + "ORDER BY DATE(kar.createdOn) DESC";
            kardex = session.createQuery(query)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .setParameter("economicNumber", economicNumber)
                    .getResultList();
            
            transaction.commit();
            logger.info("Total KardexView records found " + kardex.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Cannot load KardexView list", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method findAllKardexView", e);
        }

        return kardex;
    }
    
}
