0% found this document useful (0 votes)
22 views20 pages

Ventas Corregido V3 Insertar

This document contains code for a stored procedure called VENTAS_CORREGIDO_V3 that takes in date parameters and returns sales data from the VENTAS_MAKITA_BOLIVIA table for those dates. The procedure contains logic to first check if the period between the dates is closed, and if so it will delete existing data and re-populate from invoices, otherwise it will simply select the data range. It then populates the output table with sales fields mapped from the invoices data.

Uploaded by

JuLio Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

Ventas Corregido V3 Insertar

This document contains code for a stored procedure called VENTAS_CORREGIDO_V3 that takes in date parameters and returns sales data from the VENTAS_MAKITA_BOLIVIA table for those dates. The procedure contains logic to first check if the period between the dates is closed, and if so it will delete existing data and re-populate from invoices, otherwise it will simply select the data range. It then populates the output table with sales fields mapped from the invoices data.

Uploaded by

JuLio Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 20

CALL "VENTAS_CORREGIDO_V3" ('20180101','20181231')

ALTER PROCEDURE "VENTAS_CORREGIDO_V3"


(
IN FECHAINI DATETIME,
IN FECHAFIN DATETIME
--OUT RESUL VENTAS_MAKITA_BOLIVIA
)
AS
BEGIN

--SELECT * FROM VENTAS_MAKITA_BOLIVIA


/*
DROP TABLE VENTAS_MAKITA_BOLIVIA;
CREATE TABLE VENTAS_MAKITA_BOLIVIA(
"Clase Documento" NVARCHAR(50), --- 1
"Concepto de Venta" NVARCHAR(50), --- 2
"Tipo Documento" NVARCHAR(50), --- 3
"Tipo Makita" NVARCHAR(50), --- 4
"Area" NVARCHAR(50), --- 5
"C�digo Cliente" NVARCHAR(50), --- 6
"Nombre de Cliente" NVARCHAR(200), --- 7
"Grupo Cliente" NVARCHAR(100), --- 8
"Canal Cliente" NVARCHAR(100), --- 9
"Vendedor" NVARCHAR(200), --- 10
"Vendedor Comision" NVARCHAR(200), --- 11
"N� Documento" NVARCHAR(50), --- 12
"Orden Cliente" NVARCHAR(50), --- 13
"Anio Fiscal" NVARCHAR(50), --- 14
"Fecha" DATETIME, --- 15
"Anio" INT, --- 16
"Mes" NVARCHAR(50), --- 17
"D�a" INT, --- 18
"Moneda Doc." NVARCHAR(50), --- 19
"T.C." DECIMAL(19,6), --- 20
"Estado" NVARCHAR(50), --- 21
"Articulo Principal" NVARCHAR(25), --- 22
"Articulo" NVARCHAR(25), --- 23
"Descripci�n Articulo" NVARCHAR(200), --- 24
"Combo" NVARCHAR(10), --- 25
"Regalo" NVARCHAR(10), --- 26
"Familia SAP" NVARCHAR(50), --- 27
"Grupo SAP" NVARCHAR(50), --- 28
"Categoria" NVARCHAR(50), --- 29
"Sub Categoria" NVARCHAR(50), --- 30
"Linea" NVARCHAR(50), --- 31
"Familia" NVARCHAR(50), --- 32
"Sub Familia" NVARCHAR(50), --- 33
"Outdoor" NVARCHAR(50), --- 34
"Sector" NVARCHAR(50), --- 35
"Cantidad" INT, --- 36
"Costo Art�culo Bs." DECIMAL(21,6), --- 37
"Total Costo Bs." DECIMAL(21,6), --- 38
"Costo Art�culo S/." DECIMAL(21,6), --- 39
"Total Costo S/." DECIMAL(21,6), --- 40
"% Descuento" DECIMAL(21,6), --- 41
"% Descuento Global" DECIMAL(21,6), --- 42
"Precio Unitario Bs." DECIMAL(21,6), --- 43
"Total Linea Bs." DECIMAL(21,6), --- 44
"Total Linea S/." DECIMAL(21,6), --- 45
"Contribuci�n" DECIMAL(21,6), --- 46
"% Margen" DECIMAL(21,6) --- 47
);
*/

/*
DECLARE FECHAINI DATE;
DECLARE FECHAFIN DATE;
DECLARE ESTADO NVARCHAR(1);
DECLARE CONTADOR INT;

SELECT COUNT("PeriodStat") INTO CONTADOR


FROM "OFPR"
WHERE "F_RefDate" BETWEEN :FINI AND :FFIN
AND "PeriodStat" <> 'C';

IF(:CONTADOR>0) THEN

SELECT "PeriodStat",MIN("F_RefDate"),MAX("T_RefDate") INTO


ESTADO ,FECHAINI,FECHAFIN FROM(
SELECT "PeriodStat","F_RefDate","T_RefDate"
FROM "OFPR"
WHERE "F_RefDate" BETWEEN :FINI AND :FFIN
AND "PeriodStat" <> 'C'
)
GROUP BY "PeriodStat";

ELSE

SELECT :FINI,:FFIN INTO FECHAINI,FECHAFIN FROM DUMMY;

END IF;
*/

/*
IF(:CONTADOR=0) THEN

RESUL = SELECT * FROM "VENTAS_MAKITA_BOLIVIA" WHERE "Fecha" BETWEEN :FINI AND


:FFIN;

ELSE

DELETE FROM "VENTAS_MAKITA_BOLIVIA" WHERE "Fecha" BETWEEN :FECHAINI


AND :FECHAFIN;
*/

TABLA_COMBOS=
SELECT AA."DocEntry",
AA."BaseEntry",
AA."Father",
AA."ItemCode",
T4."CalcPrice",
AA."Quantity",
SUM(T4."CalcPrice" * AA."Quantity") OVER(PARTITION BY
AA."DocEntry",AA."BaseEntry","Father") AS "Total Costo Total"
FROM(
SELECT DISTINCT T2."DocEntry",T2."BaseEntry",T2."ItemCode" AS
"Father",T3."ItemCode",T3."LineNum",(T2."Quantity"*T3."Quantity") AS "Quantity"
FROM OINV T1
INNER JOIN INV1 T2 ON T1."DocEntry"=T2."DocEntry"
INNER JOIN DLN14 T3 ON T3."DocEntry"=T2."BaseEntry" AND
T2."BaseLine"=T3."LineNum"
WHERE (T1."DocDate" >= :FECHAINI AND T1."DocDate" <= :FECHAFIN)

--WHERE T2."DocEntry"=79308
)AA
INNER JOIN OINM T4 ON T4."ItemCode"=AA."ItemCode" AND
AA."LineNum"=T4."DocLineNum" AND T4."CreatedBy"=AA."BaseEntry"
ORDER BY 2;

--INSERT INTO "VENTAS_MAKITA_BOLIVIA"


SELECT
"Clase Documento",
"Concepto de Venta",
"Tipo Documento",
"Tipo Makita",

CASE WHEN "Tipo"='6' AND "Clase Documento"='NACIONAL' AND


"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"='ADMINISTRATIVO' THEN
'ADMINISTRACION'
WHEN "Tipo"='6' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"='ADMINISTRATIVO' THEN
'ADMINISTRACION'
WHEN "Tipo"='6' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'COMERCIAL'

WHEN "Tipo"='8' AND "Clase Documento"='NACIONAL' AND


"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"='ADMINISTRATIVO' THEN
'ADMINISTRACION'
WHEN "Tipo"='8' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='REGALO' AND "Tipo Makita"='ADMINISTRATIVO' THEN
'ADMINISTRACION'
WHEN "Tipo"='8' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'COMERCIAL'
WHEN "Tipo"='8' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='REGALO' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'MARKETING'
WHEN "Tipo"='8' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"='ADMINISTRATIVO' THEN
'ADMINISTRACION'
WHEN "Tipo"='8' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN 'COMEX'

WHEN "Tipo"='4' AND "Clase Documento"='NACIONAL' AND


"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'COMERCIAL'
WHEN "Tipo"='4' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='REGALO' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'MARKETING'
WHEN "Tipo"='4' AND "Clase Documento"='NACIONAL' AND
"Concepto de Venta"='GARANTIA' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'SERVICIO TECNICO'

WHEN "Tipo"='4' AND "Clase Documento"='EXPORTACION' AND


"Concepto de Venta"='ESTANDAR' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN 'COMEX'
WHEN "Tipo"='4' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='REGALO' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'MARKETING'
WHEN "Tipo"='4' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='GARANTIA' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN
'SERVICIO TECNICO'
WHEN "Tipo"='4' AND "Clase Documento"='EXPORTACION' AND
"Concepto de Venta"='DEMO' AND "Tipo Makita"<>'ADMINISTRATIVO' THEN 'COMEX'
ELSE '' END AS "Area",
--"Cuenta",
--"Fecha vencimiento",
--"N� Linea Doc.",

"C�digo Cliente",
"Nombre de Cliente",
"Canal Cliente" AS "Grupo Cliente",
"Canal Cliente",
"Vendedor",
"Vendedor" AS "Vendedor Comision",
"N� Documento",
"Orden Cliente",
"Anio Fiscal",
"Fecha Contable",
"Anio",
"Mes",
"D�a",
"Moneda Doc.",
"Tipo Cambio S/.",
"Estado",
"Articulo Principal",
"Articulo",
"Descripcion Articulo",
"Combo",
"Regalo",
--CASE WHEN BBB."QryGroup1"='Y' THEN 'ACCESORIOS'
-- WHEN BBB."QryGroup2"='Y' THEN 'HERRAMIENTAS'
-- WHEN BBB."QryGroup3"='Y' THEN 'REPUESTOS'
-- WHEN BBB."QryGroup4"='Y' THEN 'PUBLICIDAD'
-- WHEN BBB."QryGroup5"='Y' THEN 'KITS'
-- WHEN BBB."QryGroup6"='Y' THEN 'SERVICIOS'
--ELSE '' END "Familia SAP",
"Grupo SAP" AS "Familia SAP",
"Grupo SAP",
"Categoria",
"Sub Categoria",
"Linea",
"Familia",
"Sub Familia",
"Outdoor",
"Sector",
"Cantidad",
"Costo Art�culo Bs.",
"Total Costo Bs.",
"Costo Articulo S/.",
"Total Costo S/.",
"% Descuento",
"Descuento Global",
"Precio Unitario Bs.",
"Total Linea Bs.",
"Total Linea S/.",

CASE
WHEN ABS("Total Costo Bs.") = 0 and ABS("Total Linea Bs.") > 0 THEN "Total
Linea Bs."
WHEN ABS("Total Linea Bs.") = 0 and ABS("Total Costo Bs.") > 0 THEN "Total
Costo Bs."*-1
ELSE
CASE WHEN IFNULL("Total Linea Bs.",0) = 0

THEN 0

ELSE
CASE WHEN IFNULL("Cant. Total Linea",0) = 0 THEN
ROUND(("Total Linea Bs."-"Total Costo Bs.")*"Cant. Total
Linea",2)
ELSE
ROUND((("Total Linea Bs.")-("Total Costo Bs.")),2)
END
END
END
AS "Contribuci�n",

CASE
WHEN ABS("Total Costo Bs.") = 0 and ABS("Total Linea Bs.") > 0 and "Total
Linea Bs." > 0 THEN 100
WHEN ABS("Total Costo Bs.") = 0 and ABS("Total Linea Bs.") > 0 and "Total
Linea Bs." < 0 THEN 100*-1
WHEN ABS("Total Linea Bs.") = 0 and ABS("Total Costo Bs.") > 0 THEN 100*-1
ELSE
CASE WHEN "Tipo Documento" = 'NOTA DE CREDITO' THEN
(
CASE WHEN ("Total Linea Bs." = 0) OR IFNULL("Cant. Total
Linea",0) = 0 THEN 0
ELSE
ROUND( ((("Total Linea Bs.")-("Total Costo Bs."))/("Total
Linea Bs."))*100 ,2)
END
)*-1
ELSE
(
CASE WHEN ("Total Linea Bs." = 0) OR IFNULL("Cant. Total
Linea",0) = 0 THEN 0
ELSE
ROUND( ((("Total Linea Bs.")-("Total Costo Bs."))/("Total
Linea Bs."))*100 ,2)
END
)
END
END
AS "% Margen"
FROM
(

SELECT
CASE WHEN T0."DocSubType" = '--'
THEN 'NACIONAL' ELSE 'EXPORTACION' END AS "Clase Documento",
CASE WHEN T0."U_MKA_TIPFACT" = '01' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '01' AND T1."TaxOnly"='Y' THEN
'REGALO'
WHEN T0."U_MKA_TIPFACT" = '02' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '02' AND T1."TaxOnly"='Y' THEN
'REGALO'
WHEN T0."U_MKA_TIPFACT" = '03' THEN 'DEMO'
WHEN T0."U_MKA_TIPFACT" = '04' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '04' AND T1."TaxOnly"='Y' THEN
'GARANTIA'
ELSE '' END AS "Concepto de Venta",
(SELECT UPPER("Name") FROM "@MKA_TIDOMA" WHERE
"Code"=IFNULL(T0."U_MKA_TIDOMA",'01')) AS "Tipo Makita",
T6."LicTradNum" AS "C�digo Cliente",
T0."CardName" AS "Nombre de Cliente",
(SELECT "FormatCode" FROM OACT WHERE "AcctCode"=T1."AcctCode") AS
"Cuenta",

(SELECT "GroupName" FROM OCRG WHERE "GroupCode" = (SELECT


O1."GroupCode" FROM OCRD O1 WHERE O1."CardCode" = T0."CardCode") ) AS "Canal
Cliente",
CASE WHEN T6."QryGroup1"='Y' THEN 'CLIENTES NACIONALES'
WHEN T6."QryGroup2"='Y' THEN 'CLIENTES ATENDIDOS POR PERU'
WHEN T6."QryGroup3"='Y' THEN 'OTROS'
ELSE '' END AS "Grupo Cliente",
(SELECT "SlpName" FROM OSLP WHERE "SlpCode" = (SELECT "SlpCode" FROM
OCRD WHERE "CardCode" = T0."CardCode")) AS "Vendedor",
--T0."U_LB_NumeroFactura" AS "N� Documento",
T0."U_BPV_NCON2" AS "N� Documento",
T0."U_MKA_OCCL" AS "Orden Cliente",
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'PEN') AS "Tipo Cambio S/.",
(SELECT XX."Name" FROM "@MKA_AJAPONES" XX WHERE T0."DocDate" BETWEEN
XX."U_MKA_FINICIO" AND XX."U_MKA_FFINAL") AS "Anio Fiscal",

T0."DocDate" AS "Fecha Contable",


T0."DocDueDate" AS "Fecha vencimiento",
YEAR(T0."DocDate") AS "Anio",
CASE
WHEN MONTH(T0."DocDate") = '01' THEN 'Enero'
WHEN MONTH(T0."DocDate") = '02' THEN 'Febrero'
WHEN MONTH(T0."DocDate") = '03' THEN 'Marzo'
WHEN MONTH(T0."DocDate") = '04' THEN 'Abril'
WHEN MONTH(T0."DocDate") = '05' THEN 'Mayo'
WHEN MONTH(T0."DocDate") = '06' THEN 'Junio'
WHEN MONTH(T0."DocDate") = '07' THEN 'Julio'
WHEN MONTH(T0."DocDate") = '08' THEN 'Agosto'
WHEN MONTH(T0."DocDate") = '09' THEN 'Setiembre'
WHEN MONTH(T0."DocDate") = '10' THEN 'Octubre'
WHEN MONTH(T0."DocDate") = '11' THEN 'Noviembre'
WHEN MONTH(T0."DocDate") = '12' THEN 'Diciembre'
END AS "Mes",
EXTRACT(DAY FROM T0."DocDate") AS "D�a",
CASE WHEN T0."DocCur" = 'SOL' THEN 'S/.'
WHEN T0."DocCur" = 'BS' THEN 'BS'
ELSE 'USD' END "Moneda Doc.",
CASE WHEN T0."Indicator" = '4' THEN 'FACTURA DE CLIENTE'
WHEN T0."Indicator" = '6' THEN 'NOTA DE DEBITO DE
CLIENTES'
WHEN T0."Indicator" = '8' THEN 'NOTA DE CREDITO DE
CLIENTES'
WHEN T0."U_BPP_MDTD" = '00' THEN 'FACTURA DE CLIENTE'
WHEN T0."U_BPP_MDTD" = '11' THEN 'FACTURA DE EXPORTACION'
WHEN T0."U_BPP_MDTD" = '05' THEN 'NOTA DE CREDITO'
ELSE '' END AS "Tipo Documento",
T0."Indicator" AS "Tipo",
(SELECT "Name" FROM "OCST" WHERE "Code" = (SELECT "State" FROM "CRD1"
WHERE "Address" = 'FISCAL' AND "Country" = 'BO' AND "CardCode" = T0."CardCode" AND
"AdresType"='B') AND "Country"='BO') AS "Estado",

T1."LineNum" AS "N� Linea Doc.",


T1."ItemCode" AS "Articulo Principal",
CASE WHEN IFNULL(T2."Code",'') = '' THEN T1."ItemCode" ELSE T2."Code"
END AS "Articulo",

CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Dscription" ELSE (SELECT


"ItemName" FROM "OITM" WHERE "ItemCode" = T2."Code") END AS "Descripcion
Articulo",

CASE WHEN IFNULL(T2."Code",'')='' THEN 'NO' ELSE 'SI' END AS "Combo",

CASE WHEN IFNULL(T2."Code",'') = '' THEN 'NO'


WHEN IFNULL(T2."Code",'') <> '' AND
IFNULL(T2."U_MKA_ESREGA",'') = 'Y' THEN 'SI'
ELSE 'NO' END AS "Regalo",

CASE WHEN IFNULL(T2."Code",'')='' THEN (SELECT "ItmsGrpNam" FROM "OITB"


WHERE "ItmsGrpCod" = (SELECT "ItmsGrpCod" FROM "OITM" WHERE "ItemCode" =
T1."ItemCode"))
ELSE (SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T2."Code")) END as "Grupo SAP",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T1."ItemCode"))
ELSE
(SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T2."Code")) END as "Familia por Hijos",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_CATEGORIA" YY ON
XX."U_MKA_CATEGO"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_CATEGORIA" YY ON
XX."U_MKA_CATEGO"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Categoria",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBCATEGORIA" YY ON
XX."U_MKA_SUCATE"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBCATEGORIA" YY ON
XX."U_MKA_SUCATE"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sub Categoria",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_LINEA" YY ON
XX."U_MKA_LINEA"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_LINEA" YY ON
XX."U_MKA_LINEA"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Linea",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_FAMILIA" YY ON
XX."U_MKA_FAMILI"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_FAMILIA" YY ON
XX."U_MKA_FAMILI"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Familia",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBFAMILIA" YY ON
XX."U_MKA_SUFAMI"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBFAMILIA" YY ON
XX."U_MKA_SUFAMI"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sub Familia",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_OUTDOOR" YY ON
XX."U_MKA_OUTD"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_OUTDOOR" YY ON
XX."U_MKA_OUTD"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Outdoor",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SECTOR" YY ON
XX."U_MKA_SECT"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SECTOR" YY ON
XX."U_MKA_SECT"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sector",

T1."Quantity" AS "Cantidad",
CASE WHEN IFNULL(T2."Code",'')='' THEN 0 ELSE T2."Quantity" END AS
"Cantidad Hijo",
CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END AS "Cant. Total Linea",

CASE WHEN IFNULL(T2."Code",'') ='' THEN T1."StockPrice" ELSE


T3."CalcPrice" END AS "Costo Art�culo Bs.",
--T1."StockPrice" AS "Costo Art�culo BS",

ROUND(CASE WHEN IFNULL(T2."Code",'') ='' THEN T1."StockPrice" ELSE


T3."CalcPrice" END /
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'SOL'),2) AS "Costo Articulo S/.",
--T1."StockPrice" AS "Costo Art�culo BS",

(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE


T1."Quantity"*T2."Quantity" END)
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."StockPrice" ELSE
T3."CalcPrice" END)
AS "Total Costo Bs.",
--T1."Quantity" * T1."StockPrice" AS "Total Costo BS",
ROUND(
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END)
*
CASE WHEN IFNULL(T2."Code",'')='' THEN T1."StockPrice" ELSE
T3."CalcPrice" END
/
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'SOL'),2) AS "Total Costo S/.",
--(T1."Quantity" * (T1."StockPrice" / (SELECT "Rate" FROM ORTT WHERE
"RateDate" = T0."DocDate" AND "Currency" = 'SOL'))) AS "Total Costo S/.",

T1."PriceBefDi" AS "Precio Unitario Bs.",


T1."DiscPrcnt" AS "% Descuento",
T0."DiscPrcnt" AS "Descuento Global",

CASE WHEN T0."DiscSum" > 0 THEN


( --- 3
( --- 2
( ---1
IFNULL(
ROUND((((
CASE WHEN T1."ItemCode" IN (SELECT
"Code" FROM "OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

(T3."CalcPrice" *
(T1."Quantity"*T2."Quantity"))
/

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL


PORCENTAJE EN BASE AL PRECIO VENTA
(T1."LineTotal")
ELSE
0
END
ELSE
T1."LineTotal"
END)
)),2)
,0)
/
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
) --- 1
*
(CASE WHEN T0."DiscSum" <> 0 THEN (100 - T0."DiscPrcnt")/100 ELSE
1 END)
) --- 2
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
)--- 3

ELSE

IFNULL(
ROUND(
(
(

CASE WHEN T1."ItemCode" IN (SELECT "Code" FROM


"OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" * (T1."Quantity"*T2."Quantity")

/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL PORCENTAJE


EN BASE AL PRECIO VENTA
(T1."LineTotal")
ELSE
0
END
ELSE
T1."LineTotal"
*
(CASE WHEN T0."DiscSum" <> 0 THEN (100 -
T0."DiscPrcnt")/100 ELSE 1 END)
END )
)
,2)
,0) END AS "Total Linea Bs.",

CASE WHEN T0."DiscSum" > 0 THEN


( --- 3
( --- 2
( ---1
IFNULL(
ROUND((((
CASE WHEN T1."ItemCode" IN (SELECT
"Code" FROM "OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" *
(T1."Quantity"*T2."Quantity")
/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL


PORCENTAJE EN BASE AL PRECIO VENTA
T1."TotalSumSy"--((T1."LineTotal")
* (SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN'))

ELSE
0
END
ELSE
T1."TotalSumSy"
--/
--(SELECT "Rate" FROM ORTT WHERE
"RateDate" = T0."DocDate" AND "Currency" = 'PEN')
END)
)),2)
,0)
/
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
) --- 1
*
(CASE WHEN T0."DiscSum" <> 0 THEN ((100 - T0."DiscPrcnt")/100)
ELSE 1 END)
) --- 2
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
)--- 3

ELSE

IFNULL(
ROUND(
(
(

CASE WHEN T1."ItemCode" IN (SELECT "Code" FROM


"OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" * (T1."Quantity"*T2."Quantity")

/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL PORCENTAJE


EN BASE AL PRECIO VENTA
T1."TotalSumSy"--( (T1."LineTotal") * (SELECT
"Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN'))
ELSE
0
END
ELSE
T1."TotalSumSy"--(T1."LineTotal"/(SELECT "Rate" FROM
ORTT WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN'))
*
(CASE WHEN T0."DiscSum" <> 0 THEN (100 -
T0."DiscPrcnt")/100 ELSE 1 END)
END )
)
,2)
,0) END AS "Total Linea S/."

FROM OINV T0
INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry"
LEFT JOIN ITT1 T2 ON T1."ItemCode" = T2."Father"
LEFT JOIN :TABLA_COMBOS T3 ON T1."DocEntry"=T3."DocEntry" AND
T1."ItemCode"=T3."ItemCode"
INNER JOIN OCRD T6 ON T6."CardCode" = T0."CardCode"
WHERE T0."CANCELED" = 'N' --AND T0.U_CL_TIPFACT NOT IN ('02','03','04')
AND T0.U_CL_TIPVEN<> 'E' AND T0.DocSubType = '--'
--AND T0."DocDate" BETWEEN (CASE WHEN :FECHAINI<>:FINI THEN :FECHAINI
ELSE :FINI END) AND :FECHAFIN
AND T0."DocDate" BETWEEN :FECHAINI AND :FECHAFIN

UNION

SELECT
CASE WHEN T0."DocSubType" = '--'
THEN 'NACIONAL' ELSE 'EXPORTACION' END AS "Clase Documento",
CASE WHEN T0."U_MKA_TIPFACT" = '01' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '01' AND T1."TaxOnly"='Y' THEN
'REGALO'
WHEN T0."U_MKA_TIPFACT" = '02' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '02' AND T1."TaxOnly"='Y' THEN
'REGALO'
WHEN T0."U_MKA_TIPFACT" = '03' THEN 'DEMO'
WHEN T0."U_MKA_TIPFACT" = '04' AND T1."TaxOnly"='N' THEN
'ESTANDAR'
WHEN T0."U_MKA_TIPFACT" = '04' AND T1."TaxOnly"='Y' THEN
'GARANTIA'
ELSE '' END AS "Concepto de Venta",
---------- CABECERA ----------

(SELECT UPPER("Name") FROM "@MKA_TIDOMA" WHERE


"Code"=IFNULL(T0."U_MKA_TIDOMA",'01')) AS "Tipo Makita",
T6."LicTradNum" AS "C�digo Cliente",
T0."CardName" AS "Nombre de Cliente",
T1."AcctCode" AS "Cuenta",
(SELECT "GroupName" FROM OCRG WHERE "GroupCode" = (SELECT
O1."GroupCode" FROM OCRD O1 WHERE O1."CardCode" = T0."CardCode") ) AS "Canal
Cliente",
CASE WHEN T6."QryGroup1"='Y' THEN 'CLIENTES NACIONALES'
WHEN T6."QryGroup2"='Y' THEN 'CLIENTES ATENDIDOS POR PERU'
WHEN T6."QryGroup3"='Y' THEN 'OTROS'
ELSE '' END AS "Grupo Cliente",
(SELECT "SlpName" FROM OSLP WHERE "SlpCode" = (SELECT "SlpCode" FROM
OCRD WHERE "CardCode" = T0."CardCode")) AS "Vendedor",

--T0."U_LB_NumeroFactura" AS "N� Documento",


T0."U_BPV_NCON2" AS "N� Documento",
T0."U_MKA_OCCL" AS "Orden Cliente",
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'PEN') AS "Tipo Cambio S/.",

(SELECT XX."Name" FROM "@MKA_AJAPONES" XX WHERE T0."DocDate" BETWEEN


XX."U_MKA_FINICIO" AND XX."U_MKA_FFINAL") AS "Anio Fiscal",

T0."DocDate" AS "Fecha Contable",


T0."DocDueDate" AS "Fecha vencimiento",
YEAR(T0."DocDate") AS "A�o",

CASE
WHEN MONTH(T0."DocDate") = '01' THEN 'Enero'
WHEN MONTH(T0."DocDate") = '02' THEN 'Febrero'
WHEN MONTH(T0."DocDate") = '03' THEN 'Marzo'
WHEN MONTH(T0."DocDate") = '04' THEN 'Abril'
WHEN MONTH(T0."DocDate") = '05' THEN 'Mayo'
WHEN MONTH(T0."DocDate") = '06' THEN 'Junio'
WHEN MONTH(T0."DocDate") = '07' THEN 'Julio'
WHEN MONTH(T0."DocDate") = '08' THEN 'Agosto'
WHEN MONTH(T0."DocDate") = '09' THEN 'Setiembre'
WHEN MONTH(T0."DocDate") = '10' THEN 'Octubre'
WHEN MONTH(T0."DocDate") = '11' THEN 'Noviembre'
WHEN MONTH(T0."DocDate") = '12' THEN 'Diciembre'
END AS "Mes",
EXTRACT(DAY FROM T0."DocDate") AS "D�a",
CASE WHEN T0."DocCur" = 'SOL' THEN 'S/.'
WHEN T0."DocCur" = 'BS' THEN 'BS'
ELSE 'USD' END "Moneda Doc.",
CASE WHEN T0."Indicator" = '4' THEN 'FACTURA DE CLIENTE'
WHEN T0."Indicator" = '6' THEN 'NOTA DE DEBITO DE
CLIENTES'
WHEN T0."Indicator" = '8' THEN 'NOTA DE CREDITO DE
CLIENTES'
WHEN T0."U_BPP_MDTD" = '00' THEN 'FACTURA DE CLIENTE'
WHEN T0."U_BPP_MDTD" = '11' THEN 'FACTURA DE EXPORTACION'
WHEN T0."U_BPP_MDTD" = '05' THEN 'NOTA DE CREDITO'
ELSE '' END AS "Tipo Documento",
T0."Indicator" AS "Tipo",

(SELECT "Name" FROM "OCST" WHERE "Code" = (SELECT "State" FROM "CRD1"
WHERE "Address" = 'FISCAL' AND "Country" = 'BO' AND "CardCode" = T0."CardCode" AND
"AdresType"='B') AND "Country"='BO') AS "Estado",

T1."LineNum" AS "N� Linea Doc.",


T1."ItemCode" AS "Articulo Principal",
CASE WHEN IFNULL(T2."Code",'') = '' THEN T1."ItemCode" ELSE T2."Code"
END AS "Articulo",

CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Dscription" ELSE (SELECT


"ItemName" FROM "OITM" WHERE "ItemCode" = T2."Code") END AS "Descripcion
Articulo",

CASE WHEN IFNULL(T2."Code",'')='' THEN 'NO' ELSE 'SI' END AS "Combo",

CASE WHEN IFNULL(T2."Code",'') = '' THEN 'NO'


WHEN IFNULL(T2."Code",'') <> '' AND
IFNULL(T2."U_MKA_ESREGA",'') = 'Y' THEN 'SI'
ELSE 'NO' END AS "Regalo",

CASE WHEN IFNULL(T2."Code",'')='' THEN (SELECT "ItmsGrpNam" FROM "OITB"


WHERE "ItmsGrpCod" = (SELECT "ItmsGrpCod" FROM "OITM" WHERE "ItemCode" =
T1."ItemCode"))
ELSE (SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T2."Code")) END as "Grupo SAP",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T1."ItemCode"))
ELSE
(SELECT "ItmsGrpNam" FROM "OITB" WHERE "ItmsGrpCod" = (SELECT
"ItmsGrpCod" FROM "OITM" WHERE "ItemCode" = T2."Code")) END as "Familia por Hijos",
CASE WHEN IFNULL(T2."Code",'')='' THEN
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_CATEGORIA" YY ON
XX."U_MKA_CATEGO"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_CATEGORIA" YY ON
XX."U_MKA_CATEGO"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Categoria",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBCATEGORIA" YY ON
XX."U_MKA_SUCATE"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBCATEGORIA" YY ON
XX."U_MKA_SUCATE"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sub Categoria",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_LINEA" YY ON
XX."U_MKA_LINEA"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_LINEA" YY ON
XX."U_MKA_LINEA"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Linea",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_FAMILIA" YY ON
XX."U_MKA_FAMILI"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_FAMILIA" YY ON
XX."U_MKA_FAMILI"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Familia",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBFAMILIA" YY ON
XX."U_MKA_SUFAMI"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SUBFAMILIA" YY ON
XX."U_MKA_SUFAMI"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sub Familia",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_OUTDOOR" YY ON
XX."U_MKA_OUTD"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_OUTDOOR" YY ON
XX."U_MKA_OUTD"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Outdoor",

CASE WHEN IFNULL(T2."Code",'')='' THEN


(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SECTOR" YY ON
XX."U_MKA_SECT"=YY."Code" WHERE "ItemCode" = T1."ItemCode"
)
ELSE
(
SELECT YY."Name" FROM "OITM" XX
INNER JOIN "@MKA_SECTOR" YY ON
XX."U_MKA_SECT"=YY."Code" WHERE "ItemCode" = T2."Code"
) END AS "Sector",

T1."Quantity" AS "Cantidad",
CASE WHEN IFNULL(T2."Code",'')='' THEN 0 ELSE T2."Quantity" END AS
"Cantidad Hijo",
CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END AS "Cant. Total Linea",

CASE WHEN IFNULL(T2."Code",'') ='' THEN T1."StockPrice" ELSE


T3."CalcPrice" END AS "Costo Art�culo Bs.",
--T1."StockPrice" AS "Costo Art�culo BS",

ROUND(CASE WHEN IFNULL(T2."Code",'') ='' THEN T1."StockPrice" ELSE


T3."CalcPrice" END /
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'SOL'),2) * -1 AS "Costo Articulo S/.",
--T1."StockPrice" AS "Costo Art�culo BS",

ROUND((CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE


T1."Quantity"*T2."Quantity" END)
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."StockPrice" ELSE
T3."CalcPrice" END),2) * -1
AS "Total Costo Bs.",
--T1."Quantity" * T1."StockPrice" AS "Total Costo BS",

ROUND(
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END)
*
CASE WHEN IFNULL(T2."Code",'')='' THEN T1."StockPrice" ELSE
T3."CalcPrice" END
/
(SELECT "Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency"
= 'SOL'),2) * -1 AS "Total Costo S/.",
--(T1."Quantity" * (T1."StockPrice" / (SELECT "Rate" FROM ORTT WHERE
"RateDate" = T0."DocDate" AND "Currency" = 'SOL'))) AS "Total Costo S/.",

T1."PriceBefDi" * -1000 AS "Precio Unitario Bs.",


T1."DiscPrcnt" AS "% Descuento",
T0."DiscPrcnt" AS "Descuento Global",

(CASE WHEN T0."DiscSum" > 0 THEN


( --- 3
( --- 2
( ---1
IFNULL(
ROUND((((
CASE WHEN T1."ItemCode" IN (SELECT
"Code" FROM "OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

(T3."CalcPrice" *
(T1."Quantity"*T2."Quantity"))
/

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL


PORCENTAJE EN BASE AL PRECIO VENTA
(T1."LineTotal")
ELSE
0
END
ELSE
T1."LineTotal"
END)
)),2)
,0)
/
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
) --- 1
*
(CASE WHEN T0."DiscSum" > 0 THEN ((100 - T0."DiscPrcnt")/100)
ELSE 1 END)
) --- 2
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
)--- 3

ELSE

IFNULL(
ROUND(
(
(

CASE WHEN T1."ItemCode" IN (SELECT "Code" FROM


"OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" * (T1."Quantity"*T2."Quantity")

/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL PORCENTAJE


EN BASE AL PRECIO VENTA
(T1."LineTotal")
ELSE
0
END
ELSE
T1."LineTotal"
*
(CASE WHEN T0."DiscSum" <> 0 THEN (100 -
T0."DiscPrcnt")/100 ELSE 1 END)
END )
)
,2)
,0) END)*-1 AS "Total Linea Bs.",

CASE WHEN T0."DiscSum" > 0 THEN


( --- 3
( --- 2
( ---1
IFNULL(
ROUND((((
CASE WHEN T1."ItemCode" IN (SELECT
"Code" FROM "OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" *
(T1."Quantity"*T2."Quantity")
/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL


PORCENTAJE EN BASE AL PRECIO VENTA
--((T1."LineTotal") * (SELECT
"Rate" FROM ORTT WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN'))
T1."TotalSumSy"
ELSE
0
END
ELSE
--T1."LineTotal" / (SELECT "Rate" FROM
ORTT WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN')
T1."TotalSumSy"
END)
)),2)
,0)
/
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
) --- 1
*
(CASE WHEN T0."DiscSum" > 0 THEN ((100 - T0."DiscPrcnt")/100)
ELSE 1 END)
) --- 2
*
(CASE WHEN IFNULL(T2."Code",'')='' THEN T1."Quantity" ELSE
T1."Quantity"*T2."Quantity" END )
)--- 3

ELSE

IFNULL(
ROUND(
(
(

CASE WHEN T1."ItemCode" IN (SELECT "Code" FROM


"OITT") THEN CASE WHEN T2."U_MKA_ESREGA" = 'N' THEN

T3."CalcPrice" * (T1."Quantity"*T2."Quantity")

/ --- DIVISION PARA SACAR EL MARGEN

(T3."Total Costo Total")

* --- MULTIPLICACION PARA SACAR EL PORCENTAJE


EN BASE AL PRECIO VENTA
--( (T1."LineTotal") * (SELECT "Rate" FROM ORTT
WHERE "RateDate" = T0."DocDate" AND "Currency" = 'PEN'))
T1."TotalSumSy"
ELSE
0
END
ELSE
(
--T1."LineTotal"/(SELECT "Rate" FROM ORTT WHERE
"RateDate" = T0."DocDate" AND "Currency" = 'PEN')
T1."TotalSumSy"
*
(CASE WHEN T0."DiscSum" <> 0 THEN (100 -
T0."DiscPrcnt")/100 ELSE 1 END)
)
END )
)
,2)
,0) END *-1 AS "Total Linea S/."
--T0."DiscPrcnt" AS "Descuento Global"

FROM ORIN T0
INNER JOIN RIN1 T1 ON T0."DocEntry" = T1."DocEntry"
LEFT JOIN ITT1 T2 ON T1."ItemCode" = T2."Father"
LEFT JOIN :TABLA_COMBOS T3 ON T1."DocEntry"=T3."DocEntry" AND
T1."ItemCode"=T3."ItemCode"
INNER JOIN OCRD T6 ON T6."CardCode" = T0."CardCode"
WHERE T0."CANCELED" = 'N' --AND T0.U_CL_TIPFACT NOT IN ('02','03','04')
AND T0.U_CL_TIPVEN<> 'E' AND T0.DocSubType = '--'
--AND T0."DocDate" BETWEEN (CASE WHEN :FECHAINI<>:FINI THEN :FECHAINI
ELSE :FINI END) AND :FECHAFIN
AND T0."DocDate" BETWEEN :FECHAINI AND :FECHAFIN
) AAA
LEFT JOIN OITM BBB ON AAA."Articulo"=BBB."ItemCode"
ORDER BY AAA."N� Documento";

--RESUL = SELECT * FROM "VENTAS_MAKITA_BOLIVIA" WHERE "Fecha" BETWEEN :FINI


AND :FFIN ORDER BY "Fecha","D�a","N� Documento";

--END IF;

END

You might also like