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

import com.cosmodas.EntCosmos.Cargos;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * @author obper
 */
@Stateless
public class CargosFacade extends AbstractFacade<Cargos> implements CargosFacadeLocal {

    @PersistenceContext(unitName = "wcosmodasPersistent")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CargosFacade() {
        super(Cargos.class);
    }

    @Override
    public Double cargosPorConsultorPublico(String consultorCodigo) {

        Double rs = 0.00;
        try {
            StringBuilder sql = new StringBuilder("SELECT sum(cargo_publico) as cargo_publico_total FROM dbcosmodas.cargos where consultor_codigo = ?;");
            Query q = em.createNativeQuery(sql.toString());
            q.setParameter(1, consultorCodigo);
            List<Double> result = q.getResultList();
            rs = result.get(0);
        } catch (Exception e) {

        }

        return rs;
    }

    @Override
    public List<Cargos> listaCargos(String consultorCodigo) {
        return null;
    }

    @Override
    public List<Cargos> ObtenerCargos(String consultorCodigo, int tipo) {

        List<Cargos> lsR = em.createNativeQuery("{CALL obtenerCargos(?,?)}",
                Cargos.class)
                .setParameter(1, consultorCodigo)
                .setParameter(2, tipo).getResultList();
        return lsR;
    }

    @Override
    public List<Cargos> ObtenerCargos(String consultorCodigo, int tipo, String sucursalCodigo) {

        List<Cargos> lsR = em.createNativeQuery("{CALL obtenerCargosSuc(?,?,?)}",
                        Cargos.class)
                .setParameter(1, consultorCodigo)
                .setParameter(2, tipo)
                .setParameter(3, sucursalCodigo).getResultList();
        return lsR;
    }

    @Override
    public String ObtenerUltimaFactura(String sucursalCodigo, String documentoCodigo, String facturaSerie) {

        StringBuilder sql = new StringBuilder("SELECT ultima_factura(?,?,?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, sucursalCodigo);
        q.setParameter(2, documentoCodigo);
        q.setParameter(3, facturaSerie);
        List<String> result = q.getResultList();

        String rs = result.get(0);
        return rs;
    }

    @Override
    public Double obtenerCargosPremios(String consultor) {

        StringBuilder sql = new StringBuilder("SELECT cargos_de_regalo(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultor);
        List<Double> result = q.getResultList();
        Double rs = result.get(0);
        return rs;
    }

    @Override
    public String obtenerFacturaSig(String sucursalCodigo, String documentoCodigo, String facturaSerie) {
        StringBuilder sql = new StringBuilder("SELECT ultima_factura(?,?,?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, sucursalCodigo);
        q.setParameter(2, documentoCodigo);
        q.setParameter(3, facturaSerie);
        List<String> result = q.getResultList();

        String rs = result.get(0);
        return rs;
    }

    @Override
    public String obtenerUltimoRecibo(String sucursal, String tipoRecibo) {
        StringBuilder sql = new StringBuilder("SELECT ultimo_recibo(?,?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, sucursal);
        q.setParameter(2, tipoRecibo);
        List<String> result = q.getResultList();

        String rs = result.get(0);
        return rs;
    }


    @Override
    public boolean ObtenerBolsoPrimerPedido(String consultorCodigo) {
        StringBuilder sql = new StringBuilder("SELECT bolso_primer_pedido(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        List<Boolean> result = q.getResultList();

        Boolean rsint = false;
        try {
            rsint = result.get(0);
        } catch (Exception e) {
            System.err.println(e);
        }
        return rsint;
    }

    @Override
    public boolean ObtenerEquipoTrabajo(String consultorCodigo) {
        StringBuilder sql = new StringBuilder("SELECT muestras_pedido(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        List<Boolean> result = q.getResultList();

        Boolean rsint = false;
        try {
            rsint = result.get(0);
        } catch (Exception e) {
            System.err.println(e);
        }

        return rsint;
    }

    @Override
    public String validaBoleta(String consultorCodigo, Double valor, String banco) {
        StringBuilder sql = new StringBuilder("SELECT valida_boleta(?,?,?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        q.setParameter(2, valor.toString());
        q.setParameter(3, banco);
        List<String> result = q.getResultList();

        String rs = result.get(0);
        return rs;
    }

    @Override
    public Integer ObtenerPuntos(String consultorCodigo, String tipo) {
        StringBuilder sql = new StringBuilder("SELECT fPuntos2(?,?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        q.setParameter(2, tipo);
        List<Integer> result = q.getResultList();

        Integer rs = result.get(0);
        return rs;

    }

    @Override
    public String obtenerCargos(String consultorCodigo) {
        StringBuilder sql = new StringBuilder("SELECT cargos_consultor(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        List<Double> result = q.getResultList();

        String rs = result.get(0).toString();
        return rs;
    }

    @Override
    public boolean ObtenerPrimeraFactura(String consultorCodigo) {
        StringBuilder sql = new StringBuilder("SELECT primer_pedido_mes(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        List<Boolean> result = q.getResultList();

        Boolean rsint = false;
        try {
            rsint = result.get(0);
        } catch (Exception e) {
            System.err.println(e);
        }
        return rsint;
    }

//// para poder validar saldos
    @Override
    public boolean validaSaldoMesAnteriror(String consultorCodigo) {
        StringBuilder sql = new StringBuilder("SELECT valida_cargos(?)");
        Query q = em.createNativeQuery(sql.toString());
        q.setParameter(1, consultorCodigo);
        List<Integer> result = q.getResultList();

        Boolean rs = false;
        int rsint = 0;

        try {
            rsint = result.get(0);
            rs = rsint == 1;
        } catch (Exception e) {
            System.err.println(e);
        }

        return rs;
    }

    @Override
    public Boolean validaBoletaBolean(String consultorCodigo, Double valor, String banco){

        Boolean rs = false;
        //fboleta_duplicada
        Query q = em.createNativeQuery("select fboleta_duplicada(?,?,?)", Boolean.class);
        q.setParameter(1, consultorCodigo);
        q.setParameter(2, valor.toString());
        q.setParameter(3, banco);

        rs = (Boolean) q.getResultList().get(0);

        return rs;

    }



}
