Buenas tardes les comparto el query de libro de compras , por si en su pais lo utilizan en los cierres contables.
saludos cordiales
set dateformat DMY
Create table #LIBROCV004(
TIPO_DOCTO varchar(3)
,SERIES varchar(20)
,S_NUMATCARD varchar(100)
,U_FACSERIE varchar(20)
,LICTRADNUM varchar(20)
,CARDCODE varchar(20)
,U_FACNIT varchar(20)
,DOCNUM varchar(20)
,U_FACNUM varchar(20)
,N_NUMATCARD varchar(100)
,CARDNAME varchar(100)
,U_FACNOM varchar(100)
,TAXDATE datetime
,DOCDATE datetime
,DOCDUEDATE datetime
,U_FACFECHA datetime
,TITULO_DOCTO varchar(100)
,SUCURSAL varchar(50)
,RESOLUCION varchar(50)
,TOTAL money
,IVA money
,IMPORTACIONES money
,BIEN_GRAVADO money
,BIEN_EXENTO money
,SERVICIO_GRAVADO money
,SERVICIO_EXENTO money
,TIPO_TRAN char(1)
,TIPO_LIBRO Char(1)
,ESTADO_DOCTO char(1)
,NUMERO_EXENCION varchar(15)
,VALOR_EXENCION money
,BIEN_PEQCONT money
,SERVICIO_PEQCONT money
,MES varchar(15)
,ANIO varchar(4)
,U_NIT varchar(20))
Declare
@linea varchar(375)
,@fecha1 datetime
,@fecha2 datetime
,@nota_anulacion varchar(8)
,@titulo_reporte varchar(100)
,@resolucion varchar(50)
,@total_iva money
,@graba smallint
,@serie_nc varchar(20)
,@TargetType int
,@TrgetEntry int
,@vatsum money
,@U_TipoA char(1)
,@taxcode varchar(15)
,@total_con_iva money
,@total_sin_iva money
,@importaciones money
,@bien_gravado money
,@servicio_gravado money
,@bien_exento money
,@servicio_exento money
,@bien_peqcont money
,@servicio_peqcont money
,@Docentry money
,@ok smallint
,@gastos_sin_iva money
,@iva_gastos money
,@doctotal money
,@seriesname varchar(20)
,@c_serie varchar(20)
,@c_nit varchar(20)
,@c_numero varchar(20)
,@c_nombre varchar(100)
,@c_fecha varchar(15)
,@c_fecha_visualizar varchar(15)
,@pais_cliente varchar(3)
,@pais_db varchar(3)
,@tipo_tran char(1)
,@ok2 smallint
,@cod_sucursal varchar(8)
,@suc_ini varchar(8)
,@suc_fin varchar(8)
,@tienda varchar(8)
,@nombre_tienda varchar(100)
,@estado_docto char(1)
,@status_nc char(1)
,@c_mostrar_anul char(1)
,@total_documento money
,@ok3 smallint
,@reg int
,@mes varchar(15)
,@anio varchar(4)
,@subtipo varchar(2)
,@tipo varchar(2)
,@fac_ant char(1)
,@por_impuesto money
,@codigo_iva varchar(10)
,@SERIES varchar(20)
,@NUMATCARD varchar(100)
,@S_NUMATCARD varchar(100)
,@U_FACSERIE varchar(20)
,@LICTRADNUM varchar(15)
,@CARDCODE varchar(15)
,@U_FACNIT varchar(15)
,@DOCNUM int
,@U_FACNUM varchar(20)
,@CARDNAME varchar(100)
,@U_FACNOM varchar(100)
,@TAXDATE datetime
,@DOCDATE datetime
,@DOCDUEDATE datetime
,@U_FACFECHA datetime
,@U_NIT varchar(20)
,@discsum numeric(16,4)
,@rounddif numeric(16,4)
,@total_detalles numeric(16,4)
,@subtipo2 char(3)
Select @Reg = count(1) from [dbo].[OPCH] T0 where T0.[docDate] >= '[%0]' and T0.[docDate] <= '[%1]' and T0.[U_TIENDA] = '[%2]' and 1=2
Select @fecha1 = '[%0]'
Select @fecha2 = '[%1]'
Select @cod_sucursal = '[%2]'
--Set @fecha1 = '25/03/2010'
--Set @fecha2 = '25/03/2010'
--Set @cod_sucursal = '01'
Set @mes = month(@fecha1)
Set @anio = year(@fecha1)
If @cod_sucursal is null or @cod_sucursal = '' Begin
Set @Suc_ini = ' '
Set @Suc_fin = 'zzzzz'
End
Else begin
Set @Suc_ini = @cod_sucursal
Set @Suc_fin = @cod_sucursal
end
Select @pais_db =country From OADM
Select @nota_anulacion= U_NC_ANULACION
,@titulo_reporte =U_TITULO_REPORTE
,@resolucion =U_RESOLUCION
,@c_serie =U_TIPO_SERIE
,@c_nit =U_TIPO_NIT
,@c_numero =U_NUMERO_DOCUMENTO
,@c_nombre =U_NOMBRE_FACTURA
,@c_fecha =U_TIPO_FECHA
,@c_mostrar_anul =U_MOSTRAR_ANULADAS
,@c_fecha_visualizar = U_FECHA_VISUALIZAR
from [dbo].[@LIBRO_COMPRAS]
where code = 1
Declare com_enc cursor for Select SERIES,NUMATCARD,U_FACSERIE,LICTRADNUM ,CARDCODE ,U_FACNIT,DOCNUM,U_FACNUM,NUMATCARD,CARDNAME,U_FACNOM
,TAXDATE,DOCDATE,DOCDUEDATE,U_FACFECHA,DOCENTRY,doctotal,u_tienda,DocSubType,'N',discsum,rounddif
,U_TIPO_DOCUMENTO,u_status_nc
From OPCH Where CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end >= @fecha1
and
CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end <= @fecha2
and Isnull(u_tienda,' ') >= @Suc_ini and Isnull(u_tienda,'zzzzz') <= @Suc_fin
union all Select SERIES,NUMATCARD,U_FACSERIE,LICTRADNUM ,CARDCODE ,U_FACNIT,DOCNUM,U_FACNUM,NUMATCARD,CARDNAME,U_FACNOM
,TAXDATE,DOCDATE,DOCDUEDATE,U_FACFECHA,DOCENTRY,doctotal,u_tienda,DocSubType,'S',discsum,rounddif
,U_TIPO_DOCUMENTO,u_status_nc
From ODPO Where CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end >= @fecha1
and
CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end <= @fecha2
and Isnull(u_tienda,' ') >= @Suc_ini and Isnull(u_tienda,'zzzzz') <= @Suc_fin
and createtran = 'Y'
Open com_enc
Fetch com_enc into @SERIES, @S_NUMATCARD ,@U_FACSERIE
,@LICTRADNUM ,@CARDCODE ,@U_FACNIT,@DOCNUM ,@U_FACNUM ,@NUMATCARD
,@CARDNAME, @U_FACNOM ,@TAXDATE ,@DOCDATE ,@DOCDUEDATE ,@U_FACFECHA,@docentry,@doctotal,@tienda,@subtipo
,@fac_ant,@discsum,@rounddif,@subtipo2,@status_nc
While @@fetch_status = 0 begin
If @subtipo = 'DN' Set @tipo = 'ND' Else Set @tipo = 'FC'
If @tienda is null or @tienda = ' ' Select @nombre_tienda = 'Todas' else Select @nombre_tienda= U_nomsuc from [dbo].[@TIENDAS] where code = @tienda
If @SERIES <> '-1' Select @seriesname= seriesname from NNM1 where series = @SERIES Else Select @seriesname= 'Manual'
Set @ok2 = 0 Set @total_documento = 0
Set @estado_docto = 'E'
If @c_serie = 'SERIES' and @seriesname in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C') Set @ok2 = 1
If @c_serie = 'S_NUMATCARD' and @s_numatcard in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C') Set @ok2 = 1
If @c_serie = 'U_FACSERIE' and @u_facserie in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C') Set @ok2 = 1
select @U_NIT = U_NIT FROM OCRD WHERE CARDCODE = @CARDCODE
If @ok2 = 0 begin
If @status_nc = 'V' begin
Set @total_iva= 0 set @graba = 0 set @serie_nc = ''
Select @pais_cliente = Isnull(Country,(select Country from OADM)) FROM CRD1 where cardcode =@CARDCODE
If @pais_cliente = @pais_db Set @tipo_tran = 'L' Else Set @tipo_tran = 'I'
--If @tipo_tran = 'L' begin
Select @importaciones = 0, @bien_gravado =0 , @servicio_gravado =0 ,
@servicio_exento= 0,@bien_exento= 0, @bien_peqcont = 0, @servicio_peqcont = 0
Select @total_detalles = sum(linetotal) from PCH1 where docentry = @Docentry
If @fac_ant = 'N' Begin
Declare com_det cursor for
Select TargetType, TrgetEntry, vatsum, substring(U_TipoA,1,1),taxcode,GTotal, lineTotal
From PCH1 Where Docentry = @Docentry and substring(U_TipoA,1,1) in('B','S','I','E')
End
Else begin
Declare com_det cursor for
Select TargetType, TrgetEntry, vatsum, substring(U_TipoA,1,1),taxcode,GTotal, lineTotal
From DPO1 Where Docentry = @Docentry and substring(U_TipoA,1,1) in('B','S','I','E')
End
Open com_det
Fetch com_det into
@TargetType, @TrgetEntry, @vatsum, @U_TipoA,@taxcode, @total_con_iva, @total_sin_iva
While @@fetch_status = 0 begin
Select @OK = 0
If @TargetType = 19 begin /* buscamos si la nota fue por anualcion*/
Select @OK = 1, @serie_nc = Isnull(t2.seriesname,' ')
From ORPC T0, RPC1 T1, NNM1 T2
Where T0.Docentry = T1.Docentry
and T0.Docentry = @TrgetEntry
and T0.series= T2.series
and T2.seriesname = @nota_anulacion
end
If @OK = 0 begin
--quitar los descuentos y redondeos globales
If @total_detalles <> 0 begin
Set @total_sin_iva =@total_sin_iva -((@total_sin_iva*(@discsum-@rounddif))/@total_detalles)
end
Else Begin
Set @total_sin_iva =@total_sin_iva -((@total_sin_iva*(@discsum-@rounddif))/1)
end
If @U_TipoA = 'I' begin
If @tipo_tran = 'L' begin
Select @taxcode = Isnull(taxcodevnd,'xxx') from OADM
select @por_impuesto = Isnull(rate,0) from OSTA WHERE code = @taxcode
If @por_impuesto <> 0 begin
Set @importaciones = @importaciones + round(@total_sin_iva/(@por_impuesto/100),2)
End
Else Begin
Set @importaciones = @importaciones + round(@total_sin_iva/1,2)
End
Set @total_iva= @total_iva + @total_sin_iva
If @por_impuesto <> 0 begin
Set @total_sin_iva = round(@total_sin_iva/(@por_impuesto/100),2)+@total_sin_iva
End
Else Begin
Set @total_sin_iva = round(@total_sin_iva/1,2)+@total_sin_iva
end
End
Else begin
Set @importaciones = @importaciones + @total_sin_iva
Set @total_iva= @total_iva + @vatsum
End
Set @graba = 1
end
If @U_TipoA = 'B' begin
If @vatsum <> 0 Select @bien_gravado = @bien_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @bien_peqcont = @bien_peqcont + @total_sin_iva
else Select @bien_exento= @bien_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'S' begin
If @vatsum <> 0 Select @servicio_gravado = @servicio_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @servicio_peqcont = @servicio_peqcont + @total_sin_iva
else Select @servicio_exento = @servicio_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'E' begin
Select @bien_exento = @bien_exento + @total_sin_iva
Set @graba = 1
End
If (@U_TipoA = 'I') or (@U_TipoA = 'B') or (@U_TipoA = 'S') or (@U_TipoA = 'E') begin
Set @total_documento = @total_documento + @total_sin_iva +round(@vatsum,2)
end
End
Else Begin
If @serie_nc = @nota_anulacion begin
Set @U_FACNOM = '***** FACTURA ANULADA ******'
Set @CARDNAME = '***** FACTURA ANULADA ******'
Set @total_documento = 0
Set @estado_docto = 'A'
End
Set @graba = 1
If @c_mostrar_anul = 'N' Set @graba = 0
End
Fetch com_det into @TargetType, @TrgetEntry, @vatsum, @U_TipoA,@taxcode, @total_con_iva, @total_sin_iva
End
Close com_det
Deallocate com_det
If @serie_nc <> @nota_anulacion and @fac_ant = 'N' begin
/* GASTOS ADICIONALES */
Declare gastos_ad cursor for
Select linetotal, vatsum,u_tipoA
From PCH3
Where docentry = @Docentry
open gastos_ad
Fetch gastos_ad into @total_sin_iva, @vatsum ,@U_TipoA
While @@fetch_status = 0 begin
If @U_TipoA = 'B' begin
If @vatsum <> 0 Select @bien_gravado= @bien_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @bien_peqcont = @bien_peqcont + @total_sin_iva
else Select @bien_exento= @bien_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'S' begin
If @vatsum <> 0 Select @servicio_gravado = @servicio_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @servicio_peqcont = @servicio_peqcont + @total_sin_iva
else Select @servicio_exento = @servicio_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'E' begin
Select @bien_exento = @bien_exento + @total_sin_iva
Set @graba = 1
End
If (@U_TipoA = 'I') or (@U_TipoA = 'B') or (@U_TipoA = 'S') or (@U_TipoA = 'E') begin
Set @total_documento = @total_documento + @total_sin_iva +round(@vatsum,2)
end
Fetch gastos_ad into @total_sin_iva, @vatsum ,@U_TipoA
end
close gastos_ad
Deallocate gastos_ad
end
End
Else Begin
Set @U_FACNOM = '***** FACTURA ANULADA ******'
Set @CARDNAME = '***** FACTURA ANULADA ******'
Set @total_documento= 0
Set @estado_docto = 'A'
If @c_mostrar_anul = 'N' Set @graba = 0 else Set @graba = 1
End
If @graba = 1 begin
Insert into #LIBROCV004(TIPO_DOCTO ,SERIES ,S_NUMATCARD ,U_FACSERIE ,LICTRADNUM ,CARDCODE ,U_FACNIT
,DOCNUM ,U_FACNUM ,N_NUMATCARD ,CARDNAME ,U_FACNOM ,TAXDATE,DOCDATE ,DOCDUEDATE
,U_FACFECHA ,TITULO_DOCTO ,SUCURSAL ,RESOLUCION, TOTAL ,IVA ,IMPORTACIONES ,BIEN_GRAVADO
,BIEN_EXENTO,SERVICIO_GRAVADO ,SERVICIO_EXENTO,TIPO_TRAN,TIPO_LIBRO,ESTADO_DOCTO,
NUMERO_EXENCION,VALOR_EXENCION,BIEN_PEQCONT,SERVICIO_PEQCONT,MES,ANIO,U_NIT)
Select @subtipo2, @seriesname, @S_NUMATCARD ,@U_FACSERIE ,@LICTRADNUM ,@CARDCODE ,@U_FACNIT
,@DOCNUM ,@U_FACNUM ,@NUMATCARD ,@CARDNAME, @U_FACNOM ,@TAXDATE ,@DOCDATE ,@DOCDUEDATE
,@U_FACFECHA ,@titulo_reporte ,@nombre_tienda ,@resolucion ,@total_documento,round(@total_iva,2),@importaciones, @bien_gravado
,@bien_exento, @servicio_gravado,@servicio_exento,@tipo_tran,'C',@estado_docto,' ' ,0.00, @bien_peqcont, @servicio_peqcont ,@mes, @anio,@U_NIT
end
--end /*tipo=L*/
end /*ok2=0*/
Fetch com_enc into @SERIES, @S_NUMATCARD ,@U_FACSERIE
,@LICTRADNUM ,@CARDCODE ,@U_FACNIT
,@DOCNUM ,@U_FACNUM ,@NUMATCARD
,@CARDNAME, @U_FACNOM
,@TAXDATE ,@DOCDATE ,@DOCDUEDATE ,@U_FACFECHA,@docentry,@doctotal,@tienda,@subtipo,@fac_ant,@discsum,@rounddif
,@subtipo2,@status_nc
end
close com_enc
deallocate com_enc
/* -------------------*/
/* NOTAS DE CREDITO */
/* -------------------*/
Set @tipo = 'NC'
Declare not_enc cursor for Select SERIES,NUMATCARD,U_FACSERIE,LICTRADNUM ,CARDCODE ,U_FACNIT,DOCNUM,U_FACNUM,NUMATCARD,CARDNAME,U_FACNOM
,TAXDATE,DOCDATE,DOCDUEDATE,U_FACFECHA,DOCENTRY,doctotal,u_tienda,u_status_nc,discsum,rounddif,U_TIPO_DOCUMENTO
From ORPC Where CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end >= @fecha1
and
CASE when @c_fecha = 'DocDate' Then (Select DocDate)
when @c_fecha = 'DocDueDate' Then (select DocDueDate)
when @c_fecha = 'TaxDate' Then (Select TaxDate)
when @c_fecha = 'U_FacFecha' Then (Select U_FacFecha) end <= @fecha2
and Isnull(u_tienda,' ') >= @Suc_ini and Isnull(u_tienda,'zzzzz') <= @Suc_fin
Open not_enc
Fetch not_enc into @SERIES, @S_NUMATCARD ,@U_FACSERIE
,@LICTRADNUM ,@CARDCODE ,@U_FACNIT,@DOCNUM ,@U_FACNUM ,@NUMATCARD
,@CARDNAME, @U_FACNOM ,@TAXDATE ,@DOCDATE ,@DOCDUEDATE ,@U_FACFECHA,@docentry,@doctotal,@tienda,@status_nc,@discsum,@rounddif,@subtipo2
While @@fetch_status = 0 begin
Set @graba = 1
Set @total_documento = 0
Set @estado_docto = 'A'
If @tienda is null or @tienda = ' ' Select @nombre_tienda = 'Todas' else Select @nombre_tienda= U_nomsuc from [dbo].[@TIENDAS] where code = @tienda
If @SERIES <> '-1' Select @seriesname= seriesname from NNM1 where series = @SERIES Else Select @seriesname= 'Manual'
Set @ok2 = 0
Set @estado_docto = 'E'
If @c_serie = 'SERIES' and (@seriesname = @nota_anulacion or @seriesname in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C')) Set @ok2 = 1
If @c_serie = 'S_NUMATCARD' and (@s_numatcard = @nota_anulacion or @numatcard in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C')) Set @ok2 = 1
If @c_serie = 'U_FACSERIE' and (@u_facserie = @nota_anulacion or @u_facserie in (select U_serie from [dbo].[@LIBROCV] where U_tipo = 'C')) Set @ok2 = 1
select @U_NIT = U_NIT FROM OCRD WHERE CARDCODE = @CARDCODE
If @ok2 = 0 begin
Set @total_iva= 0 set @graba = 0 set @serie_nc = ''
Select @pais_cliente = Isnull(Country,(select Country from OADM)) FROM CRD1 where cardcode =@CARDCODE
If @pais_cliente = @pais_db Set @tipo_tran = 'L' Else Set @tipo_tran = 'I'
Select @importaciones = 0, @bien_gravado =0 , @servicio_gravado =0 ,@servicio_exento= 0,@bien_exento= 0
Select @total_detalles = sum(linetotal) from PCH1 where docentry = @Docentry
If @status_nc = 'V' begin
Declare not_det cursor for
Select TargetType, TrgetEntry, vatsum, substring(U_TipoA,1,1),taxcode,GTotal, lineTotal
From RPC1 Where Docentry = @Docentry and substring(U_TipoA,1,1) in('B','S','I','E')
Open not_det
Fetch not_det into
@TargetType, @TrgetEntry, @vatsum, @U_TipoA,@taxcode, @total_con_iva, @total_sin_iva
While @@fetch_status = 0 begin
--quitar los descuentos y redondeos globales
If @total_detalles <> 0 begin
Set @total_sin_iva =@total_sin_iva -((@total_sin_iva*(@discsum-@rounddif))/@total_detalles)
End
Else Begin
Set @total_sin_iva =@total_sin_iva -((@total_sin_iva*(@discsum-@rounddif))/1)
End
If @U_TipoA = 'I' begin
If @tipo_tran = 'L' begin
Select @taxcode = Isnull(taxcodevnd,'xxx') from OADM
select @por_impuesto = Isnull(rate,0) from OSTA WHERE code = @taxcode
If @por_impuesto <> 0 begin
Set @importaciones = @importaciones + round(@total_sin_iva/(@por_impuesto/100),2)
End
Else Begin
Set @importaciones = @importaciones + round(@total_sin_iva/1,2)
End
Set @total_iva= @total_iva + @total_sin_iva
If @por_impuesto <> 0 begin
Set @total_sin_iva = round(@total_sin_iva/(@por_impuesto/100),2)+@total_sin_iva
end
Else Begin
Set @total_sin_iva = round(@total_sin_iva/1,2)+@total_sin_iva
End
End
Else begin
Set @importaciones = @importaciones + @total_sin_iva
Set @total_iva= @total_iva + @vatsum
End
Set @graba = 1
End
If @U_TipoA = 'B' begin
If @vatsum <> 0 Select @bien_gravado= @bien_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @bien_peqcont = @bien_peqcont + @total_sin_iva
else Select @bien_exento= @bien_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'S' begin
If @vatsum <> 0 Select @servicio_gravado = @servicio_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @servicio_peqcont = @servicio_peqcont + @total_sin_iva
else Select @servicio_exento = @servicio_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'E' begin
Select @bien_exento = @bien_exento + @total_sin_iva
Set @graba = 1
End
If (@U_TipoA = 'I') or (@U_TipoA = 'B') or (@U_TipoA = 'S') or (@U_TipoA = 'E') begin
Set @total_documento = @total_documento + @total_sin_iva + round(@vatsum,2)
end
Fetch not_det into @TargetType, @TrgetEntry, @vatsum, @U_TipoA,@taxcode, @total_con_iva, @total_sin_iva
End
Close not_det
Deallocate not_det
/* GASTOS ADICIONALES */
Declare gastos_ad cursor for
Select linetotal, vatsum,u_tipoA
From RPC3
Where docentry = @Docentry
open gastos_ad
Fetch gastos_ad into @total_sin_iva, @vatsum ,@U_TipoA
While @@fetch_status = 0 begin
If @U_TipoA = 'B' begin
If @vatsum <> 0 Select @bien_gravado= @bien_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @bien_peqcont = @bien_peqcont + @total_sin_iva
else Select @bien_exento= @bien_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'S' begin
If @vatsum <> 0 Select @servicio_gravado = @servicio_gravado + @total_sin_iva
Else
If @subtipo2 = 'FPQ' Select @servicio_peqcont = @servicio_peqcont + @total_sin_iva
else Select @servicio_exento = @servicio_exento + @total_sin_iva
Set @graba = 1
Set @total_iva= @total_iva + @vatsum
end
If @U_TipoA = 'E' begin
Select @bien_exento = @bien_exento + @total_sin_iva
Set @graba = 1
End
If (@U_TipoA = 'I') or (@U_TipoA = 'B') or (@U_TipoA = 'S') or (@U_TipoA = 'E') begin
Set @total_documento = @total_documento + @total_sin_iva +round(@vatsum,2)
end
Fetch gastos_ad into @total_sin_iva, @vatsum ,@U_TipoA
end
close gastos_ad
Deallocate gastos_ad
End
Else begin
Set @U_FACNOM = '***** NOTA CREDITO ANULADA ******'
Set @CARDNAME = '***** NOTA CREDITO ANULADA ******'
Set @total_documento= 0
Set @estado_docto = 'A'
If @c_mostrar_anul = 'N' Set @graba = 0 else Set @graba = 1
End
If @graba = 1 begin
Insert into #LIBROCV004(TIPO_DOCTO ,SERIES ,S_NUMATCARD ,U_FACSERIE ,LICTRADNUM ,CARDCODE ,U_FACNIT
,DOCNUM ,U_FACNUM ,N_NUMATCARD ,CARDNAME ,U_FACNOM ,TAXDATE,DOCDATE ,DOCDUEDATE
,U_FACFECHA ,TITULO_DOCTO ,SUCURSAL ,RESOLUCION, TOTAL ,IVA ,IMPORTACIONES,BIEN_GRAVADO
,BIEN_EXENTO,SERVICIO_GRAVADO ,SERVICIO_EXENTO,TIPO_TRAN,TIPO_LIBRO,ESTADO_DOCTO,
NUMERO_EXENCION,VALOR_EXENCION,BIEN_PEQCONT,SERVICIO_PEQCONT,MES,ANIO,U_NIT)
Select @subtipo2, @seriesname, @S_NUMATCARD ,@U_FACSERIE ,@LICTRADNUM ,@CARDCODE ,@U_FACNIT
,@DOCNUM ,@U_FACNUM ,@NUMATCARD ,@CARDNAME, @U_FACNOM ,@TAXDATE ,@DOCDATE ,@DOCDUEDATE
,@U_FACFECHA ,@titulo_reporte ,@nombre_tienda,@resolucion ,@total_documento*-1,round(@total_iva*-1,2),@importaciones*-1, @bien_gravado*-1
,@bien_exento*-1, @servicio_gravado*-1,@servicio_exento*-1,@tipo_tran,'C',@estado_docto,' ',0.00,@bien_peqcont,@servicio_peqcont,@mes,@anio,@U_NIT
end
end /*ok2=0*/
Fetch not_enc into @SERIES, @S_NUMATCARD ,@U_FACSERIE
,@LICTRADNUM ,@CARDCODE ,@U_FACNIT,@DOCNUM ,@U_FACNUM ,@NUMATCARD
,@CARDNAME, @U_FACNOM ,@TAXDATE ,@DOCDATE ,@DOCDUEDATE ,@U_FACFECHA,@docentry,@doctotal,@tienda,@status_nc,@discsum,@rounddif,@subtipo2
end
close not_enc
Deallocate not_enc
-- se cambia el cambio @c_fecha para que muestre la fecha del documento
Set @c_fecha =@c_fecha_visualizar
If @cod_sucursal is null
Set @linea = 'Select TIPO_LIBRO, '+ @c_fecha+ ',TIPO_DOCTO,'+ @c_serie+ ',' + @c_numero + ',TIPO_TRAN,ESTADO_DOCTO,'+@c_nit +',' +@c_nombre+
',IMPORTACIONES, BIEN_GRAVADO, SERVICIO_GRAVADO, BIEN_EXENTO, SERVICIO_EXENTO, IVA, TOTAL ,SUCURSAL,NUMERO_EXENCION,VALOR_EXENCION '+
',TITULO_DOCTO,BIEN_PEQCONT,SERVICIO_PEQCONT,MES,ANIO from #LIBROCV004 order by '+@c_fecha +', TIPO_DOCTO ,' + @c_serie +',' +@c_numero
Else
Set @linea = 'Select TIPO_LIBRO, '+ @c_fecha+ ',TIPO_DOCTO,'+ @c_serie+ ',' + @c_numero + ',TIPO_TRAN ,ESTADO_DOCTO, '+@c_nit +',' +@c_nombre+
',IMPORTACIONES, BIEN_GRAVADO, SERVICIO_GRAVADO, BIEN_EXENTO, SERVICIO_EXENTO, IVA, TOTAL ,SUCURSAL,NUMERO_EXENCION,VALOR_EXENCION ' +
',TITULO_DOCTO,BIEN_PEQCONT,SERVICIO_PEQCONT,MES,ANIO from #LIBROCV004 order by SUCURSAL, '+@c_fecha +', TIPO_DOCTO ,' + @c_serie +',' +@c_numero
exec(@linea)
Drop table #LIBROCV004
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |