Sie sind auf Seite 1von 317

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_serie_TipoDoc]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].

[serie] DROP CONSTRAINT FK_serie_TipoDoc GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_imo_articulo]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[imo] DROP CONSTRAINT FK_imo_articulo GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_docventa_clientes]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[docventa] DROP CONSTRAINT FK_docventa_clientes GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_zonequ_equipo]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[zonequ] DROP CONSTRAINT FK_zonequ_equipo GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_imo_mov]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[imo] DROP CONSTRAINT FK_imo_mov GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_zonper_personal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[zonper] DROP CONSTRAINT FK_zonper_personal GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_zonal_region]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[zonal] DROP CONSTRAINT FK_zonal_region GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_mov_zonal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[mov] DROP CONSTRAINT FK_mov_zonal GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_parametros_zonal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[parametros] DROP CONSTRAINT FK_parametros_zonal GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_serie_zonal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[serie] DROP CONSTRAINT FK_serie_zonal GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_zonequ_zonal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[zonequ] DROP CONSTRAINT FK_zonequ_zonal GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_zonper_zonal]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[zonper] DROP CONSTRAINT FK_zonper_zonal GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sig]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sig] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_1] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_abrir_cierre_anterior]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_abrir_cierre_anterior] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back1B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back1B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back2B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back2B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back3B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back3B] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back5B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back5B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back6B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back6B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_actualizar_back8B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_actualizar_back8B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_adelantado_delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_adelantado_delete] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_backup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_backup] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_buscar_guia]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_buscar_guia] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_cierre]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_cierre] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_codigo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_codigo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_copiar_doc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_copiar_doc] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_copiar_guia]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_copiar_guia] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_docventa_guia]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_docventa_guia] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_gra_comparativo_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_gra_comparativo_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_gra_comparativo_art_planta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_gra_comparativo_art_planta] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_gra_res_cobranza]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_gra_res_cobranza] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_gra_res_cobranza_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_gra_res_cobranza_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_gra_res_comparativo_cli]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_gra_res_comparativo_cli] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_idocventa_ade_delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_idocventa_ade_delete] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_idocventa_delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_idocventa_delete] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_mes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_mes] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_notacredito]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_notacredito] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_percepciones]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_percepciones] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_perserie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_perserie] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cli_dias]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cli_dias] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cobranza_facturacion]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cobranza_facturacion] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_articulo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_articulo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_facbolart]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_facbolart] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general1A]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general1A] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general1B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general1B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general2A]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general2A] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general2B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general2B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general3]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general3] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general3B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general3B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general4]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general4] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general5]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general5] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general5B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general5B] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general6]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general6] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general6B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general6B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general7]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general7] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general8]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general8] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_general8B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_general8B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_granel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_granel] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_granel1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_granel1] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_granelA]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_granelA] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_guia]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_guia] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_guia_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_guia_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_cons_guias_donfaltante]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_cons_guias_donfaltante] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back1B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back1B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back2B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back2B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back3B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back3B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back5B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back5B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back6B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back6B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_general_back8B]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_general_back8B] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_reg_ventas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_reg_ventas] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ade]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ade] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ade_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ade_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ade_deta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ade_deta] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ade_deta_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ade_deta_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_cre_deta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_cre_deta] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ncr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ncr] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_res_cobranza_ncr_deta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_res_cobranza_ncr_deta] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_rep_ser_pers]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_rep_ser_pers] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cob]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cob] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_art_neto]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_art_neto] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_cliente_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_cliente_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_doc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_doc] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_doc_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_doc_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_val]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_val] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_cobranza_vendedor_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_cobranza_vendedor_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_nuereg_ventas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_nuereg_ventas] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_ant]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_ant] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_art]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_art] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_per]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_per] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_per_new]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_per_new] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_per_resumen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_per_resumen] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_res_reg_ventas_per_zonal]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_res_reg_ventas_per_zonal] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_valida_pers_equ]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_valida_pers_equ] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_valida_pers_equ1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_valida_pers_equ1] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_verifica_sec]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_verifica_sec] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[su_2004]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[su_2004] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[su_daot_2004]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[su_daot_2004] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[su_daot_bol_2004]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[su_daot_bol_2004] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[su_daot_fac_2004]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[su_daot_fac_2004] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[su_ventas_2004]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[su_ventas_2004] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TipoDoc]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TipoDoc] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[amigo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[amigo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[articulo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[articulo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[banco]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[banco] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cli_ant]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[cli_ant] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[clientes]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[clientes] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cobranza]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[cobranza] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[creditos]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[creditos] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[depositar]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[depositar] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[doc_ant]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[doc_ant] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[docventa]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[docventa] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[equipo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[equipo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[exonerado]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[exonerado] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[exonerados]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[exonerados] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gasto]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[gasto] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[idocventa]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[idocventa] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[imo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[imo] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[lista]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[lista] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[marca]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[marca] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[mes]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[mes] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[mov]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[mov] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[num]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[num] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pago]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pago] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[parametros]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[parametros] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pbcatcol]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pbcatcol] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pbcatedt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pbcatedt] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pbcatfmt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pbcatfmt] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pbcattbl]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pbcattbl] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pbcatvld]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pbcatvld] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[perserie]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[perserie] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[personal]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[personal] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pub]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[pub] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[region]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[region] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[serie]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[serie] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[transito]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[transito] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vaucher]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[vaucher] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ventacredito]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[ventacredito] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ventaplanta]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[ventaplanta] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[x]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[x] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[y]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[y] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[zonal]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[zonal] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[zonequ]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[zonequ] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[zonper]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[zonper] GO CREATE TABLE [dbo].[TipoDoc] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [des_tipodoc] [char] (30) COLLATE Modern_Spanish_CI_AS NULL ,

[nco_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[amigo] ( [codigo] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [nombre] [char] (20) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[articulo] ( [cod_art] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [des_art] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [ume_art] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [dim_art] [char] (4) COLLATE Modern_Spanish_CI_AS NOT NULL , [pos_art] [int] NULL , [gui_art] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [kgr_art] [float] NULL , [est_art] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[banco] ( [cod_banco] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [des_banco] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [tel_banco] [char] (10) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[cli_ant] ( [cod_cli] [varchar] (5) COLLATE Modern_Spanish_CI_AS NULL , [razon_soc] [varchar] (35) COLLATE Modern_Spanish_CI_AS NULL , [dir_cli] [varchar] (40) COLLATE Modern_Spanish_CI_AS NULL , [ruc] [varchar] (11) COLLATE Modern_Spanish_CI_AS NULL , [tel_cli] [varchar] (7) COLLATE Modern_Spanish_CI_AS NULL , [contacto] [varchar] (30) COLLATE Modern_Spanish_CI_AS NULL , [a_ruc] [varchar] (8) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[clientes] ( [cod_clientes] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_clientes] [datetime] NULL , [tip_clientes] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [ruc_dni_clientes] [char] (11) COLLATE Modern_Spanish_CI_AS NULL , [rso_clientes] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL , [dir_clientes] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [urb_clientes] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [dis_clientes] [char] (20) COLLATE Modern_Spanish_CI_AS NULL , [tel_clientes] [char] (13) COLLATE Modern_Spanish_CI_AS NULL ,

[fax_clientes] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [bal_clientes] [char] (7) COLLATE Modern_Spanish_CI_AS NULL , [cba_clientes] [int] NULL , [cla_clientes] [char] (20) COLLATE Modern_Spanish_CI_AS NULL , [vlu_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vma_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vmi_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vju_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vvi_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vsa_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [vdo_clientes] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [est_clientes] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [ful_clientes] [datetime] NULL , [per_clientes] [int] NULL , [fpo_clientes] [datetime] NULL , [cod_marca] [char] (5) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[cobranza] ( [fecha] [datetime] NULL , [m00_cli] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [m01_doc] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m02_ser] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m03_sec] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [m04_tip] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m05_gru] [int] NULL , [m06_ele] [int] NULL , [m08_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i04_art] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i05_pun] [float] NULL , [i06_can] [float] NULL , [i07_tot] [float] NULL , [i08_mod] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i09_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i10_fac] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [i11_bol] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [i12_reg] [int] NULL , [a01_art] [int] NULL , [a02_art] [int] NULL , [a03_art] [int] NULL , [a04_art] [int] NULL , [a05_art] [int] NULL , [a06_art] [int] NULL , [a07_art] [int] NULL , [a08_art] [int] NULL , [a09_art] [int] NULL , [a10_art] [int] NULL , [a11_art] [int] NULL , [a12_art] [int] NULL , [a13_art] [int] NULL , [a14_art] [int] NULL ,

[a15_art] [v01_art] [v02_art] [v03_art] [v04_art] [v05_art] [v06_art] [v07_art] [v08_art] [v09_art] [v10_art] [v11_art] [v12_art] [v13_art] [v14_art] [v15_art] [t01_art] [t02_art] [t03_art] [t04_art] [t05_art] [t06_art] [t07_art] [t08_art] [t09_art] [t10_art] [t11_art] [t12_art] [t13_art] [t14_art] [t15_art] [s01_art] [s02_art] [s03_art] [s04_art] [s05_art] [s06_art] [s07_art] [s08_art] [s09_art] [s10_art] [s11_art] [s12_art] [s13_art] [s14_art] [s15_art] [f01_art] [f02_art] [f03_art] [f04_art] [f05_art] [f06_art] [f07_art] [f08_art]

[int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int] [int]

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

[f09_art] [f10_art] [f11_art] [f12_art] [f13_art] [f14_art] [f15_art] [k01_art] [k02_art] [k03_art] [k04_art] [k05_art] [k06_art] [k07_art] [k08_art] [k09_art] [k10_art] [k11_art] [k12_art] [k13_art] [k14_art] [k15_art] [kgr_art] [kg1_art] [kg2_art] [kg3_art] ) ON [PRIMARY] GO

[int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [float] NULL , [float] NULL , [float] NULL , [float] NULL

CREATE TABLE [dbo].[creditos] ( [fecha] [datetime] NULL , [c01_doc] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c02_ser] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c03_sec] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [c04_fin] [datetime] NULL , [c05_zon] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [c06_tip] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c07_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c08_des] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [a01_art] [int] NULL , [a02_art] [int] NULL , [a03_art] [int] NULL , [a04_art] [int] NULL , [a05_art] [int] NULL , [a06_art] [int] NULL , [a07_art] [int] NULL , [a08_art] [int] NULL , [a09_art] [int] NULL , [a10_art] [int] NULL , [a11_art] [int] NULL , [a12_art] [int] NULL , [a13_art] [int] NULL , [a14_art] [int] NULL , [a15_art] [int] NULL ,

[v01_art] [v02_art] [v03_art] [v04_art] [v05_art] [v06_art] [v07_art] [v08_art] [v09_art] [v10_art] [v11_art] [v12_art] [v13_art] [v14_art] [v15_art] [kgr_art] [kg1_art] [des_art] [t01_art] [t02_art] [t03_art] [t04_art] [t05_art] [t06_art] [t07_art] [t08_art] [t09_art] [t10_art] [t11_art] [t12_art] [t13_art] [t14_art] [t15_art] [s01_art] [s02_art] [s03_art] [s04_art] [s05_art] [s06_art] [s07_art] [s08_art] [s09_art] [s10_art] [s11_art] [s12_art] [s13_art] [s14_art] [s15_art] [fac_art] [bol_art] [pre_art] [can_art] [tot_art] [mod_art]

[int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [float] NULL , [float] NULL , [float] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [float] NULL , [float] NULL , [float] NULL , [char] (15) COLLATE Modern_Spanish_CI_AS NULL ,

[est_art] [f01_art] [f02_art] [f03_art] [f04_art] [f05_art] [f06_art] [f07_art] [f08_art] [f09_art] [f10_art] [f11_art] [f12_art] [f13_art] [f14_art] [f15_art] [k01_art] [k02_art] [k03_art] [k04_art] [k05_art] [k06_art] [k07_art] [k08_art] [k09_art] [k10_art] [k11_art] [k12_art] [k13_art] [k14_art] [k15_art] ) ON [PRIMARY] GO

[char] (3) [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL [int] NULL

COLLATE Modern_Spanish_CI_AS NULL , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

CREATE TABLE [dbo].[depositar] ( [fecha] [datetime] NULL , [a01_tit] [char] (30) COLLATE Modern_Spanish_CI_AS NULL [a02_des] [char] (50) COLLATE Modern_Spanish_CI_AS NULL [a03_bal] [char] (30) COLLATE Modern_Spanish_CI_AS NULL [a04_can] [char] (15) COLLATE Modern_Spanish_CI_AS NULL [a05_pre] [char] (15) COLLATE Modern_Spanish_CI_AS NULL [a06_doc] [char] (25) COLLATE Modern_Spanish_CI_AS NULL [a07_tot] [float] NULL , [a08_ban] [float] NULL , [a09_gru] [int] NULL , [a10_sec] [char] (8) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO

, , , , , ,

CREATE TABLE [dbo].[doc_ant] ( [tipo_doc] [varchar] (1) COLLATE Modern_Spanish_CI_AS NULL , [fec_doc] [datetime] NULL , [num_doc] [varchar] (9) COLLATE Modern_Spanish_CI_AS NULL , [cod_cli] [varchar] (5) COLLATE Modern_Spanish_CI_AS NULL ,

[total] [float] NULL , [credito] [bit] NULL , [fec_pago] [datetime] NULL , [anulada] [bit] NULL , [doc_padre] [varchar] (9) COLLATE Modern_Spanish_CI_AS NULL , [a_cuenta] [float] NULL , [juicio] [bit] NULL , [emision] [varchar] (6) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[docventa] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [mod_docventa] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [mot_docventa] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fpa_docventa] [datetime] NULL , [fin_docventa] [datetime] NULL , [fsa_docventa] [datetime] NULL , [fnu_docventa] [datetime] NULL , [ffi_docventa] [datetime] NULL , [cod_clientes] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [bru_docventa] [float] NULL , [igv_docventa] [float] NULL , [tot_docventa] [float] NULL , [cod_tipodocori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serieori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serieori] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_tipodocpad] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_seriepad] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_seriepad] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [glo_docventa] [varchar] (300) COLLATE Modern_Spanish_CI_AS NULL , [est_docventa] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [fco_docventa] [float] NULL , [ode_docventa] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cod_tipodocper] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serieper] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serieper] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [tcliper_docventa] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [porper_docventa] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [mper_docventa] [real] NULL , [ade_docventa] [char] (3) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[equipo] ( [cod_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [des_equ] [char] (40) COLLATE Modern_Spanish_CI_AS NOT NULL , [mar_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL ,

[pla_equ] [cer_equ] [fin_equ] [est_equ] [fin_usr] [cod_usr] ) ON [PRIMARY] GO

[char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [datetime] NOT NULL , [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [datetime] NULL , [char] (15) COLLATE Modern_Spanish_CI_AS NULL

CREATE TABLE [dbo].[exonerado] ( [nro] [float] NULL , [ruc] [varchar] (12) COLLATE Modern_Spanish_CI_AS NULL , [nombre_o_r] [varchar] (44) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[exonerados] ( [nro] [float] NULL , [ruc] [varchar] (12) COLLATE Modern_Spanish_CI_AS NULL , [nombre_o_r] [varchar] (44) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[gasto] ( [ano] [char] (4) COLLATE Modern_Spanish_CI_AS NOT NULL , [mes] [char] (2) COLLATE Modern_Spanish_CI_AS NOT NULL , [dia] [char] (2) COLLATE Modern_Spanish_CI_AS NOT NULL , [fsa_gasto] [datetime] NULL , [mot_gasto] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [tot_gasto] [float] NULL , [glo_gasto] [varchar] (300) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[idocventa] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_art] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [can_idocventa] [float] NULL , [pun_idocventa] [float] NULL , [igv_idocventa] [float] NULL , [tot_idocventa] [float] NULL , [tgv_idocventa] [float] NULL , [est_idocventa] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fco_idocventa] [float] NULL , [vgi_idocventa] [float] NULL , [cod_tipodocpad] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_seriepad] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_seriepad] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_tipodocori] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serieori] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serieori] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fec_idocventa] [datetime] NULL ,

[dur_idocventa] [int] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[imo] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_art] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cge_imo] [float] NULL , [can_imo] [float] NULL , [llen_imo] [float] NULL , [vac_imo] [float] NULL , [cam_imo] [float] NULL , [don_imo] [float] NULL , [fal_imo] [float] NULL , [ffi_imo] [float] NULL , [fco_imo] [float] NULL , [est_imo] [char] (3) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[lista] ( [ruc] [varchar] (17) COLLATE Modern_Spanish_CI_AS NULL , [razon_soci] [varchar] (42) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[marca] ( [cod_marca] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [des_marca] [char] (30) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[mes] ( [ano] [char] (4) COLLATE Modern_Spanish_CI_AS NOT NULL , [mes] [char] (2) COLLATE Modern_Spanish_CI_AS NOT NULL , [nom] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin] [datetime] NOT NULL , [ffi] [datetime] NOT NULL , [dia01] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia02] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia03] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia04] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia05] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia06] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia07] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia08] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia09] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia10] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia11] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia12] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia13] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia14] [char] (1) COLLATE Modern_Spanish_CI_AS NULL ,

[dia15] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia16] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia17] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia18] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia19] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia20] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia21] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia22] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia23] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia24] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia25] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia26] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia27] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia28] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia29] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia30] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [dia31] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[mov] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_mov] [datetime] NULL , [fsa_mov] [datetime] NULL , [fnu_mov] [datetime] NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [tip_cli] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [des_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_clientes] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [ven_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [tip_mov] [char] (18) COLLATE Modern_Spanish_CI_AS NULL , [mot_mov] [char] (18) COLLATE Modern_Spanish_CI_AS NULL , [cod_tipodoc_ori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serie_ori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serie_ori] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [tip_tra] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [tra_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cod_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [co1_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [fac_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [bol_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [fle_mov] [float] NULL , [car_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [emp_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [dir_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [ruc_tra] [char] (11) COLLATE Modern_Spanish_CI_AS NULL , [pla_tra] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [pl1_tra] [char] (10) COLLATE Modern_Spanish_CI_AS NULL ,

[mar_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [ma1_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cer_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cho_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [lic_tra] [char] (12) COLLATE Modern_Spanish_CI_AS NULL , [dni_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [prov_pers] [char] (8) COLLATE Modern_Spanish_CI_AS NULL , [est_mov] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [ode_mov] [char] (300) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[num] ( [co_doc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [fe_ult_sec] [datetime] NULL , [nu_sec_int] [int] NULL , [nu_sec_cad] [char] (8) COLLATE Modern_Spanish_CI_AS NULL , [us_cod] [varchar] (40) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[pago] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_pago] [datetime] NULL , [fsa_pago] [datetime] NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [mod_pago] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_clientes] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [bru_pago] [float] NULL , [igv_pago] [float] NULL , [tot_pago] [float] NULL , [cod_tipodocori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serieori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serieori] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_tipodocpad] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_seriepad] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_seriepad] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [glo_pago] [varchar] (300) COLLATE Modern_Spanish_CI_AS NULL , [est_pago] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fco_pago] [float] NULL , [ter_pago] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [fc1_pago] [float] NULL , [fin_usr] [datetime] NULL , [cod_tipodocper] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serieper] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serieper] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [porper_docventa] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [mper_docventa] [real] NULL ) ON [PRIMARY]

GO CREATE TABLE [dbo].[parametros] ( [cod_param] [char] (1) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [emp_param] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [ruc_param] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [igv_param] [float] NULL , [cod_mar] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [pbo_param] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [pfa_param] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [pbg_param] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [b1_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [b2_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [b3_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [b4_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [be_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [f1_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [f2_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [f3_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [f4_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [fe_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [bg1_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [bg2_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [bg3_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [bg4_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL , [bge_param] [char] (40) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[pbcatcol] ( [pbc_tnam] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [pbc_tid] [int] NULL , [pbc_ownr] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [pbc_cnam] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [pbc_cid] [smallint] NULL , [pbc_labl] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL , [pbc_lpos] [smallint] NULL , [pbc_hdr] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL , [pbc_hpos] [smallint] NULL , [pbc_jtfy] [smallint] NULL , [pbc_mask] [varchar] (31) COLLATE Modern_Spanish_CI_AS NULL , [pbc_case] [smallint] NULL , [pbc_hght] [smallint] NULL , [pbc_wdth] [smallint] NULL , [pbc_ptrn] [varchar] (31) COLLATE Modern_Spanish_CI_AS NULL , [pbc_bmap] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbc_init] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL , [pbc_cmnt] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL , [pbc_edit] [varchar] (31) COLLATE Modern_Spanish_CI_AS NULL , [pbc_tag] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO

CREATE TABLE [dbo].[pbcatedt] ( [pbe_name] [varchar] (30) COLLATE Modern_Spanish_CI_AS NOT NULL , [pbe_edit] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL , [pbe_type] [smallint] NOT NULL , [pbe_cntr] [int] NULL , [pbe_seqn] [smallint] NOT NULL , [pbe_flag] [int] NULL , [pbe_work] [char] (32) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[pbcatfmt] ( [pbf_name] [varchar] (30) COLLATE Modern_Spanish_CI_AS NOT NULL , [pbf_frmt] [varchar] (254) COLLATE Modern_Spanish_CI_AS NOT NULL , [pbf_type] [smallint] NOT NULL , [pbf_cntr] [int] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[pbcattbl] ( [pbt_tnam] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [pbt_tid] [int] NULL , [pbt_ownr] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [pbd_fhgt] [smallint] NULL , [pbd_fwgt] [smallint] NULL , [pbd_fitl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbd_funl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbd_fchr] [smallint] NULL , [pbd_fptc] [smallint] NULL , [pbd_ffce] [char] (32) COLLATE Modern_Spanish_CI_AS NULL , [pbh_fhgt] [smallint] NULL , [pbh_fwgt] [smallint] NULL , [pbh_fitl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbh_funl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbh_fchr] [smallint] NULL , [pbh_fptc] [smallint] NULL , [pbh_ffce] [char] (32) COLLATE Modern_Spanish_CI_AS NULL , [pbl_fhgt] [smallint] NULL , [pbl_fwgt] [smallint] NULL , [pbl_fitl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbl_funl] [char] (1) COLLATE Modern_Spanish_CI_AS NULL , [pbl_fchr] [smallint] NULL , [pbl_fptc] [smallint] NULL , [pbl_ffce] [char] (32) COLLATE Modern_Spanish_CI_AS NULL , [pbt_cmnt] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[pbcatvld] ( [pbv_name] [varchar] (30) COLLATE Modern_Spanish_CI_AS NOT NULL , [pbv_vald] [varchar] (254) COLLATE Modern_Spanish_CI_AS NOT NULL , [pbv_type] [smallint] NOT NULL , [pbv_cntr] [int] NULL , [pbv_msg] [varchar] (254) COLLATE Modern_Spanish_CI_AS NULL

) ON [PRIMARY] GO CREATE TABLE [dbo].[perserie] ( [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [tse_serie] [int] NULL , [sei_serie] [int] NULL , [est_perserie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[personal] ( [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL [tip_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL [ape_pers] [char] (30) COLLATE Modern_Spanish_CI_AS NOT NULL [nom_pers] [char] (30) COLLATE Modern_Spanish_CI_AS NOT NULL [dir_pers] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL [tel_pers] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [fna_pers] [datetime] NOT NULL , [lic_pers] [char] (9) COLLATE Modern_Spanish_CI_AS NULL , [est_pers] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (30) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO , , , , ,

CREATE TABLE [dbo].[pub] ( [nro] [float] NULL , [ruc] [varchar] (12) COLLATE Modern_Spanish_CI_AS NULL , [nombre_o_r] [varchar] (59) COLLATE Modern_Spanish_CI_AS NULL , [tipo] [varchar] (26) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[region] ( [cod_region] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [ciu_region] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL , [dep_region] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[serie] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [est_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY]

GO CREATE TABLE [dbo].[transito] ( [fecha] [datetime] NULL , [m00_cli] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [m01_doc] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m02_ser] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m03_sec] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [m04_tip] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [m05_gru] [int] NULL , [m06_ele] [int] NULL , [m08_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i04_art] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i05_pun] [float] NULL , [i06_can] [float] NULL , [i07_tot] [float] NULL , [i08_mod] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i09_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [i10_fac] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [i11_bol] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [i12_reg] [int] NULL , [a01_art] [int] NULL , [a02_art] [int] NULL , [a03_art] [int] NULL , [a04_art] [int] NULL , [a05_art] [int] NULL , [a06_art] [int] NULL , [a07_art] [int] NULL , [a08_art] [int] NULL , [a09_art] [int] NULL , [a10_art] [int] NULL , [a11_art] [int] NULL , [a12_art] [int] NULL , [a13_art] [int] NULL , [a14_art] [int] NULL , [a15_art] [int] NULL , [v01_art] [int] NULL , [v02_art] [int] NULL , [v03_art] [int] NULL , [v04_art] [int] NULL , [v05_art] [int] NULL , [v06_art] [int] NULL , [v07_art] [int] NULL , [v08_art] [int] NULL , [v09_art] [int] NULL , [v10_art] [int] NULL , [v11_art] [int] NULL , [v12_art] [int] NULL , [v13_art] [int] NULL , [v14_art] [int] NULL , [v15_art] [int] NULL , [t01_art] [int] NULL , [t02_art] [int] NULL , [t03_art] [int] NULL ,

[t04_art] [t05_art] [t06_art] [t07_art] [t08_art] [t09_art] [t10_art] [t11_art] [t12_art] [t13_art] [t14_art] [t15_art] [s01_art] [s02_art] [s03_art] [s04_art] [s05_art] [s06_art] [s07_art] [s08_art] [s09_art] [s10_art] [s11_art] [s12_art] [s13_art] [s14_art] [s15_art] [kgr_art] [kg1_art] ) ON [PRIMARY] GO

[int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [float] NULL , [float] NULL

CREATE TABLE [dbo].[vaucher] ( [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_banco] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [ope_vaucher] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL , [mot_vaucher] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [fec_vaucher] [datetime] NULL , [imp_vaucher] [float] NULL , [glo_vaucher] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [sec_vaucher] [char] (8) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[ventacredito] ( [fecha] [datetime] NULL , [c01_doc] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c02_ser] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c03_sec] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [c04_fin] [datetime] NULL , [c05_zon] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [c06_tip] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [c07_est] [char] (3) COLLATE Modern_Spanish_CI_AS NULL ,

[c08_des] [a01_art] [a02_art] [a03_art] [a04_art] [a05_art] [a06_art] [a07_art] [a08_art] [a09_art] [a10_art] [a11_art] [a12_art] [a13_art] [a14_art] [a15_art] [v01_art] [v02_art] [v03_art] [v04_art] [v05_art] [v06_art] [v07_art] [v08_art] [v09_art] [v10_art] [v11_art] [v12_art] [v13_art] [v14_art] [v15_art] [kgr_art] [kg1_art] [des_art] [t01_art] [t02_art] [t03_art] [t04_art] [t05_art] [t06_art] [t07_art] [t08_art] [t09_art] [t10_art] [t11_art] [t12_art] [t13_art] [t14_art] [t15_art] [s01_art] [s02_art] [s03_art] [s04_art] [s05_art]

[char] (50) COLLATE Modern_Spanish_CI_AS NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [float] NULL , [float] NULL , [float] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL ,

[s06_art] [s07_art] [s08_art] [s09_art] [s10_art] [s11_art] [s12_art] [s13_art] [s14_art] [s15_art] [fac_art] [bol_art] [pre_art] [can_art] [tot_art] [mod_art] [est_art] [f01_art] [f02_art] [f03_art] [f04_art] [f05_art] [f06_art] [f07_art] [f08_art] [f09_art] [f10_art] [f11_art] [f12_art] [f13_art] [f14_art] [f15_art] [k01_art] [k02_art] [k03_art] [k04_art] [k05_art] [k06_art] [k07_art] [k08_art] [k09_art] [k10_art] [k11_art] [k12_art] [k13_art] [k14_art] [k15_art] ) ON [PRIMARY] GO

[int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [float] NULL , [float] NULL , [float] NULL , [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL , [int] NULL

CREATE TABLE [dbo].[ventaplanta] ( [fecha] [datetime] NULL , [c00_des] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [c01_art] [char] (30) COLLATE Modern_Spanish_CI_AS NULL ,

[c02_can] [int] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[x] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_mov] [datetime] NULL , [fsa_mov] [datetime] NULL , [fnu_mov] [datetime] NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [tip_cli] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [des_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [cod_clientes] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [ven_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [tip_mov] [char] (18) COLLATE Modern_Spanish_CI_AS NULL , [mot_mov] [char] (18) COLLATE Modern_Spanish_CI_AS NULL , [cod_tipodoc_ori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_serie_ori] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [sec_serie_ori] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [tip_tra] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [tra_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cod_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [co1_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NULL , [fac_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [bol_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [fle_mov] [float] NULL , [car_mov] [char] (50) COLLATE Modern_Spanish_CI_AS NULL , [emp_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [dir_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [ruc_tra] [char] (11) COLLATE Modern_Spanish_CI_AS NULL , [pla_tra] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [pl1_tra] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [mar_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [ma1_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cer_equ] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [cho_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [lic_tra] [char] (9) COLLATE Modern_Spanish_CI_AS NULL , [dni_tra] [char] (30) COLLATE Modern_Spanish_CI_AS NULL , [prov_pers] [char] (8) COLLATE Modern_Spanish_CI_AS NULL , [est_mov] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [ode_mov] [char] (100) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[y] ( [cod_tipodoc] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_serie] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL , [sec_serie] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_art] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL ,

[cge_imo] [float] NULL , [can_imo] [float] NULL , [llen_imo] [float] NULL , [vac_imo] [float] NULL , [cam_imo] [float] NULL , [don_imo] [float] NULL , [fal_imo] [float] NULL , [ffi_imo] [float] NULL , [fco_imo] [float] NULL , [est_imo] [char] (3) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[zonal] ( [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL [cod_region] [char] (3) COLLATE Modern_Spanish_CI_AS NOT NULL [des_zonal] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL [dir_zonal] [char] (50) COLLATE Modern_Spanish_CI_AS NOT NULL [tel_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [fax_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NULL , [est_zonal] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL ) ON [PRIMARY] GO , , , ,

CREATE TABLE [dbo].[zonequ] ( [mov_zqu] [char] (8) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_equ] [char] (5) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_zqu] [datetime] NULL , [ffi_zqu] [datetime] NULL , [est_zqu] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[zonper] ( [mov_zpe] [char] (8) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_pers] [char] (15) COLLATE Modern_Spanish_CI_AS NOT NULL , [cod_zonal] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL , [fin_zpe] [datetime] NULL , [ffi_zpe] [datetime] NULL , [est_zpe] [char] (3) COLLATE Modern_Spanish_CI_AS NULL , [fin_usr] [datetime] NULL , [cod_usr] [char] (15) COLLATE Modern_Spanish_CI_AS NULL ) ON [PRIMARY] GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

/****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO --select * from creditos where fecha='12-07-2005 00:00:00' --exec sp_rep_cons_general3B 'PLANTA', '12-07-2005 00:00:00', '12-07-2005 23:59:59' CREATE procedure sp_1 (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo DECLARE @guia char(3), @art char(3), @dim char(3), @conta int, @docu char(3), @seri char(3), @secu char(10), @esta char(3) DECLARE @can int, @venta int, @cli char(5), @zde char(10), @ven char(15) DECLARE @fal int, @kgr float, @fac char(3), @bol char(3) declare @c01_doc char(3), @c02_ser char(3), @c03_sec char(10), @c04_fin datetime, @c05_zon char(10), @c06_tip char(3), @c07_est char(3), @c08_des char(50) declare @a01_art int, @a02_art int, @a03_art int, @a04_art int, @a05_art int, @a06_art int, @a07_art int, @a08_art int, @a09_art int, @a10_art int, @a11_art int, @a12_art int, @a13_art int, @a14_art int, @a15_art int declare @v01_art int, @v02_art int, @v03_art int, @v04_art int, @v05_art int, @v06_art int, @v07_art int, @v08_art int, @v09_art int, @v10_art int, @v11_art int, @v12_art int, @v13_art int, @v14_art int, @v15_art int declare @idoc char(3), @iser char(3), @isec char(10), @iart char(3), @ipun float, @ican float, @itot float, @imod char(3), @iest char(3) declare @fac_art char(30), @bol_art char(30), @pre_art float, @can_art float, @tot_art float, @mod_art char(15), @est_art char(3), @des_art float declare @fila int, @linea int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3) select @fle=cod_art from articulo where ltrim(rtrim(des_art)) = 'SERVICIO POR FLETE' select @gra=cod_art from articulo where ltrim(rtrim(des_art)) = 'GAS A GRANEL' select @b12=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 12 KILOS' select @b25=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 2.50 KILOS' -inicia fila y columna declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3)

declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3) declare @tfila int, @ttotal float, @reg int, @tcant int select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @guia=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' delete creditos where fecha=@inicio create table #t1 ( c01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision c02_ser char(3) NULL, -- Codigo de la serie c03_sec char(10) NULL, -- Secuencia de la serie c04_fin datetime NULL, -- Fecha de inicio c05_zon char(10) NULL, -- Zonal Origen c06_tip char(3) null, -- tipo de cliente c07_est char(3) null, -- estado del documento c08_des char(50) null, -- destino a01_art int null default 0, a02_art int null default 0, a03_art int null default 0, a04_art int null default 0, a05_art int null default 0, a06_art int null default 0, a07_art int null default 0, a08_art int null default 0, a09_art int null default 0, a10_art int null default 0, a11_art int null default 0, a12_art int null default 0, a13_art int null default 0, a14_art int null default 0, a15_art int null default 0, v01_art int null default 0, v02_art int null default 0, v03_art int null default 0, v04_art int null default 0, v05_art int null default 0, v06_art int null default 0, v07_art int null default 0, v08_art int null default 0, v09_art int null default 0, v10_art int null default 0, v11_art int null default 0, v12_art int null default 0, v13_art int null default 0, v14_art int null default 0, v15_art int null default 0, kgr_art float null default 0,

kg1_art des_art t01_art t02_art t03_art t04_art t05_art t06_art t07_art t08_art t09_art t10_art t11_art t12_art t13_art t14_art t15_art s01_art s02_art s03_art s04_art s05_art s06_art s07_art s08_art s09_art s10_art s11_art s12_art s13_art s14_art s15_art fac_art bol_art pre_art can_art tot_art mod_art est_art f01_art f02_art f03_art f04_art f05_art f06_art f07_art f08_art f09_art f10_art f11_art f12_art f13_art

float null default 0, float null default 0, int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, char(30) default '', char(30) default '', float null default 0, float null default 0, float null default 0, char(15) null, char(3) null, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0,

int int int int int int int int int int int int int

f14_art f15_art k01_art k02_art k03_art k04_art k05_art k06_art k07_art k08_art k09_art k10_art k11_art k12_art k13_art k14_art k15_art )

int int int int int int int int int int int int int int int int int

null null null null null null null null null null null null null null null null null

default default default default default default default default default default default default default default default default default

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

--Llenando datos de guia en tabla temporal insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est) select distinct m.cod_tipodoc, m.cod_serie, m.sec_serie, m.fin_mov, m.cod_zonal, m.tip_cli, m.est_mov from idocventa i, mov m, pago p where p.cod_tipodocpad=i.cod_tipodoc and p.cod_seriepad=i.cod_serie and p.sec_seriepad=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and p.fsa_pago between @inicio and @final and i.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and m.cod_tipodoc=@guia and m.cod_zonal like ltrim(rtrim(@zonal))+'%' and p.est_pago<>'ANU' order by m.cod_zonal, m.cod_tipodoc, m.cod_serie, m.sec_serie ------------------------------------------------ calculando despachados y vendidos ------------------------------------------------cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in( select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR

select c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN set @can=0 set @venta=0 --calculado cantidad despachada select @can=isnull(can_imo, 0), @venta=isnull(fco_imo, 0) from imo where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu and cod_art=@art /* --calculando cantidad vendida select @venta=isnull(sum(i.can_idocventa), 0)

from idocventa i, docventa d where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_tipodocpad=@docu and i.cod_seriepad=@seri and i.sec_seriepad=@secu and i.cod_art=@art and d.fsa_docventa between @inicio and @final*/ --actualiza cantidad de articulo 1 if @conta=1 begin update #t1 set a01_art=@can, v01_art=@venta, kgr_art= kgr_art + @venta * @kgr, t01_art=@can, s01_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a01_art=@can, v01_art=@can, kgr_art= kgr_art + @can * @kgr, t01_art=@can, s01_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 1 if @conta=2 begin update #t1 set a02_art=@can, v02_art=@venta, kgr_art= kgr_art + @venta * @kgr, t02_art=@can, s02_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a02_art=@can, v02_art=@can, kgr_art= kgr_art + @can * @kgr, t02_art=@can, s02_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA'

END --actualiza cantidad de articulo 3 if @conta=3 begin update #t1 set a03_art=@can, v03_art=@venta, kgr_art= kgr_art + @venta * @kgr , t03_art=@can, s03_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a03_art=@can, v03_art=@can, kgr_art= kgr_art + @can * @kgr, t03_art=@can, s03_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 4 if @conta=4 begin update #t1 set a04_art=@can, v04_art=@venta, kgr_art= kgr_art + @venta * @kgr , t04_art=@can, s04_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a04_art=@can, v04_art=@can, kgr_art= kgr_art + @can * @kgr, t04_art=@can, s04_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 5 if @conta=5 begin update #t1 set a05_art=@can, v05_art=@venta , kgr_art= kgr_art + @venta * @kgr, t05_art=@can, s05_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a05_art=@can, v05_art=@can , kgr_art= kgr_art + @can * @kgr, t05_art=@can, s05_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 6 if @conta=6 begin

update #t1 set a06_art=@can, v06_art=@venta , kgr_art= kgr_art + @venta * @kgr, t06_art=@can, s06_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a06_art=@can, v06_art=@can , kgr_art= kgr_art + @can * @kgr, t06_art=@can, s06_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 7 if @conta=7 begin update #t1 set a07_art=@can, v07_art=@venta , kgr_art= kgr_art + @venta * @kgr, t07_art=@can, s07_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a07_art=@can, v07_art=@can , kgr_art= kgr_art + @can * @kgr, t07_art=@can, s07_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 8 if @conta=8 begin update #t1 set a08_art=@can, v08_art=@venta , kgr_art= kgr_art + @venta * @kgr , t08_art=@can, s08_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a08_art=@can, v08_art=@can , kgr_art= kgr_art + @can* @kgr, t08_art=@can, s08_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 9 if @conta=9 begin update #t1 set a09_art=@can, v09_art=@venta , kgr_art= kgr_art + @venta * @kgr, t09_art=@can, s09_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a09_art=@can, v09_art=@can , kgr_art= kgr_art + @can * @kgr, t09_art=@can, s09_art=@can , kg1_art= kg1_art + @can * @kgr

where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 10 if @conta=10 begin update #t1 set a10_art=@can, v10_art=@venta , kgr_art= kgr_art + @venta * @kgr, t10_art=@can, s10_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a10_art=@can, v10_art=@can , kgr_art= kgr_art + @can * @kgr, t10_art=@can, s10_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 11 if @conta=11 begin update #t1 set a11_art=@can, v11_art=@venta , kgr_art= kgr_art + @venta * @kgr, t11_art=@can, s11_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a11_art=@can, v11_art=@can , kgr_art= kgr_art + @can * @kgr, t11_art=@can, s11_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 12 if @conta=12 begin update #t1 set a12_art=@can, v12_art=@venta , kgr_art= kgr_art + @venta * @kgr, t12_art=@can, s12_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a12_art=@can, v12_art=@can , kgr_art= kgr_art + @can * @kgr, t12_art=@can, s12_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 13 if @conta=13 begin update #t1 set a13_art=@can, v13_art=@venta , kgr_art= kgr_art + @venta * @kgr, t13_art=@can, s13_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA'

update #t1 set a13_art=@can, v13_art=@can , kgr_art= kgr_art + @can * @kgr, t13_art=@can, s13_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 14 if @conta=14 begin update #t1 set a14_art=@can, v14_art=@venta , kgr_art= kgr_art + @venta * @kgr, t14_art=@can, s14_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a14_art=@can, v14_art=@can , kgr_art= kgr_art + @can * @kgr, t14_art=@can, s14_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 15 if @conta=15 begin update #t1 set a15_art=@can, v15_art=@venta , kgr_art= kgr_art + @venta * @kgr, t15_art=@can, s15_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a15_art=@can, v15_art=@can , kgr_art= kgr_art + @can * @kgr, t15_art=@can, s15_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art DECLARE cursor_mov CURSOR FOR select c01_doc, c02_ser, c03_sec, c06_tip from #t1 order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu, @esta WHILE @@FETCH_STATUS=0 BEGIN

select @cli=cod_clientes, @zde=des_zonal , @ven=ven_pers from mov where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu if @esta='VAR' select @c08_des=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@ven)) if @esta='TRA' select @c08_des=@zde if @esta='CLI' select @c08_des=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@cli update #t1 set c08_des=@c08_des where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu FETCH cursor_mov into @docu, @seri, @secu, @esta END CLOSE cursor_mov DEALLOCATE cursor_mov --ingresar total de articulos facturados DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR select top 1 c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c06_tip desc, c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN select @can=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_art=@art select @venta=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+d.cod_serie+d.sec_serie in (select p.cod_tipodocpad+p.cod_seriepad+p.sec_seriepad from pago p where p.est_pago<>'ANU' and p.fsa_pago between and @final) and i.cod_art=@art --actualiza cantidad de articulo 1 if @conta=1 update #t1 set f01_art=@can, k01_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 1 if @conta=2 update #t1 set f02_art=@can, k02_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 3 if @conta=3 update #t1 set f03_art=@can, k03_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 4 if @conta=4 update #t1 set f04_art=@can, k04_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 5 if @conta=5 update #t1 set f05_art=@can, k05_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 6 if @conta=6 update #t1 set f06_art=@can, k06_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 7 if @conta=7 update #t1 set f07_art=@can, k07_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 8 if @conta=8 update #t1 set f08_art=@can, k08_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 9 if @conta=9 update #t1 set f09_art=@can, k09_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu @inicio

--actualiza cantidad de articulo 10 if @conta=10 update #t1 set f10_art=@can, k10_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 11 if @conta=11 update #t1 set f11_art=@can, k11_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 12 if @conta=12 update #t1 set f12_art=@can, k12_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 13 if @conta=13 update #t1 set f13_art=@can, k13_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 14 if @conta=14 update #t1 set f14_art=@can, k14_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 15 if @conta=15 update #t1 set f15_art=@can, k15_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art declare @cguia int set @cguia=0 -- inserta documentos que validan la venta DECLARE cursor_movi CURSOR FOR select c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art from #t1 order by c01_doc, c02_ser, c03_sec

OPEN cursor_movi FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art WHILE @@FETCH_STATUS=0 BEGIN set @cguia=@cguia+1 -Recupera el numero de documentos de venta de la guia select @conta=count(*) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=1 and d.mod_docventa='CRE' and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) select @des_art=isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and a.pos_art=-1 and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) if @cguia=1 update #t1 set des_art= @des_art where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec -Cursor por cada documento DECLARE cursor_docu CURSOR FOR select i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie

and i.cod_art=a.cod_art and a.pos_art=1 and d.mod_docventa='CRE' and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final and p.est_pago<>'ANU') order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art OPEN cursor_docu FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest set @reg=0 set @tfila=0 set @fila=0 WHILE @@FETCH_STATUS=0 BEGIN set @reg = @reg + 1 -- cuenta el numero de documentos --inicializa documento final set @findoc=null set @finser=null set @finsec=null set @finart=null set @finpun=0 set @fincan=0 set @fintot=0 set @finmod=null set @finest=null --buscar linea posterior set rowcount 1 select @findoc=i.cod_tipodoc, @finser=i.cod_serie, @finsec=i.sec_serie, @finart=i.cod_art, @finpun=i.pun_idocventa, @fincan=i.can_idocventa, @fintot=i.tot_idocventa, @finmod=d.mod_docventa, @finest=d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=1 and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and (i.cod_tipodoc+i.cod_serie+i.sec_serie+i.cod_art)>(@idoc+@iser+@isec+@iar t) and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art set rowcount 0 --La fila actual es igual a 0 if @tfila = 0 begin set @tfila = 1 --total de filas del grupo set @inidoc=@idoc set @iniser=@iser set @inisec=@isec set @iniart=@iart set @inipun=@ipun set @inican=@ican set @ttotal=@itot set @tcant=@ican end else

begin set @tfila = @tfila + 1 set @ttotal=@ttotal + @itot set @tcant=@tcant + @ican end --EL registro es igual al primero if @reg=@conta begin set @fila = @fila + 1 insertadas if @fila= 1 if @idoc=@fac update #t1 set fac_art= @iniser + '-' + @inisec + ' / '+@isec, pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art,

--cuenta lineas

v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end else --verificar si se debe de hacer quiebre begin if @findoc<>@idoc or @finser<>@iser or (convert(int, @inisec) + @tfila <> convert(int, @finsec)) or @finpun<>@ipun or @finmod<>@imod or @finest<>@iest begin set @fila=@fila+1 if @fila= 1 if @idoc=@fac update #t1 set fac_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art,

v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end end FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest END CLOSE cursor_docu DEALLOCATE cursor_docu FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art,

@v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art END CLOSE cursor_movi DEALLOCATE cursor_movi --insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est) --set fac_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest insert into #t1 (c01_doc, c02_ser, c03_sec, pre_art, can_art, tot_art, mod_art, est_art, fac_art, bol_art) select '', 'ADE', '', 0, 0, i.tot_pago, d.mod_docventa, d.est_docventa, '', i.cod_seriepad+'-'+ltrim(rtrim(i.sec_seriepad))+' / '+ltrim(rtrim(i.sec_seriepad)) from pago i, docventa d where d.cod_tipodoc=i.cod_tipodocpad and d.cod_serie=i.cod_seriepad and d.sec_serie=i.sec_seriepad and i.fsa_pago between @inicio and @final and d.mot_docventa='SGU' and i.cod_tipodocpad+i.cod_tipodocpad not in ( select distinct cod_tipodoc+cod_serie from idocventa where cod_art in (select cod_art from articulo where ume_art='GRANEL')) and d.cod_zonal=@zonal and d.cod_tipodoc in (@bol, @fac) order by i.cod_tipodoc, i.cod_serie, i.sec_serie insert into creditos select @inicio, * from #t1 order by c01_doc, c02_ser, c03_sec, fac_art, bol_art drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_abrir_cierre_anterior as BEGIN transaction codigo declare @fact datetime, @fnue datetime, @meses int, @conta int, @ban int -- recuperamos mes activo: marzo

select @fact=max(fin) from mes where est='ACT' -- recuperamos mes de ultimo documento ingresado: mayo select @fnue=max(fsa_docventa) from docventa -- contamos si existen meses antes del mes activo set @conta=0 select @conta=count(*) from mes where fin< @fact and est='CER' if @conta=0 begin set @ban=0 GOTO mensaje end set @meses=DATEDIFF(month, @fact, @fnue) -- if ultimo mes ingresado - mes activo > 1 then /*if @meses>3 begin set @ban=1 GOTO mensaje end */ -- restamos un mes: febrero delete mes where fin=@fact -- activamos mes de febrero select @fact=max(fin) from mes update mes set est='ACT' where fin=@fact set @ban=2 GOTO mensaje mensaje: if @ban=0 select 'No existen meses cerrados antes del mes activo' if @ban=1 select 'NO puede continuar porque existe mas de un mes adelante del mes activo' if @ban=2 select 'Exito' COMMIT transaction codigo GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO create procedure sp_backup as begin BACKUP DATABASE llamagas80 TO backup_llamagas80 end

WITH INIT

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

CREATE procedure sp_buscar_guia (@zonal char(10), @inicio datetime, @final datetime, @tipo char(3), @ser char(3), @sec char(10), @des char(50)) as BEGIN transaction codigo DECLARE @guia char(3) DECLARE @c00 datetime, @c01 char(3), @c02 char(3), @c03 char(10), @c04 datetime, @c05 char(10), @c06 char(3), @c07 char(15) DECLARE @c08 char(10), @c09 char(5), @c10 char(50), @c11 char(3) SET @ser= isnull(ltrim(rtrim(@ser)), '') SET @sec= isnull(ltrim(rtrim(@sec)), '') SET @ser= ltrim(rtrim(@ser)) SET @sec= ltrim(rtrim(@sec)) select @guia=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' create table #t1 ( c00_fsa datetime NULL, -- Tipo de Documento Ej Guia de Remision c01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision c02_ser char(3) NULL, -- Codigo de la serie c03_sec char(10) NULL, -- Secuencia de la serie c04_fin datetime NULL, -- Fecha de inicio c05_zon char(10) NULL, -- Zonal Origen c06_tip char(3) null, c07_pers char(15) NULL, -- Persona que registra c08_zde char(10) NULL, -- Zonal destino c09_cli char(5) NULL, -- Cliente destino c10_des char(50) null, c11_est char(3) null ) if @tipo='TOD' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09

insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='PEN' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' and est_mov='PEN' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='ANU' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%'

and est_mov='ANU' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='BAJ' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' and est_mov='BAJ' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END

if @tipo='DET' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' and est_mov='DET' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='OTR' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' and est_mov='OTR' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09

insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='CER' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, cod_equ, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' and est_mov='CER' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='TRA' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, ven_pers, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and tip_cli='TRA' and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%'

and sec_serie like ltrim(rtrim(@sec))+'%' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='CLI' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, ven_pers, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and tip_cli='CLI' and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov

DEALLOCATE cursor_mov END if @tipo='VAR' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, ven_pers, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and tip_cli='VAR' and cod_zonal=@zonal and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00,@c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11

END CLOSE cursor_mov DEALLOCATE cursor_mov END if @tipo='GUI' BEGIN DECLARE cursor_mov CURSOR FOR select fsa_mov, cod_tipodoc, cod_serie, sec_serie, fin_mov, cod_zonal, tip_cli, ven_pers, des_zonal, cod_clientes, est_mov from mov where fin_mov between @inicio and @final and cod_tipodoc=@guia and cod_serie like ltrim(rtrim(@ser))+'%' and sec_serie like ltrim(rtrim(@sec))+'%' order by cod_zonal, cod_tipodoc, cod_serie, sec_serie OPEN cursor_mov FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11

WHILE @@FETCH_STATUS=0 BEGIN if @c06='VAR' select @c10=ltrim(rtrim(des_equ)) from equipo where cod_equ=@c07 if @c06='TRA' select @c10=@c08 if @c06='CLI' select @c10=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@c09 insert into #t1 values (@c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09,@c10, @c11) FETCH cursor_mov into @c00, @c01, @c02, @c03, @c04, @c05, @c06, @c07, @c08, @c09, @c11 END CLOSE cursor_mov DEALLOCATE cursor_mov END select * from #t1 where c10_des like rtrim(rtrim(@des))+'%' order by c01_doc, c02_ser, c03_sec drop table #t1 COMMIT transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

CREATE procedure sp_copiar_doc @doc char(3), @ser char(3), @sec char(10), @ini int, @fin int as BEGIN transaction codigo declare @ban int, @secini char(10), @secfin char(10), @secuencia char(10), @conta int, @con int declare @docg char(3), @serg char(3), @secg char(10), @personal char(15) set @ban=0 set @conta=0 set @ini=ISNULL(@ini, 0) set @fin=ISNULL(@fin, @ini) if @fin=0 set @fin=@ini --verificar que documento sea valido select @conta=count(*) from docventa where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec if @conta=0 begin set @ban=9 GOTO mensaje end -- verificar que la secuencia inicial y final no deben ser cero if @ini=0 or @fin=0 begin set @ban=1 GOTO mensaje end -- verificar que la secuencia final no debe ser mayor de la secuencia inicial if @ini>@fin begin set @ban=2 GOTO mensaje end -- convertir a cadena secuencia de inicio y final set @secini=replicate('0', 7datalength(ltrim(rtrim(str(@ini)))))+ltrim(rtrim(str(@ini))) set @secfin=replicate('0', 7datalength(ltrim(rtrim(str(@fin)))))+ltrim(rtrim(str(@fin))) -- verificar que no exista registros duplicados select @conta=count(*) from docventa where cod_tipodoc=@doc and cod_serie=@ser and sec_serie>=ltrim(rtrim(@secini)) and ltrim(rtrim(sec_serie))<=@secfin if @conta>0 begin set @ban=3 GOTO mensaje end set @con=@ini -- insertar registros WHILE @con<=@fin BEGIN

set @secuencia=replicate('0', 7datalength(ltrim(rtrim(str(@con)))))+ltrim(rtrim(str(@con))) --Actualiza registro insert into docventa SELECT @doc,@ser, @secuencia, cod_zonal, cod_pers, mod_docventa, mot_docventa, fpa_docventa, fin_docventa, fsa_docventa, fnu_docventa, ffi_docventa, cod_clientes, bru_docventa, igv_docventa, tot_docventa,cod_tipodocori, cod_serieori, sec_serieori, cod_tipodocpad,cod_seriepad, sec_seriepad, glo_docventa, est_docventa, fin_usr, cod_usr, fco_docventa, ode_docventa, cod_tipodocper, '', '', tcliper_docventa, porper_docventa,mper_docventa,ade_docventa FROM docventa WHERE cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec --sp_help docventa insert into idocventa SELECT @doc,@ser,@secuencia, cod_art, can_idocventa, pun_idocventa,igv_idocventa, tot_idocventa, tgv_idocventa, est_idocventa, fco_idocventa, vgi_idocventa, cod_tipodocpad, cod_seriepad, sec_seriepad, cod_tipodocori, cod_serieori, sec_serieori, fec_idocventa, dur_idocventa FROM idocventa WHERE cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec set @con=@con+1 END IF NOT EXISTS(SELECT * from docventa where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@secuencia ) begin set @ban=5 GOTO mensaje end mensaje: if @ban=0 begin set @fin = @fin + 1 select @personal=cod_pers FROM docventa WHERE cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec set @secfin=replicate('0', 7datalength(ltrim(rtrim(str(@fin)))))+ltrim(rtrim(str(@fin))) update perserie set sec_serie= @secfin, sei_serie= @fin where cod_pers= @personal and cod_tipodoc= @doc and cod_serie= @ser select 'Proceso exitoso' -COMMIT transaction codigo end if @ban=1 select if @ban=2 select inicial' if @ban=3 select existentes' if @ban=4 select 'La secuencia inicial y final no deben ser cero' 'La secuencia final no debe ser mayor de la secuencia 'No puede crear registros por que duplicaria los 'Si registro virtual si tienen datos'

if @ban=5 select 'No se pudo crear porque superan la guia' if @ban=9 select 'Documento no valido' COMMIT transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_idocventa_ade_delete (@doc char(3), @ser char(3), @sec char(10), @art char(3)) as BEGIN transaction codigo declare @conta int, @num int, @des char(3) declare @ser1 char(3), @sec1 char(10), @acumula int, @vac float, @fco float, @can float /*Inicia variables*********************************************/ select @des = cod_art from articulo where des_art='DESCUENTO' /********RECIBO DE RECOJO DE FACTURAS Y BOLETAS x ARTICULO****************/ select @fco= ISNULL(sum(can_idocventa), 0) from docventa d, idocventa i, tipodoc t where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and t.cod_tipodoc=d.cod_tipodoc and t.cod_tipodoc=i.cod_tipodoc and t.des_tipodoc='RECIBO DE RECOJO' and d.est_docventa<>'ANU' and d.mod_docventa='ADE' and i.cod_tipodocori=@doc and i.cod_serieori=@ser and i.sec_serieori=@sec and i.cod_art = @art IF EXISTS(SELECT * from imo where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec and cod_art=@art) begin select @can = can_idocventa from idocventa where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec and cod_art=@art and est_idocventa<>'ANU' if round(@fco,3) = round(@can,3) update idocventa set fco_idocventa=@fco, est_idocventa='CER' where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec and cod_art=@art else update idocventa set fco_idocventa=@fco, est_idocventa='PEN' where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec and cod_art=@art select @acumula=count(*) from idocventa where est_idocventa<>'CER' and cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec if @acumula=0 update docventa set est_docventa='CER' where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec else update docventa set est_docventa='PEN' where cod_tipodoc=@doc and cod_serie=@ser and sec_serie=@sec end

COMMIT transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON

GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO --select * from clientes where cod_clientes='19836' --@banper, @encontrado, @percepcion, @porcentaje --exec sp_percepciones '002', '087', '19836', 10, '01-02-2005 00:00:00', 2 CREATE procedure sp_percepciones @tipodoc char(3), @serie char(3), @cod_clientes char(5), @monto real, @fecha datetime, @bal int as begin transaction codigo declare @preferencial int, @exonerado int, @banper char(10), @parper char(40), @encontrado int, @doc char(3), @art char(3) declare @pi char(1), @pf char(1), @inicio char(10), @fin char(10), @porcentaje real, @percepcion real select @preferencial=0, @encontrado=0, @exonerado=0 select @preferencial=count(*) from clientes c, lista l where ltrim(rtrim(c.ruc_dni_clientes))=ltrim(rtrim(l.ruc)) and c.cod_clientes=@cod_clientes select @exonerado=count(*) from clientes c, pub l where ltrim(rtrim(c.ruc_dni_clientes))=ltrim(rtrim(l.ruc)) and c.cod_clientes=@cod_clientes select @doc=ltrim(rtrim(des_tipodoc)) from tipodoc where cod_tipodoc=@tipodoc if exists(select distinct cod_tipodoc, cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art='GAS A GRANEL' and cod_tipodoc=@tipodoc and cod_serie=@serie) set @art='GRA' else set @art='NIN' if @doc='FAC' and @preferencial=0 and @art='NIN' --cliente no es preferencial begin select @banper=isnull(pfa_param, '0') from parametros if ltrim(rtrim(@banper))='1' begin -------------------------------------------select @parper=f1_param from parametros if len(@parper)> 0 begin select @pi=substring(f1_param,1,1), @pf=substring(f1_param,CHARINDEX('=', f1_param) - 1 , 1), @inicio=substring(f1_param,2, CHARINDEX('-', f1_param) - 2 ), @fin=substring(f1_param,CHARINDEX('-', f1_param) + 1, CHARINDEX('=', f1_param)- CHARINDEX('-', f1_param) - 2),

@porcentaje=substring(f1_param, CHARINDEX('=', len(f1_param)- CHARINDEX('=', f1_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end f1_param) + 1, if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto > convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end -------------------------------------------select @parper=f2_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(f2_param,1,1), @pf=substring(f2_param,CHARINDEX('=', f2_param) - 1 , 1), @inicio=substring(f2_param,2, CHARINDEX('-', f2_param) - 2 ), @fin=substring(f2_param,CHARINDEX('-', f2_param) + 1, CHARINDEX('=', f2_param)- CHARINDEX('-', f2_param) - 2), @porcentaje=substring(f2_param, CHARINDEX('=', f2_param) + 1, len(f2_param)- CHARINDEX('=', f2_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio)

@porcentaje) * @monto

begin SET @percepcion=convert(real, set @encontrado=1 end

end ---------------------------------------------select @parper=f3_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(f3_param,1,1), @pf=substring(f3_param,CHARINDEX('=', f3_param) - 1 , 1), @inicio=substring(f3_param,2, CHARINDEX('-', f3_param) - 2 ), @fin=substring(f3_param,CHARINDEX('-', f3_param) + 1, CHARINDEX('=', f3_param)- CHARINDEX('-', f3_param) - 2), @porcentaje=substring(f3_param, CHARINDEX('=', f3_param) + 1, len(f3_param)- CHARINDEX('=', f3_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------select @parper=f4_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(f4_param,1,1), @pf=substring(f4_param,CHARINDEX('=', f4_param) - 1 , 1), @inicio=substring(f4_param,2, CHARINDEX('-', f4_param) - 2 ), @fin=substring(f4_param,CHARINDEX('-', f4_param) + 1, CHARINDEX('=', f4_param)- CHARINDEX('-', f4_param) - 2), @porcentaje=substring(f4_param, CHARINDEX('=', f4_param) + 1, len(f4_param)- CHARINDEX('=', f4_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end

if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------end end if @doc='FAC' and @preferencial>0 and @art='NIN'--cliente es preferencial begin select @banper=isnull(pfa_param, '0') from parametros if ltrim(rtrim(@banper))='1' begin select @parper=fe_param from parametros if len(@parper)> 0 begin select @pi=substring(fe_param,1,1), @pf=substring(fe_param,CHARINDEX('=', fe_param) - 1 , 1), @inicio=substring(fe_param,2, CHARINDEX('-', fe_param) - 2 ), @fin=substring(fe_param,CHARINDEX('-', fe_param) + 1, CHARINDEX('=', fe_param)- CHARINDEX('-', fe_param) - 2), @porcentaje=substring(fe_param, CHARINDEX('=', fe_param) + 1, len(fe_param)- CHARINDEX('=', fe_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end

if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end end end if @doc='BOL' and @art='NIN' --cliente no es prefere--cial begin select @banper=isnull(pbo_param, '0') from parametros if ltrim(rtrim(@banper))='1' begin -------------------------------------------select @parper=b1_param from parametros if len(@parper)> 0 begin select @pi=substring(b1_param,1,1), @pf=substring(b1_param,CHARINDEX('=', b1_param) - 1 , 1), @inicio=substring(b1_param,2, CHARINDEX('-', b1_param) - 2 ), @fin=substring(b1_param,CHARINDEX('-', b1_param) + 1, CHARINDEX('=', b1_param)- CHARINDEX('-', b1_param) - 2), @porcentaje=substring(b1_param, CHARINDEX('=', b1_param) + 1, len(b1_param)- CHARINDEX('=', b1_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end -------------------------------------------select @parper=b2_param from parametros if len(@parper)> 0 and @encontrado=0 begin

select @pi=substring(b2_param,1,1), @pf=substring(b2_param,CHARINDEX('=', b2_param) - 1 , 1), @inicio=substring(b2_param,2, CHARINDEX('-', b2_param) - 2 ), @fin=substring(b2_param,CHARINDEX('-', b2_param) + 1, CHARINDEX('=', b2_param)- CHARINDEX('-', b2_param) - 2), @porcentaje=substring(b2_param, CHARINDEX('=', b2_param) + 1, len(b2_param)- CHARINDEX('=', b2_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------select @parper=b3_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(b3_param,1,1), @pf=substring(b3_param,CHARINDEX('=', b3_param) - 1 , 1), @inicio=substring(b3_param,2, CHARINDEX('-', b3_param) - 2 ), @fin=substring(b3_param,CHARINDEX('-', b3_param) + 1, CHARINDEX('=', b3_param)- CHARINDEX('-', b3_param) - 2), @porcentaje=substring(b3_param, CHARINDEX('=', b3_param) + 1, len(b3_param)- CHARINDEX('=', b3_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F'

if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------select @parper=b4_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(b4_param,1,1), @pf=substring(b4_param,CHARINDEX('=', b4_param) - 1 , 1), @inicio=substring(b4_param,2, CHARINDEX('-', b4_param) - 2 ), @fin=substring(b4_param,CHARINDEX('-', b4_param) + 1, CHARINDEX('=', b4_param)- CHARINDEX('-', b4_param) - 2), @porcentaje=substring(b4_param, CHARINDEX('=', b4_param) + 1, len(b4_param)- CHARINDEX('=', b4_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------end end if @doc='BOL' and @art='NIN' and @bal<=2 begin SET @banper='0' SET @percepcion=0 set @encontrado=1 set @porcentaje=0 end

if @doc='FAC' and @art='GRA' --cliente no es preferencial begin select @banper='1', @porcentaje=0.02 SET @percepcion = convert(real, @porcentaje) * @monto set @encontrado=1 end if @doc='BOL' and @art='GRA' --cliente no es preferencial begin select @banper=isnull(pbg_param, '0') from parametros if ltrim(rtrim(@banper))='1' begin -----------------------------------------select @parper=bg1_param from parametros if len(@parper)> 0 begin select @pi=substring(bg1_param,1,1), @pf=substring(bg1_param,CHARINDEX('=', bg1_param) - 1 , 1), @inicio=substring(bg1_param,2, CHARINDEX('-', bg1_param) - 2 ), @fin=substring(bg1_param,CHARINDEX('-', bg1_param) + 1, CHARINDEX('=', bg1_param)- CHARINDEX('-', bg1_param) - 2), @porcentaje=substring(bg1_param, CHARINDEX('=', bg1_param) + 1, len(bg1_param)- CHARINDEX('=', bg1_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto > convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end -------------------------------------------select @parper=bg2_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(bg2_param,1,1), @pf=substring(bg2_param,CHARINDEX('=', bg2_param) - 1 , 1),

@inicio=substring(bg2_param,2, CHARINDEX('-', bg2_param) - 2 ), @fin=substring(bg2_param,CHARINDEX('-', bg2_param) + CHARINDEX('=', bg2_param)- CHARINDEX('-', bg2_param) - 2), @porcentaje=substring(bg2_param, CHARINDEX('=', bg2_param) + 1, len(bg2_param)- CHARINDEX('=', bg2_param) ) from parametros -select @parper, @pi, @pf, @inicio, @fin, @porcentaje if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end 1, if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------select @parper=bg3_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(bg3_param,1,1), @pf=substring(bg3_param,CHARINDEX('=', bg3_param) - 1 , 1), @inicio=substring(bg3_param,2, CHARINDEX('-', bg3_param) - 2 ), @fin=substring(bg3_param,CHARINDEX('-', bg3_param) + 1, CHARINDEX('=', bg3_param)- CHARINDEX('-', bg3_param) - 2), @porcentaje=substring(bg3_param, CHARINDEX('=', bg3_param) + 1, len(bg3_param)- CHARINDEX('=', bg3_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F'

if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------select @parper=bg4_param from parametros if len(@parper)> 0 and @encontrado=0 begin select @pi=substring(bg4_param,1,1), @pf=substring(bg4_param,CHARINDEX('=', bg4_param) - 1 , 1), @inicio=substring(bg4_param,2, CHARINDEX('-', bg4_param) - 2 ), @fin=substring(bg4_param,CHARINDEX('-', bg4_param) + 1, CHARINDEX('=', bg4_param)- CHARINDEX('-', bg4_param) - 2), @porcentaje=substring(bg4_param, CHARINDEX('=', bg4_param) + 1, len(bg4_param)- CHARINDEX('=', bg4_param) ) from parametros if @inicio='F' and @fin='F' begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio='F' and @fin<>'F' if @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin<>'F' if @monto >= convert(real, @inicio) and @monto <= convert(real, @fin) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end if @inicio<>'F' and @fin='F' if @monto > convert(real, @inicio) begin SET @percepcion=convert(real, @porcentaje) * @monto set @encontrado=1 end end ---------------------------------------------end end if @exonerado>0 begin SET @percepcion=0 set @encontrado=1 set @porcentaje=0 end if @fecha < '01-01-2005 00:00:00' begin set @banper='0' end select @banper, @encontrado, @percepcion, @porcentaje commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO --exec sp_rep_cobranza_facturacion 'PLANTA', '10-01-2005 00:00:00', '1031-2005 23:59:59' CREATE procedure sp_rep_cobranza_facturacion (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @fac char(3), @bol char(3), @ncr char(3) select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA'

select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @ncr=cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' create table #t1 ( ano char(4) null, mes char(4) null, dia char(4) null, fac float null, cre float null) create table #t2 ( ano char(4) null, mes char(4) null, dia char(4) null, fac float null, cre float null) insert into #t1 (ano, mes, dia, fac) select DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa), sum(tot_docventa) from docventa where fin_docventa between @inicio and @final and est_docventa<>'ANU' and (cod_tipodoc=@fac or cod_tipodoc=@bol) and cod_zonal like ltrim(rtrim(@zonal)) + '%' group by DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa) order by DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa) insert into #t2 (ano, mes, dia, cre) select DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa), sum(tot_docventa) from docventa where fin_docventa between @inicio and @final and est_docventa<>'ANU' and (cod_tipodoc=@ncr) and cod_zonal like ltrim(rtrim(@zonal)) + '%' group by DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa) order by DATEPART(year, fin_docventa), DATEPART(month, fin_docventa), DATEPART(day, fin_docventa) --dia qhe exite en facturas se actualiza update #t1 set cre=#t2.cre from #t1, #t2 where #t1.ano=#t2.ano and #t1.mes=#t2.mes and #t1.dia=#t2.dia --insertar dias que no tienen facturas insert into #t1 select * from #t2 where ano+mes+dia not in (select ano+mes+dia from #t1)

select * from #t1 order by ano, mes, convert(int, dia) drop table #t1 drop table #t2 commit transaction codigo GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

CREATE procedure sp_rep_cons_general1B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @fac char(3), @bol char(3), @gui char(3) declare @m01_doc char(3), @m02_ser char(3), @m03_sec char(10), @m04_tip char(3), @m05_cli char(5) declare @m06_zde char(10), @m07_ven char(15), @m08_est char(3), @i01_doc char(3), @i02_ser char(3) declare @i03_sec char(10), @i04_art char(3), @i05_pun float, @i06_can float, @i07_tot float, @i08_mod char(3), @i09_est char(3) declare @i10_fac char(30), @i11_bol char(30), @i12_reg int declare @fila int, @conta int, @art char(3), @dim char(4), @kgr float declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3) declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3) declare @igdoc char(3), @igser char(3), @igsec char(10), @fgdoc char(3), @fgser char(3), @fgsec char(10) declare @tfila int, @ttotal float, @reg int, @tcant int , @anusec char(10), @sumador int, @m00_cli char(50), @ini_cli char(50) declare @m05_gru int, @m06_ele int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3), @can float, @venta float declare @facturado float, @credito float select @fle=cod_art from articulo where ltrim(rtrim(des_art)) = 'SERVICIO POR FLETE' select @gra=cod_art from articulo where ltrim(rtrim(des_art)) = 'GAS A GRANEL' select @b12=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 12 KILOS'

select @b25=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 2.50 KILOS' select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' delete cobranza where fecha=@inicio create table #t1 ( m00_cli char(50) null, m01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision m02_ser char(3) NULL, -- Codigo de la serie m03_sec char(10) NULL, -- Secuencia de la serie m04_tip char(3) NULL, -- Secuencia de la serie m05_gru int null, m06_ele int null, m08_est char(3) NULL , -- Secuencia de la serie i04_art char(3) null, -- articulo i05_pun float NULL default 0, -- Zonal Origen i06_can float NULL default 0, -- Zonal Origen i07_tot float NULL default 0, -- Zonal Origen i08_mod char(3) NULL, -- Secuencia de la serie i09_est char(3) NULL, -- Secuencia de la serie i10_fac char(30) default '' null, i11_bol char(30) default '' null, i12_reg int null default 0, a01_art int null default 0, a02_art int null default 0, a03_art int null default 0, a04_art int null default 0, a05_art int null default 0, a06_art int null default 0, a07_art int null default 0, a08_art int null default 0, a09_art int null default 0, a10_art int null default 0, a11_art int null default 0, a12_art int null default 0, a13_art int null default 0, a14_art int null default 0, a15_art int null default 0, v01_art int null default 0, v02_art int null default 0, v03_art int null default 0, v04_art int null default 0, v05_art int null default 0, v06_art int null default 0, v07_art int null default 0, v08_art int null default 0, v09_art int null default 0,

v10_art v11_art v12_art v13_art v14_art v15_art t01_art t02_art t03_art t04_art t05_art t06_art t07_art t08_art t09_art t10_art t11_art t12_art t13_art t14_art t15_art s01_art s02_art s03_art s04_art s05_art s06_art s07_art s08_art s09_art s10_art s11_art s12_art s13_art s14_art s15_art f01_art f02_art f03_art f04_art f05_art f06_art f07_art f08_art f09_art f10_art f11_art f12_art f13_art f14_art f15_art k01_art k02_art k03_art

int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int

null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null

default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default default

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

k04_art k05_art k06_art k07_art k08_art k09_art k10_art k11_art k12_art k13_art k14_art k15_art kgr_art kg1_art kg2_art kg3_art

int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, float null default 0, float null default 0, float null default 0, float null default 0

) ------------------------------------------------------------ Carga todos los registros incluyendo anulados ---------------------------------------------------------cuenta numero de lineas detalle no anulados select @conta=count(*) from docventa d, idocventa i, articulo a, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and (a.gui_art='GUI' or a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) and fsa_docventa between @inicio and @final and d.cod_zonal=@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) --and m.cod_tipodoc='003' and m.cod_serie='083' and m.sec_serie='0028840' --leer las lineas de los documentos de venta de la cobranza actual DECLARE cursor_docu CURSOR FOR select m.cod_tipodoc, m.cod_serie, m.sec_serie, m.tip_cli, m.cod_clientes, m.des_zonal , m.ven_pers, m.est_mov, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa from docventa d, idocventa i, articulo a, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR')) and fsa_docventa between @inicio and @final and d.cod_zonal=@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol)

--and m.cod_tipodoc='003' and m.cod_serie='083' and m.sec_serie='0028840' order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art OPEN cursor_docu FETCH cursor_docu into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est, @i01_doc, @i02_ser, @i03_sec, @i04_art, @i05_pun, @i06_can, @i07_tot, @i08_mod, @i09_est set @reg=0 set @tfila=0 set @fila=0 WHILE @@FETCH_STATUS=0 BEGIN ------------------------------------------------------------------------set @reg = @reg + 1 -- cuenta el numero de documentos --inicializa documento final set @findoc=null set @finser=null set @finsec=null set @finart=null set @finpun=0 set @fincan=0 set @fintot=0 set @finmod=null set @finest=null --buscar linea posterior set rowcount 1 select @fgdoc=m.cod_tipodoc, @fgser=m.cod_serie, @fgsec=m.sec_serie, @findoc=i.cod_tipodoc, @finser=i.cod_serie, @finsec=i.sec_serie, @finart=i.cod_art, @finpun=i.pun_idocventa, @fincan=i.can_idocventa, @fintot=i.tot_idocventa, @finmod=d.mod_docventa, @finest=d.est_docventa from docventa d, idocventa i, articulo a, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR')) and d.fsa_docventa between @inicio and @final and d.cod_zonal=@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (i.cod_tipodoc+i.cod_serie+i.sec_serie+i.cod_art)>(@i01_doc+@i02_ser+@i03 _sec+@i04_art) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art set rowcount 0 -if @fgsec = '0028939' or @fgsec = '0028940' -select @m00_cli, @m01_doc, @m02_ser, @findoc, @i01_doc, @finser, @i02_ser, -convert(int, @inisec) + @tfila, @i03_sec, convert(int, @finsec), -@tfila, @inisec, @fgdoc,@igdoc, @fgser,@igser, @fgsec,@igsec --Esta condicion acumala el total y la cantidad por grupo if @tfila = 0

begin set @tfila = 1 --total de filas del grupo set @inidoc=@i01_doc set @iniser=@i02_ser set @inisec=@i03_sec set @iniart=@i04_art set @inipun=@i05_pun set @inican=@i06_can set @ttotal=@i07_tot set @tcant=@i06_can set @igdoc=@m01_doc set @igser=@m02_ser set @igsec=@m03_sec end else begin set @tfila = @tfila + 1 set @ttotal=@ttotal + @i07_tot set @tcant=@tcant + @i06_can end --EL registro es igual al primero if @reg=@conta begin set @fila = @fila + 1 insertadas if @m04_tip='VAR' select @m00_cli=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@m07_ven)) if @m04_tip='TRA' select @m00_cli=@m06_zde if @m04_tip='CLI' select @m00_cli=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@m05_cli if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, '', @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), @fila) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0

--cuenta lineas

set @fgdoc=null set @fgser=null set @fgsec=null end else --verificar si se debe de hacer quiebre begin -if @fgsec = '0028939' or @fgsec = '0028940' -select @m00_cli, @m01_doc, @m02_ser, @findoc, @i01_doc, @finser, @i02_ser, -convert(int, @inisec) + @tfila, @i03_sec, convert(int, @finsec), -@tfila, @inisec, @fgdoc,@igdoc, @fgser,@igser, @fgsec,@igsec if @findoc<>@i01_doc or @finser<>@i02_ser or (convert(int, @inisec) + @tfila <> convert(int, @finsec)) or @finpun<>@i05_pun or @finmod<>@i08_mod or @finest<>@i09_est or @finart<>@iniart or @fgdoc<>@igdoc or @fgser<>@igser or @fgsec<>@igsec begin set @fila=@fila+1 if @m04_tip='VAR' select @m00_cli=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@m07_ven)) if @m04_tip='TRA' select @m00_cli=@m06_zde if @m04_tip='CLI' select @m00_cli=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@m05_cli if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, '', @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), @fila) --incrementar anulados

if @findoc=@i01_doc and @finser=@i02_ser and (convert(int, @inisec) + @tfila <> convert(int, @finsec)) begin set @sumador=@tfila WHILE convert(int, @inisec) + @sumador < convert(int, @finsec) BEGIN SET @anusec=replicate('0',7datalength(ltrim(rtrim(str(convert(int, @inisec) + @sumador)))))+ltrim(rtrim(str(convert(int, @inisec) + @sumador))) if exists(select * from docventa where cod_tipodoc=@i01_doc and cod_serie=@i02_ser and sec_serie=@anusec and est_docventa='ANU' and fsa_docventa between @inicio and @final) begin set @fila=@fila+1 if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, 0, 0, 0, 'CON', 'ANU', @iniser+'-'+ltrim(rtrim(@anusec))+' / '+ltrim(rtrim(@anusec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, 0, 0, 0, 'CON', 'ANU', '', @iniser+'-'+ltrim(rtrim(@anusec))+' / '+ltrim(rtrim(@anusec)), @fila) end set @sumador=@sumador+1 END end --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 set @fgdoc=null set @fgser=null set @fgsec=null end end ------------------------------------------------------------------------FETCH cursor_docu into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est, @i01_doc, @i02_ser, @i03_sec, @i04_art, @i05_pun, @i06_can, @i07_tot, @i08_mod, @i09_est END

CLOSE cursor_docu DEALLOCATE cursor_docu ---------------------------------------------------------cargar guias anuladas y de transito ------------------------------------------------------DECLARE cursor_guias CURSOR FOR select cod_tipodoc, cod_serie, sec_serie, tip_cli, cod_clientes, des_zonal , ven_pers, est_mov from mov where fsa_mov between @inicio and @final and cod_tipodoc+cod_serie+sec_serie not in ( select distinct m01_doc+ m02_ser+ m03_sec from #t1 ) OPEN cursor_guias FETCH cursor_guias into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est WHILE @@FETCH_STATUS=0 BEGIN --verificar si historicamente la guia no se usa en granel select @conta=count(*) from imo where cod_tipodoc=@m01_doc and cod_serie=@m02_ser and cod_art=@gra --verificando que la guia no sea de gas a granel - si la guia tiene granel no aparece if exists(select * from mov m, imo i where m.cod_tipodoc=i.cod_tipodoc and m.cod_serie=i.cod_serie and m.sec_serie=i.sec_serie and i.cod_art<>@gra) and @conta=0 begin set @fila=@fila+1 if @m04_tip='VAR' select @m00_cli=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@m07_ven)) if @m04_tip='TRA' select @m00_cli=@m06_zde if @m04_tip='CLI' select @m00_cli=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@m05_cli insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i12_reg) values(@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @fila) end FETCH cursor_guias into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est END CLOSE cursor_guias DEALLOCATE cursor_guias ---------------------------------------------------------crear grupos de en las guias -------------------------------------------------------set @tfila = 0 set @fila = 0 DECLARE cursor_mov CURSOR FOR select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i12_reg from #t1 order by m01_doc, m02_ser, m03_sec, i10_fac, i11_bol OPEN cursor_mov

FETCH cursor_mov into @m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i12_reg set @inidoc='' set @iniser='' set @inisec='' set @ini_cli='' WHILE @@FETCH_STATUS=0 BEGIN set @fila=@fila + 1 if (@m00_cli<>@ini_cli or @m01_doc<>@inidoc or @m02_ser<>@iniser or @m03_sec<>@inisec) begin set @fila=1 set @tfila=@tfila + 1 update #t1 set m05_gru=@tfila, m06_ele=@fila where i12_reg=@i12_reg set @inidoc=@m01_doc set @iniser=@m02_ser set @inisec=@m03_sec set @ini_cli=@m00_cli end else update #t1 set m05_gru=@tfila, m06_ele=@fila where i12_reg=@i12_reg FETCH cursor_mov into @m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i12_reg END CLOSE cursor_mov DEALLOCATE cursor_mov ------------------------------------------------ calculando despachados y vendidos ------------------------------------------------cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in(@fle, @gra, @b12, @b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR select i12_reg, m05_gru, m01_doc, m02_ser, m03_sec, m04_tip from #t1 where m06_ele=1 order by i12_reg OPEN cursor_mov FETCH cursor_mov into @i12_reg, @m05_gru, @m01_doc, @m02_ser, @m03_sec, @m04_tip WHILE @@FETCH_STATUS=0 BEGIN set @can=0 set @venta=0 --calculado cantidad despachada select @can=isnull(can_imo, 0)

from imo where cod_tipodoc=@m01_doc and cod_serie=@m02_ser and sec_serie=@m03_sec and cod_art=@art --calculando cantidad vendida select @venta=isnull(sum(i06_can),0) from #t1 where m05_gru=@m05_gru and i04_art=@art --actualiza cantidad de articulo 1 if @conta=1 begin if @m04_tip<>'TRA' begin update #t1 set a01_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t01_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a01_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t01_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 1 if @conta=2 begin if @m04_tip<>'TRA' begin update #t1 set a02_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t02_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a02_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t02_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END

v01_art=@venta, s01_art=@venta,

kgr_art= kg1_art=

v01_art=@can, s01_art=@can,

kgr_art= kg1_art=

v02_art=@venta, s02_art=@venta,

kgr_art= kg1_art=

v02_art=@can, s02_art=@can,

kgr_art= kg1_art=

--actualiza cantidad de articulo 3 if @conta=3 begin if @m04_tip<>'TRA' begin update #t1 set a03_art=@can, v03_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t03_art=@can, s03_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg

end else begin update #t1 set a03_art=@can, v03_art=@can, kgr_art= kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t03_art=@can, s03_art=@can, kg1_art= kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 4 if @conta=4 begin if @m04_tip<>'TRA' begin update #t1 set a04_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t04_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a04_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t04_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 5 if @conta=5 begin if @m04_tip<>'TRA' begin update #t1 set a05_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t05_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a05_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t05_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 6 if @conta=6 begin if @m04_tip<>'TRA' begin

v04_art=@venta, s04_art=@venta,

kgr_art= kg1_art=

v04_art=@can, s04_art=@can,

kgr_art= kg1_art=

v05_art=@venta, s05_art=@venta,

kgr_art= kg1_art=

v05_art=@can, s05_art=@can,

kgr_art= kg1_art=

update #t1 set a06_art=@can, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t06_art=@can, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a06_art=@can, kgr_art= kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t06_art=@can, kg1_art= kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 7 if @conta=7 begin if @m04_tip<>'TRA' begin update #t1 set a07_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t07_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a07_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t07_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 8 if @conta=8 begin if @m04_tip<>'TRA' begin update #t1 set a08_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t08_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a08_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t08_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 9 if @conta=9 begin if @m04_tip<>'TRA'

v06_art=@venta, s06_art=@venta,

v06_art=@can, s06_art=@can,

v07_art=@venta, s07_art=@venta,

kgr_art= kg1_art=

v07_art=@can, s07_art=@can,

kgr_art= kg1_art=

v08_art=@venta, s08_art=@venta,

kgr_art= kg1_art=

v08_art=@can, s08_art=@can,

kgr_art= kg1_art=

kgr_art= kgr_art + kg1_art= kg1_art +

kgr_art= kgr_art + kg1_art= kg1_art +

begin update #t1 set a09_art=@can, v09_art=@venta, @venta * @kgr where m05_gru=@m05_gru update #t1 set t09_art=@can, s09_art=@venta, @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a09_art=@can, v09_art=@can, @can * @kgr where m05_gru=@m05_gru update #t1 set t09_art=@can, s09_art=@can, @can * @kgr where i12_reg=@i12_reg end

END --actualiza cantidad de articulo 10 if @conta=10 begin if @m04_tip<>'TRA' begin update #t1 set a10_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t10_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a10_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t10_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 11 if @conta=11 begin if @m04_tip<>'TRA' begin update #t1 set a11_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t11_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a11_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t11_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 12

v10_art=@venta, s10_art=@venta,

kgr_art= kg1_art=

v10_art=@can, s10_art=@can,

kgr_art= kg1_art=

v11_art=@venta, s11_art=@venta,

kgr_art= kg1_art=

v11_art=@can, s11_art=@can,

kgr_art= kg1_art=

kgr_art= kg1_art=

kgr_art= kg1_art=

kgr_art= kg1_art=

kgr_art= kg1_art=

kgr_art= kg1_art=

kgr_art= kg1_art=

if @conta=12 begin if @m04_tip<>'TRA' begin update #t1 set a12_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t12_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a12_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t12_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 13 if @conta=13 begin if @m04_tip<>'TRA' begin update #t1 set a13_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t13_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a13_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t13_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 14 if @conta=14 begin if @m04_tip<>'TRA' begin update #t1 set a14_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t14_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a14_art=@can, kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t14_art=@can, kg1_art + @can * @kgr where i12_reg=@i12_reg end END --actualiza cantidad de articulo 15

v12_art=@venta, s12_art=@venta,

v12_art=@can, s12_art=@can,

v13_art=@venta, s13_art=@venta,

v13_art=@can, s13_art=@can,

v14_art=@venta, s14_art=@venta,

v14_art=@can, s14_art=@can,

if @conta=15 begin if @m04_tip<>'TRA' begin update #t1 set a15_art=@can, v15_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t15_art=@can, s15_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg end else begin update #t1 set a15_art=@can, v15_art=@can, kgr_art= kgr_art + @can * @kgr where m05_gru=@m05_gru update #t1 set t15_art=@can, s15_art=@can, kg1_art= kg1_art + @can * @kgr where i12_reg=@i12_reg end END ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @i12_reg, @m05_gru, @m01_doc, @m02_ser, @m03_sec, @m04_tip END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art --------------------------------------------------------------------------------------------------------ingresar total de articulos facturados y cancelados DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in(@fle, @gra, @b12, @b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN set @facturado=0 set @credito=0 select @can=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_art=@art select @venta=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' --and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+d.cod_serie+d.sec_serie in (select p.cod_tipodocpad+p.cod_seriepad+p.sec_seriepad from pago p where p.est_pago<>'ANU' and p.fsa_pago between @inicio and @final) and i.cod_art=@art --actualiza cantidad de articulo 1 if @conta=1 update #t1 set f01_art=@can, k01_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 1 if @conta=2 update #t1 set f02_art=@can, k02_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 3 if @conta=3 update #t1 set f03_art=@can, k03_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 4 if @conta=4 update #t1 set f04_art=@can, k04_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 5 if @conta=5 update #t1 set f05_art=@can, k05_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 6 if @conta=6 update #t1 set f06_art=@can, k06_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1

--actualiza cantidad de articulo 7 if @conta=7 update #t1 set f07_art=@can, k07_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 8 if @conta=8 update #t1 set f08_art=@can, k08_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 9 if @conta=9 update #t1 set f09_art=@can, k09_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 10 if @conta=10 update #t1 set f10_art=@can, k10_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 11 if @conta=11 update #t1 set f11_art=@can, k11_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 12 if @conta=12 update #t1 set f12_art=@can, k12_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 13 if @conta=13 update #t1 set f13_art=@can, k13_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 14 if @conta=14 update #t1 set f14_art=@can, k14_art= @can + @venta,

kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 --actualiza cantidad de articulo 15 if @conta=15 update #t1 set f15_art=@can, k15_art= @can + @venta, kg2_art= kg2_art + @can * @kgr, kg3_art = kg3_art + (@can + @venta)* @kgr where i12_reg=1 SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art --insertar documentos sin guia BOLETAS insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'REG', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, '', d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and d.mot_docventa='SGU' and ltrim(rtrim(d.ade_docventa))<>'1' and a.des_art in ( 'REGULADOR') and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('REGULADOR') and d.cod_zonal=@zonal and (d.cod_tipodoc=@bol) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art --insertar documentos sin guia FACTURAS insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'REG', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)), '' from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and d.mot_docventa='SGU' and ltrim(rtrim(d.ade_docventa))<>'1' and a.des_art not in ('REGULADOR') and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('REGULADOR') and d.cod_zonal=@zonal and (d.cod_tipodoc=@fac)

order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art --insertar documentos sin guia BOLETAS y son con pago adelantado insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'ADE', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, '', d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and d.mot_docventa='SGU' and ltrim(rtrim(d.ade_docventa))='1' -- and a.des_art not in ('SERVICIO POR FLETE', 'REGULADOR') --and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal=@zonal and (d.cod_tipodoc=@bol) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art --insertar documentos sin guia FACTURAS y son con pago adelantado insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'ADE', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)), '' from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and d.mot_docventa='SGU' and ltrim(rtrim(d.ade_docventa))='1' -- and a.des_art not in ('SERVICIO POR FLETE', 'REGULADOR') --and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal=@zonal and (d.cod_tipodoc=@fac) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art --muestra el reporte insert into cobranza select @inicio, * from #t1 order by m00_cli, m01_doc, m02_ser, m03_sec, i10_fac, i11_bol drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_cons_general2B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @fac char(3), @bol char(3), @gui char(3) declare @m01_doc char(3), @m02_ser char(3), @m03_sec char(10), @m04_tip char(3), @m05_cli char(5) declare @m06_zde char(10), @m07_ven char(15), @m08_est char(3), @i01_doc char(3), @i02_ser char(3) declare @i03_sec char(10), @i04_art char(3), @i05_pun float, @i06_can float, @i07_tot float, @i08_mod char(3), @i09_est char(3) declare @i10_fac char(30), @i11_bol char(30), @i12_reg int, @regmin int, @regmax int, @serie char(10) declare @fila int, @conta int, @art char(3), @dim char(4), @kgr float declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3) declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3)

declare @igdoc char(3), @igser char(3), @igsec char(10), @fgdoc char(3), @fgser char(3), @fgsec char(10) declare @tfila int, @ttotal float, @reg int, @tcant int , @anusec char(10), @sumador int, @m00_cli char(50), @ini_cli char(50) declare @m05_gru int, @m06_ele int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3), @can float, @venta float declare @facturado float, @credito float select @fle=cod_art POR FLETE' select @gra=cod_art GRANEL' select @b12=cod_art DE 12 KILOS' select @b25=cod_art DE 2.50 KILOS' from articulo where ltrim(rtrim(des_art)) = 'SERVICIO from articulo where ltrim(rtrim(des_art)) = 'GAS A from articulo where ltrim(rtrim(des_art)) = 'BALONES from articulo where ltrim(rtrim(des_art)) = 'BALONES

select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' delete transito where fecha=@inicio create table #t1 ( m00_cli char(50) null, m01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision m02_ser char(3) NULL, -- Codigo de la serie m03_sec char(10) NULL, -- Secuencia de la serie m04_tip char(3) NULL, -- Secuencia de la serie m05_gru int null, m06_ele int null, m08_est char(3) NULL , -- Secuencia de la serie i04_art char(3) null, -- articulo i05_pun float NULL default 0, -- Zonal Origen i06_can float NULL default 0, -- Zonal Origen i07_tot float NULL default 0, -- Zonal Origen i08_mod char(3) NULL, -- Secuencia de la serie i09_est char(3) NULL, -- Secuencia de la serie i10_fac char(30) default '' null, i11_bol char(30) default '' null, i12_reg int null default 0, a01_art int null default 0, a02_art int null default 0, a03_art int null default 0, a04_art int null default 0, a05_art int null default 0, a06_art int null default 0, a07_art int null default 0, a08_art int null default 0, a09_art int null default 0, a10_art int null default 0, a11_art int null default 0,

a12_art a13_art a14_art a15_art v01_art v02_art v03_art v04_art v05_art v06_art v07_art v08_art v09_art v10_art v11_art v12_art v13_art v14_art v15_art t01_art t02_art t03_art t04_art t05_art t06_art t07_art t08_art t09_art t10_art t11_art t12_art t13_art t14_art t15_art s01_art s02_art s03_art s04_art s05_art s06_art s07_art s08_art s09_art s10_art s11_art s12_art s13_art s14_art s15_art kgr_art kg1_art

int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, int null default 0, float null default 0, float null default 0

) ------------------------------------------------------------ Carga todos los registros incluyendo anulados

---------------------------------------------------------cuenta numero de lineas detalle no anulados select @conta=count(*) from docventa d, idocventa i, articulo a, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') --and (a.gui_art='GUI' or a.des_art ='SERVICIO POR FLETE') and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR')) and fsa_docventa between @inicio and @final and d.cod_zonal<>@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) --leer las lineas de los documentos de venta de la cobranza actual DECLARE cursor_docu CURSOR FOR select m.cod_tipodoc, m.cod_serie, m.sec_serie, m.tip_cli, m.cod_clientes, m.des_zonal , m.ven_pers, m.est_mov, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa from docventa d, idocventa i, articulo a, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') --and (a.gui_art='GUI' or a.des_art ='SERVICIO POR FLETE') and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR')) and fsa_docventa between @inicio and @final and d.cod_zonal<>@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art OPEN cursor_docu FETCH cursor_docu into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est, @i01_doc, @i02_ser, @i03_sec, @i04_art, @i05_pun, @i06_can, @i07_tot, @i08_mod, @i09_est set @reg=0 set @tfila=0 set @fila=0 WHILE @@FETCH_STATUS=0 BEGIN ------------------------------------------------------------------------set @reg = @reg + 1 -- cuenta el numero de documentos --inicializa documento final set @findoc=null set @finser=null set @finsec=null set @finart=null set @finpun=0 set @fincan=0 set @fintot=0 set @finmod=null set @finest=null set @fgdoc=null set @fgser=null set @fgsec=null --buscar linea posterior set rowcount 1

select @fgdoc=i.cod_tipodocpad, @fgser=i.cod_seriepad, @fgsec=i.sec_seriepad, @findoc=i.cod_tipodoc, @finser=i.cod_serie, @finsec=i.sec_serie, @finart=i.cod_art, @finpun=i.pun_idocventa, @fincan=i.can_idocventa, @fintot=i.tot_idocventa, @finmod=d.mod_docventa, @finest=d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') -and (a.gui_art='GUI' or a.des_art ='SERVICIO POR FLETE') and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR')) and d.cod_zonal<>@zonal and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (i.cod_tipodoc+i.cod_serie+i.sec_serie+i.cod_art)>(@i01_doc+@i02_ser+@i03 _sec+@i04_art) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art set rowcount 0 --Esta condicion acumala el total y la cantidad por grupo if @tfila = 0 begin set @tfila = 1 --total de filas del grupo set @inidoc=@i01_doc set @iniser=@i02_ser set @inisec=@i03_sec set @iniart=@i04_art set @inipun=@i05_pun set @inican=@i06_can set @ttotal=@i07_tot set @tcant=@i06_can set @igdoc=@m01_doc set @igser=@m02_ser set @igsec=@m03_sec end else begin set @tfila = @tfila + 1 set @ttotal=@ttotal + @i07_tot set @tcant=@tcant + @i06_can end --EL registro es igual al primero if @reg=@conta begin set @fila = @fila + 1 insertadas if @m04_tip='VAR' select @m00_cli=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@m07_ven)) if @m04_tip='TRA' select @m00_cli=@m06_zde if @m04_tip='CLI' select @m00_cli=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@m05_cli if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est,

--cuenta lineas

i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, '', @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), @fila) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 set @fgdoc=null set @fgser=null set @fgsec=null end else --verificar si se debe de hacer quiebre begin if @findoc<>@i01_doc or @finser<>@i02_ser or (convert(int, @inisec) + @tfila <> convert(int, @finsec)) or @finpun<>@i05_pun or @finmod<>@i08_mod or @finest<>@i09_est or @finart<>@iniart or @fgdoc<>@igdoc or @fgser<>@igser or @fgsec<>@igsec begin set @fila=@fila+1 if @m04_tip='VAR' select @m00_cli=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@m07_ven)) if @m04_tip='TRA' select @m00_cli=@m06_zde if @m04_tip='CLI' select @m00_cli=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@m05_cli if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est,

@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, @i05_pun, @tcant, @ttotal, @i08_mod, @i09_est, '', @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@i03_sec)), @fila) --incrementar anulados if @findoc=@i01_doc and @finser=@i02_ser and (convert(int, @inisec) + @tfila <> convert(int, @finsec)) begin set @sumador=@tfila WHILE convert(int, @inisec) + @sumador < convert(int, @finsec) BEGIN SET @anusec=replicate('0',7datalength(ltrim(rtrim(str(convert(int, @inisec) + @sumador)))))+ltrim(rtrim(str(convert(int, @inisec) + @sumador))) if exists(select * from docventa where cod_tipodoc=@i01_doc and cod_serie=@i02_ser and sec_serie=@anusec and est_docventa='ANU' and fsa_docventa between @inicio and @final ) begin set @fila=@fila+1 if @i01_doc=@fac insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, 0, 0, 0, 'CON', 'ANU', @iniser+'-'+ltrim(rtrim(@anusec))+' / '+ltrim(rtrim(@anusec)), '', @fila) else insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) values (@m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i04_art, 0, 0, 0, 'CON', 'ANU', '', @iniser+'-'+ltrim(rtrim(@anusec))+' / '+ltrim(rtrim(@anusec)), @fila) end set @sumador=@sumador+1 END

end --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 set @fgdoc=null set @fgser=null set @fgsec=null end end ------------------------------------------------------------------------FETCH cursor_docu into @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m05_cli, @m06_zde , @m07_ven, @m08_est, @i01_doc, @i02_ser, @i03_sec, @i04_art, @i05_pun, @i06_can, @i07_tot, @i08_mod, @i09_est END CLOSE cursor_docu DEALLOCATE cursor_docu ---------------------------------------------------------crear grupos de en las guias -------------------------------------------------------set @tfila = 0 set @fila = 0 DECLARE cursor_mov CURSOR FOR select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i12_reg from #t1 order by m00_cli, i10_fac, i11_bol OPEN cursor_mov FETCH cursor_mov into @m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i12_reg set @inidoc='' set @iniser='' set @inisec='' set @ini_cli='' WHILE @@FETCH_STATUS=0 BEGIN set @fila=@fila + 1 if (@m00_cli<>@ini_cli or @m01_doc<>@inidoc or @m02_ser<>@iniser or @m03_sec<>@inisec) begin set @fila=1 set @tfila=@tfila + 1 update #t1 set m05_gru=@tfila, m06_ele=@fila where i12_reg=@i12_reg set @inidoc=@m01_doc set @iniser=@m02_ser set @inisec=@m03_sec set @ini_cli=@m00_cli end else update #t1 set m05_gru=@tfila, m06_ele=@fila where i12_reg=@i12_reg FETCH cursor_mov into @m00_cli, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m08_est, @i12_reg END CLOSE cursor_mov DEALLOCATE cursor_mov

------------------------------------------------ calculando despachados y vendidos ------------------------------------------------cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in(@fle, @gra, @b12, @b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR select i12_reg, m05_gru, m01_doc, m02_ser, m03_sec, m04_tip from #t1 where m06_ele=1 order by i12_reg OPEN cursor_mov FETCH cursor_mov into @i12_reg, @m05_gru, @m01_doc, @m02_ser, @m03_sec, @m04_tip WHILE @@FETCH_STATUS=0 BEGIN set @can=0 set @venta=0 --calculado cantidad despachada select @can=isnull(can_imo, 0) from imo where cod_tipodoc=@m01_doc and cod_serie=@m02_ser and sec_serie=@m03_sec and cod_art=@art --calculando cantidad vendida select @venta=isnull(sum(i06_can),0) from #t1 where m05_gru=@m05_gru and i04_art=@art --actualiza cantidad de articulo 1 if @conta=1 begin update #t1 set a01_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t01_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 1 if @conta=2 begin update #t1 set a02_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t02_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END

v01_art=@venta, s01_art=@venta,

kgr_art= kg1_art=

v02_art=@venta, s02_art=@venta,

kgr_art= kg1_art=

--actualiza cantidad de articulo 3 if @conta=3 begin update #t1 set a03_art=@can, v03_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t03_art=@can, s03_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 4 if @conta=4 begin update #t1 set a04_art=@can, v04_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t04_art=@can, s04_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 5 if @conta=5 begin update #t1 set a05_art=@can, v05_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t05_art=@can, s05_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 6 if @conta=6 begin update #t1 set a06_art=@can, v06_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t06_art=@can, s06_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 7 if @conta=7 begin update #t1 set a07_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t07_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 8 if @conta=8 begin update #t1 set a08_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t08_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 9 if @conta=9

v07_art=@venta, s07_art=@venta,

kgr_art= kg1_art=

v08_art=@venta, s08_art=@venta,

kgr_art= kg1_art=

begin update #t1 set a09_art=@can, v09_art=@venta, kgr_art= kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t09_art=@can, s09_art=@venta, kg1_art= kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 10 if @conta=10 begin update #t1 set a10_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t10_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 11 if @conta=11 begin update #t1 set a11_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t11_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 12 if @conta=12 begin update #t1 set a12_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t12_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 13 if @conta=13 begin update #t1 set a13_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t13_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 14 if @conta=14 begin update #t1 set a14_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t14_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg END --actualiza cantidad de articulo 15 if @conta=15 begin update #t1 set a15_art=@can, kgr_art + @venta * @kgr where m05_gru=@m05_gru update #t1 set t15_art=@can, kg1_art + @venta * @kgr where i12_reg=@i12_reg

v10_art=@venta, s10_art=@venta,

kgr_art= kg1_art=

v11_art=@venta, s11_art=@venta,

kgr_art= kg1_art=

v12_art=@venta, s12_art=@venta,

kgr_art= kg1_art=

v13_art=@venta, s13_art=@venta,

kgr_art= kg1_art=

v14_art=@venta, s14_art=@venta,

kgr_art= kg1_art=

v15_art=@venta, s15_art=@venta,

kgr_art= kg1_art=

END ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @i12_reg, @m05_gru, @m01_doc, @m02_ser, @m03_sec, @m04_tip END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art ---------------------------------------------------DECLARE cursor_anu CURSOR FOR SELECT 'B'+substring(i11_bol, 1,3), min(i11_bol), max(i11_bol), max(i12_reg) from #t1 where i11_bol<>'' group by 'B'+substring(i11_bol, union SELECT 'F'+substring(i10_fac, 1,3), min(i10_fac), max(i10_fac), max(i12_reg) from #t1 where i10_fac<>'' group by 'F'+substring(i10_fac, min(i12_reg), 1,3) min(i12_reg), 1,3)

OPEN cursor_anu FETCH cursor_anu into @serie, @i11_bol, @regmin, @i10_fac, @regmax WHILE @@FETCH_STATUS=0 BEGIN -select @serie, @i11_bol, @regmin, @i10_fac, @regmax if substring(@serie, 1,1)='B' begin DECLARE cursor_antes CURSOR FOR select cod_tipodoc, cod_serie, sec_serie from docventa where fsa_docventa between @inicio and @final and est_docventa='ANU' and cod_zonal<>@zonal and cod_tipodoc=@bol and cod_serie=substring(@serie, 2,3) and sec_serie<substring(@i11_bol,5,7) OPEN cursor_antes FETCH cursor_antes into @i01_doc, @i02_ser, @i03_sec WHILE @@FETCH_STATUS=0 BEGIN insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, 0, 0, 0, 'CON', 'ANU', '', substring(@serie,2,3)+'-'+ltrim(rtrim(@i03_sec))+' / '+ltrim(rtrim(@i03_sec)), 0

from #t1 where i12_reg=@regmin and @i01_doc+@i02_ser+@i03_sec in (select cod_tipodoc+cod_serie+sec_serie from docventa where fsa_docventa between @inicio and @final) FETCH cursor_antes into @i01_doc, @i02_ser, @i03_sec END CLOSE cursor_antes DEALLOCATE cursor_antes --despues DECLARE cursor_despues CURSOR FOR select cod_tipodoc, cod_serie, sec_serie from docventa where fsa_docventa between @inicio and @final and est_docventa='ANU' and cod_zonal<>@zonal and cod_tipodoc=@bol and cod_serie=substring(@serie, 2,3) and sec_serie>substring(@i10_fac,15,7) OPEN cursor_despues FETCH cursor_despues into @i01_doc, @i02_ser, @i03_sec WHILE @@FETCH_STATUS=0 BEGIN insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, 0, 0, 0, 'CON', 'ANU', '', substring(@serie,2,3)+'-'+ltrim(rtrim(@i03_sec))+' / '+ltrim(rtrim(@i03_sec)), 0 from #t1 where i12_reg=@regmax and @i01_doc+@i02_ser+@i03_sec in (select cod_tipodoc+cod_serie+sec_serie from docventa where fsa_docventa between @inicio and @final) FETCH cursor_despues into @i01_doc, @i02_ser, @i03_sec END CLOSE cursor_despues DEALLOCATE cursor_despues end else begin --antes DECLARE cursor_antes1 CURSOR FOR select cod_tipodoc, cod_serie, sec_serie from docventa where fsa_docventa between @inicio and @final and est_docventa='ANU' and cod_zonal<>@zonal and cod_tipodoc=@fac and cod_serie=substring(@serie, 2,3) and sec_serie<substring(@i11_bol,5,7) OPEN cursor_antes1 FETCH cursor_antes1 into @i01_doc, @i02_ser, @i03_sec WHILE @@FETCH_STATUS=0 BEGIN insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est,

i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, 0, 0, 0, 'CON', 'ANU', substring(@serie,2,3)+'-'+ltrim(rtrim(@i03_sec))+' / '+ltrim(rtrim(@i03_sec)), '', 0 from #t1 where i12_reg=@regmin and @i01_doc+@i02_ser+@i03_sec in (select cod_tipodoc+cod_serie+sec_serie from docventa where fsa_docventa between @inicio and @final) FETCH cursor_antes1 into @i01_doc, @i02_ser, @i03_sec END CLOSE cursor_antes1 DEALLOCATE cursor_antes1 --despues DECLARE cursor_despues1 CURSOR FOR select cod_tipodoc, cod_serie, sec_serie from docventa where fsa_docventa between @inicio and @final and est_docventa='ANU' and cod_zonal<>@zonal and cod_tipodoc=@fac and cod_serie=substring(@serie, 2,3) and sec_serie>substring(@i10_fac,15,7) OPEN cursor_despues1 FETCH cursor_despues1 into @i01_doc, @i02_ser, @i03_sec WHILE @@FETCH_STATUS=0 BEGIN insert into #t1 (m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol, i12_reg) select m00_cli, m01_doc, m02_ser, m03_sec, m04_tip, m08_est, i04_art, 0, 0, 0, 'CON', 'ANU', substring(@serie,2,3)+'-'+ltrim(rtrim(@i03_sec))+' / '+ltrim(rtrim(@i03_sec)), '', 0 from #t1 where i12_reg=@regmax and @i01_doc+@i02_ser+@i03_sec in (select cod_tipodoc+cod_serie+sec_serie from docventa where fsa_docventa between @inicio and @final) FETCH cursor_despues1 into @i01_doc, @i02_ser, @i03_sec END CLOSE cursor_despues1 DEALLOCATE cursor_despues1 end FETCH cursor_anu into @serie, @i11_bol, @regmin, @i10_fac, @regmax END CLOSE cursor_anu DEALLOCATE cursor_anu --insertar documentos sin guia BOLETAS insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol)

select d.cod_zonal, 'REG', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, '', d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal<>@zonal and (d.cod_tipodoc=@bol) and d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) not in (select i11_bol from #t1) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art --insertar documentos sin guia FACTURAS insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'REG', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)), '' from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and a.gui_art='SGU' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal<>@zonal and (d.cod_tipodoc=@fac) and d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) not in (select i10_fac from #t1) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art ---------------------------------------insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'ADE', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, '', d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and a.gui_art='SGU' and ltrim(rtrim(d.ade_docventa))='1' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal<>@zonal and (d.cod_tipodoc=@bol) and d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) not in (select i11_bol from #t1) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art

--insertar documentos sin guia FACTURAS insert into #t1 (m00_cli, m02_ser, i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac, i11_bol) select d.cod_zonal, 'ADE', i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)), '' from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and a.cod_art=i.cod_art and a.gui_art='SGU' and ltrim(rtrim(d.ade_docventa))='1' and a.des_art in ('SERVICIO POR FLETE', 'REGULADOR') and d.cod_zonal<>@zonal and (d.cod_tipodoc=@fac) and d.cod_serie+'-'+ltrim(rtrim(d.sec_serie))+' / '+ltrim(rtrim(d.sec_serie)) not in (select i10_fac from #t1) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art

---------------------------------------insert into transito select @inicio, * from #t1 order by m00_cli, i10_fac, i11_bol drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO --select * from creditos where fecha='12-07-2005 00:00:00' --exec sp_rep_cons_general3B 'PLANTA', '12-07-2005 00:00:00', '12-07-2005 23:59:59' CREATE procedure sp_rep_cons_general3B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo DECLARE @guia char(3), @art char(3), @dim char(3), @conta int, @docu char(3), @seri char(3), @secu char(10), @esta char(3) DECLARE @can int, @venta int, @cli char(5), @zde char(10), @ven char(15) DECLARE @fal int, @kgr float, @fac char(3), @bol char(3) declare @c01_doc char(3), @c02_ser char(3), @c03_sec char(10), @c04_fin datetime, @c05_zon char(10), @c06_tip char(3), @c07_est char(3), @c08_des char(50) declare @a01_art int, @a02_art int, @a03_art int, @a04_art int, @a05_art int, @a06_art int, @a07_art int, @a08_art int, @a09_art int, @a10_art int, @a11_art int, @a12_art int, @a13_art int, @a14_art int, @a15_art int declare @v01_art int, @v02_art int, @v03_art int, @v04_art int, @v05_art int, @v06_art int, @v07_art int, @v08_art int, @v09_art int, @v10_art int, @v11_art int, @v12_art int, @v13_art int, @v14_art int, @v15_art int declare @idoc char(3), @iser char(3), @isec char(10), @iart char(3), @ipun float, @ican float, @itot float, @imod char(3), @iest char(3) declare @fac_art char(30), @bol_art char(30), @pre_art float, @can_art float, @tot_art float, @mod_art char(15), @est_art char(3), @des_art float declare @fila int, @linea int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3) select @fle=cod_art from articulo where ltrim(rtrim(des_art)) = 'SERVICIO POR FLETE' select @gra=cod_art from articulo where ltrim(rtrim(des_art)) = 'GAS A GRANEL' select @b12=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 12 KILOS' select @b25=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 2.50 KILOS' -inicia fila y columna declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3)

declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3) declare @tfila int, @ttotal float, @reg int, @tcant int select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @guia=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' delete creditos where fecha=@inicio create table #t1 ( c01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision c02_ser char(3) NULL, -- Codigo de la serie c03_sec char(10) NULL, -- Secuencia de la serie c04_fin datetime NULL, -- Fecha de inicio c05_zon char(10) NULL, -- Zonal Origen c06_tip char(3) null, -- tipo de cliente c07_est char(3) null, -- estado del documento c08_des char(50) null, -- destino a01_art int null default 0, a02_art int null default 0, a03_art int null default 0, a04_art int null default 0, a05_art int null default 0, a06_art int null default 0, a07_art int null default 0, a08_art int null default 0, a09_art int null default 0, a10_art int null default 0, a11_art int null default 0, a12_art int null default 0, a13_art int null default 0, a14_art int null default 0, a15_art int null default 0, v01_art int null default 0, v02_art int null default 0, v03_art int null default 0, v04_art int null default 0, v05_art int null default 0, v06_art int null default 0, v07_art int null default 0, v08_art int null default 0, v09_art int null default 0, v10_art int null default 0, v11_art int null default 0, v12_art int null default 0, v13_art int null default 0, v14_art int null default 0, v15_art int null default 0, kgr_art float null default 0,

kg1_art des_art t01_art t02_art t03_art t04_art t05_art t06_art t07_art t08_art t09_art t10_art t11_art t12_art t13_art t14_art t15_art s01_art s02_art s03_art s04_art s05_art s06_art s07_art s08_art s09_art s10_art s11_art s12_art s13_art s14_art s15_art fac_art bol_art pre_art can_art tot_art mod_art est_art f01_art f02_art f03_art f04_art f05_art f06_art f07_art f08_art f09_art f10_art f11_art f12_art f13_art

float null default 0, float null default 0, int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, char(30) default '', char(30) default '', float null default 0, float null default 0, float null default 0, char(15) null, char(3) null, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0,

int int int int int int int int int int int int int

f14_art f15_art k01_art k02_art k03_art k04_art k05_art k06_art k07_art k08_art k09_art k10_art k11_art k12_art k13_art k14_art k15_art )

int int int int int int int int int int int int int int int int int

null null null null null null null null null null null null null null null null null

default default default default default default default default default default default default default default default default default

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

--Llenando datos de guia en tabla temporal insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est) select distinct m.cod_tipodoc, m.cod_serie, m.sec_serie, m.fin_mov, m.cod_zonal, m.tip_cli, m.est_mov from idocventa i, mov m, pago p where p.cod_tipodocpad=i.cod_tipodoc and p.cod_seriepad=i.cod_serie and p.sec_seriepad=i.sec_serie and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and p.fsa_pago between @inicio and @final and i.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and m.cod_tipodoc=@guia and m.cod_zonal like ltrim(rtrim(@zonal))+'%' and p.est_pago<>'ANU' order by m.cod_zonal, m.cod_tipodoc, m.cod_serie, m.sec_serie ------------------------------------------------ calculando despachados y vendidos ------------------------------------------------cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in( select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR

select c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN set @can=0 set @venta=0 --calculado cantidad despachada select @can=isnull(can_imo, 0), @venta=isnull(fco_imo, 0) from imo where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu and cod_art=@art /* --calculando cantidad vendida select @venta=isnull(sum(i.can_idocventa), 0)

from idocventa i, docventa d where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_tipodocpad=@docu and i.cod_seriepad=@seri and i.sec_seriepad=@secu and i.cod_art=@art and d.fsa_docventa between @inicio and @final*/ --actualiza cantidad de articulo 1 if @conta=1 begin update #t1 set a01_art=@can, v01_art=@venta, kgr_art= kgr_art + @venta * @kgr, t01_art=@can, s01_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a01_art=@can, v01_art=@can, kgr_art= kgr_art + @can * @kgr, t01_art=@can, s01_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 1 if @conta=2 begin update #t1 set a02_art=@can, v02_art=@venta, kgr_art= kgr_art + @venta * @kgr, t02_art=@can, s02_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a02_art=@can, v02_art=@can, kgr_art= kgr_art + @can * @kgr, t02_art=@can, s02_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA'

END --actualiza cantidad de articulo 3 if @conta=3 begin update #t1 set a03_art=@can, v03_art=@venta, kgr_art= kgr_art + @venta * @kgr , t03_art=@can, s03_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a03_art=@can, v03_art=@can, kgr_art= kgr_art + @can * @kgr, t03_art=@can, s03_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 4 if @conta=4 begin update #t1 set a04_art=@can, v04_art=@venta, kgr_art= kgr_art + @venta * @kgr , t04_art=@can, s04_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a04_art=@can, v04_art=@can, kgr_art= kgr_art + @can * @kgr, t04_art=@can, s04_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 5 if @conta=5 begin update #t1 set a05_art=@can, v05_art=@venta , kgr_art= kgr_art + @venta * @kgr, t05_art=@can, s05_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a05_art=@can, v05_art=@can , kgr_art= kgr_art + @can * @kgr, t05_art=@can, s05_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 6 if @conta=6 begin

update #t1 set a06_art=@can, v06_art=@venta , kgr_art= kgr_art + @venta * @kgr, t06_art=@can, s06_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a06_art=@can, v06_art=@can , kgr_art= kgr_art + @can * @kgr, t06_art=@can, s06_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 7 if @conta=7 begin update #t1 set a07_art=@can, v07_art=@venta , kgr_art= kgr_art + @venta * @kgr, t07_art=@can, s07_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a07_art=@can, v07_art=@can , kgr_art= kgr_art + @can * @kgr, t07_art=@can, s07_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 8 if @conta=8 begin update #t1 set a08_art=@can, v08_art=@venta , kgr_art= kgr_art + @venta * @kgr , t08_art=@can, s08_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a08_art=@can, v08_art=@can , kgr_art= kgr_art + @can* @kgr, t08_art=@can, s08_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 9 if @conta=9 begin update #t1 set a09_art=@can, v09_art=@venta , kgr_art= kgr_art + @venta * @kgr, t09_art=@can, s09_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a09_art=@can, v09_art=@can , kgr_art= kgr_art + @can * @kgr, t09_art=@can, s09_art=@can , kg1_art= kg1_art + @can * @kgr

where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 10 if @conta=10 begin update #t1 set a10_art=@can, v10_art=@venta , kgr_art= kgr_art + @venta * @kgr, t10_art=@can, s10_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a10_art=@can, v10_art=@can , kgr_art= kgr_art + @can * @kgr, t10_art=@can, s10_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 11 if @conta=11 begin update #t1 set a11_art=@can, v11_art=@venta , kgr_art= kgr_art + @venta * @kgr, t11_art=@can, s11_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a11_art=@can, v11_art=@can , kgr_art= kgr_art + @can * @kgr, t11_art=@can, s11_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 12 if @conta=12 begin update #t1 set a12_art=@can, v12_art=@venta , kgr_art= kgr_art + @venta * @kgr, t12_art=@can, s12_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a12_art=@can, v12_art=@can , kgr_art= kgr_art + @can * @kgr, t12_art=@can, s12_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 13 if @conta=13 begin update #t1 set a13_art=@can, v13_art=@venta , kgr_art= kgr_art + @venta * @kgr, t13_art=@can, s13_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA'

update #t1 set a13_art=@can, v13_art=@can , kgr_art= kgr_art + @can * @kgr, t13_art=@can, s13_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 14 if @conta=14 begin update #t1 set a14_art=@can, v14_art=@venta , kgr_art= kgr_art + @venta * @kgr, t14_art=@can, s14_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a14_art=@can, v14_art=@can , kgr_art= kgr_art + @can * @kgr, t14_art=@can, s14_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 15 if @conta=15 begin update #t1 set a15_art=@can, v15_art=@venta , kgr_art= kgr_art + @venta * @kgr, t15_art=@can, s15_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a15_art=@can, v15_art=@can , kgr_art= kgr_art + @can * @kgr, t15_art=@can, s15_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art DECLARE cursor_mov CURSOR FOR select c01_doc, c02_ser, c03_sec, c06_tip from #t1 order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu, @esta WHILE @@FETCH_STATUS=0 BEGIN

select @cli=cod_clientes, @zde=des_zonal , @ven=ven_pers from mov where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu if @esta='VAR' select @c08_des=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@ven)) if @esta='TRA' select @c08_des=@zde if @esta='CLI' select @c08_des=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@cli update #t1 set c08_des=@c08_des where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu FETCH cursor_mov into @docu, @seri, @secu, @esta END CLOSE cursor_mov DEALLOCATE cursor_mov --ingresar total de articulos facturados DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR select top 1 c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c06_tip desc, c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN select @can=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_art=@art select @venta=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+d.cod_serie+d.sec_serie in (select p.cod_tipodocpad+p.cod_seriepad+p.sec_seriepad from pago p where p.est_pago<>'ANU' and p.fsa_pago between and @final) and i.cod_art=@art --actualiza cantidad de articulo 1 if @conta=1 update #t1 set f01_art=@can, k01_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 1 if @conta=2 update #t1 set f02_art=@can, k02_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 3 if @conta=3 update #t1 set f03_art=@can, k03_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 4 if @conta=4 update #t1 set f04_art=@can, k04_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 5 if @conta=5 update #t1 set f05_art=@can, k05_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 6 if @conta=6 update #t1 set f06_art=@can, k06_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 7 if @conta=7 update #t1 set f07_art=@can, k07_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 8 if @conta=8 update #t1 set f08_art=@can, k08_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 9 if @conta=9 update #t1 set f09_art=@can, k09_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu @inicio

--actualiza cantidad de articulo 10 if @conta=10 update #t1 set f10_art=@can, k10_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 11 if @conta=11 update #t1 set f11_art=@can, k11_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 12 if @conta=12 update #t1 set f12_art=@can, k12_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 13 if @conta=13 update #t1 set f13_art=@can, k13_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 14 if @conta=14 update #t1 set f14_art=@can, k14_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 15 if @conta=15 update #t1 set f15_art=@can, k15_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art declare @cguia int set @cguia=0 -- inserta documentos que validan la venta DECLARE cursor_movi CURSOR FOR select c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art from #t1 order by c01_doc, c02_ser, c03_sec

OPEN cursor_movi FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art WHILE @@FETCH_STATUS=0 BEGIN set @cguia=@cguia+1 -Recupera el numero de documentos de venta de la guia select @conta=count(*) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=1 and d.mod_docventa='CRE' and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) select @des_art=isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and a.pos_art=-1 and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) if @cguia=1 update #t1 set des_art= @des_art where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec -Cursor por cada documento DECLARE cursor_docu CURSOR FOR select i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie

and i.cod_art=a.cod_art and a.pos_art=1 and d.mod_docventa='CRE' and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final and p.est_pago<>'ANU') order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art OPEN cursor_docu FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest set @reg=0 set @tfila=0 set @fila=0 WHILE @@FETCH_STATUS=0 BEGIN set @reg = @reg + 1 -- cuenta el numero de documentos --inicializa documento final set @findoc=null set @finser=null set @finsec=null set @finart=null set @finpun=0 set @fincan=0 set @fintot=0 set @finmod=null set @finest=null --buscar linea posterior set rowcount 1 select @findoc=i.cod_tipodoc, @finser=i.cod_serie, @finsec=i.sec_serie, @finart=i.cod_art, @finpun=i.pun_idocventa, @fincan=i.can_idocventa, @fintot=i.tot_idocventa, @finmod=d.mod_docventa, @finest=d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=1 and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and (i.cod_tipodoc+i.cod_serie+i.sec_serie+i.cod_art)>(@idoc+@iser+@isec+@iar t) and d.cod_tipodoc+ d.cod_serie+ d.sec_serie in (select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art set rowcount 0 --La fila actual es igual a 0 if @tfila = 0 begin set @tfila = 1 --total de filas del grupo set @inidoc=@idoc set @iniser=@iser set @inisec=@isec set @iniart=@iart set @inipun=@ipun set @inican=@ican set @ttotal=@itot set @tcant=@ican end else

begin set @tfila = @tfila + 1 set @ttotal=@ttotal + @itot set @tcant=@tcant + @ican end --EL registro es igual al primero if @reg=@conta begin set @fila = @fila + 1 insertadas if @fila= 1 if @idoc=@fac update #t1 set fac_art= @iniser + '-' + @inisec + ' / '+@isec, pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art,

--cuenta lineas

v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end else --verificar si se debe de hacer quiebre begin if @findoc<>@idoc or @finser<>@iser or (convert(int, @inisec) + @tfila <> convert(int, @finsec)) or @finpun<>@ipun or @finmod<>@imod or @finest<>@iest begin set @fila=@fila+1 if @fila= 1 if @idoc=@fac update #t1 set fac_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art,

v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end end FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest END CLOSE cursor_docu DEALLOCATE cursor_docu FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art,

@v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art END CLOSE cursor_movi DEALLOCATE cursor_movi --insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est) --set fac_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest insert into #t1 (c01_doc, c02_ser, c03_sec, pre_art, can_art, tot_art, mod_art, est_art, fac_art, bol_art) select '', 'ADE', '', 0, 0, i.tot_pago, d.mod_docventa, d.est_docventa, '', i.cod_seriepad+'-'+ltrim(rtrim(i.sec_seriepad))+' / '+ltrim(rtrim(i.sec_seriepad)) from pago i, docventa d where d.cod_tipodoc=i.cod_tipodocpad and d.cod_serie=i.cod_seriepad and d.sec_serie=i.sec_seriepad and i.fsa_pago between @inicio and @final and d.mot_docventa='SGU' and i.cod_tipodocpad+i.cod_tipodocpad not in ( select distinct cod_tipodoc+cod_serie from idocventa where cod_art in (select cod_art from articulo where ume_art='GRANEL')) and d.cod_zonal=@zonal and d.cod_tipodoc in (@bol, @fac) order by i.cod_tipodoc, i.cod_serie, i.sec_serie insert into creditos select @inicio, * from #t1 order by c01_doc, c02_ser, c03_sec, fac_art, bol_art drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_cons_general4 (@zon char(10), @final datetime) as @inicio datetime,

begin transaction codigo declare @bol char(3), @fac char(3), @gui char(3), @des char(30), @tot float, @gra char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' create table #t1 ( c01_des char(30), -- Descripcin del articulo c02_zon char(10), -- Zonal c03_pun float default 0, -- Precio c04_can float default 0, -- Cantidad c05_tot float default 0, -- Total c06_des float default 0 -- Total ) insert into #t1 select a.des_art, d.cod_zonal, i.pun_idocventa, sum(i.can_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CON' and d.cod_zonal = ltrim(rtrim(@zon)) and a.gui_art='GUI' and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') -and (a.gui_art='GUI' or a.des_art not in ('GAS A GRANEL')) group by a.des_art, d.cod_zonal, i.pun_idocventa order by a.des_art, i.pun_idocventa, d.cod_zonal

select * from #t1 drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON

GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

CREATE procedure sp_rep_cons_general5B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo DECLARE @art char(3), @dim char(3), @conta int, @esta char(3) DECLARE @can int, @venta int, @cli char(5), @zde char(10), @ven char(15) declare @bol char(3), @fac char(3), @gui char(3), @des char(30), @tot float declare @doc char(3), @ser char(3), @sec char(10), @tip char(3) declare @docu char(3), @seri char(3), @secu char(10), @tipo char(3), @tcan float, @kgr float declare @fila int, @linea int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3), @des_cre float, @des_ade float --------------------------------------------------------------------select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' --------------------------------------------------------------------select @fle=cod_art from articulo where ltrim(rtrim(des_art)) = 'SERVICIO POR FLETE' select @gra=cod_art from articulo where ltrim(rtrim(des_art)) = 'GAS A GRANEL' select @b12=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 12 KILOS' select @b25=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 2.50 KILOS' --------------------------------------------------------------------delete ventaplanta where fecha=@inicio create table #t0 ( t00_doc char(3) null, -- Descripcin del articulo t01_ser char(3) null, -- Descripcin del articulo t02_sec char(10) null, -- Cantidad t03_tip char(3) null -- Cantidad )

create table #t1 ( c00_des char(30) null, c01_art char(30) null, c02_can int default 0 null )

-- Descripcin del articulo -- Descripcin del articulo -- Cantidad

insert into #t0 select distinct m.cod_tipodoc, m.cod_serie, m.sec_serie, m.tip_cli from mov m, imo i where m.cod_tipodoc=i.cod_tipodoc and m.cod_serie=i.cod_serie and m.sec_serie=i.sec_serie and m.fin_mov between @inicio and @final and m.cod_zonal like ltrim(rtrim(@zonal))+'%' and m.cod_tipodoc=@gui and i.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') order by m.cod_tipodoc, m.cod_serie, m.sec_serie ----------------------------------------------- calculando despachados y vendidos ------------------------------------------------cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, des_art, dim_art, kgr_art from articulo where gui_art='GUI' and cod_art not in(select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@gra or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @des, @dim, @kgr WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 DECLARE cursor_mov CURSOR FOR select t00_doc, t01_ser, t02_sec, t03_tip from #t0 order by t00_doc, t01_ser, t02_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu, @tipo WHILE @@FETCH_STATUS=0 BEGIN set @can=0 set @venta=0 --calculado cantidad despachada select @can=isnull(can_imo, 0) from imo where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu and cod_art=@art --calculando cantidad vendida select @venta=isnull(sum(i.can_idocventa), 0) from idocventa i, docventa d where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie

and i.cod_tipodocpad=@docu and i.cod_seriepad=@seri and i.sec_seriepad=@secu and i.cod_art=@art and d.fsa_docventa between @inicio and @final --actualiza cantidad de articulo 1 if @tipo<>'TRA' set @tcan=@tcan + @venta else set @tcan=@tcan + @can FETCH cursor_mov into @docu, @seri, @secu, @tipo END CLOSE cursor_mov DEALLOCATE cursor_mov if @tcan>0 insert into #t1 select 'DESPACHADO', @des, @tcan set @tcan=0 FETCH cursor_art into @art, @des, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art drop table #t0 insert into #t1 select 'VENDIDO', a.des_art, sum(i.can_idocventa) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and a.des_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and gui_art='GUI' group by a.des_art

insert into ventaplanta select @inicio, * from #t1 drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_cons_general6B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo DECLARE @guia char(3), @art char(3), @dim char(3), @conta int, @docu char(3), @seri char(3), @secu char(10), @esta char(3) DECLARE @can int, @venta int, @cli char(5), @zde char(10), @ven char(15) DECLARE @fal int, @kgr float, @fac char(3), @bol char(3) declare @c01_doc char(3), @c02_ser char(3), @c03_sec char(10), @c04_fin datetime, @c05_zon char(10), @c06_tip char(3), @c07_est char(3), @c08_des char(50) declare @a01_art int, @a02_art int, @a03_art int, @a04_art int, @a05_art int, @a06_art int, @a07_art int, @a08_art int, @a09_art int, @a10_art int, @a11_art int, @a12_art int, @a13_art int, @a14_art int, @a15_art int declare @v01_art int, @v02_art int, @v03_art int, @v04_art int, @v05_art int, @v06_art int, @v07_art int, @v08_art int, @v09_art int, @v10_art int, @v11_art int, @v12_art int, @v13_art int, @v14_art int, @v15_art int declare @idoc char(3), @iser char(3), @isec char(10), @iart char(3), @ipun float, @ican float, @itot float, @imod char(3), @iest char(3), @des_art float declare @fac_art char(30), @bol_art char(30), @pre_art float, @can_art float, @tot_art float, @mod_art char(15), @est_art char(3) declare @fila int, @linea int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3) select @fle=cod_art from articulo where ltrim(rtrim(des_art)) = 'SERVICIO POR FLETE' select @gra=cod_art from articulo where ltrim(rtrim(des_art)) = 'GAS A GRANEL' select @b12=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 12 KILOS' select @b25=cod_art from articulo where ltrim(rtrim(des_art)) = 'BALONES DE 2.50 KILOS' -inicia fila y columna

declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3) declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3) declare @tfila int, @ttotal float, @reg int, @tcant int select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @guia=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' delete ventacredito where fecha=@inicio create table #t1 ( c01_doc char(3) NULL, -- Tipo de Documento Ej Guia de Remision c02_ser char(3) NULL, -- Codigo de la serie c03_sec char(10) NOT NULL, -- Secuencia de la serie c04_fin datetime NULL, -- Fecha de inicio c05_zon char(10) NULL, -- Zonal Origen c06_tip char(3) null, -- tipo de cliente c07_est char(3) null, -- estado del documento c08_des char(50) null, -- destino a01_art int null default 0, a02_art int null default 0, a03_art int null default 0, a04_art int null default 0, a05_art int null default 0, a06_art int null default 0, a07_art int null default 0, a08_art int null default 0, a09_art int null default 0, a10_art int null default 0, a11_art int null default 0, a12_art int null default 0, a13_art int null default 0, a14_art int null default 0, a15_art int null default 0, v01_art int null default 0, v02_art int null default 0, v03_art int null default 0, v04_art int null default 0, v05_art int null default 0, v06_art int null default 0, v07_art int null default 0, v08_art int null default 0, v09_art int null default 0, v10_art int null default 0, v11_art int null default 0, v12_art int null default 0, v13_art int null default 0,

v14_art v15_art kgr_art kg1_art des_art t01_art t02_art t03_art t04_art t05_art t06_art t07_art t08_art t09_art t10_art t11_art t12_art t13_art t14_art t15_art s01_art s02_art s03_art s04_art s05_art s06_art s07_art s08_art s09_art s10_art s11_art s12_art s13_art s14_art s15_art fac_art bol_art pre_art can_art tot_art mod_art est_art f01_art f02_art f03_art f04_art f05_art f06_art f07_art f08_art f09_art f10_art

int null default 0, int null default 0, float null default 0, float null default 0, float null default 0, int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, char(30) default '', char(30) default '', float null default 0, float null default 0, float null default 0, char(15) null, char(3) null, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0, null default 0,

int int int int int int int int int int

f11_art f12_art f13_art f14_art f15_art k01_art k02_art k03_art k04_art k05_art k06_art k07_art k08_art k09_art k10_art k11_art k12_art k13_art k14_art k15_art )

int int int int int int int int int int int int int int int int int int int int

null null null null null null null null null null null null null null null null null null null null

default default default default default default default default default default default default default default default default default default default default

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

--Llenando datos de guia en tabla temporal insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est) select distinct m.cod_tipodoc, m.cod_serie, m.sec_serie, m.fin_mov, m.cod_zonal, m.tip_cli, m.est_mov from docventa d, idocventa i, mov m where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and d.mod_docventa='CRE' and i.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and m.cod_tipodoc=@guia order by m.cod_zonal, m.cod_tipodoc, m.cod_serie, m.sec_serie --cursor de articulos ordenado por diminutivo DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where pos_art=1 and cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@gra or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0

DECLARE cursor_mov CURSOR FOR select c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN select @can=isnull(can_imo, 0), @venta=isnull(fco_imo, 0) from imo where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu and cod_art=@art --actualiza cantidad de articulo 1 if @conta=1 begin update #t1 set a01_art=@can, v01_art=@venta, kgr_art= kgr_art + @venta * @kgr, t01_art=@can, s01_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a01_art=@can, v01_art=@can, kgr_art= kgr_art + @can * @kgr, t01_art=@can, s01_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 1 if @conta=2 begin update #t1 set a02_art=@can, v02_art=@venta, kgr_art= kgr_art + @venta * @kgr, t02_art=@can, s02_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a02_art=@can, v02_art=@can, kgr_art= kgr_art + @can * @kgr, t02_art=@can, s02_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 3 if @conta=3 begin update #t1 set a03_art=@can, v03_art=@venta, kgr_art= kgr_art + @venta * @kgr , t03_art=@can, s03_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA'

update #t1 set a03_art=@can, v03_art=@can, kgr_art= kgr_art + @can * @kgr, t03_art=@can, s03_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 4 if @conta=4 begin update #t1 set a04_art=@can, v04_art=@venta, kgr_art= kgr_art + @venta * @kgr , t04_art=@can, s04_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a04_art=@can, v04_art=@can, kgr_art= kgr_art + @can * @kgr, t04_art=@can, s04_art=@can, kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 5 if @conta=5 begin update #t1 set a05_art=@can, v05_art=@venta , kgr_art= kgr_art + @venta * @kgr, t05_art=@can, s05_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a05_art=@can, v05_art=@venta , kgr_art= kgr_art + @venta * @kgr, t05_art=@can, s05_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 6 if @conta=6 begin update #t1 set a06_art=@can, v06_art=@venta , kgr_art= kgr_art + @venta * @kgr, t06_art=@can, s06_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a06_art=@can, v06_art=@venta , kgr_art= kgr_art + @venta * @kgr, t06_art=@can, s06_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 7

if @conta=7 begin update #t1 set a07_art=@can, v07_art=@venta , kgr_art= kgr_art + @venta * @kgr, t07_art=@can, s07_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a07_art=@can, v07_art=@venta , kgr_art= kgr_art + @venta * @kgr, t07_art=@can, s07_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 8 if @conta=8 begin update #t1 set a08_art=@can, v08_art=@venta , kgr_art= kgr_art + @venta * @kgr , t08_art=@can, s08_art=@venta, kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a08_art=@can, v08_art=@venta , kgr_art= kgr_art + @venta * @kgr, t08_art=@can, s08_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 9 if @conta=9 begin update #t1 set a09_art=@can, v09_art=@venta , kgr_art= kgr_art + @venta * @kgr, t09_art=@can, s09_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a09_art=@can, v09_art=@venta , kgr_art= kgr_art + @venta * @kgr, t09_art=@can, s09_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 10 if @conta=10 begin update #t1 set a10_art=@can, v10_art=@venta , kgr_art= kgr_art + @venta * @kgr, t10_art=@can, s10_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA'

update #t1 set a10_art=@can, v10_art=@venta , kgr_art= kgr_art + @venta * @kgr, t10_art=@can, s10_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 11 if @conta=11 begin update #t1 set a11_art=@can, v11_art=@venta , kgr_art= kgr_art + @venta * @kgr, t11_art=@can, s11_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a11_art=@can, v11_art=@venta , kgr_art= kgr_art + @venta * @kgr, t11_art=@can, s11_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 12 if @conta=12 begin update #t1 set a12_art=@can, v12_art=@venta , kgr_art= kgr_art + @venta * @kgr, t12_art=@can, s12_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a12_art=@can, v12_art=@venta , kgr_art= kgr_art + @venta * @kgr, t12_art=@can, s12_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 13 if @conta=13 begin update #t1 set a13_art=@can, v13_art=@venta , kgr_art= kgr_art + @venta * @kgr, t13_art=@can, s13_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a13_art=@can, v13_art=@venta , kgr_art= kgr_art + @venta * @kgr, t13_art=@can, s13_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 14 if @conta=14 begin update #t1 set a14_art=@can, v14_art=@venta , kgr_art= kgr_art + @venta * @kgr, t14_art=@can, s14_art=@venta , kg1_art= kg1_art + @venta * @kgr

where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a14_art=@can, v14_art=@venta , kgr_art= kgr_art + @venta * @kgr, t14_art=@can, s14_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END --actualiza cantidad de articulo 15 if @conta=15 begin update #t1 set a15_art=@can, v15_art=@venta , kgr_art= kgr_art + @venta * @kgr, t15_art=@can, s15_art=@venta , kg1_art= kg1_art + @venta * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip<>'TRA' update #t1 set a15_art=@can, v15_art=@venta , kgr_art= kgr_art + @venta * @kgr, t15_art=@can, s15_art=@can , kg1_art= kg1_art + @can * @kgr where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu and c06_tip='TRA' END ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art DECLARE cursor_mov CURSOR FOR select c01_doc, c02_ser, c03_sec, c06_tip from #t1 order by c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu, @esta WHILE @@FETCH_STATUS=0 BEGIN select @cli=cod_clientes, @zde=des_zonal , @ven=ven_pers from mov where cod_tipodoc=@docu and cod_serie=@seri and sec_serie=@secu if @esta='VAR' select @c08_des=ltrim(rtrim(des_equ)) from equipo where cod_equ=ltrim(rtrim(@ven)) if @esta='TRA' select @c08_des=@zde if @esta='CLI' select @c08_des=ltrim(rtrim(rso_clientes)) from clientes where cod_clientes=@cli update #t1 set c08_des=@c08_des where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu FETCH cursor_mov into @docu, @seri, @secu, @esta

END CLOSE cursor_mov DEALLOCATE cursor_mov --ingresar total de articulos facturados DECLARE cursor_art CURSOR FOR select cod_art, dim_art, kgr_art from articulo where pos_art=1 and cod_art not in(select cod_art from articulo WHERE ume_art='GRANEL' or cod_art=@fle or cod_art=@gra or cod_art=@b12 or cod_art=@b25) order by dim_art OPEN cursor_art FETCH cursor_art into @art, @dim, @kgr set @conta = 1 WHILE @@FETCH_STATUS=0 BEGIN --por cada guia registrada en #t1 set @can=0 set @venta=0 DECLARE cursor_mov CURSOR FOR select top 1 c01_doc, c02_ser, c03_sec from #t1 where c07_est<>'ANU' order by c06_tip desc, c01_doc, c02_ser, c03_sec OPEN cursor_mov FETCH cursor_mov into @docu, @seri, @secu WHILE @@FETCH_STATUS=0 BEGIN select @can=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_art=@art select @venta=isnull(sum(i.can_idocventa),0) from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+d.cod_serie+d.sec_serie in (select p.cod_tipodocpad+p.cod_seriepad+p.sec_seriepad from pago p where p.est_pago<>'ANU' and p.fsa_pago between @inicio and @final) and i.cod_art=@art --actualiza cantidad de articulo 1 if @conta=1 update #t1 set f01_art=@can, k01_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu

--actualiza cantidad de articulo 1 if @conta=2 update #t1 set f02_art=@can, k02_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 3 if @conta=3 update #t1 set f03_art=@can, k03_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 4 if @conta=4 update #t1 set f04_art=@can, k04_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 5 if @conta=5 update #t1 set f05_art=@can, k05_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 6 if @conta=6 update #t1 set f06_art=@can, k06_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 7 if @conta=7 update #t1 set f07_art=@can, k07_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 8 if @conta=8 update #t1 set f08_art=@can, k08_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 9 if @conta=9 update #t1 set f09_art=@can, k09_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 10 if @conta=10 update #t1 set f10_art=@can, k10_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 11 if @conta=11 update #t1 set f11_art=@can, k11_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 12 if @conta=12 update #t1 set f12_art=@can, k12_art=@venta

where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 13 if @conta=13 update #t1 set f13_art=@can, k13_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 14 if @conta=14 update #t1 set f14_art=@can, k14_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu --actualiza cantidad de articulo 15 if @conta=15 update #t1 set f15_art=@can, k15_art=@venta where c01_doc=@docu and c02_ser=@seri and C03_sec=@secu ----------------------------------------------------set @can=0 set @venta=0 FETCH cursor_mov into @docu, @seri, @secu END CLOSE cursor_mov DEALLOCATE cursor_mov SET @conta = @conta + 1 FETCH cursor_art into @art, @dim, @kgr END CLOSE cursor_art DEALLOCATE cursor_art declare @cguia int set @cguia=0 -- inserta documentos que validan la venta DECLARE cursor_movi CURSOR FOR select c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art from #t1 order by c01_doc, c02_ser, c03_sec OPEN cursor_movi FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art WHILE @@FETCH_STATUS=0 BEGIN set @cguia=@cguia+1 -Recupera el numero de documentos de venta de la guia

select @conta=count(*) from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.mod_docventa='CRE' and i.cod_art=a.cod_art and a.pos_art=1 and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec select @des_art=isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and a.pos_art=-1 and d.fsa_docventa between @inicio and @final and d.mod_docventa='CRE' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) if @cguia=1 update #t1 set des_art= @des_art where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec -Cursor por cada documento DECLARE cursor_docu CURSOR FOR select i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.mod_docventa='CRE' and i.cod_art=a.cod_art and a.pos_art=1 and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art OPEN cursor_docu FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest set @reg=0 set @tfila=0 set @fila=0 WHILE @@FETCH_STATUS=0 BEGIN set @reg = @reg + 1 -- cuenta el numero de documentos --inicializa documento final set @findoc=null set @finser=null set @finsec=null set @finart=null set @finpun=0 set @fincan=0 set @fintot=0 set @finmod=null set @finest=null --buscar linea posterior set rowcount 1

select @findoc=i.cod_tipodoc, @finser=i.cod_serie, @finsec=i.sec_serie, @finart=i.cod_art, @finpun=i.pun_idocventa, @fincan=i.can_idocventa, @fintot=i.tot_idocventa, @finmod=d.mod_docventa, @finest=d.est_docventa from idocventa i, docventa d, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.mod_docventa='CRE' and i.cod_art=a.cod_art and a.pos_art=1 and i.cod_tipodocpad=@c01_doc and i.cod_seriepad=@c02_ser and i.sec_seriepad=@c03_sec and (i.cod_tipodoc+i.cod_serie+i.sec_serie+i.cod_art)>(@idoc+@iser+@isec+@iar t) order by i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art set rowcount 0 --La fila actual es igual a 0 if @tfila = 0 begin set @tfila = 1 --total de filas del grupo set @inidoc=@idoc set @iniser=@iser set @inisec=@isec set @iniart=@iart set @inipun=@ipun set @inican=@ican set @ttotal=@itot set @tcant=@ican end else begin set @tfila = @tfila + 1 set @ttotal=@ttotal + @itot set @tcant=@tcant + @ican end --EL registro es igual al primero if @reg=@conta begin set @fila = @fila + 1 insertadas if @fila= 1 if @idoc=@fac update #t1 set fac_art= @iniser + '-' + @inisec + ' / '+@iser+'-'+@isec, pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+'-'+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac

--cuenta lineas

insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+''+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+''+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end else --verificar si se debe de hacer quiebre begin if @findoc<>@idoc or @finser<>@iser or (convert(int, @inisec) + @tfila <> convert(int, @finsec)) or @finpun<>@ipun or @finmod<>@imod or @finest<>@iest

begin set @fila=@fila+1 if @fila= 1 if @idoc=@fac update #t1 set fac_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+'-'+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else update #t1 set bol_art=@iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+'-'+ltrim(rtrim(@isec)), pre_art=@ipun, can_art=@tcant, tot_art=@ttotal, mod_art=@imod, est_art=@iest where c01_doc=@c01_doc and c02_ser=@c02_ser and c03_sec=@c03_sec else if @idoc=@fac insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, fac_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+''+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) else insert into #t1 (c01_doc, c02_ser, c03_sec, c04_fin, c05_zon, c06_tip, c07_est, c08_des, kgr_art, a01_art, a02_art, a03_art, a04_art, a05_art, a06_art, a07_art, a08_art, a09_art, a10_art, a11_art, a12_art, a13_art, a14_art, a15_art, v01_art, v02_art, v03_art, v04_art, v05_art, v06_art, v07_art, v08_art, v09_art, v10_art, v11_art, v12_art, v13_art, v14_art, v15_art, bol_art, pre_art, can_art, tot_art, mod_art, est_art) values (@c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr,

@a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art, @iniser+'-'+ltrim(rtrim(@inisec))+' / '+@iser+''+ltrim(rtrim(@isec)), @ipun, @tcant, @ttotal, @imod, @iest) --inicializa documento final set @inidoc=null set @iniser=null set @inisec=null set @iniart=null set @inipun=0 set @inican=0 set @initot=0 set @inimod=null set @iniest=null set @ttotal=0 set @tcant=0 set @tfila=0 end end FETCH cursor_docu into @idoc, @iser, @isec, @iart, @ipun, @ican, @itot, @imod, @iest END CLOSE cursor_docu DEALLOCATE cursor_docu FETCH cursor_movi into @c01_doc, @c02_ser, @c03_sec, @c04_fin, @c05_zon, @c06_tip, @c07_est, @c08_des, @kgr, @a01_art, @a02_art, @a03_art, @a04_art, @a05_art, @a06_art, @a07_art, @a08_art, @a09_art, @a10_art, @a11_art, @a12_art, @a13_art, @a14_art, @a15_art, @v01_art, @v02_art, @v03_art, @v04_art, @v05_art, @v06_art, @v07_art, @v08_art, @v09_art, @v10_art, @v11_art, @v12_art, @v13_art, @v14_art, @v15_art END CLOSE cursor_movi DEALLOCATE cursor_movi insert into ventacredito select @inicio, * from #t1 order by c01_doc, c02_ser, c03_sec, fac_art, bol_art drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON

GO CREATE procedure sp_rep_cons_general7 (@zon char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @bol char(3), @fac char(3), @gui char(3), @des char(30), @tot float select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' create table #t1 ( c01_des char(30), -- Descripcin del articulo c02_tot float default 0 -- Total ) insert into #t1 select 'VENTA AL CONTADO', isnull(sum(tot_docventa), 0) select 'VENTA AL CONTADO', isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal=ltrim(rtrim(@zon)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR', 'CUPON PAGINAS AMARILLAS 10 KL')) -and (a.gui_art='GUI' or a.des_art ='SERVICIO POR FLETE') and mod_docventa='CON' --

/* select 'VENTA AL CONTADO', isnull(sum(tot_docventa), 0) from docventa where mod_docventa='CON' and est_docventa<>'ANU' and fsa_docventa between @inicio and @final and cod_zonal=ltrim(rtrim(@zon)) */ insert into #t1 select 'VENTA AL CREDITO PLANTA', isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal=ltrim(rtrim(@zon)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol)

and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR', 'CUPON PAGINAS AMARILLAS 10 KL')) and mod_docventa='CRE' insert into #t1 select 'VENTA AL CREDITO TRANSITO', isnull(sum(i.tot_idocventa), 0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal<>ltrim(rtrim(@zon)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'REGULADOR', 'CUPON PAGINAS AMARILLAS 10 KL')) and mod_docventa='CRE' -select 'VENTA AL CREDITO', isnull(sum(tot_docventa), 0) docventa -where mod_docventa='CRE' and est_docventa<>'ANU' -and fsa_docventa between @inicio and @final from

insert into #t1 select 'TRANSITO CANCELADO', isnull(sum(tot_docventa), 0) from docventa where mod_docventa<>'CRE' and est_docventa<>'ANU' and fsa_docventa between @inicio and @final and (cod_tipodoc=@fac or cod_tipodoc=@bol) and cod_zonal<>ltrim(rtrim(@zon)) select * from #t1 drop table #t1 commit transaction codigo GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --select * from depositar where fecha='01-07-2006 00:00:00' --exec sp_rep_cons_general8B 'PLANTA', '01-07-2006 00:00:00', '01-07-2006 23:59:59' CREATE procedure sp_rep_cons_general8B (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @fac char(3), @bol char(3), @ncr char(3),@ing char(3), @rec char(3), @conta int, @ofepla float, @ofetra float, @zon char(10) declare @ven real, @venew real, @pag real, @trans real, @veni integer, @pagi integer select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @ing=cod_tipodoc from tipodoc where des_tipodoc='RECIBO DE INGRESO' select @rec=cod_tipodoc from tipodoc where des_tipodoc='RECIBO DE RECOJO' select @ncr=cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' delete depositar where fecha=@inicio create table #t1 ( a01_tit char(30) null, a02_des char(50) null, a03_bal char(30) null, a04_can char(15) null, a05_pre char(15) null, a06_doc char(25) null, a07_tot float null, a08_ban float null, a09_gru int null, a10_sec char(8) ) --oferta de planta select @ofepla=abs(isnull(sum(i.tot_idocventa), 0)) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=-1 and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and mod_docventa='CON' and d.cod_zonal=ltrim(rtrim(@zonal)) --oferta de transitos

select @ofetra=abs(isnull(sum(i.tot_idocventa), 0)) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=-1 and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and mod_docventa='CON' and d.cod_zonal<>ltrim(rtrim(@zonal)) -- venta al contado de la planta insert into #t1 --select 'DEPOSITAR', 'VENTA AL CONTADO', '','','', '',isnull(sum(i.tot_idocventa),0) + @ofepla, 0.00 , 1, '' select 'DEPOSITAR', 'VENTA AL CONTADO', '','','', '',isnull(sum(i.tot_idocventa),0), 0.00 , 1, '' from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal=ltrim(rtrim(@zonal)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'INC TRANSF. TITULO GRATUITO', 'REGULADOR')) and mod_docventa<>'CRE' --and mod_docventa='CON' -- credito cancelado insert into #t1 select 'DEPOSITAR','CREDITO CANCELADO', '','','', '', isnull(sum(p.tot_pago), 0), 0.00 , 2, '' from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.mod_docventa='CRE' and d.cod_tipodoc in (@fac, @bol) and p.fsa_pago between @inicio and @final and d.cod_tipodoc+d.cod_serie not in (select d.cod_tipodoc+ d.cod_serie from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL') group by d.cod_tipodoc+ d.cod_serie) /*select 'DEPOSITAR','CREDITO CANCELADO', '','','', '', isnull(sum(tot_docventa), 0), 0.00 , 2, '' from docventa where mod_docventa='CRE' and (cod_tipodoc=@fac or cod_tipodoc=@bol) and cod_tipodoc+cod_serie not in (select d.cod_tipodoc+ d.cod_serie from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL') group by d.cod_tipodoc+ d.cod_serie) and cod_tipodoc + cod_serie + sec_serie in ( select d.cod_tipodoc+ d.cod_serie+ d.sec_serie from docventa d, pago p where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and p.fsa_pago between @inicio and @final) */ -- transito cancelado select @trans=isnull(sum(i.tot_idocventa),0) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal<>ltrim(rtrim(@zonal)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and a.des_art in ('INC TRANSF. TITULO GRATUITO') and mod_docventa<>'CRE'

insert into #t1 --select 'DEPOSITAR', 'TRANSITO CANCELADO', '','','', '',isnull(sum(i.tot_idocventa),0) + @ofetra, 0.00 , 3, '' select 'DEPOSITAR', 'TRANSITO CANCELADO', 'TRANSF. TITULO GRATUITO: '+ ltrim(rtrim(str(@trans, 15, 2))),'','', '',isnull(sum(i.tot_idocventa),0), 0.00 , 3, '' from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and a.cod_art=i.cod_art and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.cod_zonal<>ltrim(rtrim(@zonal)) and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and (a.gui_art='GUI' or a.des_art in ('SERVICIO POR FLETE', 'INC TRANSF. TITULO GRATUITO', 'REGULADOR')) and mod_docventa<>'CRE' --percepciones insert into #t1 values ('PERCEPCIONES', '','','','','', 0.00, 0.00, 4, '') DECLARE cursor_zonalper CURSOR FOR select cod_zonal from zonal OPEN cursor_zonalper FETCH cursor_zonalper into @zon WHILE @@FETCH_STATUS=0

BEGIN set @ven=0 set @pag=0 set @veni=0 set @pagi=0 set @venew=0 select @venew=isnull(sum(mper_docventa), 0) from docventa where cod_tipodocper is null and cod_serieper is null and sec_serieper is null and est_docventa<>'ANU' and mod_docventa='CON' and fsa_docventa between @inicio and @final and cod_zonal=@zon and cod_tipodoc+cod_serie not in (select distinct cod_tipodoc+cod_serie from idocventa i, articulo a where cod_serie<>'086' and i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) select @ven=isnull(sum(mper_docventa), 0) from docventa where cod_tipodocper is not null and cod_serieper is not null and sec_serieper is not null and est_docventa<>'ANU' and mod_docventa='CON' and len(cod_tipodocper)>0 and len(cod_serieper)>0 and len(sec_serieper)>0 and fsa_docventa between @inicio and @final and cod_zonal=@zon and cod_tipodoc+cod_serie not in (select distinct cod_tipodoc+cod_serie from idocventa i, articulo a where cod_serie<>'086' and i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) select @pag=isnull(sum(p.mper_docventa), 0) from pago p, docventa d where p.cod_tipodocpad=d.cod_tipodoc and p.cod_seriepad=d.cod_serie and p.sec_seriepad=d.sec_serie and p.cod_tipodocper is not null and p.cod_serieper is not null and p.sec_serieper is not null and p.est_pago<>'ANU' and d.mod_docventa='CRE' and len(p.cod_tipodocper)>0 and len(p.cod_serieper)>0 and len(p.sec_serieper)>0 and p.fsa_pago between @inicio and @final and p.cod_zonal=@zon and p.cod_tipodocpad+p.cod_seriepad not in (select distinct cod_tipodoc+cod_serie from idocventa i, articulo a where cod_serie<>'086' and i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) if @ven + @venew + @pag > 0 insert into #t1 values ('PERCEPCIONES', @zon,'','','','', @ven + @venew + @pag, 0.00, 4, '') FETCH cursor_zonalper into @zon END CLOSE cursor_zonalper DEALLOCATE cursor_zonalper

-- pago adelantado select @conta=count(*) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.cod_tipodoc=@ing and d.fsa_docventa between @inicio and @final and d.mod_docventa='ADE' and d.cod_tipodoc+d.cod_serie not in (select distinct i.cod_tipodoc+ i.cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) if @conta> 0 insert into #t1 select 'PAGO ADELANTADO', left(c.rso_clientes, 30), a.dim_art, STR(i.can_idocventa, 10, 2), STR(i.pun_idocventa, 10, 2),'', i.tot_idocventa, 0.00, 5, '' from docventa d, idocventa i, clientes c, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and d.cod_tipodoc=@ing and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and d.mod_docventa='ADE' else insert into #t1 values ('PAGO ADELANTADO', '', '','','','', 0.00, 0.00, 5, '') -- descuentos de pago adelantado select @conta=count(*) from docventa where fsa_docventa between @inicio and @final and mod_docventa='ADE' and (cod_tipodoc=@fac or cod_tipodoc=@bol) and cod_tipodoc+cod_serie not in (select distinct i.cod_tipodoc+ i.cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) if @conta> 0 insert into #t1 select 'RECOJO DE PAGO ADELANTADO', convert(char(30), c.rso_clientes), a.dim_art, STR(i.can_idocventa, 10, 2), STR(i.pun_idocventa, 10, 2), CASE d.cod_tipodoc WHEN '001' THEN 'FA '+d.cod_serie+'-'+d.sec_serie WHEN '002' THEN 'BV '+d.cod_serie+'-'+d.sec_serie END, i.tot_idocventa * (-1), 0.00, 6, '' from docventa d, idocventa i, clientes c, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and d.mod_docventa='ADE' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc+d.cod_serie not in (select distinct i.cod_tipodoc+ i.cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.cod_art in (select cod_art from articulo WHERE ume_art='GRANEL')) else insert into #t1 values ('RECOJO DE PAGO ADELANTADO', '', '','','','', 0.00, 0.00, 6, '') --recojo de pagos adelantado con guias select @conta=count(*) from docventa where fsa_docventa between @inicio and @final and mod_docventa='ADE' and cod_tipodoc=@rec if @conta> 0 insert into #t1 select 'RECOJO CANCELADO ', convert(char(30), c.rso_clientes), a.dim_art+'- GUIA : ' + i.cod_seriepad +'-'+i.sec_seriepad, STR(i.can_idocventa, 10, 2), STR(i.pun_idocventa, 10, 2), CASE i.cod_tipodocori WHEN '001' THEN 'FA '+i.cod_serieori+'-'+i.sec_serieori WHEN '002' THEN 'BV '+i.cod_serieori+'-'+i.sec_serieori END, i.tot_idocventa, 0, 7, '' from docventa d, idocventa i, clientes c, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and d.mod_docventa='ADE' and d.cod_tipodoc=@rec else insert into #t1 values ('RECOJO CANCELADO', '', '','','','', 0.00, 0.00, 7, '') --ofertas que reducen las ventas select @conta=count(*) from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=-1 and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and mod_docventa='CON' if @conta> 0 insert into #t1

select 'OFERTAS', d.cod_zonal, a.des_art,'','','', sum(i.tot_idocventa), 0.00, 8, d.cod_zonal from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and a.pos_art=-1 and a.cod_art not in (select cod_art from articulo WHERE ume_art='GRANEL') and d.fsa_docventa between @inicio and @final and mod_docventa='CON' group by d.cod_zonal, a.des_art else insert into #t1 values ('OFERTAS', '','','','','', 0.00, 0.00, 8, '') --notas de credito select @conta=count(*) from docventa where fsa_docventa between @inicio and @final and est_docventa<>'ANU' and cod_tipodoc=@ncr /* select count(*) from docventa where fsa_docventa between @inicio and @final and est_docventa='ANU' and cod_tipodoc=@ncr '10-18-2005 00:00:00' and '10-18-2005 23:59:59' order by a01_tit select count(*) from docventa select * from docventa where fsa_docventa between '10-18-2005 00:00:00' and '10-18-2005 23:59:59' and cod_tipodoc='008' and est_docventa<>'ANU' select 'NOTAS DE CREDITO', convert(char(30), c.rso_clientes), a.dim_art, STR(i.can_idocventa, 10, 2), STR(i.pun_idocventa, 10, 2), CASE d.cod_tipodocori WHEN '001' THEN 'FA '+d.cod_serieori+'-'+d.sec_serieori WHEN '002' THEN 'BV '+d.cod_serieori+'-'+d.sec_serieori END, i.tot_idocventa * (-1), 0, 9, '' from docventa d, idocventa i, clientes c, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between '10-18-2005 00:00:00' and '10-18-2005 23:59:59' and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and d.est_docventa<>'ANU' and d.cod_tipodoc='008' */ if @conta> 0 insert into #t1 select 'NOTAS DE CREDITO', convert(char(30), c.rso_clientes), a.dim_art, STR(i.can_idocventa, 10, 2), STR(i.pun_idocventa, 10, 2),

CASE d.cod_tipodocori WHEN '001' THEN 'FA '+d.cod_serieori+'-'+d.sec_serieori WHEN '002' THEN 'BV '+d.cod_serieori+'-'+d.sec_serieori END, i.tot_idocventa * (-1), 0, 9, '' from docventa d, idocventa i, clientes c, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.fsa_docventa between @inicio and @final and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and d.est_docventa<>'ANU' and d.cod_tipodoc=@ncr else insert into #t1 values ('NOTAS DE CREDITO', '', '','','','', 0.00, 0.00, 9, '') --gastos falta select @conta=count(*) from gasto where fsa_gasto between @inicio and @final and mot_gasto='BALONES' if @conta> 0 insert into #t1 select 'GASTOS', '','','','','', tot_gasto*(-1), 0.00, 10, '' from gasto where fsa_gasto between @inicio and @final and mot_gasto='BALONES' else insert into #t1 values ('GASTOS', '','','','','', 0.00, 0.00, 10, '')

--bancos select @conta=count(*) from vaucher where fec_vaucher between @inicio and @final if @conta> 0 insert into #t1 select 'BANCOS', convert(char(10), v.cod_zonal), convert(char(30), b.des_banco), '', 'VAUCHER', convert(char(15), v.ope_vaucher), 0.00, v.imp_vaucher, 11, v.sec_vaucher from vaucher v, banco b where v.cod_banco=b.cod_banco and v.fec_vaucher between @inicio and @final and v.mot_vaucher='GENERAL' order by v.cod_zonal, b.des_banco, v.sec_vaucher else insert into #t1 values ('BANCOS', '','','','','', 0.00, 0.00, 11, '') insert into depositar select @inicio, * from #t1 drop table #t1

commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

SET ANSI_NULLS ON GO CREATE procedure sp_rep_cons_granelA (@zonal char(10), @inicio datetime, @final datetime) as begin transaction codigo declare @fac char(3), @bol char(3), @gui char(3), @zon char(10) declare @m01_doc char(3), @m02_ser char(3), @m03_sec char(10), @m04_tip char(3), @m05_cli char(5), @m06_est char(3) declare @i01_doc char(3), @i02_ser char(3), @i03_sec char(10), @i04_art char(3), @i05_pun float, @i06_can float declare @i07_tot float, @i08_mod char(3), @i09_est char(3), @i10_fac char(30), @i11_bol char(30), @i12_reg int declare @fila int, @conta int, @art char(3), @dim char(4), @kgr float declare @inidoc char(3), @iniser char(3), @inisec char(10), @iniart char(3), @inipun float, @inican float, @initot float, @inimod char(3), @iniest char(3) declare @findoc char(3), @finser char(3), @finsec char(10), @finart char(3), @finpun float, @fincan float, @fintot float, @finmod char(3), @finest char(3) declare @igdoc char(3), @igser char(3), @igsec char(10), @fgdoc char(3), @fgser char(3), @fgsec char(10) declare @tfila int, @ttotal float, @reg int, @tcant int , @anusec char(10), @sumador int, @m00_cli char(50), @ini_cli char(50) declare @m05_gru int, @m06_ele int, @fle char(3), @gra char(3), @b12 char(3), @b25 char(3), @can float, @venta float declare @facturado float, @credito float declare @b01_ano int, @b02_mes int, @b03_dia int, @b04_zon char(10), @b05_des char(10), @b06_fsa datetime, @b07_rso char(50) declare @ven real, @pag real select @fle=cod_art POR FLETE' select @gra=cod_art GRANEL' select @b12=cod_art DE 12 KILOS' select @b25=cod_art DE 2.50 KILOS' from articulo where ltrim(rtrim(des_art)) = 'SERVICIO from articulo where ltrim(rtrim(des_art)) = 'GAS A from articulo where ltrim(rtrim(des_art)) = 'BALONES from articulo where ltrim(rtrim(des_art)) = 'BALONES

select @fac=cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol=cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui=cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' create table #t1 ( b01_ano Remision b02_mes Remision b03_dia Remision

int int int

NULL, NULL, NULL,

-- Tipo de Documento Ej Guia de -- Tipo de Documento Ej Guia de -- Tipo de Documento Ej Guia de

b04_zon b05_des b06_fsa Remision b07_rso b08_ode m01_doc Remision m02_ser m03_sec m04_gru m05_ele m06_est i01_doc i02_ser i03_sec i04_art i05_pun i06_can i07_tot i08_mod i09_est i10_fac i11_bol ba1_cod ba2_ope ba3_imp ba4_glo )

char(10) null, char(10) null, datetime NULL, char(50) NULL, char(15) null, char(3) NULL,

-- Tipo de Documento Ej Guia de -- Codigo de la serie -- Tipo de Documento Ej Guia de

char(3) NULL, -- Codigo de la serie char(10) NULL, -- Secuencia de la serie int null, int null, char(3) NULL , -- Secuencia de la serie char(3) null, -- articulo char(3) null, -- articulo char(10) null, -- articulo char(3) null, -- articulo float NULL default 0, -- Zonal Origen float NULL default 0, -- Zonal Origen float NULL default 0, -- Zonal Origen char(3) NULL, -- Secuencia de la serie char(3) NULL, -- Secuencia de la serie char(30) default '' null, char(30) default '' null, char(5) null default '', char(15)null default '', float null default 0, char(30) null default ''

------------------------------------------------------------ Carga todos los registros con guia -------------------------------------------------------insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac) select year(d.fsa_docventa), month(d.fsa_docventa), day(d.fsa_docventa), d.cod_zonal, 'DOCUMENTOS', d.fsa_docventa, c.rso_clientes, d.ode_docventa, m.cod_tipodoc, m.cod_serie, m.sec_serie, m.est_mov, d.cod_tipodoc, d.cod_serie, d.sec_serie,i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+d.sec_serie from docventa d, idocventa i, articulo a, mov m, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.des_art in ('GAS A GRANEL', 'GRANEL KILOS') and fsa_docventa between @inicio and @final and d.cod_zonal like ltrim(rtrim(@zonal)) + '%' and (d.cod_tipodoc=@fac) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art

insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i11_bol) select year(d.fsa_docventa), month(d.fsa_docventa), day(d.fsa_docventa), d.cod_zonal, 'DOCUMENTOS', d.fsa_docventa, c.rso_clientes, d.ode_docventa, m.cod_tipodoc, m.cod_serie, m.sec_serie, m.est_mov, d.cod_tipodoc, d.cod_serie, d.sec_serie,i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+d.sec_serie from docventa d, idocventa i, articulo a, mov m, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and m.cod_tipodoc=i.cod_tipodocpad and m.cod_serie=i.cod_seriepad and m.sec_serie=i.sec_seriepad and a.cod_art=i.cod_art and a.des_art in ('GAS A GRANEL', 'GRANEL KILOS') and fsa_docventa between @inicio and @final and d.cod_zonal like ltrim(rtrim(@zonal)) + '%' and (d.cod_tipodoc=@bol) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art ------------------------------------------------------------ Carga todos los registros sin guia -------------------------------------------------------/*insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac) select year(d.fsa_docventa), month(d.fsa_docventa), day(d.fsa_docventa), d.cod_zonal, 'DOCUMENTOS', d.fsa_docventa, c.rso_clientes, d.ode_docventa, '', '', '', '', d.cod_tipodoc, d.cod_serie, d.sec_serie,i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+d.sec_serie from docventa d, idocventa i, articulo a, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and a.des_art='GAS A GRANEL' and fsa_docventa between @inicio and @final and d.cod_zonal like ltrim(rtrim(@zonal)) + '%' and (d.cod_tipodoc=@fac) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i11_bol) select year(d.fsa_docventa), month(d.fsa_docventa), day(d.fsa_docventa), d.cod_zonal, 'DOCUMENTOS', d.fsa_docventa, c.rso_clientes, d.ode_docventa,

'', '', '', '', d.cod_tipodoc, d.cod_serie, d.sec_serie,i.cod_art, i.pun_idocventa, i.can_idocventa, i.tot_idocventa, d.mod_docventa, d.est_docventa, d.cod_serie+'-'+d.sec_serie from docventa d, idocventa i, articulo a, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and a.cod_art=i.cod_art and a.des_art='GAS A GRANEL' and fsa_docventa between @inicio and @final and d.cod_zonal like ltrim(rtrim(@zonal)) + '%' and (d.cod_tipodoc=@bol) order by d.cod_tipodoc, d.cod_serie, d.sec_serie, i.cod_art */ ------------------------------------------------------------ Carga percepcion -------------------------------------------------------insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac) select year(d.fsa_docventa), month(d.fsa_docventa), day(d.fsa_docventa), d.cod_zonal, 'PERCEPCION', d.fsa_docventa, 'PERCEPCION '+d.cod_serieper+'-'+ltrim(rtrim(d.sec_serieper)),'', d.cod_tipodoc, d.cod_serie, ltrim(rtrim(d.sec_serie))+ CASE d.cod_tipodoc WHEN '001' THEN 'F' WHEN '002' THEN 'B' END, '', '', '', '', '', '', 0, d.mper_docventa, d.mod_docventa, d.est_docventa, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and cod_tipodocper is not null and cod_serieper is not null and sec_serieper is not null and est_docventa<>'ANU' and mod_docventa='CON' and len(cod_tipodocper)>0 and len(cod_serieper)>0 and len(sec_serieper)>0 and fsa_docventa between @inicio and @final and cod_zonal like @zonal +'%' and d.cod_tipodoc+d.cod_serie in (select distinct cod_tipodoc+cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art in ('GAS A GRANEL', 'GRANEL KILOS')) insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, b08_ode, m01_doc, m02_ser, m03_sec, m06_est,i01_doc,i02_ser,i03_sec,i04_art, i05_pun, i06_can, i07_tot, i08_mod, i09_est, i10_fac) select year(d.fsa_pago), month(d.fsa_pago), day(d.fsa_pago), d.cod_zonal, 'PERCEPCION', d.fsa_pago, 'PERCEPCION '+d.cod_serieper+''+ltrim(rtrim(d.sec_serieper)),'', d.cod_tipodoc, d.cod_serie, ltrim(rtrim(d.sec_serie))+ CASE d.cod_tipodoc

WHEN '001' THEN 'F' WHEN '002' THEN 'B' WHEN '009' THEN 'P' END, '', '', '', '', '', '', 0, d.mper_docventa, '', d.est_pago, '' from pago d where cod_tipodocper is not null and cod_serieper is not null and sec_serieper is not null and est_pago<>'ANU' and len(cod_tipodocper)>0 and len(cod_serieper)>0 and len(sec_serieper)>0 and fsa_pago between @inicio and @final and cod_zonal=@zonal and cod_tipodoc+cod_serie in (select distinct cod_tipodocpad+cod_seriepad from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art in ('GAS A GRANEL', 'GRANEL KILOS')) /* DECLARE cursor_zonalper CURSOR FOR select cod_zonal from zonal OPEN cursor_zonalper FETCH cursor_zonalper into @zon WHILE @@FETCH_STATUS=0 BEGIN set @ven=0 set @pag=0 select @ven=isnull(sum(mper_docventa), 0) from docventa where cod_tipodocper is not null and cod_serieper is not null and sec_serieper is not null and est_docventa<>'ANU' and mod_docventa='CON' and len(cod_tipodocper)>0 and len(cod_serieper)>0 and len(sec_serieper)>0 and fsa_docventa between @inicio and @final and cod_zonal=@zon and cod_tipodoc+cod_serie in (select distinct cod_tipodoc+cod_serie from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art='GAS A GRANEL') select @pag=isnull(sum(mper_docventa), 0) from pago where cod_tipodocper is not null and cod_serieper is not null and sec_serieper is not null and est_pago<>'ANU' and len(cod_tipodocper)>0 and len(cod_serieper)>0 and len(sec_serieper)>0 and fsa_pago between @inicio and @final and cod_zonal=@zon and cod_tipodoc+cod_serie in (select distinct cod_tipodocpad+cod_seriepad from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art='GAS A GRANEL')

if @ven + @pag > 0 insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, i07_tot) select year(@inicio), month(@inicio), day(@inicio), @zon, 'PERCEPCION', @inicio, 'PERCEPCION', @ven + @pag FETCH cursor_zonalper into @zon END CLOSE cursor_zonalper DEALLOCATE cursor_zonalper */ ------------------------------------------------------------ Carga todos los documentos anulados de granel -------------------------------------------------------DECLARE cursor_anu CURSOR FOR select d.cod_zonal, c.rso_clientes, d.fsa_docventa, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.est_docventa from docventa d, clientes c where d.cod_clientes=c.cod_clientes and est_docventa='ANU' and fsa_docventa between @inicio and @final OPEN cursor_anu FETCH cursor_anu into @b04_zon, @b07_rso, @b06_fsa, @i01_doc, @i02_ser, @i03_sec, @i09_est WHILE @@FETCH_STATUS=0 BEGIN --verificar si historicamente la guia no se usa en granel select @conta=count(*) from idocventa where cod_tipodoc=@i01_doc and cod_serie=@i02_ser and cod_art=@gra --verificando que la guia no sea de gas a granel - si la guia tiene granel no aparece if exists(select * from docventa d, idocventa i where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=@gra) and @conta>0 begin if @i01_doc=@fac insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, i01_doc, i02_ser, i03_sec, i08_mod, i09_est, i10_fac) values(year(@b06_fsa), month(@b06_fsa), day(@b06_fsa), @b04_zon, 'DOCUMENTOS', @b06_fsa, @b07_rso, @i01_doc, @i02_ser, @i03_sec, '', @i09_est, @i02_ser+'-'+@i03_sec) else insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, b07_rso, i01_doc, i02_ser, i03_sec, i08_mod, i09_est, i11_bol)

values(year(@b06_fsa), month(@b06_fsa), day(@b06_fsa), @b04_zon, 'DOCUMENTOS', @b06_fsa, @b07_rso, @i01_doc, @i02_ser, @i03_sec, '', @i09_est, @i02_ser+'-'+@i03_sec) end FETCH cursor_anu into @b04_zon, @b07_rso, @b06_fsa, @i01_doc, @i02_ser, @i03_sec, @i09_est END CLOSE cursor_anu DEALLOCATE cursor_anu --------------------------------------------------------cargar guias anuladas y de transito ------------------------------------------------------DECLARE cursor_guias CURSOR FOR select cod_zonal, fsa_mov, cod_tipodoc, cod_serie, sec_serie, tip_cli, est_mov from mov where fsa_mov between @inicio and @final and est_mov='ANU' OPEN cursor_guias FETCH cursor_guias into @b04_zon, @b06_fsa, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m06_est WHILE @@FETCH_STATUS=0 BEGIN --verificar si historicamente la guia no se usa en granel select @conta=count(*) from imo where cod_tipodoc=@m01_doc and cod_serie=@m02_ser and cod_art=@gra --verificando que la guia no sea de gas a granel - si la guia tiene granel no aparece if exists(select * from mov m, imo i where m.cod_tipodoc=i.cod_tipodoc and m.cod_serie=i.cod_serie and m.sec_serie=i.sec_serie and i.cod_art=@gra) and @conta>0 begin set @fila=@fila+1 insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, m01_doc, m02_ser, m03_sec, m06_est) values(year(@b06_fsa), month(@b06_fsa), day(@b06_fsa), @b04_zon, 'DOCUMENTOS', @b06_fsa, @m01_doc, @m02_ser, @m03_sec, @m06_est) end FETCH cursor_guias into @b04_zon, @b06_fsa, @m01_doc, @m02_ser, @m03_sec, @m04_tip, @m06_est END CLOSE cursor_guias DEALLOCATE cursor_guias --------------------------------------------------------cargar vaucher -------------------------------------------------------

insert into #t1 (b01_ano, b02_mes, b03_dia, b04_zon, b05_des, b06_fsa, ba1_cod, ba2_ope, ba3_imp, ba4_glo) select year(fec_vaucher), month(fec_vaucher), day(fec_vaucher), cod_zonal, 'BANCOS', fec_vaucher, cod_banco, ope_vaucher, imp_vaucher, glo_vaucher from vaucher where mot_vaucher='GRANELERO' and fec_vaucher between @inicio and @final and cod_zonal like ltrim(rtrim(@zonal))+'%' select * from #t1 order by b01_ano, b02_mes, b03_dia, b04_zon, b05_des desc, i01_doc, i02_ser, i03_sec drop table #t1 commit transaction codigo

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF

GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_general_back1B as begin declare @total int, @zona char(10), @ini datetime, @fin datetime, @conta int declare @inimes datetime, @finmes datetime declare @iniusr datetime declare @inicio datetime, @final datetime declare @sini char(19), @sfin char(19) set @conta = 0 select @inimes=fin from mes where est='ACT' select @finmes=max(fsa_docventa) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') select @ini=@finmes --set @total = DATEDIFF(day, @inimes, @finmes) + 1 set @total = 90 select @zona=cod_zonal from parametros set @ini=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 00:00:00') set @fin=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 23:59:59') while @conta<@total begin set @inicio= dateadd(day, -@conta, @ini) set @final=dateadd(day, -@conta, @fin) select @iniusr=max(d.fin_usr) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') and fsa_docventa between @inicio and @final and d.fin_usr<=getdate()

if @iniusr >= convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 00:00:00') and @iniusr <=convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 23:59:59') begin set @inicio=convert(datetime, @inicio) set @final=convert(datetime, @final) exec sp_rep_cons_general1B @zona, @inicio, @final end if not exists (select * from cobranza where fecha between @inicio and @final) begin exec sp_rep_cons_general1B @zona, @inicio, @final end set @conta = @conta + 1 end end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_general_back2B as begin declare @total int, @zona char(10), @ini datetime, @fin datetime, @conta int declare @inimes datetime, @finmes datetime declare @iniusr datetime declare @inicio datetime, @final datetime declare @sini char(19), @sfin char(19) set @conta = 0 select @inimes=fin from mes where est='ACT' select @finmes=max(fsa_docventa) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') select @ini=@finmes --set @total = DATEDIFF(day, @inimes, @finmes) + 1 set @total = 90 select @zona=cod_zonal from parametros set @ini=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 00:00:00')

set @fin=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 23:59:59') while @conta<@total begin set @inicio= dateadd(day, -@conta, @ini) set @final=dateadd(day, -@conta, @fin) select @iniusr=max(d.fin_usr) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') and fsa_docventa between @inicio and @final and d.fin_usr<=getdate() if @iniusr >= convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 00:00:00') and @iniusr <=convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 23:59:59') begin set @inicio=convert(datetime, @inicio) set @final=convert(datetime, @final) exec sp_rep_cons_general2B @zona, @inicio, @final end set @conta = @conta + 1 end end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_general_back3B as begin declare @total int, @zona char(10), @ini datetime, @fin datetime, @conta int declare @inimes datetime, @finmes datetime, @finpago datetime declare @iniusr datetime, @inivau datetime declare @inicio datetime, @final datetime declare @i datetime, @f datetime declare @sini char(19), @sfin char(19) set @conta = 0 select @inimes=fin from mes where est='ACT' select @finmes=max(fsa_docventa) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') select @finpago=max(fsa_pago) from pago

if @finmes>=@finpago begin select @ini=@finmes set @total = DATEDIFF(day, @inimes, @finmes) + 1 end else begin select @ini=@finpago set @total = DATEDIFF(day, @inimes, @finpago) + 1 end set @total = 90 select @zona=cod_zonal from parametros set @ini=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 00:00:00') set @fin=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 23:59:59') while @conta<@total begin set @inicio= dateadd(day, -@conta, @ini) set @final=dateadd(day, -@conta, @fin) select @iniusr=max(d.fin_usr) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') and fsa_docventa between @inicio and @final and d.fin_usr<=getdate() select @inivau=max(fin_usr) from pago where fsa_pago between @inicio and @final set @i=convert(datetime, convert(char(2), month(getdate()))+''+convert(char(2), day(getdate()))+'-'+convert(char(4), year(getdate()))+ ' 00:00:00') set @f=convert(datetime, convert(char(2), month(getdate()))+''+convert(char(2), day(getdate()))+'-'+convert(char(4), year(getdate()))+ ' 23:59:59') if (@iniusr >= @i and @iniusr <= @f) or (@inivau >= @i and @inivau <= @f) begin exec sp_rep_cons_general3B @zona, @inicio, @final end set @conta = @conta + 1 end end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_general_back5B as begin declare @total int, @zona char(10), @ini datetime, @fin datetime, @conta int declare @inimes datetime, @finmes datetime declare @iniusr datetime declare @inicio datetime, @final datetime declare @sini char(19), @sfin char(19) set @conta = 0 select @inimes=fin from mes where est='ACT' select @finmes=max(fsa_docventa) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') select @ini=@finmes --set @total = DATEDIFF(day, @inimes, @finmes) + 1 set @total = 90 select @zona=cod_zonal from parametros set @ini=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 00:00:00') set @fin=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 23:59:59') while @conta<@total begin set @inicio= dateadd(day, -@conta, @ini) set @final=dateadd(day, -@conta, @fin) select @iniusr=max(d.fin_usr) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') and fsa_docventa between @inicio and @final and d.fin_usr<=getdate() if @iniusr >= convert(datetime, month(getdate()))+'-'+convert(char(2), '+convert(char(4), year(getdate()))+ ' and @iniusr <=convert(datetime, month(getdate()))+'-'+convert(char(2), '+convert(char(4), year(getdate()))+ ' convert(char(2), day(getdate()))+'00:00:00') convert(char(2), day(getdate()))+'23:59:59')

begin set @inicio=convert(datetime, @inicio) set @final=convert(datetime, @final) exec sp_rep_cons_general5B @zona, @inicio, @final end set @conta = @conta + 1 end end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_rep_general_back6B as begin declare @total int, @zona char(10), @ini datetime, @fin datetime, @conta int declare @inimes datetime, @finmes datetime declare @iniusr datetime declare @inicio datetime, @final datetime declare @sini char(19), @sfin char(19) set @conta = 0 select @inimes=fin from mes where est='ACT' select @finmes=max(fsa_docventa) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') select @ini=@finmes --set @total = DATEDIFF(day, @inimes, @finmes) + 1 set @total = 90 select @zona=cod_zonal from parametros set @ini=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 00:00:00') set @fin=convert(datetime, convert(char(2), month(@ini))+''+convert(char(2), day(@ini))+'-'+convert(char(4), year(@ini))+ ' 23:59:59') while @conta<@total begin set @inicio= dateadd(day, -@conta, @ini) set @final=dateadd(day, -@conta, @fin) select @iniusr=max(d.fin_usr) from docventa d, tipodoc t where d.cod_tipodoc=t.cod_tipodoc and (des_tipodoc='FACTURA' or des_tipodoc='BOLETA') and fsa_docventa between @inicio and @final and d.fin_usr<=getdate() if @iniusr >= convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 00:00:00')

and @iniusr <=convert(datetime, convert(char(2), month(getdate()))+'-'+convert(char(2), day(getdate()))+''+convert(char(4), year(getdate()))+ ' 23:59:59') begin set @inicio=convert(datetime, @inicio) set @final=convert(datetime, @final) exec sp_rep_cons_general6B @zona, @inicio, @final end set @conta = @conta + 1 end end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

/****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_res_cobranza_art_neto (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @des char(30), @tot float, @art char(3) declare @docu char(3), @seri char(3), @secu char(10), @tota float, @ing char(3) declare @b01 char(3), @b02 char(3), @b03 char(3), @b04 char(3), @b05 char(3), @b10 char(3), @b12 char(3), @b15 char(3), @b45 char(3) declare @d01 char(3), @d02 char(3), @d03 char(3), @d04 char(3), @d05 char(3), @d10 char(3), @d12 char(3), @d15 char(3), @d45 char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ing = cod_tipodoc from tipodoc where des_tipodoc='RECIBO DE INGRESO' --balones select @b01=cod_art select @b02=cod_art select @b03=cod_art select @b04=cod_art select @b05=cod_art select @b10=cod_art from from from from from from articulo articulo articulo articulo articulo articulo where where where where where where des_art='BALONES des_art='BALONES des_art='BALONES des_art='BALONES des_art='BALONES des_art='BALONES DE DE DE DE DE DE 01 02 03 04 05 10 KILOS' KILOS' KILOS' KILOS' KILOS' KILOS'

select @b12=cod_art from articulo where des_art='BALONES DE 12 KILOS' select @b15=cod_art from articulo where des_art='BALONES DE 15 KILOS' select @b45=cod_art from articulo where des_art='BALONES DE 45 KILOS' --descuento select @d01=cod_art select @d02=cod_art select @d03=cod_art select @d04=cod_art select @d05=cod_art select @d10=cod_art select @d12=cod_art select @d15=cod_art select @d45=cod_art create table #t0 ( t00_zon t01_doc t02_ser t03_sec t04_art t05_can t06_pun t07_tot ) from from from from from from from from from articulo articulo articulo articulo articulo articulo articulo articulo articulo where where where where where where where where where des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO des_art='DESCUENTO DE DE DE DE DE DE DE DE DE 01 02 03 04 05 10 12 15 45 KILOS' KILOS' KILOS' KILOS' KILOS' KILOS' KILOS' KILOS' KILOS'

char (10) null, char (3) null, char (3) null, char (10) null, char (3) null, float null, float null, float null

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final

and -and 'SERVICIO -and and and and

d.cod_tipodoc=@fac (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', POR FLETE', 'REGULADOR')) a.pos_art=1 and a.gui_art='GUI' d.cod_zonal like ltrim(rtrim(@zon)) +'%' i.cod_serie like ltrim(rtrim(@ser)) + '%' i.sec_serie like ltrim(rtrim(@sec)) + '%'

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and d.cod_tipodoc=@bol -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='PAGO ADELANTADO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.mod_docventa='ADE' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocori like ltrim(rtrim(@ing)) + '%' and i.cod_serieori like ltrim(rtrim(@ser)) + '%' and i.sec_serieori like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='NCR' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='CONTADO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CON' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='CREDITO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CRE' -and a.pos_art=1 and a.gui_art='GUI' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.mot_docventa='SGU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='GUI' and i.cod_tipodocpad= @gui -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and d.cod_tipodoc=@fac -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and d.cod_tipodoc=@bol -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='PAGO ADELANTADO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='ADE' --and a.pos_art=1 and a.gui_art='GUI' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='NCR' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='CONTADO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CON' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='CREDITO') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mod_docventa='CRE' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa

from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='SGU' -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') insert into #t0 select d.cod_zonal, i.cod_tipodoc, i.cod_serie, i.sec_serie, i.cod_art, i.can_idocventa , i.pun_idocventa, i.tot_idocventa from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='GUI' and i.cod_tipodocpad= @gui -and (a.gui_art='GUI' or a.des_art in ('CILINDROS VACIOS X 5 KL.', 'SERVICIO POR FLETE', 'REGULADOR')) -and a.pos_art=1 and a.gui_art='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' /*DECLARE cursor_doc CURSOR FOR select cod_art, des_art from articulo where pos_art=-1 OPEN cursor_doc FETCH cursor_doc into @art, @des WHILE @@FETCH_STATUS=0 BEGIN ------------------------------------------------DECLARE cursor_art CURSOR FOR select t01_doc, t02_ser, t03_sec, t07_tot from #t0 where t04_art=@art OPEN cursor_art FETCH cursor_art into @docu, @seri, @secu, @tota WHILE @@FETCH_STATUS=0 BEGIN if @art=@d01 update #t0 set t06_pun= t06_pun + @tota/t05_can, t07_tot= (t06_pun + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t03_sec=@secu and t04_art=@b01 if @art=@d02

update #t0 set t06_pun= t06_pun + @tota/t05_can, t07_tot= (t06_pun + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t03_sec=@secu and t04_art=@b02 if @art=@d03 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b03 if @art=@d04 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b04 if @art=@d05 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b05 if @art = @d10 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art = @b10 if @art=@d12 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b12 if @art=@d15 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b15 if @art=@d45 update #t0 set t06_pun= t06_pun + @tota/t05_can, + @tota/t05_can) * t05_can where t01_doc=@docu and t02_ser=@seri and t04_art=@b45

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

t07_tot= (t06_pun and t03_sec=@secu

FETCH cursor_art into @docu, @seri, @secu, @tota END CLOSE cursor_art

DEALLOCATE cursor_art ------------------------------------------------FETCH cursor_doc into @art, @des END CLOSE cursor_doc DEALLOCATE cursor_doc delete #t0 from #t0 t, articulo a where t.t04_art=a.cod_art and a.pos_art=-1 */ --------------------------------------------------------Agrupa por articulo -------------------------------------------------------select a.des_art, d.t00_zon, d.t06_pun, sum(d.t05_can), sum(d.t07_tot) from #t0 d,articulo a where d.t04_art=a.cod_art group by a.des_art, d.t00_zon, d.t06_pun order by a.des_art, d.t06_pun, d.t00_zon drop table #t0 end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_res_nuereg_ventas (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3), @min int, @max int select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA'

select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' declare @zona char(10), @feca datetime, @doca char(3), @sera char(3), @esta char(10) declare @conta int, @cadena char(50), @seca char(10) create table #t1 ( c01_zon char(10) null, c02_fsa datetime null, c03_doc char(3) null, -- Documento c04_ser char(3) null, -- Serie c05_est char(3) null, -- Estado c06_sei char(10) null, -- Secuencia c07_sef char(10) null, -- Secuencia c08_ruc char(11) null, -- Ruc c09_rso char(50) null, -- Razon social c10_bru float default 0 null, -- bruto c11_igv float default 0 null, -- igv c12_tot float default 0 null, -- Total c13_doc char(2) null, -- Documento sunat c14_tdo c15_tca c16_ina ) if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') begin --insertar boletas activas insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie), sum(bru_docventa), sum(igv_docventa), sum(tot_docventa), c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc char(2) null, char(4) null, float default 0 null

-- Inafecto

WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@bol and est_docventa<>'ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa --insertar facturas activas insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, sec_serie, sec_serie, c.ruc_dni_clientes, c.rso_clientes, bru_docventa, igv_docventa, tot_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@fac and est_docventa<>'ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' /* group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+

'-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa, c.ruc_dni_clientes, c.rso_clientes */ /* insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie), c.ruc_dni_clientes, c.rso_clientes, sum(bru_docventa), sum(igv_docventa), sum(tot_docventa), c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@fac and est_docventa<>'ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa, c.ruc_dni_clientes, c.rso_clientes */ ------------------------------insertar facturas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal,

convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@fac and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1 set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01'

WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end end set @min = @min + 1 end if @conta<4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor ------------------------------insertar boletas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa,

min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@bol and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1 set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC'

END, 'S/. ', 0) set @cadena = '' set @conta = 0 end end set @min = @min + 1 end if @conta<4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') begin ------------------------------insertar facturas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final

and cod_tipodoc=@fac and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1 set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end

end set @min = @min + 1 end if @conta<4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor ------------------------------insertar boletas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@bol and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+

'-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1 set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end end set @min = @min + 1 end if @conta<4 begin insert into #t1

(c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor end c06_sei, if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') begin --insertar facturas activas insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, sec_serie, sec_serie, c.ruc_dni_clientes, c.rso_clientes, bru_docventa, igv_docventa, tot_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0

from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@fac and est_docventa<>'ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' ------------------------------insertar facturas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@fac and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1

set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end end set @min = @min + 1 end if @conta<4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor

end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') begin --insertar boletas activas insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa, min(sec_serie), max(sec_serie), sum(bru_docventa), sum(igv_docventa), sum(tot_docventa), c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@bol and est_docventa<>'ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa ------------------------------insertar boletas anuladas -----------------------------DECLARE anuf_cursor CURSOR FOR select d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), d.cod_tipodoc, d.cod_serie, d.est_docventa,

min(sec_serie), max(sec_serie) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and cod_tipodoc=@bol and est_docventa='ANU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by d.cod_zonal, convert(datetime, ltrim(rtrim(convert(char(2),month(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(d.fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(d.fin_docventa))))+ ' 00:00:00'), cod_tipodoc, cod_serie, est_docventa OPEN anuf_cursor FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max set @conta=0 WHILE @@FETCH_STATUS = 0 BEGIN WHILE @min<=@max begin set @seca=replicate('0',7 datalength(ltrim(rtrim(str(@min)))))+ltrim(rtrim(str(@min))) if exists(select * from docventa where est_docventa='ANU' and cod_tipodoc=@doca and cod_serie=@sera and sec_serie=@seca and convert(datetime, ltrim(rtrim(convert(char(2),month(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(2),day(fin_docventa))))+ '-'+ltrim(rtrim(convert(char(4),year(fin_docventa))))+ ' 00:00:00')= @feca) begin set @conta = @conta + 1 set @cadena=ltrim(rtrim(isnull(@cadena, ''))) + ' '+ @seca if @conta = 4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC'

END, 'S/. ', 0) set @cadena = '' set @conta = 0 end end set @min = @min + 1 end if @conta<4 begin insert into #t1 (c01_zon, c02_fsa, c03_doc, c04_ser, c05_est, c06_sei, c07_sef, c08_ruc, c09_rso, c10_bru, c11_igv, c12_tot, c13_doc, c14_tdo, c15_tca, c16_ina) values (@zona, @feca, @doca, @sera, 'ANU', '', '', '', @cadena, 0, 0, 0, CASE @doca WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, CASE @doca WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0) set @cadena = '' set @conta = 0 end set @conta=0 FETCH NEXT FROM anuf_cursor INTO @zona, @feca, @doca, @sera, @esta, @min, @max END CLOSE anuf_cursor DEALLOCATE anuf_cursor end select * from #t1 order by c02_fsa, drop table #t1 end

c14_tdo,

c04_ser,

c05_est desc

--select * from docventa where sec_serie='0000384'

GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

CREATE procedure sp_res_reg_ventas (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' create table #t1 ( c00_zon char(10) null, c01_doc char(3) null, -c02_ser char(3) null, -c03_sec char(10) null, c04_glo char(300) null, c05_mod char(3) null, -c06_est char(3) null, -c07_ruc char(11) null, c08_rso char(50) null, c09_bru float default 0 null, c10_igv float default 0 null, c11_tot float default 0 null, c12_fin datetime null, c13_fsa datetime null, c14_doc char(2) null, c15_tdo c16_tca c17_ina ) if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc char(2) null, char(4) null, float default 0 null

Documento Serie -- Secuencia -- Glosa Modalidad Estado -- Ruc -- Razon social -- bruto -- igv -- Total -- Documento sunat

-- Inafecto

WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes,

c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='SGU'

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie else insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, idocventa i, clientes c

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' order by d.cod_serie, d.cod_tipodoc, d.fsa_docventa, d.sec_serie -order by d.fsa_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc

WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01'

WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%'

order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie else insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0 from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' order by d.cod_serie, d.cod_tipodoc, d.fin_docventa, d.sec_serie -order by d.fin_docventa, d.cod_serie, d.cod_tipodoc, d.sec_serie -update #t1 set c09_bru=c09_bru*(-1), c10_igv=c10_igv*(-1), c11_tot=c11_tot*(-1), c17_ina=c17_ina*(-1) where c01_doc=@ncr select * from #t1 order by c02_ser, c14_doc, c12_fin, c03_sec --order by c00_zon, c14_doc , c02_ser, c03_sec drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --exec sp_res_reg_ventas_ant '', 'EMISION', '11-01-1999 00:00:00', '1101-2004 23:59:59', 'TODOS', '083', '' create procedure sp_res_reg_ventas_ant (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' create table #t1 ( c00_zon char(10) null, c01_doc char(3) null, -c02_ser char(3) null, -c03_sec char(10) null, c04_glo char(300) null, c05_mod char(3) null, -c06_est char(3) null, -c07_ruc char(11) null, c08_rso char(50) null, c09_bru float default 0 null, c10_igv float default 0 null, c11_tot float default 0 null, c12_fin datetime null, c13_fsa datetime null, c14_doc char(2) null, c15_tdo c16_tca c17_ina ) if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada char(2) null, char(4) null, float default 0 null

Documento Serie -- Secuencia -- Glosa Modalidad Estado -- Ruc -- Razon social -- bruto -- igv -- Total -- Documento sunat

-- Inafecto

when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.fec_pago between @inicio and @final and tipo_doc in ('F', 'B') and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_pago, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and anulada=1 and d.fec_pago between @inicio and @final and tipo_doc in ('F', 'B') and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%'

order by d.fec_pago, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.tipo_doc='F' and d.fec_pago between @inicio and @final and tipo_doc in ('F', 'B') and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_pago, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6)

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01'

WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.tipo_doc='B' and d.fec_pago between @inicio and @final and tipo_doc in ('F', 'B') and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_pago, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.fec_doc between @inicio and @final and tipo_doc in ('F', 'B') and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_doc, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '',

case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.fec_doc between @inicio and @final and tipo_doc in ('F', 'B') and anulada=1 and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_doc, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6)

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0

from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.fec_doc between @inicio and @final and tipo_doc in ('F', 'B') and tipo_doc='F' and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_doc, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select '', d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6), '', case credito when 0 then 'CON' when 1 then 'CRE' end, case anulada when 0 then 'ACT' when 1 then 'ANU' end, c.ruc, c.razon_soc, d.total / 1.18, d.total - d.total / 1.18, d.total, d.fec_doc, d.fec_pago, CASE d.tipo_doc WHEN 'F' THEN '01' WHEN 'B' THEN '03' END, CASE d.tipo_doc WHEN 'F' THEN 'FA' WHEN 'B' THEN 'BV' END, 'S/. ', 0 from doc_ant d, cli_ant c where d.cod_cli=c.cod_cli and d.fec_doc between @inicio and @final and tipo_doc in ('F', 'B') and tipo_doc='B' and substring(d.num_doc, 1,3) like ltrim(rtrim(@ser)) + '%' and substring(d.num_doc, 4,6) like ltrim(rtrim(@sec)) + '%' order by d.fec_doc, d.tipo_doc, substring(d.num_doc, 1,3), substring(d.num_doc, 4,6) select * from #t1 order by c00_zon, c14_doc , c02_ser, c03_sec drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF

GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE procedure sp_res_reg_ventas_art (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @des char(30), @tot float, @tde float declare @ncr char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' create table #t1 ( c01_des char(30), c02_can float default c03_bru float default 0, c04_igv float default c05_tot float default c06_ina float default )

-- Descripcin del articulo 0, -- Cantidad -- Total 0, -- Total 0, -- Total 0 -- Total

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1

select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='SGU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art

order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.mot_docventa='SGU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad= @gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.mot_docventa='GUI'

and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad= @gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie

and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and i.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a

where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') begin insert into #t1 select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='SGU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.mot_docventa='SGU' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_serie like ltrim(rtrim(@ser)) + '%' and i.sec_serie like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') begin insert into #t1

select a.des_art, sum(i.can_idocventa), sum(i.tgv_idocventa), sum(i.tot_idocventa) - sum(i.tgv_idocventa), sum(i.tot_idocventa), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.mot_docventa='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad= @gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art insert into #t1 select ltrim(rtrim(a.des_art))+' /NC', sum(i.can_idocventa)*(-1), sum(i.tgv_idocventa)*(-1), (sum(i.tot_idocventa) sum(i.tgv_idocventa))*(-1), sum(i.tot_idocventa)*(-1), 0 from docventa d, idocventa i, articulo a where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and i.cod_art=a.cod_art and i.est_idocventa<>'ANU' and d.est_docventa<>'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@ncr) and d.mot_docventa='GUI' and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad= @gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec)) + '%' group by a.des_art order by a.des_art end /* --DESCUENTO DE 05 KILOS SELECT @tde=isnull(sum(c05_tot), 0) FROM #t1 WHERE c01_des='DESCUENTO DE 05 KILOS' SELECT @tot=isnull(sum(c05_tot),0) FROM #t1 WHERE c01_des='BALONES DE 05 KILOS' update #t1 set c05_tot = @tot + @tde, c03_bru = (@tot + @tde) /1.18, c04_igv = (@tot + @tde) - (@tot + @tde) /1.18 where c01_des='BALONES DE 05 KILOS' --DESCUENTO DE 10 KILOS SELECT @tde=isnull(sum(c05_tot), 0) FROM #t1 WHERE c01_des='DESCUENTO DE 10 KILOS' SELECT @tot=isnull(sum(c05_tot),0) FROM #t1 WHERE c01_des='BALONES DE 10 KILOS' update #t1 set c05_tot = @tot + @tde, c03_bru = (@tot + @tde) /1.18, c04_igv = (@tot + @tde) - (@tot + @tde) /1.18

where c01_des='BALONES DE 10 KILOS' --DESCUENTO DE 15 KILOS SELECT @tde=isnull(sum(c05_tot), 0) FROM #t1 WHERE c01_des='DESCUENTO DE 15 KILOS' SELECT @tot=isnull(sum(c05_tot),0) FROM #t1 WHERE c01_des='BALONES DE 15 KILOS' update #t1 set c05_tot = @tot + @tde, c03_bru = (@tot + @tde) /1.18, c04_igv = (@tot + @tde) - (@tot + @tde) /1.18 where c01_des='BALONES DE 15 KILOS' --DESCUENTO DE 45 KILOS SELECT @tde=isnull(sum(c05_tot), 0) FROM #t1 WHERE c01_des='DESCUENTO DE 45 KILOS' SELECT @tot=isnull(sum(c05_tot),0) FROM #t1 WHERE c01_des='BALONES DE 45 KILOS' update #t1 set c05_tot = @tot + @tde, c03_bru = (@tot + @tde) /1.18, c04_igv = (@tot + @tde) - (@tot + @tde) /1.18 where c01_des='BALONES DE 45 KILOS' */ select * from #t1 --where c01_des<>'DESCUENTO DE 05 KILOS' and c01_des<>'DESCUENTO DE 10 KILOS' and c01_des<>'DESCUENTO DE 15 KILOS' and c01_des<>'DESCUENTO DE 45 KILOS' drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

--exec sp_res_reg_ventas_per 'PLANTA', 'COBRANZA', '01-01-2005 00:00:00', '01-31-2005 23:59:59', 'PERCEPCION', '004', '' CREATE procedure sp_res_reg_ventas_per (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3), @per char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE'

select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' select @per = cod_tipodoc from tipodoc where des_tipodoc='COMP. DE PERCEPCION VENTA INT.' create table #t1 ( c00_zon char(10) null, c01_doc char(3) null, -c02_ser char(3) null, -c03_sec char(10) null, c04_glo char(300) null, c05_mod char(3) null, -c06_est char(3) null, -c07_ruc char(11) null, c08_rso char(50) null, c09_bru float default 0 null, c10_igv float default 0 null, c11_tot float default 0 null, c12_fin datetime null, c13_fsa datetime null, c14_doc char(2) null, c15_tdo c16_tca c17_ina c18_per percepcion c19_docp percepcion c20_serp percepcion c21_secp percepcion c22_monp c23_totp c24_estp ) if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc char(2) null, char(4) null, float default 0 null, char(10) null, char(3) null, char(3) null, char(10) null, real null, real null, char(3)

Documento Serie -- Secuencia -- Glosa Modalidad Estado -- Ruc -- Razon social -- bruto -- igv -- Total -- Documento sunat

-- Inafecto --porcentaje de --tipo de documento de --serie de documento de --secuencia de documento de --monto de percepcion --total percibido

WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null mod_docventa='CON'

and

insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fsa_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' insert into #t1

select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%'

and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON'

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07'

END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @ncr and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON'

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%'

and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' else insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV'

WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.mper_docventa + d.tot_docventa as total, '' from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON'

-and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fsa_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS')

begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad

and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END,

c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON'

-and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @ncr and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV'

WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' else

insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.mper_docventa + d.tot_docventa as total, '' from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END,

'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' insert into #t1

select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' END update #t1 set c09_bru=c09_bru*(-1), c10_igv=c10_igv*(-1), c11_tot=c11_tot*(-1), c17_ina=c17_ina*(-1) where c01_doc=@ncr select * from #t1 order by c19_docp, c20_serp, c21_secp --c14_doc , c02_ser, c03_sec drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --exec sp_res_reg_ventas_per_new 'HUARAZ', 'COBRANZA', '11-21-2005 00:00:00', '11-21-2005 23:59:59', 'TODOS', '', '' CREATE procedure sp_res_reg_ventas_per_new (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3), @per char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' select @per = cod_tipodoc from tipodoc where des_tipodoc='COMP. DE PERCEPCION VENTA INT.' create table #t1 ( c00_zon char(10) null, c01_doc char(3) null, -c02_ser char(3) null, -c03_sec char(10) null, c04_glo char(300) null, c05_mod char(3) null, -c06_est char(3) null, -c07_ruc char(11) null, c08_rso char(50) null, c09_bru float default 0 null, c10_igv float default 0 null, c11_tot float default 0 null, c12_fin datetime null, c13_fsa datetime null, c14_doc char(2) null, c15_tdo c16_tca c17_ina c18_per percepcion char(2) null, char(4) null, float default 0 null, char(10) null,

Documento Serie -- Secuencia -- Glosa Modalidad Estado -- Ruc -- Razon social -- bruto -- igv -- Total -- Documento sunat

-- Inafecto --porcentaje de

c19_docp percepcion c20_serp percepcion c21_secp percepcion c22_monp c23_totp c24_estp )

char(3) null, char(3) null, char(10) null, real null, real null, char(3)

--tipo de documento de --serie de documento de --secuencia de documento de --monto de percepcion --total percibido

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='CONTADO') begin --insertando documentos con percepcion nuevas insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and d.cod_tipodocper is null and d.cod_serieper is null and d.sec_serieper is null and mod_docventa='CON' and mper_docventa>0 end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='CREDITO') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes,

c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fsa_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') begin --insertando documentos con percepcion nuevas insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, ''

from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and d.cod_tipodocper is null and d.cod_serieper is null and d.sec_serieper is null and mod_docventa='CON' and mper_docventa>0 insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc

WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fsa_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t1

select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final

and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON'

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @ncr and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON'

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1

select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c

where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' else insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.mper_docventa + d.tot_docventa as total, '' from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes,

c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fsa_pago between @inicio and @final

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%'

and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and p.cod_serieper like ltrim(rtrim(@ser)) + '%' and p.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' end

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select d.cod_zonal, '', '', '', '', '', '', '', '', 0, 0, 0, d.fin_docventa, d.fsa_docventa, c14_doc ='', c15_tdo = '', 'S/. ', 0, '0', d.cod_tipodoc, d.cod_serie, d.sec_serie, 0, 0 as total, 'ANU' from docventa d where d.est_docventa = 'ANU' and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@per) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @fac and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes,

c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @bol and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final

and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_tipodoc = @ncr and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes,

c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' else insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.mper_docventa + d.tot_docventa as total, '' from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI'

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, d.fin_docventa, d.fsa_docventa, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, d.porper_docventa, d.cod_tipodocper, d.cod_serieper, d.sec_serieper, d.mper_docventa, d.tot_docventa as total, '' from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa<>'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and len(d.cod_serieper)<>0 and d.cod_serieper is not null and mod_docventa='CON' -and (d.cod_serieper is not null or len(d.cod_serieper)=0) and mod_docventa='CON' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03'

WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' insert into #t1 select d.cod_zonal, d.cod_tipodoc, d.cod_serie, d.sec_serie, d.glo_docventa, d.mod_docventa, d.est_docventa, c.ruc_dni_clientes, c.rso_clientes, d.bru_docventa, d.igv_docventa, d.tot_docventa, p.fin_pago, p.fsa_pago, c14_doc = CASE d.cod_tipodoc WHEN @fac THEN '01' WHEN @bol THEN '03' WHEN @ncr THEN '07' END, c15_tdo = CASE d.cod_tipodoc WHEN @fac THEN 'FA' WHEN @bol THEN 'BV' WHEN @ncr THEN 'NC' END, 'S/. ', 0, p.porper_docventa, p.cod_tipodocper, p.cod_serieper, p.sec_serieper, p.mper_docventa, p.tot_pago as total, '' from docventa d, pago p, clientes c where d.cod_tipodoc=p.cod_tipodocpad and d.cod_serie=p.cod_seriepad and d.sec_serie=p.sec_seriepad and d.cod_clientes=c.cod_clientes and p.cod_clientes=c.cod_clientes and est_pago<>'ANU' and d.est_docventa <> 'ANU' and p.fin_pago between @inicio and @final

and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol or d.cod_tipodoc=@ncr) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and len(p.cod_serieper)<>0 and p.cod_serieper is not null and mod_docventa='CRE' -and (p.cod_serieper is not null or len(p.cod_serieper)=0) and mod_docventa='CRE' END update #t1 set c09_bru=c09_bru*(-1), c10_igv=c10_igv*(-1), c11_tot=c11_tot*(-1), c17_ina=c17_ina*(-1) where c01_doc=@ncr select * from #t1 order by c19_docp, c20_serp, c21_secp --c14_doc , c02_ser, c03_sec drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE procedure sp_res_reg_ventas_per_resumen (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3), @per char(3), @zonal char(10) declare @por char(10), @conta int, @fec datetime, @monto real select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA'

select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' select @per = cod_tipodoc from tipodoc where des_tipodoc='COMP. DE PERCEPCION VENTA INT.' create table #t1 ( c00_fec datetime null, c01_zon char(10) null, p01_por float default p02_por float default p03_por float default p04_por float default p05_por float default p06_por float default p07_por float default )

0 0 0 0 0 0 0

--fecha --zonal null, -null, -null, -null, -null, -null, -null --

venta1 venta2 venta3 venta4 venta5 venta6 venta7

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal UNION select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0

from pago d where d.fsa_pago between @inicio and @final and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN set @monto=0 select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00')= @fec and d.cod_tipodocper is not null and d.cod_serieper is not null and d.sec_serieper is not null and len(d.cod_tipodocper)>0 and len(d.cod_serieper)>0 and len(d.sec_serieper)>0 and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por /* select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d

from

where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por */ select @monto = @monto + isnull(sum(p.mper_docventa), 0) from pago p, docventa d where p.cod_tipodocpad=d.cod_tipodoc and p.cod_seriepad=d.cod_serie and p.sec_seriepad=d.sec_serie and p.cod_tipodocper is not null and p.cod_serieper is not null and p.sec_serieper is not null and p.est_pago<>'ANU' and d.mod_docventa='CRE' and len(p.cod_tipodocper)>0 and len(p.cod_serieper)>0 and len(p.sec_serieper)>0 and (p.cod_tipodocpad=@fac or p.cod_tipodocpad=@bol) and convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, p.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, p.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, p.fsa_pago))))+ ' 00:00:00')=@fec and p.cod_zonal like ltrim(rtrim(@zonal)) +'%' and p.cod_serie like ltrim(rtrim(@ser)) + '%' and p.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(p.porper_docventa, '0'))) =@por /* select @pag=isnull(sum(p.mper_docventa), 0) from pago p, docventa d where p.cod_tipodocpad=d.cod_tipodoc and p.cod_seriepad=d.cod_serie and p.sec_seriepad=d.sec_serie and p.cod_tipodocper is not null and p.cod_serieper is not null and p.sec_serieper is not null and p.est_pago<>'ANU' and d.mod_docventa='CRE' and len(p.cod_tipodocper)>0 and len(p.cod_serieper)>0 and len(p.sec_serieper)>0 and p.fsa_pago between @inicio and @final and p.cod_zonal=@zon and p.cod_tipodoc+p.cod_serie not in (select distinct cod_tipodocpad+cod_seriepad from idocventa i, articulo a where i.cod_art=a.cod_art and a.des_art='GAS A GRANEL') */

if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal

update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fsa_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal

set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=0 if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal

from

update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+

ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00')= @fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @fac and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por

from

select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00')= @fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @fac and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='COBRANZA' begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), '+ ltrim(rtrim(convert(char(2), ltrim(rtrim(convert(char(4), 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 and ltrim(rtrim(@opc))='BOLETA')

datepart(month, d.fsa_docventa))))+'datepart(day, d.fsa_docventa))))+'-'+ datepart(year, d.fsa_docventa))))+ '

from docventa d where d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @bol and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime,

from

ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @bol and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end

if (ltrim(rtrim(@tipo))='COBRANZA' CREDITO') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), '+ ltrim(rtrim(convert(char(2), ltrim(rtrim(convert(char(4), 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d

and ltrim(rtrim(@opc))='NOTA DE

datepart(month, d.fsa_docventa))))+'datepart(day, d.fsa_docventa))))+'-'+ datepart(year, d.fsa_docventa))))+ '

where d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00')='01-01-2005 00:00:00' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @ncr and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime,

from

ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00')='01-01-2005 00:00:00' and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @ncr and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final

and d.cod_tipodocper = @per and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') union select distinct isnull(porper_docventa, '0')

from docventa from pago

OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1

from

OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @ncr and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_pago))))+'-'+

ltrim(rtrim(convert(char(2), datepart(day, d.fsa_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @ncr and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%'

and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol)

from

and and and and and

d.est_pago<>'ANU' d.cod_zonal like ltrim(rtrim(@zonal)) +'%' d.cod_serie like ltrim(rtrim(@ser)) + '%' d.sec_serie like ltrim(rtrim(@sec)) + '%' ltrim(rtrim(isnull(porper_docventa, '0'))) =@por update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal

FETCH NEXT FROM ctf_cursor INTO @fec END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fin_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+

ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=0 if @conta = 1 update #t1 set p01_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 2 update #t1 set p02_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 3 update #t1 set p03_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 4 update #t1 set p04_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 5 update #t1 set p05_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 6 update #t1 set p06_por=@monto c00_fec=@fec and c01_zon=@zonal if @conta = 7 update #t1 set p07_por=@monto c00_fec=@fec and c01_zon=@zonal FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end

from

where where where where where where where

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') begin insert into #t1 select convert(datetime,

ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fin_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @fac and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%'

from

and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @fac and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+

ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fin_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @bol and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por

from

select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @bol and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'),

d.cod_zonal, 0,0,0,0,0,0,0 from docventa d where d.fin_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') from docventa union select distinct isnull(porper_docventa, '0') from pago OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1 OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @ncr and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0)

from

from pago d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @ncr and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='PERCEPCION') begin insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, 0,0,0,0,0,0,0

from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.cod_tipodocper = @per and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal set @conta=0 DECLARE ct_cursor CURSOR FOR select distinct isnull(porper_docventa, '0') union select distinct isnull(porper_docventa, '0')

from docventa from pago

OPEN ct_cursor FETCH NEXT FROM ct_cursor INTO @por WHILE @@FETCH_STATUS = 0 BEGIN set @conta = @conta + 1 DECLARE ctf_cursor CURSOR FOR select c00_fec, c01_zon #t1

from

OPEN ctf_cursor FETCH NEXT FROM ctf_cursor INTO @fec, @zonal WHILE @@FETCH_STATUS = 0 BEGIN select @monto=isnull(sum(d.mper_docventa), 0) from docventa d where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00')=@fec and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_tipodoc = @ncr and d.est_docventa<>'ANU' and d.mod_docventa='CON' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por select @monto = @monto + isnull(sum(d.mper_docventa), 0) from pago d

where convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_pago))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_pago))))+ ' 00:00:00')=@fec and (d.cod_tipodocpad=@fac or d.cod_tipodocpad=@bol) and d.cod_tipodocpad = @ncr and d.est_pago<>'ANU' and d.cod_zonal like ltrim(rtrim(@zonal)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' and ltrim(rtrim(isnull(porper_docventa, '0'))) =@por if @conta = 1 c00_fec=@fec and c01_zon=@zonal if @conta = 2 c00_fec=@fec and c01_zon=@zonal if @conta = 3 c00_fec=@fec and c01_zon=@zonal if @conta = 4 c00_fec=@fec and c01_zon=@zonal if @conta = 5 c00_fec=@fec and c01_zon=@zonal if @conta = 6 c00_fec=@fec and c01_zon=@zonal if @conta = 7 c00_fec=@fec and c01_zon=@zonal update #t1 set p01_por=@monto where update #t1 set p02_por=@monto where update #t1 set p03_por=@monto where update #t1 set p04_por=@monto where update #t1 set p05_por=@monto where update #t1 set p06_por=@monto where update #t1 set p07_por=@monto where

FETCH NEXT FROM ctf_cursor INTO @fec, @zonal END CLOSE ctf_cursor DEALLOCATE ctf_cursor FETCH NEXT FROM ct_cursor INTO @por END CLOSE ct_cursor DEALLOCATE ct_cursor end select * from #t1 order by c00_fec drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO --exec sp_res_reg_ventas_per 'PLANTA', 'COBRANZA', '01-01-2005 00:00:00', '01-01-2005 23:59:59', --'TODOS', '', '' CREATE procedure sp_res_reg_ventas_per_zonal (@zon char(10), @tipo char(15), @inicio datetime, @final datetime, @opc char(15), @ser char(3), @sec char(10)) as begin declare @bol char(3), @fac char(3), @gui char(3), @ncr char(3), @per char(3) select @fac = cod_tipodoc from tipodoc where des_tipodoc='FACTURA' select @bol = cod_tipodoc from tipodoc where des_tipodoc='BOLETA' select @gui = cod_tipodoc from tipodoc where des_tipodoc='GUIA DE REMISION REMITENTE' select @ncr = cod_tipodoc from tipodoc where des_tipodoc='NOTA DE CREDITO' select @per = cod_tipodoc from tipodoc where des_tipodoc='COMP. DE PERCEPCION VENTA INT.' create table #t1 ( c00_fec datetime null, --fecha c01_zon char(10) null, -c02_por char(10) null, -c03_per float default 0 null, -c04_ven float default 0 null, -)

zonal porcentaje percepcion venta

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%'

and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='ANULADOS') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes

and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='COBRANZA' CREDITO') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), '+ ltrim(rtrim(convert(char(2), ltrim(rtrim(convert(char(4), 00:00:00'), and ltrim(rtrim(@opc))='NOTA DE

datepart(month, d.fsa_docventa))))+'datepart(day, d.fsa_docventa))))+'-'+ datepart(year, d.fsa_docventa))))+ '

d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select convert(datetime,

ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') else insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='COBRANZA' and ltrim(rtrim(@opc))='PERCEPCION')

insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fsa_docventa between @inicio and @final and d.cod_tipodocper = @per and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fsa_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fsa_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fsa_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='TODOS') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='ANULADOS')

insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.est_docventa = 'ANU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='FACTURA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.cod_tipodoc = @fac and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='BOLETA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.cod_tipodoc = @bol and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='NOTA DE CREDITO') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.cod_tipodoc = @ncr and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+

ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='SIN GUIA') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='SGU' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serie like ltrim(rtrim(@ser)) + '%' and d.sec_serie like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='GUIA') if ltrim(rtrim(@ser))='' and ltrim(rtrim(@sec))='' insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' group by convert(datetime,

ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') else insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, idocventa i, clientes c where d.cod_tipodoc=i.cod_tipodoc and d.cod_serie=i.cod_serie and d.sec_serie=i.sec_serie and d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.mot_docventa='GUI' and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol) and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and i.cod_tipodocpad=@gui and i.cod_seriepad like ltrim(rtrim(@ser)) + '%' and i.sec_seriepad like ltrim(rtrim(@sec))+'%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0')

if (ltrim(rtrim(@tipo))='EMISION' and ltrim(rtrim(@opc))='PERCEPCION') insert into #t1 select convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0'), isnull(sum(d.mper_docventa), 0), isnull(sum(d.tot_docventa), 0) from docventa d, clientes c where d.cod_clientes=c.cod_clientes and d.fin_docventa between @inicio and @final and d.cod_tipodocper = @per and (d.cod_tipodoc=@fac or d.cod_tipodoc=@bol)

and d.cod_zonal like ltrim(rtrim(@zon)) +'%' and d.cod_serieper like ltrim(rtrim(@ser)) + '%' and d.sec_serieper like ltrim(rtrim(@sec)) + '%' group by convert(datetime, ltrim(rtrim(convert(char(2), datepart(month, d.fin_docventa))))+''+ ltrim(rtrim(convert(char(2), datepart(day, d.fin_docventa))))+'-'+ ltrim(rtrim(convert(char(4), datepart(year, d.fin_docventa))))+ ' 00:00:00'), d.cod_zonal, isnull(d.porper_docventa, '0') select * from #t1 order by c00_fec, c01_zon, c02_por drop table #t1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO /****** Encrypted object is not transferable, and script can not be generated. ******/ GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO create procedure su_2004 as begin SELECT tipo = CASE cod_tipodoc WHEN '001' THEN 'FAC' WHEN '002' THEN 'BOL' WHEN '008' THEN 'NCR' END, sunat = CASE cod_tipodoc WHEN '001' THEN '01' WHEN '002' THEN '02' WHEN '008' THEN '08' END, cod_serie, mes = CASE month(fin_docventa) WHEN '1' THEN '01' WHEN '2' THEN '02' WHEN '3' THEN '03' WHEN '4' THEN '04' WHEN '5' THEN '05' WHEN '6' THEN '06' WHEN '7' THEN '07' WHEN '8' THEN '08' WHEN '9' THEN '09' WHEN '10' THEN '10' WHEN '11' THEN '11' WHEN '12' THEN '12' END, venta_tot = CASE cod_tipodoc WHEN '001' THEN tot_docventa WHEN '002' THEN tot_docventa WHEN '008' THEN tot_docventa*(-1) END, venta_gra = CASE cod_tipodoc WHEN '001' THEN tot_docventa

WHEN '002' THEN tot_docventa WHEN '008' THEN tot_docventa*(-1) END from docventa where fin_docventa between '01-01-2004 00:00:00' and '12-01-2004 23:59:59' and cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('FACTURA', 'BOLETA', 'NOTA DE CREDITO')) end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --select top 10 * from docventa --exec su_ventas_2004 CREATE procedure su_daot_2004 as begin create table #1 ( tipo_doc char(3), id_tipo char(2), serie char(3), mes char(2), venta_total float, venta_grabada float ) create table #2 ( tipo_doc char(3), id_tipo char(2), serie char(3), mes char(2), venta_total float, venta_grabada float ) insert into #2 SELECT tipo = CASE cod_tipodoc WHEN '001' THEN 'FAC' WHEN '002' THEN 'BOL' WHEN '008' THEN 'NCR' END, sunat = CASE cod_tipodoc

WHEN '001' THEN '01' WHEN '002' THEN '03' WHEN '008' THEN '07' END, cod_serie, mes = CASE month(fin_docventa) WHEN '1' THEN '01' WHEN '2' THEN '02' WHEN '3' THEN '03' WHEN '4' THEN '04' WHEN '5' THEN '05' WHEN '6' THEN '06' WHEN '7' THEN '07' WHEN '8' THEN '08' WHEN '9' THEN '09' WHEN '10' THEN '10' WHEN '11' THEN '11' WHEN '12' THEN '12' END, venta_tot = CASE cod_tipodoc WHEN '001' THEN tot_docventa WHEN '002' THEN tot_docventa WHEN '008' THEN tot_docventa*(-1) END, venta_gra = CASE cod_tipodoc WHEN '001' THEN bru_docventa WHEN '002' THEN bru_docventa WHEN '008' THEN bru_docventa*(-1) END from docventa where fin_docventa between '01-01-2004 00:00:00' and '12-31-2004 23:59:59' and cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('FACTURA', 'BOLETA', 'NOTA DE CREDITO')) insert into #1 select tipo_doc, id_tipo, serie, mes, sum(venta_total), sum(venta_grabada) from #2 group by tipo_doc, id_tipo, serie, mes --select sum(venta_grabada) from #1 where mes='12' select tipo_doc, id_tipo, serie, mes, str(venta_total, 10,2) as venta_total, str(venta_grabada, 10,2) as venta_grabada from #1 order by mes, tipo_doc, serie drop table #1 drop table #2 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --select top 10 * from docventa --su_daot_bol_2004 CREATE procedure su_daot_bol_2004 as begin create table #1 ( dni char(8), nombre char(50), base_imponible decimal(10,2) ) insert into #1 select substring(ruc_dni_clientes,1,8), rso_clientes, sum(bru_docventa) from clientes c, docventa d where d.fin_docventa between '01-01-2005 00:00:00' and '12-31-2005 23:59:59' and c.cod_clientes=d.cod_clientes and d.cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('BOLETA')) group by substring(ruc_dni_clientes,1,8), rso_clientes --having sum(bru_docventa) > 6600 select dni, nombre, base_imponible from #1 order by dni, nombre drop table #1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF

GO SET ANSI_NULLS ON GO --su_daot_fac_2004 CREATE procedure su_daot_fac_2004 as begin declare @ruc char(11), @razon_social char(50), @base_imponible decimal(10, 2) create table #1 ( ruc char(11), razon_social char(50), base_imponible decimal(10, 2) ) insert into #1 select substring(ruc_dni_clientes,1,11), rso_clientes, sum(bru_docventa) from clientes c, docventa d where d.fin_docventa between '01-01-2005 00:00:00' and '12-31-2005 23:59:59' and c.cod_clientes=d.cod_clientes and d.cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('FACTURA')) and d.est_docventa in ('PEN', 'CER') group by substring(ruc_dni_clientes,1,11), rso_clientes DECLARE authors_cursor CURSOR FOR select substring(ruc_dni_clientes,1,11), rso_clientes, sum(bru_docventa) from clientes c, docventa d where d.fin_docventa between '01-01-2005 00:00:00' and '12-31-2005 23:59:59' and c.cod_clientes=d.cod_clientes and d.cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('NOTA DE CREDITO')) and d.est_docventa in ('PEN', 'CER') group by substring(ruc_dni_clientes,1,11), rso_clientes OPEN authors_cursor FETCH NEXT FROM authors_cursor INTO @ruc, @razon_social, @base_imponible WHILE @@FETCH_STATUS = 0 BEGIN update #1 set base_imponible = base_imponible - @base_imponible where ruc=@ruc FETCH NEXT FROM authors_cursor INTO @ruc, @razon_social, @base_imponible END CLOSE authors_cursor DEALLOCATE authors_cursor select ruc, razon_social, base_imponible

from #1 order by ruc, razon_social drop table #1 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO --select top 10 * from docventa --exec su_ventas_2004 CREATE procedure su_ventas_2004 as begin create table #1 ( tipo_doc char(3), id_tipo char(2), serie char(3), mes char(2), venta_total decimal(10,2), venta_grabada decimal(10,2) ) create table #2 ( tipo_doc char(3), id_tipo char(2), serie char(3), mes char(2), venta_total decimal(10,2), venta_grabada decimal(10,2) ) insert into #2 SELECT tipo = CASE cod_tipodoc WHEN '001' THEN 'FAC' WHEN '002' THEN 'BOL' WHEN '008' THEN 'NCR' END, sunat = CASE cod_tipodoc WHEN '001' THEN '01'

WHEN '002' THEN '03' WHEN '008' THEN '07' END, cod_serie, mes = CASE month(fin_docventa) WHEN '1' THEN '01' WHEN '2' THEN '02' WHEN '3' THEN '03' WHEN '4' THEN '04' WHEN '5' THEN '05' WHEN '6' THEN '06' WHEN '7' THEN '07' WHEN '8' THEN '08' WHEN '9' THEN '09' WHEN '10' THEN '10' WHEN '11' THEN '11' WHEN '12' THEN '12' END, venta_tot = CASE cod_tipodoc WHEN '001' THEN tot_docventa WHEN '002' THEN tot_docventa WHEN '008' THEN tot_docventa*(-1) END, venta_gra = CASE cod_tipodoc WHEN '001' THEN bru_docventa WHEN '002' THEN bru_docventa WHEN '008' THEN bru_docventa*(-1) END from docventa where fin_docventa between '01-01-2004 00:00:00' and '12-31-2004 23:59:59' and cod_tipodoc in (select cod_tipodoc from tipodoc where des_tipodoc in ('FACTURA', 'BOLETA', 'NOTA DE CREDITO')) insert into #1 select tipo_doc, id_tipo, serie, mes, sum(venta_total), sum(venta_grabada) from #2 group by tipo_doc, id_tipo, serie, mes --select sum(venta_grabada) from #1 where mes='12' select tipo_doc, id_tipo, serie, mes, venta_total, venta_grabada from #1 order by mes, tipo_doc, serie drop table #1 drop table #2 end

GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO