/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.controller.admin;

import com.arrebol.taxiservicios.model.admin.ClosingDay;
import com.arrebol.taxiservicios.model.admin.ClosingDayDetail;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.User;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
public class ClosingDayControllerTest {

    private Date startDate;
    private Date endDate;
    private ClosingDayController controller;
    ClosingDay closingDay;
    private final String closingDayId = "f379c611-92f5-47dd-87b8-52526b88c218";
    private final String userID = "09ea84b8-b9d1-4a71-adfe-db653b2a9982";
    private final String query_total_sales = "SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident)),0 , SUM(pay_day + pay_saving + accident)) AS total_sales FROM SALE WHERE DATE(created_on) = CURDATE() AND created_by = :userID";
    private final String query_total_sanctions_in = "SELECT IF(ISNULL(SUM(payment)),0, SUM(payment)) AS total_sanctions_in FROM SANCTION WHERE DATE(created_on) = CURDATE() AND sanction_type = 'ADD' AND created_by = :userID";
    private final String query_total_sanctions_out = "SELECT IF(ISNULL(SUM(payment)),0, SUM(payment)) AS total_sanctions_out FROM SANCTION WHERE DATE(created_on) = CURDATE() AND sanction_type IN ('SANCTION','DISPOSITION','ACCIDENT') AND created_by = :userID";
    private final String query_total_expenses = "SELECT IF(ISNULL(SUM(payment)),0, SUM(payment)) AS total_expenses FROM EXPENSE WHERE DATE(created_on) = CURDATE() AND expense_status_type = 'AUTHORIZED' AND created_by = :userID";
    private final String query_total_invoices = "SELECT IF(ISNULL(SUM(total)),0, SUM(total)) AS total_invoices FROM INVOICE  WHERE DATE(created_on) = CURDATE() AND status_type = 'APPROVED' AND created_by = :userID";

    public ClosingDayControllerTest() {
    }

    @Before
    public void setUp() {
        controller = new ClosingDayController();

        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();

        //2019-03-07 09:07:04
        start.set(2021, 2, 13, 9, 7, 4);
        //2019-03-07 21:07:04
        end.set(2021, 4, 03, 21, 7, 4);

        startDate = start.getTime();
        endDate = end.getTime();
        closingDay = new ClosingDay();
        closingDay.setId(closingDayId);
    }

    /**
     * Test of getAllActiveUsers method, of class ClosingDayController.
     */
    //@Test
    public void testGetAllActiveUsers() {
        System.out.println("getAllActiveUsers");

        List<User> results = controller.getAllActiveUsers(new Location());

        results.forEach(System.out::println);
    }

    /**
     * Test of findClosingDailyListBetweenDates method, of class
     * ClosingDayController.
     */
    ////@Test
    public void testfindClosingDailyListBetweenDates() {
        System.out.println("testfindClosingDailyListBetweenDates");

        List<ClosingDay> results = controller.findClosingDailyListBetweenDates(startDate, endDate, "");

        results.forEach(System.out::println);
    }

    ////@Test
    public void findTotals() {
        System.out.println("findTotals");

        System.out.println("query_total_sales: " + controller.findTotals(query_total_sales, userID));
        System.out.println("query_total_sanctions_in: " + controller.findTotals(query_total_sanctions_in, userID));
        System.out.println("query_total_sanctions_out: " + controller.findTotals(query_total_sanctions_out, userID));
        System.out.println("query_total_expenses: " + controller.findTotals(query_total_expenses, userID));
        System.out.println("query_total_invoices: " + controller.findTotals(query_total_invoices, userID));
    }

    //@Test
    public void findDetailsFromClosingDayID() {
        System.out.println("findDetailsFromClosingDayID");

        List<ClosingDayDetail> results = controller.findDetailsFromClosingDayID(closingDay);

        results.forEach(System.out::println);
    }

    //@Test
    public void executeNativeSQLQuery1() {
        System.out.println("executeNativeSQLQuery");

        List<ClosingDayDetail> results = controller.findClosingDayDetails(userID);

        results.forEach(System.out::println);

    }

    @Test
    public void executeNativeSQLQuerys1() {
        Assert.assertTrue(controller.existStableSmallBoxByCreatedOn(new Date()));
    }
}
