call spQuery_Report_AnalComp_Vendedor(1, NULL, '2012', 'T');

DROP PROCEDURE IF EXISTS spQuery_Report_AnalComp_Vendedor;
CREATE PROCEDURE spQuery_Report_AnalComp_Vendedor(
  varEmpresa    int,
  varProveedor  int,
	varAnio       varchar(4),
	varTipo       char(1)
)
BEGIN
  DECLARE varCadPro, varSQLCab  TEXT DEFAULT '';
  
  if varProveedor <> '' and varProveedor <> 'Todos' and varProveedor is not null then
    SET varCadPro = CONCAT(" and P.id_proveedor = ", varProveedor, " ");
  end if;
  
  if varTipo = 'V' then
    set varSQLCab = CONCAT("sum(case when month(V.fecha_emision)=1  then D.subtotal end) as VEN_ENE, ",
                           "sum(case when month(V.fecha_emision)=2  then D.subtotal end) as VEN_FEB, ",
                           "sum(case when month(V.fecha_emision)=3  then D.subtotal end) as VEN_MAR, ",
                           "sum(case when month(V.fecha_emision)=4  then D.subtotal end) as VEN_ABR, ",
                           "sum(case when month(V.fecha_emision)=5  then D.subtotal end) as VEN_MAY, ",
                           "sum(case when month(V.fecha_emision)=6  then D.subtotal end) as VEN_JUN, ",
                           "sum(case when month(V.fecha_emision)=7  then D.subtotal end) as VEN_JUL, ",
                           "sum(case when month(V.fecha_emision)=8  then D.subtotal end) as VEN_AGO, ",
                           "sum(case when month(V.fecha_emision