ALTER VIEW [dbo].[_Venta] AS
SELECT     CodEsta, REPLACE(REPLACE(REPLACE(TipoFac, 'B', 'DEV'), 'A', 'FAC'),'R','REC') AS TIPO,TIPOFAC, 
OrdenC, NumeroD, Fechae ,Contado, Credito, CodUsua, CodVend ,Factor, CodClie, Descrip,  
(MtoTax + TGravable + TExento) * Signo AS TOTAL, 
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'O')),0) * Signo AS DESCUENTO, 
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'O')),0)* 100 / CASE WHEN (MtoTax + TGravable + TExento) <> 0 THEN (MtoTax + TGravable + TExento)*signo ELSE 1 END AS PorDesc,
signo,
(SELECT TOP (1) Monto FROM dbo.SACVEN WHERE (CodVend = dbo.safact.codvend) AND (TipoRgo = 5) AND (NroUnico = 1)) as porComision, 
CASE WHEN ordenc = 'SOLO DOLAR' THEN 1 ELSE FACTOR END AS DFACTOR,  
ISNULL((SELECT     SUM(Impuesto) AS Expr1 FROM dbo.SAIPAVTA  WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'D')),0) * Signo AS D,  
ISNULL((SELECT     SUM(Impuesto) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'Z')),0) * Signo AS Z,  
ISNULL((SELECT     SUM(Impuesto) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'AD')),0) * Signo AS AD,  
ISNULL((SELECT     SUM(Impuesto) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'CD')),0) * Signo AS CD,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'B')),0) * Signo AS B,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'P')),0) * Signo AS P,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago IN( 'T'))),0) * Signo AS T,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago IN( 'M'))),0) * Signo AS M,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'X')),0) * Signo AS X,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'GD')),0) * Signo AS GD,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'GB')),0) * Signo AS GB,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'FD')),0) * Signo AS FD,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'FB')),0) * Signo AS FB,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'DCP')),0) * Signo AS DCP,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago = 'USDT')),0) * Signo AS USDT,  
ISNULL((SELECT     SUM(MONTO) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (IMPUESTO=0) AND (CodPago NOT IN('D','AD','Z','B','P','T','M','X','FD','FB','GD','GB','DCP','USDT'))),0) * Signo AS OTROS,  
ISNULL((SELECT     MAX(DESCRIP) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago IN( 'T','M'))),'')  AS DTM,  
ISNULL((SELECT     MAX(REFERENCIA) AS Expr1 FROM dbo.SAIPAVTA WHERE (TipoFac = dbo.SAFACT.TipoFac) AND (NumeroD = dbo.SAFACT.NumeroD) AND (CodPago IN( 'CD'))),'')  AS DCD From dbo.SAFACT WHERE     (TipoFac IN ('A', 'B','R'))
