-- Comisiones mensuales de acuerdo a recaudos sobre ventas en Kg
SELECT
    com.id_sucursal AS suc,
    com.id_vendedor AS cod_vendedor,
    vdr.nombre_vendedor AS nombre_vendedor,
    vt.id_tipovendedor AS tipo_vdr,
    com.id_documento AS documento,
    com.id_documento_cruce AS documento_cruce,
    com.id_item AS cod_item,
    i.descripcion AS item,
    l.descripcion AS linea,
    lc.descripcion AS linea_comision,
    com.periodo AS periodo,
    com.factor_prorrateo AS factor_prorrateo,
    com.factor_comision AS comision_cop_por_kg,
    mo.descripcion AS rango_mora,
    com.cantidad AS cantidad,
    com.peso_un AS peso_un,
    com.valor_comision AS comision
FROM
    valor_comision_recaudo_kg com
    LEFT JOIN vendedor vdr
        USING  (id_vendedor,id_sucursal)
    LEFT JOIN vendedor_has_tipovendedor vt
        USING  (id_vendedor,id_sucursal)
    LEFT JOIN item i
        USING  (id_sucursal,id_item)
    LEFT JOIN linea l
        ON (i.tipo = l.tipo AND LEFT(i.id_linea,2) = l.id_linea)
    LEFT JOIN linea_has_linea_comision lhc
        ON (l.id_linea=lhc.id_linea)
    LEFT JOIN linea_comision lc
        USING (id_linea_comision)
    LEFT JOIN tipo_mora mo
        USING (id_rangomora)

WHERE
    EXTRACT(YEAR_MONTH FROM com.periodo) = 201204
