/*
 * 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.arrebol.taxiservicios.controller.sp.report;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.Location;
import java.io.Serializable;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;

/**
 *
 * @author Oscar
 */
public class SalesFeeExpectedPaymentController extends ConnectionManager implements Serializable{
    
    public Double getTotalSale(String locationId) {
        logger.info("getTotalSale");

        Double total = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String sqlQuery = "SELECT IF(ISNULL(SUM(s.pay_day + s.pay_saving + s.accident)),0,SUM(s.pay_day + s.pay_saving + s.accident)) FROM SALE s \n"
            + "INNER JOIN CAR_DRIVER_USER u ON u.id = s.created_by \n" +
            "WHERE DATE(s.created_on) between STR_TO_DATE(:starDate, '%Y-%m-%d') AND STR_TO_DATE(:endDate,'%Y-%m-%d') \n" +
            "AND u.id_location = '"+locationId+"'";

            NativeQuery nativeQuery = session.createSQLQuery(sqlQuery);

            total = (Double) (nativeQuery.uniqueResult());
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load the total in sale", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getTotalSale()", e);
            rollback(transaction);
        }

        return total;
    }
    
    public Long getTotalToCollect(String starDate, String endDate, String locationId) {
        logger.info("getTotalSale");

        Long total = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String sqlQuery = "SELECT IF(ISNULL(SUM(s.total)),0,SUM(s.total)) FROM TO_COLLECT s \n"
            + "INNER JOIN PEOPLE u ON u.id = s.driver_id \n" +
            "WHERE DATE(s.created_on) between STR_TO_DATE(:starDate, '%Y-%m-%d') AND STR_TO_DATE(:endDate,'%Y-%m-%d') \n" +
            "AND u.id_location = '"+locationId+"'";

            NativeQuery nativeQuery = session.createSQLQuery(sqlQuery).setParameter("starDate", starDate)
                    .setParameter("endDate", endDate);

            total = (Long) (nativeQuery.uniqueResult());
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load the total in sale", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getTotalSale()", e);
            rollback(transaction);
        }

        return total;
    }
    
    private static final long serialVersionUID = 1020525241432403643L;
    
    final Logger logger = LogManager.getLogger(SalesFeeExpectedPaymentController.class);

    public SalesFeeExpectedPaymentController() {
    }
    
}
