/*
 * 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.sp.report.SundayPayment;
import java.io.Serializable;
import java.util.ArrayList;
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 Oscar
 */
public class ReportSundayPaymentController extends ConnectionManager implements Serializable {
    
    public List<SundayPayment> callReportSundayPaymentQueryMySQL(String starDate, String endDate) {
        LOG.info("callReportSundayPaymentQueryMySQL");

        List<SundayPayment> list = new ArrayList<>();
        Transaction transaction = null;

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

            List<Object> rows = session.createNativeQuery(queryMySQL)
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .getResultList();

            transaction.commit();

            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                SundayPayment pay = new SundayPayment(
                        null == results[0] ? null : results[0].toString(),
                        Double.valueOf(null == results[1] ? "0" : results[1].toString()),
                        Double.valueOf(null == results[2] ? "0" : results[2].toString()),
                        Double.valueOf(null == results[3] ? "0" : results[3].toString()),
                        null == results[4] ? null : results[4].toString(),
                        null == results[5] ? null : results[5].toString(),
                        null == results[6] ? null : results[6].toString(),
                        null == results[7] ? null : results[7].toString(),
                        null == results[8] ? null : results[8].toString(),
                        null == results[9] ? null : results[9].toString(),
                        null == results[10] ? null : results[10].toString()
                        
                );

                list.add(pay);
            });

            LOG.info("Success callReportSundayPaymentQueryMySQL");
        } catch (HibernateException e) {
            LOG.error("Report SundayPayment", e);
            rollback(transaction);
        } catch (Exception e) {
            LOG.error("Method callReportSundayPaymentQueryMySQL(" + starDate + ") ", e);
            rollback(transaction);
        }
        return list;
    }
    
    private static final long serialVersionUID = 3405798902357776578L;

    final Logger LOG = LogManager.getLogger(ReportSundayPaymentController.class);
    
    private final String queryMySQL = "SELECT \n" +
        "s.id,\n" +
        "s.pay_day,\n" +
        "s.pay_saving,\n" +
        "s.accident,\n" +
        "CASE WHEN s.transfer = 'ENEBLED' THEN 'Transferencia' \n" +
        "WHEN s.transfer = 'DISABLED' THEN 'Efectivo'\n" +
        "END as transferencia,\n" +
        "t.economic_number,\n" +
        "CONCAT(p.first_name, ' ', p.last_name) person,\n" +
        "(DATE_ADD(DATE(s.pay_date), interval 1 day)) as fecha_pago,\n" +
        "s.description,\n" +
        "s.created_on as fecha_registro,\n" +
        "u.username\n" +
        "FROM SALE s \n" +
        "LEFT JOIN PERSON p ON p.id = s.person_id \n" +
        "LEFT JOIN TAXI t ON t.id = s.taxi_id\n" +
        "INNER JOIN CAR_DRIVER_USER u ON u.id = s.created_by\n" +
        "WHERE (DATE_ADD(DATE(pay_date), interval 1 day) BETWEEN STR_TO_DATE(:starDate, '%Y-%m-%d') AND STR_TO_DATE(:endDate, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'sunday'";

}
