package com.rjconsultores.ventaboletos.relatorios.impl;

import java.sql.Connection;
import java.util.Map;

public class RelatorioAgenciaFechamentoAntigo extends RelatorioAgenciaFechamento {

	public RelatorioAgenciaFechamentoAntigo(Map<String, Object> parametros, Connection conexao) throws Exception {
		super(parametros, conexao);
		tipoRelatorio = TipoRelatorioFechamentoEnum.ANTIGO;
	}

	@Override
	public StringBuilder carregarDados(String puntoVenta, String empresa) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT                                                                                                                                                                             ");
		sql.append("          pv.PUNTOVENTA_ID AS codigo,                                                                                                                                               ");
		sql.append("		      pv.nombpuntoventa AS descricao,                                                                                                                                       ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		      COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL and (c.INDREMOTOINVERSO is null or c.INDREMOTOINVERSO =0) THEN c.caja_id  ELSE NULL END),0) AS qtdevendido,   ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		      COALESCE(COUNT(CASE WHEN c.INDREMOTOINVERSO = 1 THEN 1  ELSE NULL END),0) AS qtdedigitado,                                                                            ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("          COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID = 31  THEN 1  ELSE NULL END),0) AS qtdeAnulado,                                                                           ");
		sql.append("		      COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID is not null and c.MOTIVOCANCELACION_ID <> 31  THEN 1  ELSE NULL END),0) AS qtdeDevolvido,                             ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		        COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL and (c.INDREMOTOINVERSO is null or c.INDREMOTOINVERSO =0) THEN (                                              ");
		sql.append("            coalesce(c.IMPORTEOUTROS,0) + coalesce(c.IMPORTEPEDAGIO,0) + coalesce(c.IMPORTESEGURO,0)                                                                                ");
		sql.append("            +coalesce(IMPORTETAXAEMBARQUE ,0)+coalesce(c.preciopagado,0)) ELSE NULL END),0) AS vendido,                                                                             ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		        COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL and c.INDREMOTOINVERSO = 1 THEN (                                                                             ");
		sql.append("            coalesce(c.IMPORTEOUTROS,0) + coalesce(c.IMPORTEPEDAGIO,0) + coalesce(c.IMPORTESEGURO,0)                                                                                ");
		sql.append("            +coalesce(IMPORTETAXAEMBARQUE ,0)+coalesce(c.preciopagado,0)) ELSE NULL END),0) AS digitado,                                                                            ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		        COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID = 31  THEN (                                                                                                          ");
		sql.append("            coalesce(c.IMPORTEOUTROS,0) + coalesce(c.IMPORTEPEDAGIO,0) + coalesce(c.IMPORTESEGURO,0)                                                                                ");
		sql.append("            +coalesce(IMPORTETAXAEMBARQUE ,0)+coalesce(c.preciopagado,0)) ELSE NULL END),0) AS anulado,                                                                             ");
		sql.append("                                                                                                                                                                                    ");
		sql.append("		        COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID is not null and c.MOTIVOCANCELACION_ID <> 31  THEN (                                                                  ");
		sql.append("            coalesce(c.IMPORTEOUTROS,0) + coalesce(c.IMPORTEPEDAGIO,0) + coalesce(c.IMPORTESEGURO,0)                                                                                ");
		sql.append("            +coalesce(IMPORTETAXAEMBARQUE ,0)+coalesce(c.preciopagado,0)) ELSE NULL END),0) AS devolvido,                                                                            ");
		sql.append("          pv.PUNTOVENTA_ID AS puntoventaId                                                                                                                                                                          ");
		return sql;
	}

	@Override
	protected String criarQueryReceitaDespesa(String puntoVenta, String empresa) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT  ");
		sql.append(" 		COALESCE(COUNT(CASE WHEN cdp.IMPORTE < 0 THEN 1 ELSE NULL END),0) as qtdedespesa,     ");
		sql.append(" 		COALESCE(SUM(CASE WHEN cdp.IMPORTE > 0 THEN cdp.IMPORTE ELSE NULL END),0) AS receita,    ");
		sql.append(" 		COALESCE(ABS(SUM(CASE WHEN cdp.IMPORTE < 0 THEN cdp.IMPORTE ELSE NULL END)),0) AS despesa    ");
		sql.append(" 	FROM CAJA_DIVERSOS cd, ");
		sql.append(" 		CAJA_DIVERSOS_PAGO cdp, ");
		sql.append(" 		EVENTO_EXTRA ee ");
		sql.append("    WHERE cd.MOTIVOCANCELACION_ID IS NULL ");
		sql.append("  	 AND ee.EVENTOEXTRA_ID = cd.EVENTOEXTRA_ID ");
		sql.append("   	 AND cdp.CAJADIVERSOS_ID = cd.CAJADIVERSOS_ID ");
		sql.append("   	 AND ee.activo = 1 ");
		sql.append("   	 AND cd.TURNO_ID IS NOT NULL ");
		sql.append("     AND cd.FECCORTE BETWEEN :de AND :ate ");
		if (puntoVenta != null && !puntoVenta.equals("-1")) {
			sql.append("     AND cd.PUNTOVENTA_ID = " + puntoVenta + " ");
		}
		if (empresa != null && !empresa.equals("") && !empresa.equals("-1")) {
			sql.append("     AND ee.EMPRESA_ID = " + empresa + " ");
		}

		return sql.toString();
	}

	@Override
	protected void criarGroupByEOrderBy(StringBuilder sql) {
		sql.append("		 GROUP  BY pv.PUNTOVENTA_ID , pv.nombpuntoventa                                                                                                                             ");
		sql.append("		 ORDER  BY pv.PUNTOVENTA_ID , pv.nombpuntoventa ");

	}

}
