Dejame rehacer el query por ahí para ver…
Mientras…
¿Eso también toma en consideración las Facturas y Notas de Crédito de servicios? (la OINM me refiero) porque solo veo artículos.
El que tengo ahorita ya cuadra completo el analisis de ventas y el reporte. pero no me cuadra la balanza en costos Vs Reporte (que la intento de ligar con el campo .StockValue)
El sql de Crystal está hecho por mi jefe… y yo solo he cambiado lo que le veo malo (no estaba restando las NC sino sumandolas y así), igual dejame ver si se pega aquí
--Facturas artìculos
SELECT OINV.DocNum,
INV1.ItemCode,
INV1.Quantity,
OINV.DocType,
OINV.DocDate,
OINV.CardCode,
case when OINV.DiscPrcnt <> 0 then
inv1.INMPrice
when OINV.DiscPrcnt = 0 then
INV1.Price else
INV1.Price end as Price,
case when inv1.currency ='MXN' then
0
when INV1.currency <>'MXN' then case when OINV.DiscPrcnt <> 0 then
inv1.INMPrice
when OINV.DiscPrcnt = 0 then
INV1.Price end end as PriceUSD,
case when inv1.currency <>'MXN' then case when OINV.DiscPrcnt <> 0 then
inv1.INMPrice
when OINV.DiscPrcnt = 0 then
INV1.Price else INV1.Price end* case when INV1.Rate=0 then
(select rate
from ORTT
where RateDate=OINV.DocDate and
currency=inv1.Currency) else inv1.rate end
when INV1.currency ='MXN' then case when OINV.DiscPrcnt <> 0 then
inv1.INMPrice
when OINV.DiscPrcnt = 0 then
INV1.Price else INV1.Price end end as PriceMXN,
case when inv1.Currency <>'MXN' then case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *INV1.Quantity else 0 end as VentaUsd,
case when inv1.Currency <>'MXN' then (case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *case when INV1.Rate=0 then (select rate from ORTT where RateDate=OINV.DocDate and currency=INV1.Currency) else inv1.rate end)*INV1.Quantity when inv1.Currency ='MXN' then case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *INV1.Quantity end as VentaMXN,
INV1.Currency,
INV1.Dscription,
OITM.LastPurPrc,
case when OITM.Lastpurprc=0 or inv1.price=0 then 0 else OITM.LastPurPrc/ case when INV1.Currency<>'MXN' then INV1.Price*INV1.Rate else INV1.Price end end as AVG_Amt,
INV1.GrossBuyPr*INV1.Quantity as c_prom_vta,
OITM.LastPurCur,
case when inv1.Currency <>'MXN' then inv1.rate else 0 end as rateusd,
case when INV1.Rate=0 then (select rate from ORTT where RateDate=OINV.DocDate and currency=INV1.Currency) else inv1.rate end as rate,
case when inv1.Currency <>'MXN' then 1 end as Numrate,
OINV.CardName,
OINV.DiscPrcnt,
INV1.GrossBuyPr,
OCRG.GroupName,
OITM.ItemName,
OINV.U_cancela,
inv1.acctcode,
--(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,'01/01/2019') and Currency='USD' ) promrate,
(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,{?Desde}) and Currency='USD' ) promrate,
--/*
Case When INV1.StockValue = 0 then --Productos que no generan costo, ejemplo chatarra
case when inv1.Currency <>'MXN' then (case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *case when INV1.Rate=0 then (select rate from ORTT where RateDate=OINV.DocDate and currency=INV1.Currency) else inv1.rate end)*INV1.Quantity when inv1.Currency ='MXN' then case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *INV1.Quantity end
else
INV1.StockValue end as StockValue
, case when inv1.itemcode = 'CHA01' then
0
else
Case When INV1.StockValue = 0 then
--Productos que no generan costo, ejemplo chatarra
case when inv1.Currency <>'MXN' then (case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *case when INV1.Rate=0 then (select rate from ORTT where RateDate=OINV.DocDate and currency=INV1.Currency) else inv1.rate end)*INV1.Quantity when inv1.Currency ='MXN' then case when OINV.DiscPrcnt <> 0 then inv1.INMPrice when OINV.DiscPrcnt = 0 then INV1.Price else INV1.Price end *INV1.Quantity end
else
INV1.StockValue end end as CostoBruto
--*/
-- INV1.StockValue
FROM ((((OINV OINV INNER JOIN
INV1 INV1 ON OINV.DocEntry=INV1.DocEntry) INNER JOIN
OCRD OCRD ON OINV.CardCode=OCRD.CardCode) INNER JOIN
OITM OITM ON INV1.ItemCode=OITM.ItemCode) INNER JOIN
OCRG OCRG ON OCRD.GroupCode=OCRG.GroupCode)
WHERE -- OINV.DocDate>='01/09/2019' AND OINV.DocDate<='01/09/2019' and oinv.CANCELED ='N'
OINV.DocDate>={?Desde} AND OINV.DocDate<={?Hasta} and oinv.CANCELED ='N'
and oinv.DocType <> 'S'
Union all
--Se suman las Fac de Servicio
SELECT oinv.DocNum,
inv1.Dscription as Itemcode,
0 as Quantity,
oinv.DocType,
oinv.DocDate,
oinv.CardCode,
case when inv1.currency <>'MXN' then inv1.Price * inv1.Rate when inv1.currency ='MXN' then inv1.Price end as Price,
case when inv1.currency ='MXN' then 0 when inv1.currency <>'MXN' then case when oinv.DiscPrcnt <> 0 then inv1.INMPrice when oinv.DiscPrcnt = 0 then inv1.Price else inv1.Price end end as PriceUSD,
case when inv1.currency <>'MXN' then case when oinv.DiscPrcnt <> 0 then inv1.Price when oinv.DiscPrcnt = 0 then inv1.Price else inv1.Price end*case when inv1.Rate=0 then (select rate from ORTT where RateDate=oinv.DocDate and currency=INV1.Currency) else inv1.rate end when inv1.currency ='MXN' then case when oinv.DiscPrcnt <> 0 then inv1.Price when oinv.DiscPrcnt = 0 then inv1.Price else inv1.Price end end as PriceMXN,
case when inv1.Currency <>'MXN' then inv1.TotalFrgn else 0 end as VentaUsd,
case when inv1.Currency ='MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end
when inv1.currency <>'MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end end as VentaMXN,
inv1.Currency,
inv1.Dscription,
0 as LastPurPrc,
0 AVG_Amt,
0 as c_prom_vta,
'N/A' as LastPurCur,
case when inv1.Currency <>'MXN' then inv1.rate else 0 end as rateusd,
case when INV1.Rate=0 then (select rate from ORTT where RateDate=OINV.DocDate and currency=INV1.Currency) else inv1.rate end as rate,
case when inv1.Currency <>'MXN' then 1 end as Numrate,
oinv.CardName,
oinv.DiscPrcnt,
inv1.GrossBuyPr,
OCRG.GroupName,
inv1.Dscription as ItemName,
oinv.U_cancela,
inv1.acctcode,
--(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,'01/01/2019') and Currency='USD' ) promrate,
(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,{?Desde}) and Currency='USD' ) promrate,
case when inv1.Currency ='MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end
when inv1.currency <>'MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end end as StockValue
-- 0 as StockValue
,case when inv1.itemcode = 'CHA01' then
0
else
case when inv1.Currency ='MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end
when inv1.currency <>'MXN' then case when oinv.DiscPrcnt <> 0 then (inv1.linetotal-(inv1.linetotal*(oinv.DiscPrcnt/100))) else inv1.LineTotal end end end as CostoBruto
FROM USK.dbo.oinv oinv INNER JOIN
USK.dbo.inv1 inv1 ON oinv.DocEntry=inv1.DocEntry INNER JOIN
USK.dbo.OCRD OCRD ON oinv.CardCode=OCRD.CardCode INNER JOIN
USK.dbo.OCRG OCRG ON OCRD.GroupCode=OCRG.GroupCode
WHERE -- OINV.DocDate>='01/09/2019' AND OINV.DocDate<= '01/09/2019' and oinv.CANCELED ='N'
OINV.DocDate>={?Desde} AND OINV.DocDate<={?Hasta} and oinv.CANCELED ='N'
and inv1.AcctCode in ('505','205','401','402','403','404','410','411','750','740005A','720002') and oinv.doctype ='S'
union all
--Se restan las NC artìculos
SELECT ORIN.DocNum,
RIN1.ItemCode,
RIN1.Quantity *-1 as Quantity,
ORIN.DocType,
ORIN.DocDate,
ORIN.CardCode,
case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end*-1 as Price,
case when RIN1.currency ='MXN' then 0 when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end end *-1 as PriceUSD,
case when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end*case when RIN1.Rate=0 then (select rate from ORTT where RateDate=ORIN.DocDate and currency=RIN1.Currency) else RIN1.rate end when RIN1.currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end end * -1 as PriceMXN,
case when RIN1.Currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *RIN1.Quantity else 0 end*-1 as VentaUsd,
case when RIN1.Currency <> 'MXN' then (case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *case when RIN1.Rate=0 then (select rate from ORTT where RateDate=ORIN.DocDate and currency=RIN1.Currency) else RIN1.rate end)*RIN1.Quantity when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *RIN1.Quantity end*-1 as VentaMXN,
RIN1.Currency,
RIN1.Dscription,
OITM.LastPurPrc*-1 as LastPurPrc,
case when OITM.Lastpurprc=0 or rin1.price=0 then 0 else OITM.LastPurPrc/ case when RIN1.Currency <>'MXN' then RIN1.Price*RIN1.Rate else RIN1.Price end end as AVG_Amt,
(RIN1.GrossBuyPr*RIN1.Quantity)*-1 as c_prom_vta,
OITM.LastPurCur,
case when rin1.Currency <>'MXN' then rin1.rate else 0 end as rateusd,
case when RIN1.Rate=0 then (select rate from ORTT where RateDate=orin.DocDate and currency=RIN1.Currency) else RIN1.rate end as rate,
case when rin1.Currency <>'MXN' then 1 end as Numrate,
ORIN.CardName,
ORIN.DiscPrcnt,
RIN1.GrossBuyPr,
OCRG.GroupName,
OITM.ItemName,
ORIN.U_cancela,
rin1.acctcode,
--(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,'01/01/2019') and Currency='USD' ) promrate,
(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,{?Desde}) and Currency='USD' ) promrate,
case when rin1.StockValue = 0 then
case when RIN1.Currency <> 'MXN' then (case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *case when RIN1.Rate=0 then (select rate from ORTT where RateDate=ORIN.DocDate and currency=RIN1.Currency) else RIN1.rate end)*RIN1.Quantity when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *RIN1.Quantity end *-1
else
rin1.StockValue *-1
end
as StockValue
,
case when rin1.StockValue = 0 then
case when RIN1.Currency <> 'MXN' then (case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *case when RIN1.Rate=0 then (select rate from ORTT where RateDate=ORIN.DocDate and currency=RIN1.Currency) else RIN1.rate end)*RIN1.Quantity when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end *RIN1.Quantity end *-1
else
rin1.StockValue *-1
end as CostoBruto
FROM ((((USK.dbo.ORIN ORIN INNER JOIN
USK.dbo.RIN1 RIN1 ON ORIN.DocEntry=RIN1.DocEntry) INNER JOIN
USK.dbo.OCRD OCRD ON ORIN.CardCode=OCRD.CardCode) INNER JOIN
USK.dbo.OITM OITM ON RIN1.ItemCode=OITM.ItemCode) INNER JOIN
USK.dbo.OCRG OCRG ON OCRD.GroupCode=OCRG.GroupCode)
WHERE --ORIN.DocDate>='01/09/2019' AND ORIN.DocDate<='01/09/2019' and ORIN.CANCELED ='N'
ORIN.DocDate>={?Desde}AND ORIN.DocDate<={?Hasta} and ORIN.CANCELED ='N'
And ORIN.doctype <> 'S'
union all
-- se resta las NC de servicio
SELECT ORIN.DocNum,
RIN1.Dscription as Itemcode,
0 as Quantity,
ORIN.DocType,
ORIN.DocDate,
ORIN.CardCode,
case when RIN1.currency <>'MXN' then RIN1.Price * RIN1.Rate when RIN1.currency ='MXN' then RIN1.Price end*-1 as Price,
case when RIN1.currency ='MXN' then 0 when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.INMPrice when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end end *-1 as PriceUSD,
case when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.Price when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end*case when RIN1.Rate=0 then (select rate from ORTT where RateDate=ORIN.DocDate and currency=rin1.Currency) else RIN1.rate end when RIN1.currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then RIN1.Price when ORIN.DiscPrcnt = 0 then RIN1.Price else RIN1.Price end end * -1 as PriceMXN,
case when RIN1.Currency <>'MXN' then RIN1.TotalFrgn else 0 end*-1 as VentaUsd,
case when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100)))*-1 else RIN1.LineTotal*-1 end
when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100)))*-1 else RIN1.LineTotal*-1 end end as VentaMXN,
RIN1.Currency,
RIN1.Dscription,
0 as LastPurPrc,
0 AVG_Amt,
0 as c_prom_vta,
'N/A' as LastPurCur,
case when rin1.Currency <>'MXN' then rin1.rate else 0 end as rateusd,
case when RIN1.Rate=0 then (select rate from ORTT where RateDate=orin.DocDate and currency=rin1.Currency) else RIN1.rate end as rate,
case when rin1.Currency <>'MXN' then 1 end as Numrate,
ORIN.CardName,
ORIN.DiscPrcnt,
RIN1.GrossBuyPr,
OCRG.GroupName,
RIN1.Dscription as ItemName,
ORIN.U_cancela,
rin1.acctcode,
--(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,'01/01/2019') and Currency='USD' ) promrate,
(select avg(rate) [promrate] from ortt where datename(yy,RateDate)=datename(YEAR,{?Desde}) and Currency='USD' ) promrate,
--0 as StockValue
case when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100))) else RIN1.LineTotal end
when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100))) else RIN1.LineTotal end end *-1 as StockValue
,
case when RIN1.Currency ='MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100))) else RIN1.LineTotal end
when RIN1.currency <>'MXN' then case when ORIN.DiscPrcnt <> 0 then (rin1.linetotal-(rin1.linetotal*(orin.DiscPrcnt/100))) else RIN1.LineTotal end end *-1 as CostoBruto
FROM USK.dbo.ORIN ORIN INNER JOIN
USK.dbo.RIN1 RIN1 ON ORIN.DocEntry=RIN1.DocEntry INNER JOIN
USK.dbo.OCRD OCRD ON ORIN.CardCode=OCRD.CardCode INNER JOIN
USK.dbo.OCRG OCRG ON OCRD.GroupCode=OCRG.GroupCode
WHERE --ORIN.DocDate>='01/09/2019' AND ORIN.DocDate<= '01/09/2019' and ORIN.CANCELED ='N'
ORIN.DocDate>={?Desde}AND ORIN.DocDate<={?Hasta}and ORIN.CANCELED ='N'
and RIN1.AcctCode in ('505','205','401','402','404','406','410' ,'411','750','720001','720002') and orin.doctype ='S'