Advertisement
LovelessIsma

pa_infoPDT_exportar

Jan 27th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 64.96 KB | None | 0 0
  1. USE [SNSMART_ANALISIS]
  2. GO
  3. /****** Object:  StoredProcedure [SN].[pa_infoPDT_exportar]    Script Date: 27/01/2017 9:45:53 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER procedure [SN].[pa_infoPDT_exportar]
  10. (
  11.     @tipo varchar(10),
  12.     @anho int,
  13.     @mes int,
  14.     @tiporegistro int
  15. )
  16. as
  17. begin
  18.     declare @strContenidoPDT varchar(max),
  19.             @tipoesc varchar(1),
  20.             @nroescritura varchar(5),
  21.             @fechaNum varchar(10),
  22.             @fechaAut varchar(10),
  23.             @fechaLeg varchar(10),
  24.             @tipoActo varchar(2),
  25.             @secuenciaActo varchar(5),
  26.             @codMoneda varchar(1),
  27.             @valCuotaMC varchar(max),
  28.             @plazoInicial varchar(10),
  29.             @plazoFinal varchar(10),
  30.             @nomContrato varchar(30),
  31.             @fechaSucrip varchar(10),
  32.             @exhibeMedPago varchar(1),
  33.             @MedioPago varchar(3),
  34.             @codMonedaMedPago varchar(1),
  35.             @montoPagado varchar(max),
  36.             @fechaPago varchar(10),
  37.             @nroDocMedPago varchar(25),
  38.             @entFinanciera varchar(2)
  39.    
  40.     declare @SecuenciaBien varchar(max),
  41.             @TipoBien varchar(20),
  42.             @CodBien varchar(20),
  43.             @OpcionBien varchar(20),
  44.             @NroBien varchar(20),
  45.             @SerieBien varchar(20),
  46.             @OrigenBien varchar(100),
  47.             @UbigeoBien varchar(100),
  48.             @FechaBien varchar(100),
  49.             @DescripBien varchar(100)
  50.    
  51.     declare @SecuenciaOtorgante varchar(max),
  52.             @TipoDoc varchar(2),
  53.             @NroDoc varchar(20),
  54.             @TipoOtorgante varchar(2),
  55.             @TipoPer varchar(1),
  56.             @ubigeoPer varchar(100),
  57.             @Porcentaje varchar(max),
  58.             @RazonSocial varchar(40),
  59.             @ApellidoPat varchar(15),
  60.             @ApellidoMat varchar(15),
  61.             @Nombre1 varchar(15),
  62.             @Nombre2 varchar(15),
  63.             @Gen3Cat varchar(1),
  64.             @EraSuCasa varchar(1),
  65.             @nroOperacion varchar(10),
  66.             @porcj2Cat varchar(20),
  67.             @nomArchivo varchar(50)
  68.     declare @CuentaOTG int,
  69.                     @i int,
  70.                     @aux varchar(5)
  71.    
  72.     declare @idRegDesdeTipoReg int
  73.    
  74.     set @CuentaOTG  = 0
  75.     set @i = 0
  76.     set @aux = ''
  77.    
  78.     set @tipoesc = ''
  79.     set @nroescritura = ''
  80.     set @fechaNum = ''
  81.     set @fechaAut = ''
  82.     set @fechaLeg = ''
  83.     set @tipoActo = ''
  84.     set @secuenciaActo = ''
  85.     set @codMoneda = ''
  86.     set @valCuotaMC = ''
  87.     set @plazoInicial = ''
  88.     set @plazoFinal = ''
  89.     set @nomContrato = ''
  90.     set @fechaSucrip = ''
  91.     set @exhibeMedPago = ''
  92.     set @MedioPago = ''
  93.     set @codMonedaMedPago = ''
  94.     set @montoPagado = ''
  95.     set @fechaPago = ''
  96.     set @nroDocMedPago = ''
  97.     set @entFinanciera = ''
  98.     set @strContenidoPDT = ''
  99.  
  100.     set @SecuenciaBien = ''
  101.     set @TipoBien = ''
  102.     set @CodBien = ''
  103.     set @OpcionBien = ''
  104.     set @NroBien = ''
  105.     set @SerieBien = ''
  106.     set @OrigenBien = ''
  107.     set @UbigeoBien = ''
  108.     set @FechaBien = ''
  109.     set @DescripBien = ''
  110.    
  111.     set @TipoDoc = ''
  112.     set @NroDoc = ''
  113.     set @TipoOtorgante = ''
  114.     set @TipoPer = ''
  115.     set @ubigeoPer = ''
  116.     set @Porcentaje = ''
  117.     set @RazonSocial = ''
  118.     set @ApellidoPat = ''
  119.     set @ApellidoMat = ''
  120.     set @Nombre1 = ''
  121.     set @Nombre2 = ''
  122.     set @Gen3Cat = ''
  123.     set @EraSuCasa = ''
  124.     set @nroOperacion = ''
  125.     set @porcj2Cat = ''
  126.     set @idRegDesdeTipoReg = case @tiporegistro when 1 then 6 when 2 then 9 else 11 end
  127.    
  128.     set @nomArchivo = '3520' + CONVERT(varchar(4), @anho) + '10181338631'
  129.    
  130.     declare @xPDT table (
  131.         NomArchivo varchar(50),
  132.         strContenido varchar(max)
  133.     )
  134.    
  135.     if @tipo = '00'
  136.         begin
  137.             set @nomArchivo = @nomArchivo + '.ACT'
  138.            
  139.             declare curInfoPDT cursor local for
  140.     select distinct * from (
  141.     select 2 as tipoesc,
  142.      convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))  Tm_NumeroEscritura,
  143.                     SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) AS fechaGen,
  144.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) AS fechaFirma,
  145.                     '' as fechaLeg,
  146.                     '04' as actojur,
  147.                     '1' SecuencialActo,
  148.                     isnull(e.tm_codigoPDT, '2') as PDTMoneda,
  149.                     convert(varchar(max), d.Td_ValorExpVeh) as MercedConductiva,
  150.                     '' as PlazoInicial,
  151.                     '' as PlazoFinal,
  152.                     '' as NomContrato,
  153.                     SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) as fechaMinuta,
  154.                     case isnull(d.Td_AcreditaMP, 0) when 1 then '1' else '0' end AcreditaMP,
  155.                     case isnull(d.Td_AcreditaMP, 0) when 1 then SYSMART.ObtenerNaturalezaTaTabla(isnull(d.Ta_MedioPago, '00'), 'Ta_MedioPago') else '' end as MedioPago,
  156.                     case isnull(d.Td_AcreditaMP, 0) when 1 then isnull(e.tm_codigoPDT, '2') else '' end as PDTMonedaMP,
  157.                     case isnull(d.Td_AcreditaMP, 0) when 1 then convert(varchar(max), isnull(d.Td_ValorExpVeh, 0)) else '' end as MontoPagado,
  158.                     case isnull(d.Td_AcreditaMP, 0) when 1 then SN.fc_formatearFecha('02',a.Tm_FechaGenEsc) else '' end as fechaPago,
  159.                     '-' as nroDocPago,
  160.                     '99' as EntFinanciera
  161.                     from SN.Tm_Escritura  as a
  162.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  163.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  164.                         inner join SN.Td_Expediente_Vehiculo as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo and b.Tm_idexpediente = d.Tm_IdExpediente
  165.                         inner join SYSMART.tm_moneda as e on d.Tm_IdMoneda = e.tm_idmoneda
  166.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 9
  167.                         and not c.Ta_NatActo is null
  168.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  169. 40,
  170. 39,
  171. 38,
  172. 37,
  173. 36,
  174. 35) AND a.Tm_Excluida = 0
  175.     UNION ALL
  176.    
  177.    
  178.         select 1 as tipoesc,
  179.                         convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))Tm_NumeroEscritura,
  180.                     SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) AS fechaGen,
  181.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) AS fechaFirma,
  182.                     '' as fechaLeg,
  183.                     c.tp_codigopdt as actojur,
  184.                     '1' SecuencialActo,
  185.                     isnull(e.tm_codigoPDT, '2') as PDTMoneda,
  186.                     convert(varchar(max), d.Td_ValorExpTrans) as MercedConductiva,
  187.                     '' as PlazoInicial,
  188.                     '' as PlazoFinal,
  189.                     '' as NomContrato,
  190.                     SN.fc_formatearFecha('02', case c.Tp_FlagMinuta when 1 then isnull(f.Tm_FechaGenMin, a.Tm_FechaGenEsc) else a.Tm_FechaGenEsc end) as fechaMinuta,
  191.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then case isnull(d.Td_AcreditaMP, 0) when 1 then '1' else '0' end else '' end AcreditaMP,
  192.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then case isnull(d.Td_AcreditaMP, 0) when 1 then SYSMART.ObtenerNaturalezaTaTabla(isnull(d.Ta_MedioPago, '00'), 'Ta_MedioPago') else ''  end else '' end as MedioPago,
  193.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then case isnull(d.Td_AcreditaMP, 0) when 1 then isnull(e.tm_codigoPDT, '2') else '' end else '' end as PDTMonedaMP,
  194.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then case isnull(d.Td_AcreditaMP, 0) when 1 then convert(varchar(max), d.Td_ValorExpTrans) else '' end else '' end as MontoPagado,
  195.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then case isnull(d.Td_AcreditaMP, 0) when 1 then SN.fc_formatearFecha('02', case c.Tp_FlagMinuta when 1 then f.Tm_FechaGenMin else a.Tm_FechaGenEsc end) else '' end else '' end as fechaPago,
  196.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then '-' else '' end as nroDocPago,
  197.                     case when c.tp_codigopdt in ('04', '10', '24', '26') then '99' else '' end as EntFinanciera
  198.                     from SN.Tm_Escritura  as a
  199.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  200.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  201.                         inner join SN.Tm_Minuta as f on a.Tm_IdExpediente = f.Tm_IdExpediente
  202.                         inner join SN.Td_Escritura_Minuta as m on a.Tm_IdEscritura = m.Tm_IdEscritura and f.Tm_IdMinuta = m.Tm_IdMinuta
  203.                         inner join SN.Td_Expediente_Transferencia as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  204.                         inner join SYSMART.tm_moneda as e on d.Tm_IdMoneda = e.tm_idmoneda
  205.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 6
  206.                         and (case isnull(d.Td_AcreditaMP, 0) when 1 then case d.Td_ValorExpTrans when 0 then 1 else d.Td_ValorExpTrans end else 1 end) > 0
  207.                         and not c.Ta_NatActo is null
  208.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  209. 40,
  210. 39,
  211. 38,
  212. 37,
  213. 36,
  214. 35) AND a.Tm_Excluida = 0
  215.                         UNION ALL
  216.                        
  217.                             select case a.Tm_idRegistro when 6 then 1 else 5 end as
  218.                             tipoesc,
  219.                         convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) Tm_NumeroEscritura,
  220.                     case a.Tm_idRegistro when 6 then SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) else '' end AS fechaGen,
  221.                     case a.Tm_idRegistro when 11 then '' else SN.fc_formatearFecha('02', a.tm_fechafirmaesc) end AS fechaFirma,
  222.                     case a.Tm_idRegistro when 11 then SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) else '' end as fechaLeg,
  223.                     c.tp_codigopdt as actojur,
  224.                     case a.tm_idregistro when 6 then '1' else
  225.                     case c.tp_codigopdt when '09' then '1' else '2' end end as SecuencialActo,
  226.                     isnull(e.tm_codigoPDT, '2') as PDTMoneda,
  227.                     convert(varchar(max), case d.Td_MontoGravamen when 0 then 1 else d.Td_MontoGravamen end) as MercedConductiva,
  228.                     '' as PlazoInicial,
  229.                     '' as PlazoFinal,
  230.                     '' as NomContrato,
  231.                     case when c.tp_codigopdt in ('09', '12') then '' else SN.fc_formatearFecha('02', case c.Tp_FlagMinuta when 1 then isnull(f.Tm_FechaGenMin, a.Tm_FechaGenEsc) else a.Tm_FechaGenEsc end) end as fechaMinuta,
  232.                     '' AcreditaMP,
  233.                     '' as MedioPago,
  234.                     case when c.tp_codigopdt in ('04', '10') then isnull(e.tm_codigoPDT, '2') else '' end as PDTMonedaMP,
  235.                     '' MontoPagado,
  236.                     '' as fechaPago,
  237.                     '' as nroDocPago,
  238.                     '' as EntFinanciera
  239.                     from SN.Tm_Escritura  as a
  240.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  241.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  242.                         inner join SN.Tm_Minuta as f on a.Tm_IdExpediente = f.Tm_IdExpediente
  243.                         inner join SN.Td_Escritura_Minuta as m on a.Tm_IdEscritura = m.Tm_IdEscritura and f.Tm_IdMinuta = m.Tm_IdMinuta
  244.                         inner join SN.Td_Expediente_Gravamen as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo and  a.Tm_IdExpediente = d.Tm_idexpediente
  245.                         inner join SYSMART.tm_moneda as e on d.Tm_IdMoneda = e.tm_idmoneda
  246.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro in (6,11)
  247.                         and not c.Ta_NatActo is null
  248.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  249. 40,
  250. 39,
  251. 38,
  252. 37,
  253. 36,
  254. 35) AND a.Tm_Excluida = 0
  255.                         UNION ALL
  256.                        
  257.                            
  258.                             select case a.Tm_idRegistro when 6 then 1 when 9 then 2 else 5 end as tipoesc,
  259.                      convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))  Tm_NumeroEscritura,
  260.                     case a.Tm_idRegistro when 6 then SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) else '' end AS fechaGen,
  261.                     case a.Tm_idRegistro when 11 then '' else SN.fc_formatearFecha('02', a.tm_fechafirmaesc) end AS fechaFirma,
  262.                     case a.Tm_idRegistro when 11 then SN.fc_formatearFecha('02', a.Tm_FechaGenEsc) else '' end as fechaLeg,
  263.                     c.tp_codigopdt as actojur,
  264.                     '1' SecuencialActo,
  265.                     isnull(h.tm_codigoPDT, '2') as PDTMoneda,
  266.                     convert(varchar(max), case d.Td_CapitalSocial when  0 then 1 else  d.Td_CapitalSocial end) as MercedConductiva,
  267.                     '' as PlazoInicial,
  268.                     '' as PlazoFinal,
  269.                     '' as NomContrato,
  270.                     case when c.tp_codigopdt in ('09', '12') then '' else SN.fc_formatearFecha('02', case c.Tp_FlagMinuta when 1 then isnull(f.Tm_FechaGenMin, a.Tm_FechaGenEsc) else a.Tm_FechaGenEsc end) end as fechaMinuta,
  271.                     '' AcreditaMP,
  272.                     '' as MedioPago,
  273.                     '' as PDTMonedaMP,
  274.                     '' MontoPagado,
  275.                     '' as fechaPago,
  276.                     '' as nroDocPago,
  277.                     '' as EntFinanciera
  278.                     from SN.Tm_Escritura  as a
  279.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  280.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  281.                         inner join SN.Tm_Minuta as f on a.Tm_IdExpediente = f.Tm_IdExpediente
  282.                         inner join SN.Td_Escritura_Minuta as m on a.Tm_IdEscritura = m.Tm_IdEscritura and f.Tm_IdMinuta = m.Tm_IdMinuta
  283.                         inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  284.                         inner join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  285.                         inner join SYSMART.tm_moneda as h on d.Tm_IdMoneda = h.tm_idmoneda
  286.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro in (6,11)
  287.                         and not c.Ta_NatActo is null
  288.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  289. 40,
  290. 39,
  291. 38,
  292. 37,
  293. 36,
  294. 35)
  295.                     AND a.Tm_Excluida = 0
  296.                         ) x where tipoesc = @tiporegistro
  297.  order by  tipoesc, Tm_NumeroEscritura
  298.                        
  299.            
  300.                        
  301.             open curInfoPDT
  302.             fetch next from curInfoPDT into @tipoesc,
  303.                     @nroescritura,
  304.                     @fechaNum,
  305.                     @fechaAut,
  306.                     @fechaLeg,
  307.                     @tipoActo,
  308.                     @secuenciaActo,
  309.                     @codMoneda,
  310.                     @valCuotaMC,
  311.                     @plazoInicial,
  312.                     @plazoFinal,
  313.                     @nomContrato,
  314.                     @fechaSucrip,
  315.                     @exhibeMedPago,
  316.                     @MedioPago,
  317.                     @codMonedaMedPago,
  318.                     @montoPagado,
  319.                     @fechaPago,
  320.                     @nroDocMedPago,
  321.                     @entFinanciera
  322.             while @@FETCH_STATUS = 0
  323.                 begin
  324.                     /*if LEN(@strContenidoPDT) > 0
  325.                         begin
  326.                             set @strContenidoPDT = @strContenidoPDT + CHAR(13)
  327.                         end*/
  328.                        
  329.                     set @strContenidoPDT =
  330.                         @tipoesc + '|' + convert(varchar(5),@nroescritura) + '|' + @fechaNum + '|' + @fechaAut + '|' + @fechaLeg + '|' + @tipoActo +  
  331.                         '|' + @secuenciaActo + '|' + @codMoneda + '|' + @valCuotaMC + '|' + @plazoInicial + '|' + @plazoFinal + '|' +
  332.                         @nomContrato + '|' + @fechaSucrip + '|' + @exhibeMedPago + '|'
  333.                          --@exhibeMedPago + '|' + @MedioPago + '|' + @codMonedaMedPago + '|' + @montoPagado + '|' +
  334.                         --@fechaPago + '|' + @nroDocMedPago + '|' + @entFinanciera + '|'
  335.                    
  336.                     insert into @xPDT (NomArchivo, strContenido)
  337.                     values (@nomArchivo, @strContenidoPDT)
  338.                    
  339.                     fetch next from curInfoPDT into @tipoesc,
  340.                     @nroescritura,
  341.                     @fechaNum,
  342.                     @fechaAut,
  343.                     @fechaLeg,
  344.                     @tipoActo,
  345.                     @secuenciaActo,
  346.                     @codMoneda,
  347.                     @valCuotaMC,
  348.                     @plazoInicial,
  349.                     @plazoFinal,
  350.                     @nomContrato,
  351.                     @fechaSucrip,
  352.                     @exhibeMedPago,
  353.                     @MedioPago,
  354.                     @codMonedaMedPago,
  355.                     @montoPagado,
  356.                     @fechaPago,
  357.                     @nroDocMedPago,
  358.                     @entFinanciera
  359.                 end
  360.             close curInfoPDT
  361.             deallocate curInfoPDT
  362.            
  363.            
  364.         end
  365.     if @tipo = '01'
  366.         begin
  367.             set @aux = ''
  368.             set @nomArchivo = @nomArchivo + '.BIE'
  369.            
  370.             declare curInfoPdtBienes cursor local for
  371.                     select * from (
  372. select  2 as tipoesc,
  373.         convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))  as nroescritura,
  374.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  375.                     '1' as SecuencialActo,
  376.                     e.Tp_IdVehiculo as NroSecuencial,
  377.                     'B' as TipoBien,
  378.                     '09' as CodBien,
  379.                      '1' as OpcionBien,
  380.                     e.Tp_PlacavVehiculo as NroBien,
  381.                     '' NroSerie,
  382.                     '' OrigenBien,
  383.                     '' UbigeoBien,
  384.                     '' FechaBien,
  385.                     '' DescripBien
  386.                 from SN.Tm_Escritura  as a
  387.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  388.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  389.                     inner join SN.Td_Expediente_Vehiculo d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  390.                     inner join SN.Tp_Vehiculos as e on d.Tp_IdVehiculo = e.Tp_IdVehiculo
  391.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 9
  392.                     and not c.Ta_NatActo is null
  393.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  394. 40,
  395. 39,
  396. 38,
  397. 37,
  398. 36,
  399. 35) AND a.Tm_Excluida = 0
  400. UNION ALL                  
  401.                         select 1 as tipoesc,
  402.                         convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  403.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  404.                     '1' as SecuencialActo,
  405.                     d.Tp_IdBien as NroSecuencial,
  406.                     'B' as TipoBien,
  407.                     SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien')   as CodBien,
  408.                      case when SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien')  in ('01', '07', '09') then '1' else '' end as OpcionBien,
  409.                     '' as NroBien,
  410.                     '' NroSerie,
  411.                     isnull(f.Origen, '1') OrigenBien,
  412.                     isnull(f.Tm_CodigoPDT, '140101') as UbigeoBien,
  413.                     '' FechaBien,
  414.                     case SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien') when '99' then case len(d.Td_AnotaExpTrans) when 0 then case len(e.Tp_Anotacionespredios) when 0 then '-' else e.Tp_Anotacionespredios  end else case len(d.Td_AnotaExpTrans) when 0 then '-' else d.Td_AnotaExpTrans end end else '' end DescripBien
  415.                 from SN.Tm_Escritura  as a
  416.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  417.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  418.                     inner join SN.Td_Expediente_Transferencia d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  419.                     inner join SN.Tp_Predios as e on d.Tp_IdBien = e.Tp_IdPredio
  420.                     left join SYSMART.dbo.fc_ubigeo(0) as f on e.Tm_idUbigeo = f.tm_idubigeo
  421.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 6
  422.                      AND c.Ta_NatActo <> '01'
  423.                     and not c.Ta_NatActo is null
  424.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  425. 40,
  426. 39,
  427. 38,
  428. 37,
  429. 36,
  430. 35) AND a.Tm_Excluida = 0
  431.             UNION ALL      
  432.                    
  433.                     select 1 as tipoesc,
  434.                      convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  435.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  436.                     '1' as SecuencialActo,
  437.                     f.Tp_IdBien as NroSecuencial,
  438.                     'B' as TipoBien,
  439.                     SYSMART.dbo.ObtenerNaturalezaTaTabla(f.Ta_tipobien, 'Ta_tipobien')  as CodBien,
  440.                      '' as OpcionBien,
  441.                     '' as NroBien,
  442.                     '' NroSerie,
  443.                     isnull(i.Origen, '1') OrigenBien,
  444.                     isnull(i.Tm_CodigoPDT, '140101') as UbigeoBien,
  445.                     '' FechaBien,
  446.                     case LEN(g.Tp_Anotacionespredios) when 0 then '-' else g.Tp_Anotacionespredios end DescripBien
  447.                 from SN.Tm_Escritura  as a
  448.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  449.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  450.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  451.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  452.                     left join SN.Td_Expediente_DetAportes as f on e.Td_IdExpSocAportes = f.Td_IdExpSocAportes
  453.                     left join SN.Tp_Predios as g on f.Tp_IdBien = g.Tp_IdPredio
  454.                     left join SYSMART.dbo.fc_ubigeo(0) as i on g.Tm_idUbigeo = i.tm_idubigeo
  455.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 6
  456.                      and c.Ta_NatActo = '01' and f.Ta_TipoBien = '01'
  457.                      and not c.Ta_NatActo is null
  458.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  459. 40,
  460. 39,
  461. 38,
  462. 37,
  463. 36,
  464. 35) AND a.Tm_Excluida = 0
  465.                     union all
  466.                    
  467.                         select 1 as tipoesc,
  468.                          convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  469.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  470.                     '1' as SecuencialActo,
  471.                     f.Tp_IdBien as NroSecuencial,
  472.                     'B' as TipoBien,
  473.                     SYSMART.dbo.ObtenerNaturalezaTaTabla(f.Ta_tipobien, 'Ta_tipobien')  as CodBien,
  474.                     case when SYSMART.dbo.ObtenerNaturalezaTaTabla(f.Ta_tipobien, 'Ta_tipobien')  in ('01', '07', '09') then '1' else '' end  as OpcionBien,
  475.                     h.Tp_PlacavVehiculo as NroBien,
  476.                     '' NroSerie,
  477.                     '' OrigenBien,
  478.                     '' as UbigeoBien,
  479.                     '' FechaBien,
  480.                     '' DescripBien
  481.                 from SN.Tm_Escritura  as a
  482.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  483.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  484.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  485.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  486.                     left join SN.Td_Expediente_DetAportes as f on e.Td_IdExpSocAportes = f.Td_IdExpSocAportes
  487.                     left join SN.Tp_Vehiculos as h on f.Tp_IdBien = h.Tp_IdVehiculo
  488.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 6
  489.                      and c.Ta_NatActo = '01' and f.Ta_TipoBien = '00'
  490.                      and not c.Ta_NatActo is null
  491.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  492. 40,
  493. 39,
  494. 38,
  495. 37,
  496. 36,
  497. 35) AND a.Tm_Excluida = 0
  498.                 union all
  499.                     select 1 as tipoesc,
  500.              convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  501.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  502.                     '1' as SecuencialActo,
  503.                     f.Td_IdExpDetAportes as NroSecuencial,
  504.                     'B' as TipoBien,
  505.                     '99'  as CodBien,
  506.                      '' as OpcionBien,
  507.                     '' as NroBien,
  508.                     '' NroSerie,
  509.                      '' OrigenBien,
  510.                     '' as UbigeoBien,
  511.                     '' FechaBien,
  512.                     substring(f.Td_DetalleBien, 0, 30) DescripBien
  513.                 from SN.Tm_Escritura  as a
  514.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  515.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  516.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  517.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  518.                     left join SN.Td_Expediente_DetAportes as f on e.Td_IdExpSocAportes = f.Td_IdExpSocAportes
  519.         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro = 6
  520.                      and c.Ta_NatActo = '01' and f.Ta_TipoBien = '02'
  521.                      and not c.Ta_NatActo is null
  522.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  523. 40,
  524. 39,
  525. 38,
  526. 37,
  527. 36,
  528. 35) AND a.Tm_Excluida = 0
  529. UNION ALL
  530.            
  531.                     select  case a.Tm_idRegistro when 6 then 1 else 5 end as tipoesc,
  532.                      convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  533.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  534.                     case a.tm_idregistro when 6 then '1' else
  535.                     case c.tp_codigopdt when '09' then '1' else '2' end end as SecuencialActo,
  536.                     e.Tp_IdVehiculo as NroSecuencial,
  537.                     'B' as TipoBien,
  538.                     SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien') as CodBien,
  539.                     UPPER(case len(e.Tp_PlacavVehiculo) when 0 then CASE LEN(e.Tp_Serievehiculo) when 0 then '3' else '2' end else '1' end) as OpcionBien,
  540.                     UPPER(case len(e.Tp_PlacavVehiculo) when 0 then CASE LEN(e.Tp_Serievehiculo) when 0 then e.Tp_Motorvehiculo else e.Tp_Serievehiculo end else e.Tp_PlacavVehiculo end) as NroBien,
  541.                     '' NroSerie,
  542.                     '1' OrigenBien,
  543.                     '140101' UbigeoBien,
  544.                     '' FechaBien,
  545.                         case SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien') when '99' then '-' else '' end DescripBien
  546.                 from SN.Tm_Escritura  as a
  547.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  548.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  549.                     inner join SN.Td_Expediente_Gravamen as d on b.Td_idexpedienteacto = d.Td_idexpedienteacto  and  a.Tm_IdExpediente = d.Tm_idexpediente
  550.                     inner join SN.Tp_Vehiculos as e on e.Tp_IdVehiculo = d.Tp_idbien and d.Ta_tipobien = '00'
  551.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro in (6,11)
  552.                     and not c.Ta_NatActo is null
  553.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  554. 40,
  555. 39,
  556. 38,
  557. 37,
  558. 36,
  559. 35) AND a.Tm_Excluida = 0
  560.                      union all
  561.  
  562.             select  case a.Tm_idRegistro when 6 then 1 else 5 end as tipoesc,
  563.             convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5))   as nroescritura,
  564.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  565.                     case a.tm_idregistro when 6 then '1' else
  566.                     case c.tp_codigopdt when '09' then '1' else '2' end end as SecuencialActo,
  567.                     f.Tp_IdPredio as NroSecuencial,
  568.                     'B' as TipoBien,
  569.                     SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien') as CodBien,
  570.                     '' as OpcionBien,
  571.                     '' as NroBien,
  572.                     '' NroSerie,
  573.                     isnull(i.Origen, '1') OrigenBien,
  574.                     case isnull(i.Origen, '1') when '1' then isnull(i.Tm_CodigoPDT, '140101') else ISNULL(i.CodigoPDTPais, '589') end as UbigeoBien,
  575.                     '' FechaBien,
  576.                         case SYSMART.dbo.ObtenerNaturalezaTaTabla(d.Ta_tipobien, 'Ta_tipobien') when '99' then case len(isnull(f.Tp_Anotacionespredios, '-')) when 0 then '-' else isnull(f.Tp_Anotacionespredios, '-')  end else '' end DescripBien
  577.                 from SN.Tm_Escritura  as a
  578.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  579.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  580.                     inner join SN.Td_Expediente_Gravamen as d on b.Td_idexpedienteacto = d.Td_idexpedienteacto  and  a.Tm_IdExpediente = d.Tm_idexpediente
  581.                     inner join SN.Tp_Predios as f on d.Tp_idbien = f.Tp_IdPredio and d.Ta_tipobien = '01'
  582.                     left join SYSMART.dbo.fc_ubigeo(0) as i on f.Tm_idUbigeo = i.tm_idubigeo
  583.                     where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes and a.Tm_idRegistro in (6,11)
  584.                     and not c.Ta_NatActo is null
  585.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  586. 40,
  587. 39,
  588. 38,
  589. 37,
  590. 36,
  591. 35) AND a.Tm_Excluida = 0
  592.  
  593.                    
  594.                     ) as x
  595.                  where tipoesc = @tiporegistro
  596.                     order by  tipoesc, nroescritura
  597.                    
  598.                    
  599.                    
  600.             open curInfoPdtBienes
  601.             fetch next from curInfoPdtBienes into @tipoesc,
  602.             @nroescritura,
  603.             @fechaNum,
  604.             @secuenciaActo,
  605.             @SecuenciaBien,
  606.             @TipoBien,
  607.             @CodBien,
  608.             @OpcionBien,
  609.             @NroBien,
  610.             @SerieBien,
  611.             @OrigenBien,
  612.             @UbigeoBien,
  613.             @FechaBien,
  614.             @DescripBien
  615.             while @@FETCH_STATUS = 0
  616.                 begin
  617.                     /*if LEN(@strContenidoPDT) > 0
  618.                         begin
  619.                             set @strContenidoPDT = @strContenidoPDT + CHAR(13)
  620.                         end*/
  621.                     if @aux <> @nroescritura
  622.                         begin
  623.                             set @i = 0
  624.                         end
  625.                        
  626.                     set @i = @i + 1
  627.                    
  628.                     set @strContenidoPDT =
  629.                     @tipoesc + '|' + convert(varchar(5), @nroescritura) + '|'
  630.                     + @fechaNum + '|' + @secuenciaActo + '|'
  631.                     + Convert(varchar(max), @i) + '|' + @TipoBien + '|'
  632.                     + @CodBien + '|' + @OpcionBien + '|'
  633.                     + @NroBien + '|' + @SerieBien + '|' + @OrigenBien + '|'
  634.                     + @UbigeoBien + '|' + @FechaBien + '|' + @DescripBien + '|'
  635.                    
  636.                    
  637.                     insert into @xPDT (NomArchivo, strContenido)
  638.                     values (@nomArchivo, @strContenidoPDT)
  639.                    
  640.                     set @aux = @nroescritura
  641.                    
  642.                     fetch next from curInfoPdtBienes into @tipoesc,
  643.                     @nroescritura,
  644.                     @fechaNum,
  645.                     @secuenciaActo,
  646.                     @SecuenciaBien,
  647.                     @TipoBien,
  648.                     @CodBien,
  649.                     @OpcionBien,
  650.                     @NroBien,
  651.                     @SerieBien,
  652.                     @OrigenBien,
  653.                     @UbigeoBien,
  654.                     @FechaBien,
  655.                     @DescripBien
  656.                 end
  657.             close curInfoPdtBienes
  658.             deallocate curInfoPdtBienes
  659.            
  660.            
  661.         end
  662.     if @tipo = '02'
  663.         begin
  664.             delete from dbo.SecuenciaOtorgante
  665.             DBCC CHECKIDENT ('SecuenciaOtorgante', RESEED, 0)
  666.             declare @cuentaPorcj int,
  667.                     @idexpactox int,
  668.                     @auxIdExpActo int,
  669.                     @auxDefTipoInterv varchar(2),
  670.                     @TaTipoDefInterv varchar(2)
  671.             set @cuentaPorcj = 0
  672.             set @idexpactox = 0
  673.             set @TaTipoDefInterv = ''
  674.             set @aux = ''
  675.             set @auxIdExpActo = 0
  676.             set @auxDefTipoInterv = ''
  677.             set @nomArchivo = @nomArchivo + '.OTG'
  678.            
  679.             declare @xParticipacion table (
  680.                 Ta_DefTipoInterv varchar(2),
  681.                 Td_IdExpedienteActo int,
  682.                 CountPersona int
  683.             )
  684.  
  685.             insert into @xParticipacion
  686.                     select c.Ta_DefTipoInterv, a.Td_IdExpedienteActo, COUNT(a.tp_idpersona) as CountPersona
  687.                     from sn.td_expediente_interv as a
  688.                         inner join sn.tp_tipointerv as c on a.Tp_IdTipointerv = c.Tp_IdTipoInterv
  689.                         inner join SN.Tm_Escritura as d on a.Tm_IdExpediente = d.Tm_IdExpediente
  690.                         inner join SN.Td_Expediente_Acto as e on a.Td_IdExpedienteActo = e.Td_idexpedienteacto
  691.                         inner join SN.Tp_Actos as f on e.Tp_idacto = f.Tp_IdActo
  692.                     where c.Ta_DefTipoInterv in ('00', '01') and c.Tp_IdTipointerv <> 15 and year(d.Tm_FechaGenEsc) = @anho AND month(d.Tm_FechaGenEsc) = @mes
  693.                      and d.Tm_idRegistro = @idRegDesdeTipoReg
  694.                     and not f.Ta_NatActo is null
  695.                     and not f.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  696. 40,
  697. 39,
  698. 38,
  699. 37,
  700. 36,
  701. 35)
  702.                      AND d.Tm_Excluida = 0
  703.                     group by c.Ta_DefTipoInterv, a.Td_IdExpedienteActo
  704.  
  705. declare curInfoPDTOtorgantes cursor local for
  706. select case a.Tm_idRegistro when 6 then 1 when 9 then 2 else 5 end as tipoesc,
  707.  convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) ,
  708.                     SN.fc_formatearFecha('02', a.tm_fechafirmaesc) as fechaNum,
  709.                     '1',
  710.                     '',
  711.                     convert(varchar(max), h.Tp_IdPersona),
  712.                     SYSMART.ObtenerNaturalezaTaTabla(h.Ta_TipoIdentifica, 'Ta_TipoIdentifica') as TipoDoc,
  713.                     case SYSMART.ObtenerNaturalezaTaTabla(h.Ta_TipoIdentifica, 'Ta_TipoIdentifica') when '-' then '' else h.Numero end as NroDoc,
  714.                     case a.Tm_idRegistro
  715.                         when 6 then
  716.                             case c.Tp_CodigoPDT
  717.                                 when '01' then
  718.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  719.                                 when '02' then
  720.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  721.                                 when '03' then
  722.                                     case i.Ta_DefTipoInterv when '01' then '05' when '00' then '06' end
  723.                                 when '04' then
  724.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  725.                                 when '06' then
  726.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  727.                                 when '07' then
  728.                                     case i.Ta_DefTipoInterv when '01' then '20' when '00' then '21' end
  729.                                 when '08' then
  730.                                     case i.Ta_DefTipoInterv when '01' then '13' when '00' then '40' end
  731.                                 when '09' then
  732.                                     case i.Ta_DefTipoInterv when '01' then '14' when '00' then '15' end
  733.                                 when '10' then
  734.                                     case i.Ta_DefTipoInterv when '01' then '07' when '00' then '08' end
  735.                                 when '11' then
  736.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '' end
  737.                                 when '12' then
  738.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '19' end
  739.                                 when '13' then
  740.                                     case i.Ta_DefTipoInterv when '01' then '09' when '00' then '10' end
  741.                                 when '14' then
  742.                                     case i.Ta_DefTipoInterv when '01' then '11' when '00' then '' end
  743.                                 when '15' then
  744.                                     case i.Ta_DefTipoInterv when '01' then '26' when '00' then '27' end
  745.                                 when '16' then
  746.                                     case i.Ta_DefTipoInterv when '01' then '28' when '00' then '' end
  747.                                 when '17' then
  748.                                     case i.Ta_DefTipoInterv when '01' then '24' when '00' then '25' end
  749.                                 when '18' then
  750.                                     case i.Ta_DefTipoInterv when '01' then '22' when '00' then '23' end
  751.                                 when '19' then
  752.                                     case i.Ta_DefTipoInterv when '01' then '16' when '00' then '17' end
  753.                                 when '20' then
  754.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  755.                                 when '21' then
  756.                                     case i.Ta_DefTipoInterv when '01' then '18' when '00' then '' end
  757.                                 when '22' then
  758.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  759.                                 when '23' then
  760.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  761.                             end
  762.                         when 9 then
  763.                             case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  764.                         when 11 then
  765.                             case c.Tp_CodigoPDT
  766.                                 when '09' then
  767.                                     case i.Ta_DefTipoInterv when '01' then '14' when '00' then '15' end
  768.                                 when '12' then
  769.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '19' end
  770.                         end
  771.                     end tipoOtorgante,
  772.                     case h.Tp_Idtipopers when '00' then '2' else '1' end as TipoPer,
  773.                     case when len( isnull(k.Tm_CodigoPDT, '') ) = 6 then isnull(k.Tm_CodigoPDT, '') else '' end as Ubigeo,
  774.                     convert(varchar(max), round(100 / j.CountPersona, 2)) as porcentaje,
  775.                     case h.Tp_Idtipopers when '00' then h.RazonSocial else '' end as RazonSocial,
  776.                     h.ApePaterno,
  777.                     h.ApeMaterno,
  778.                     case PATINDEX('% %',rtrim(ltrim(h.Nombres)))
  779.                         when 0 then rtrim(ltrim(h.Nombres))
  780.                         else substring(rtrim(ltrim(h.Nombres)),0,(PATINDEX('% %',rtrim(ltrim(h.Nombres))))) end as Nombre1,
  781.                     case PATINDEX('% %',rtrim(ltrim(h.Nombres)))
  782.                         when 0 then '' else substring(rtrim(ltrim(h.Nombres)),(PATINDEX('% %',rtrim(ltrim(h.Nombres))) + 1), len(rtrim(ltrim(h.Nombres)))) end as Nombre2,
  783.                     case h.Tp_Idtipopers
  784.                         when '00' then ''
  785.                         else
  786.                             case a.Tm_idRegistro
  787.                                 when 6 then
  788.                                     case c.Tp_CodigoPDT
  789.                                         when '01' then
  790.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  791.                                         when '02' then
  792.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  793.                                         when '03' then
  794.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  795.                                         when '04' then
  796.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  797.                                         when '06' then
  798.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  799.                                         when '07' then
  800.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  801.                                         when '08' then
  802.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  803.                                         when '09' then
  804.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  805.                                         when '10' then
  806.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  807.                                         when '11' then
  808.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  809.                                         when '12' then
  810.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  811.                                         when '13' then
  812.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  813.                                         when '14' then
  814.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  815.                                         when '15' then
  816.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  817.                                         when '16' then
  818.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  819.                                         when '17' then
  820.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  821.                                         when '18' then
  822.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  823.                                         when '19' then
  824.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  825.                                         when '20' then
  826.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  827.                                         when '21' then
  828.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  829.                                         when '22' then
  830.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  831.                                         when '23' then
  832.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  833.                                     end
  834.                                 when 9 then
  835.                                     case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  836.                                 when 11 then
  837.                                     case c.Tp_CodigoPDT
  838.                                         when '09' then
  839.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  840.                                         when '12' then
  841.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  842.                                         end
  843.                             end
  844.                         end as TieneEnajTerceraCat,
  845.                         case h.Tp_Idtipopers
  846.                         when '00' then ''
  847.                         else
  848.                             case a.Tm_idRegistro
  849.                             when 6 then
  850.                                 case c.Tp_CodigoPDT
  851.                                     when '01' then
  852.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  853.                                     when '02' then
  854.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  855.                                     when '03' then
  856.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  857.                                     when '04' then
  858.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  859.                                     when '06' then
  860.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  861.                                     when '07' then
  862.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  863.                                     when '08' then
  864.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  865.                                     when '09' then
  866.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  867.                                     when '10' then
  868.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  869.                                     when '11' then
  870.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  871.                                     when '12' then
  872.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  873.                                     when '13' then
  874.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  875.                                     when '14' then
  876.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  877.                                     when '15' then
  878.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  879.                                     when '16' then
  880.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  881.                                     when '17' then
  882.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  883.                                     when '18' then
  884.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  885.                                     when '19' then
  886.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  887.                                     when '20' then
  888.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  889.                                     when '21' then
  890.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  891.                                     when '22' then
  892.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  893.                                     when '23' then
  894.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  895.                                     end
  896.                                 when 9 then
  897.                                     case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  898.                                 when 11 then
  899.                                     case c.Tp_CodigoPDT
  900.                                     when '09' then
  901.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  902.                                     when '12' then
  903.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  904.                                     end
  905.                             end
  906.                         end as EraSuCasa,
  907.                         case h.Tp_Idtipopers  when '01' then '0' else '' end as NroOperacion,
  908.                         j.CountPersona ,
  909.                         b.Td_idexpedienteacto,
  910.                         i.Ta_DefTipoInterv
  911.                     from SN.Tm_Escritura  as a
  912.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  913.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  914.                         inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  915.                         inner join SN.vstInterviniente as h on f.Tp_IdPersona = h.Tp_idpersona
  916.                         inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  917.                         inner join @xParticipacion j on b.Td_idexpedienteacto = j.Td_IdExpedienteActo and i.Ta_DefTipoInterv = j.Ta_DefTipoInterv
  918.                         inner join SYSMART.dbo.fc_ubigeo(0) k on h.Tm_idubigeo = k.tm_idubigeo
  919.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes
  920.                         and i.Ta_DefTipoInterv in ('00', '01')
  921.                         and f.Tp_IdTipointerv <> 15
  922.                         and a.Tm_idRegistro = @idRegDesdeTipoReg
  923.                          AND a.Tm_Excluida = 0
  924.                         and not c.Ta_NatActo is null
  925.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  926. 40,
  927. 39,
  928. 38,
  929. 37,
  930. 36,
  931. 35)
  932.                          order by tipoesc, a.Tm_NumeroEscritura, tipoOtorgante
  933.             open curInfoPDTOtorgantes
  934.             fetch next from curInfoPDTOtorgantes  into @tipoesc,
  935.             @nroescritura,
  936.             @fechaNum,
  937.             @secuenciaActo,
  938.             @SecuenciaBien,
  939.             @SecuenciaOtorgante,
  940.             @TipoDoc,
  941.             @NroDoc,
  942.             @TipoOtorgante,
  943.             @TipoPer,
  944.             @ubigeoPer,
  945.             @Porcentaje,
  946.             @RazonSocial,
  947.             @ApellidoPat,
  948.             @ApellidoMat,
  949.             @Nombre1,
  950.             @Nombre2,
  951.             @Gen3Cat,
  952.             @EraSuCasa,
  953.             @nroOperacion,
  954.             @CuentaOTG,
  955.             @idexpactox,
  956.             @TaTipoDefInterv
  957.             while @@FETCH_STATUS = 0
  958.                 begin
  959.                     /*if LEN(@strContenidoPDT) > 0
  960.                         begin
  961.                             set @strContenidoPDT = @strContenidoPDT + CHAR(13)
  962.                         end*/
  963.                    
  964.                    
  965.                     if @aux <> @nroescritura
  966.                         begin
  967.                             set @i = 0
  968.                         end
  969.                        
  970.                     set @i = @i + 1
  971.                    
  972.                     if @auxIdExpActo = @idexpactox
  973.                         begin
  974.                             if @auxDefTipoInterv <> @TipoOtorgante
  975.                                 begin
  976.                                     set @cuentaPorcj = 0
  977.                                 end
  978.                         end
  979.                     else
  980.                         begin
  981.                             set @cuentaPorcj = 0
  982.                         end
  983.                     if @CuentaOTG > 1
  984.                         begin
  985.                             set @cuentaPorcj = @cuentaPorcj + CONVERT(int, @Porcentaje)
  986.                             if (100 - @cuentaPorcj) > 0
  987.                                 begin
  988.                                     if (100 - @cuentaPorcj) < CONVERT(int, @Porcentaje)
  989.                                         begin
  990.                                             set @Porcentaje = CONVERT(varchar(max), CONVERT(int, @Porcentaje) + (100 - @cuentaPorcj))
  991.                                         end
  992.                                 end
  993.                             print @nroescritura + ' ' + convert(varchar(max), @cuentaPorcj) + ' ' + @auxDefTipoInterv
  994.                         end        
  995.                    
  996.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'Ü', 'U' ), 'Ñ', 'N' )
  997.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'Ú', 'U' ), 'Í', 'I' )
  998.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'É', 'E' ), 'Ó', 'O' )
  999.                     set @RazonSocial = replace( @RazonSocial, 'Á', 'A' )
  1000.                     set @RazonSocial = replace( @RazonSocial, '´', ' ' )
  1001.                     set @RazonSocial = replace( @RazonSocial, '-', '' )
  1002.                     set @RazonSocial = replace( @RazonSocial, 'º', '' )
  1003.                    
  1004.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'Ü', 'U' ), 'Ñ', 'N' )
  1005.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'Ú', 'U' ), 'Í', 'I' )
  1006.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'É', 'E' ), 'Ó', 'O' )
  1007.                     set @ApellidoPat = replace( @ApellidoPat, 'Á', 'A' )
  1008.                     set @ApellidoPat = replace( @ApellidoPat, '´', ' ' )
  1009.                     set @ApellidoPat = replace( @ApellidoPat, '-', '' )
  1010.                     set @ApellidoPat = replace( @ApellidoPat, 'º', '' )
  1011.                    
  1012.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'Ü', 'U' ) , 'Ñ', 'N')
  1013.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'Ú', 'U' ), 'Í', 'I' )
  1014.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'É', 'E' ), 'Ó', 'O' )
  1015.                     set @ApellidoMat = replace( @ApellidoMat, 'Á', 'A' )
  1016.                     set @ApellidoMat = replace( @ApellidoMat, '´', ' ' )
  1017.                     set @ApellidoMat = replace( @ApellidoMat, '-', '' )
  1018.                     set @ApellidoMat = replace( @ApellidoMat, 'º', '' )
  1019.                    
  1020.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'Ü', 'U' ) , 'Ñ', 'N')
  1021.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'Ú', 'U' ), 'Í', 'I' )
  1022.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'É', 'E' ), 'Ó', 'O' )
  1023.                     set @Nombre1 = replace( @Nombre1, 'Á', 'A' )
  1024.                     set @Nombre1 = replace( @Nombre1, '´', ' ' )
  1025.                     set @Nombre1 = replace( @Nombre1, '-', '' )
  1026.                     set @Nombre1 = replace( @Nombre1, 'º', '' )
  1027.                    
  1028.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'Ü', 'U' ) , 'Ñ', 'N')
  1029.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'Ú', 'U' ), 'Í', 'I' )
  1030.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'É', 'E' ), 'Ó', 'O' )
  1031.                     set @Nombre2 = replace( @Nombre2, 'Á', 'A' )
  1032.                     set @Nombre2 = replace( @Nombre2, '´', ' ' )
  1033.                     set @Nombre2 = replace( @Nombre2, '-', '' )
  1034.                     set @Nombre2 = replace( @Nombre2, 'º', '' )
  1035.                    
  1036.                     set @strContenidoPDT =
  1037.                     @tipoesc + '|' +  @nroescritura + '|' + @fechaNum +
  1038.                     '|' + @secuenciaActo + '|' + @SecuenciaBien + '|' + Convert(varchar(max), @i) + '|' +
  1039.                     @TipoDoc + '|' + @NroDoc + '|' + @TipoOtorgante + '|' + @TipoPer + '|' + @ubigeoPer + '|' + @Porcentaje +
  1040.                     '|' + @RazonSocial + '|' + @ApellidoPat + '|' + @ApellidoMat + '|'
  1041.                     +  @Nombre1 + '|' + @Nombre2 + '|'
  1042.                     + @Gen3Cat + '|' + @EraSuCasa + '|' + @nroOperacion + '|'
  1043.                    
  1044.                     insert into @xPDT (NomArchivo, strContenido)
  1045.                     values (@nomArchivo, @strContenidoPDT)
  1046.                    
  1047.                    
  1048.                     set @aux = @nroescritura
  1049.                     set @auxDefTipoInterv = @TipoOtorgante
  1050.                     set @auxIdExpActo = @idexpactox
  1051.                    
  1052.                     insert into dbo.SecuenciaOtorgante (IdExpActo, IdPersona, NroSecuencial)
  1053.                     values (@idexpactox, Convert(int, @SecuenciaOtorgante), @i)
  1054.                    
  1055.                        
  1056.                     fetch next from curInfoPDTOtorgantes  into @tipoesc,
  1057.                     @nroescritura,
  1058.                     @fechaNum,
  1059.                     @secuenciaActo,
  1060.                     @SecuenciaBien,
  1061.                     @SecuenciaOtorgante,
  1062.                     @TipoDoc,
  1063.                     @NroDoc,
  1064.                     @TipoOtorgante,
  1065.                     @TipoPer,
  1066.                     @ubigeoPer,
  1067.                     @Porcentaje,
  1068.                     @RazonSocial,
  1069.                     @ApellidoPat,
  1070.                     @ApellidoMat,
  1071.                     @Nombre1,
  1072.                     @Nombre2,
  1073.                     @Gen3Cat,
  1074.                     @EraSuCasa,
  1075.                     @nroOperacion,
  1076.                     @CuentaOTG,
  1077.                     @idexpactox,
  1078.                     @TaTipoDefInterv
  1079.                 end
  1080.                
  1081.         end
  1082.     if @tipo = '03'
  1083.         begin
  1084.             set @nomArchivo = @nomArchivo + '.MPD'
  1085.            
  1086.             declare curMedioPago cursor local for
  1087.                 select * from (
  1088.                         select  2 as tipoesc,
  1089.                                 convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) Tm_NumeroEscritura,
  1090.                                 '1' as SecuencialActo,
  1091.                                 SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') MedioPago,
  1092.                                 isnull(e.tm_codigoPDT, '2') as PDTMonedaMP,
  1093.                                 convert(varchar(max), isnull(d.Td_ValorExpVeh, 0)) as MontoPagado,
  1094.                                 SN.fc_formatearFecha('02',a.Tm_FechaGenEsc) as fechaPago,
  1095.                                  case when SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') in ('008', '009') then '' else  '-' end as nroDocPago,
  1096.                                 case when SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') in ('008', '009') then '' else  '99' end as EntFinanciera
  1097.                         from SN.Tm_Escritura  as a
  1098.                                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  1099.                                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  1100.                                     inner join SN.Td_Expediente_Vehiculo as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo and b.Tm_idexpediente = d.Tm_IdExpediente
  1101.                                     inner join SYSMART.tm_moneda as e on d.Tm_IdMoneda = e.tm_idmoneda
  1102.                          where d.Td_AcreditaMP = 1 and a.Tm_idRegistro = 9 and YEAR(a.Tm_FechaGenEsc) = @anho
  1103.                          and not c.Ta_NatActo is null
  1104.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  1105. 40,
  1106. 39,
  1107. 38,
  1108. 37,
  1109. 36,
  1110. 35) AND a.Tm_Excluida = 0
  1111.                         union all
  1112.                         select  1 as tipoesc,
  1113.                                 convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) Tm_NumeroEscritura,
  1114.                                 '1' as SecuencialActo,
  1115.                                 SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') MedioPago,
  1116.                                 isnull(e.tm_codigoPDT, '2') as PDTMonedaMP,
  1117.                                 convert(varchar(max), case d.Td_ValorExpTrans when 0 then 1 else d.Td_ValorExpTrans end ) as MontoPagado,
  1118.                                 SN.fc_formatearFecha('02',a.Tm_FechaGenEsc) as fechaPago,
  1119.                                  case when
  1120.                                     SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') in ('008', '009')
  1121.                                     then '' else  '-' end as nroDocPago,
  1122.                                 case when SYSMART.dbo.ObtenerNaturalezaTaTabla(Ta_MedioPago, 'Ta_MedioPago') in ('008', '009')
  1123.                                     then '' else  '99' end as EntFinanciera
  1124.                         from SN.Tm_Escritura  as a
  1125.                                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  1126.                                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  1127.                                     inner join SN.Td_Expediente_Transferencia as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo and b.Tm_idexpediente = d.Tm_IdExpediente
  1128.                                     inner join SYSMART.tm_moneda as e on d.Tm_IdMoneda = e.tm_idmoneda
  1129.                          where d.Td_AcreditaMP = 1 and a.Tm_idRegistro = 6 and YEAR(a.Tm_FechaGenEsc) = @anho
  1130.                          and not c.Ta_NatActo is null
  1131.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  1132. 40,
  1133. 39,
  1134. 38,
  1135. 37,
  1136. 36,
  1137. 35) AND a.Tm_Excluida = 0
  1138.                     ) x  WHERE tipoesc = @tiporegistro
  1139.                       order by tipoesc, Tm_NumeroEscritura
  1140.                    
  1141.             open curMedioPago
  1142.             fetch next from curMedioPago into @tipoesc,
  1143.                 @nroescritura,
  1144.                 @secuenciaActo,
  1145.                 @MedioPago,
  1146.                 @codMonedaMedPago,
  1147.                 @montoPagado,
  1148.                 @fechaPago,
  1149.                 @nroDocMedPago,
  1150.                 @entFinanciera
  1151.             while @@FETCH_STATUS = 0
  1152.                 begin
  1153.                     set @strContenidoPDT =
  1154.                         @tipoesc + '|' + convert(varchar(5),@nroescritura) + '|' + @secuenciaActo + '|' + @MedioPago + '|' + @codMonedaMedPago + '|' + @montoPagado + '|' +
  1155.                         @fechaPago + '|' + @nroDocMedPago + '|' + @entFinanciera + '|'
  1156.                    
  1157.                     insert into @xPDT (NomArchivo, strContenido)
  1158.                     values (@nomArchivo, @strContenidoPDT)
  1159.                    
  1160.                     fetch next from curMedioPago  into @tipoesc,
  1161.                         @nroescritura,
  1162.                         @secuenciaActo,
  1163.                         @MedioPago,
  1164.                         @codMonedaMedPago,
  1165.                         @montoPagado,
  1166.                         @fechaPago,
  1167.                         @nroDocMedPago,
  1168.                         @entFinanciera
  1169.                 end
  1170.             close curMedioPago
  1171.             deallocate curMedioPago
  1172.         end
  1173.     if @tipo = '04'
  1174.         begin
  1175.             set @nomArchivo = @nomArchivo + '.FOR'
  1176.             set @i = 0
  1177.             declare curFormulario cursor local for
  1178.                 select b.tipoesc,
  1179.                     convert(varchar(max), b.NroEscritura),
  1180.                     b.Tp_IdActo,
  1181.                     CONVERT(varchar(max), b.NroSecuencial),
  1182.                     b.tipoOtorgante,
  1183.                     '-',
  1184.                     '0.1'
  1185.                 from SN.Td_Expediente_Interv as a
  1186.                     inner join (
  1187.  
  1188. select case a.Tm_idRegistro when 6 then '1' when 9 then '2' else '4' end as tipoesc,
  1189.  convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) as NroEscritura,
  1190.                     case when a.Tm_idRegistro in (6, 9) then '1' else
  1191.                     case c.tp_codigopdt when '09' then '1' else '2' end end Tp_IdActo,
  1192.                     SO.NroSecuencial,
  1193.                     f.Td_ExpedienteInterv,
  1194.                     case a.Tm_idRegistro
  1195.                         when 6 then
  1196.                             case c.Tp_CodigoPDT
  1197.                                 when '01' then
  1198.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  1199.                                 when '02' then
  1200.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  1201.                                 when '03' then
  1202.                                     case i.Ta_DefTipoInterv when '01' then '05' when '00' then '06' end
  1203.                                 when '04' then
  1204.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  1205.                                 when '06' then
  1206.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  1207.                                 when '07' then
  1208.                                     case i.Ta_DefTipoInterv when '01' then '20' when '00' then '21' end
  1209.                                 when '08' then
  1210.                                     case i.Ta_DefTipoInterv when '01' then '13' when '00' then '40' end
  1211.                                 when '09' then
  1212.                                     case i.Ta_DefTipoInterv when '01' then '14' when '00' then '15' end
  1213.                                 when '10' then
  1214.                                     case i.Ta_DefTipoInterv when '01' then '07' when '00' then '08' end
  1215.                                 when '11' then
  1216.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '' end
  1217.                                 when '12' then
  1218.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '' end
  1219.                                 when '13' then
  1220.                                     case i.Ta_DefTipoInterv when '01' then '09' when '00' then '10' end
  1221.                                 when '14' then
  1222.                                     case i.Ta_DefTipoInterv when '01' then '11' when '00' then '' end
  1223.                                 when '15' then
  1224.                                     case i.Ta_DefTipoInterv when '01' then '26' when '00' then '27' end
  1225.                                 when '16' then
  1226.                                     case i.Ta_DefTipoInterv when '01' then '28' when '00' then '' end
  1227.                                 when '17' then
  1228.                                     case i.Ta_DefTipoInterv when '01' then '24' when '00' then '25' end
  1229.                                 when '18' then
  1230.                                     case i.Ta_DefTipoInterv when '01' then '22' when '00' then '23' end
  1231.                                 when '19' then
  1232.                                     case i.Ta_DefTipoInterv when '01' then '16' when '00' then '17' end
  1233.                                 when '20' then
  1234.                                     case i.Ta_DefTipoInterv when '01' then '03' when '00' then '04' end
  1235.                                 when '21' then
  1236.                                     case i.Ta_DefTipoInterv when '01' then '18' when '00' then '' end
  1237.                                 when '22' then
  1238.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  1239.                                 when '23' then
  1240.                                     case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  1241.                             end
  1242.                         when 9 then
  1243.                             case i.Ta_DefTipoInterv when '01' then '01' when '00' then '02' end
  1244.                         when 11 then
  1245.                             case c.Tp_CodigoPDT
  1246.                                 when '09' then
  1247.                                     case i.Ta_DefTipoInterv when '01' then '14' when '00' then '15' end
  1248.                                 when '12' then
  1249.                                     case i.Ta_DefTipoInterv when '01' then '19' when '00' then '' end
  1250.                         end
  1251.                     end tipoOtorgante,
  1252.                     case h.Tp_Idtipopers when '00' then '2' else '1' end as TipoPer,
  1253.                     case h.Tp_Idtipopers
  1254.                         when '00' then ''
  1255.                         else
  1256.                             case a.Tm_idRegistro
  1257.                                 when 6 then
  1258.                                     case c.Tp_CodigoPDT
  1259.                                         when '01' then
  1260.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1261.                                         when '02' then
  1262.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1263.                                         when '03' then
  1264.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1265.                                         when '04' then
  1266.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1267.                                         when '06' then
  1268.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1269.                                         when '07' then
  1270.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1271.                                         when '08' then
  1272.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1273.                                         when '09' then
  1274.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1275.                                         when '10' then
  1276.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1277.                                         when '11' then
  1278.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1279.                                         when '12' then
  1280.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1281.                                         when '13' then
  1282.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1283.                                         when '14' then
  1284.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1285.                                         when '15' then
  1286.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1287.                                         when '16' then
  1288.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1289.                                         when '17' then
  1290.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1291.                                         when '18' then
  1292.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1293.                                         when '19' then
  1294.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1295.                                         when '20' then
  1296.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1297.                                         when '21' then
  1298.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1299.                                         when '22' then
  1300.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1301.                                         when '23' then
  1302.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1303.                                     end
  1304.                                 when 9 then
  1305.                                     case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1306.                                 when 11 then
  1307.                                     case c.Tp_CodigoPDT
  1308.                                         when '09' then
  1309.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1310.                                         when '12' then
  1311.                                             case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1312.                                         end
  1313.                             end
  1314.                         end as TieneEnajTerceraCat,
  1315.                         case h.Tp_Idtipopers
  1316.                         when '00' then ''
  1317.                         else
  1318.                             case a.Tm_idRegistro
  1319.                             when 6 then
  1320.                                 case c.Tp_CodigoPDT
  1321.                                     when '01' then
  1322.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1323.                                     when '02' then
  1324.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1325.                                     when '03' then
  1326.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1327.                                     when '04' then
  1328.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1329.                                     when '06' then
  1330.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1331.                                     when '07' then
  1332.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1333.                                     when '08' then
  1334.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1335.                                     when '09' then
  1336.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1337.                                     when '10' then
  1338.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1339.                                     when '11' then
  1340.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1341.                                     when '12' then
  1342.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1343.                                     when '13' then
  1344.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1345.                                     when '14' then
  1346.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1347.                                     when '15' then
  1348.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1349.                                     when '16' then
  1350.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1351.                                     when '17' then
  1352.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1353.                                     when '18' then
  1354.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1355.                                     when '19' then
  1356.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1357.                                     when '20' then
  1358.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1359.                                     when '21' then
  1360.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1361.                                     when '22' then
  1362.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1363.                                     when '23' then
  1364.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1365.                                     end
  1366.                                 when 9 then
  1367.                                     case i.Ta_DefTipoInterv when '01' then '' when '00' then '0' end
  1368.                                 when 11 then
  1369.                                     case c.Tp_CodigoPDT
  1370.                                     when '09' then
  1371.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1372.                                     when '12' then
  1373.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1374.                                     end
  1375.                             end
  1376.                         end as EraSuCasa,
  1377.                         case h.Tp_Idtipopers
  1378.                         when '00' then ''
  1379.                         else
  1380.                             case a.Tm_idRegistro
  1381.                             when 6 then
  1382.                                 case c.Tp_CodigoPDT
  1383.                                     when '01' then
  1384.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '1' end
  1385.                                     when '02' then
  1386.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1387.                                     when '03' then
  1388.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1389.                                     when '04' then
  1390.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '1' end
  1391.                                     when '06' then
  1392.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1393.                                     when '07' then
  1394.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1395.                                     when '08' then
  1396.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1397.                                     when '09' then
  1398.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1399.                                     when '10' then
  1400.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1401.                                     when '11' then
  1402.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1403.                                     when '12' then
  1404.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1405.                                     when '13' then
  1406.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1407.                                     when '14' then
  1408.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1409.                                     when '15' then
  1410.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1411.                                     when '16' then
  1412.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1413.                                     when '17' then
  1414.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1415.                                     when '18' then
  1416.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1417.                                     when '19' then
  1418.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1419.                                     when '20' then
  1420.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1421.                                     when '21' then
  1422.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1423.                                     when '22' then
  1424.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '1' end
  1425.                                     when '23' then
  1426.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '1' end
  1427.                                     end
  1428.                                 when 9 then
  1429.                                     case i.Ta_DefTipoInterv when '01' then '' when '00' then '1' end
  1430.                                 when 11 then
  1431.                                     case c.Tp_CodigoPDT
  1432.                                     when '09' then
  1433.                                         case i.Ta_DefTipoInterv when '01' then '' when '00' then '' end
  1434.                                     when '12' then
  1435.                                         case i.Ta_DefTipoInterv when '01' then '' when
  1436.                                          '00' then '' end
  1437.                                     end
  1438.                             end
  1439.                         end as ImpPagoEsCero
  1440.                     from SN.Tm_Escritura  as a
  1441.                         inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  1442.                         inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  1443.                         inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  1444.                         inner join SN.vstInterviniente as h on f.Tp_IdPersona = h.Tp_idpersona
  1445.                         inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  1446.                         inner join SYSMART.dbo.fc_ubigeo(0) k on h.Tm_idubigeo = k.tm_idubigeo
  1447.                         inner join dbo.SecuenciaOtorgante SO on b.Td_Idexpedienteacto = SO.IdExpActo and f.Tp_idpersona = SO.IdPersona
  1448.                         where year(a.Tm_FechaGenEsc) = @anho AND month(a.Tm_FechaGenEsc) = @mes
  1449.                         and i.Ta_DefTipoInterv in ('00', '01') and a.Tm_idRegistro in (6,9,11)
  1450.                         and not c.Ta_NatActo is null
  1451.                     and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  1452. 40,
  1453. 39,
  1454. 38,
  1455. 37,
  1456. 36,
  1457. 35) AND a.Tm_Excluida = 0
  1458.                      
  1459.                     ) as b on a.Td_ExpedienteInterv = b.Td_ExpedienteInterv
  1460.                     where b.EraSuCasa = '0' and
  1461.                     b.TieneEnajTerceraCat = '0' and b.tipoesc = @tiporegistro
  1462.                     order by tipoesc, b.NroEscritura
  1463.             open curFormulario
  1464.             fetch next from curFormulario into @tipoesc,
  1465.                 @nroescritura,
  1466.                 @secuenciaActo,
  1467.                 @SecuenciaOtorgante,
  1468.                 @TipoOtorgante,
  1469.                 @nroOperacion,
  1470.                 @montoPagado
  1471.             while @@FETCH_STATUS = 0
  1472.                 begin
  1473.                     set @i = @i + 1
  1474.                    
  1475.                     set @strContenidoPDT =
  1476.                     @tipoesc + '|' + @nroescritura + '|' +
  1477.                     @secuenciaActo + '|' + @SecuenciaOtorgante + '|' + @TipoOtorgante + '|'
  1478.                      + CONVERT(varchar(max), @i) + '|' + @montoPagado + '|'
  1479.                      
  1480.                      insert into @xPDT (NomArchivo, strContenido)
  1481.                     values (@nomArchivo, @strContenidoPDT)
  1482.                    
  1483.                     fetch next from curFormulario into @tipoesc,
  1484.                     @nroescritura,
  1485.                     @secuenciaActo,
  1486.                     @SecuenciaOtorgante,
  1487.                     @TipoOtorgante,
  1488.                     @nroOperacion,
  1489.                     @montoPagado
  1490.                 end
  1491.             close curFormulario
  1492.             deallocate curFormulario
  1493.            
  1494.             --delete from dbo.SecuenciaOtorgante
  1495.         end
  1496.     if @tipo = '05'
  1497.         begin
  1498.             declare @NumFolio varchar(max),
  1499.                     @TipoLibro varchar(2)
  1500.            
  1501.             set @NumFolio = ''
  1502.             set @TipoLibro = ''
  1503.             set @nomArchivo = @nomArchivo + '.LIB'
  1504.            
  1505.             declare curLibroAperCierre cursor local for
  1506.             select
  1507.                 convert(int, SUBSTRING(a.Td_NumeroApertCierre, 6, 5)) ,  
  1508.                 SN.fc_formatearFecha('02', a.Td_FechaRegistro) as FechaLibro,
  1509.                 --SYSMART.dbo.ObtenerNaturalezaTaTabla(a.Ta_TipoLibroApertCierre, 'Ta_TipoLibroApertCierre') as TipoLibro,
  1510.                 d.ta_naturaleza as TipoLibro,
  1511.                 convert(varchar(max), dbo.EXTRACT_YEAR(a.Td_observalibrocierre)) as NumFolio,
  1512.                 case c.Tp_Idtipopers when '00' then '2' else '1' end as TipoPer,
  1513.                 c.Numero,
  1514.                 c.ApePaterno,
  1515.                 c.ApeMaterno,
  1516.                 case PATINDEX('% %',rtrim(ltrim(c.Nombres)))
  1517.                         when 0 then rtrim(ltrim(c.Nombres))
  1518.                         else substring(rtrim(ltrim(c.Nombres)),0,(PATINDEX('% %',rtrim(ltrim(c.Nombres))))) end as Nombre1,
  1519.                     case PATINDEX('% %',rtrim(ltrim(c.Nombres)))
  1520.                         when 0 then '' else substring(rtrim(ltrim(c.Nombres)),(PATINDEX('% %',rtrim(ltrim(c.Nombres))) + 1), len(rtrim(ltrim(c.Nombres)))) end as Nombre2,
  1521.                 c.RazonSocial
  1522.                 from SN.Td_Expediente_AperturaCierre a
  1523.                 inner join SN.Td_Expediente_Interv b
  1524.                     on a.Tm_IdExpediente = b.Tm_IdExpediente
  1525.                     and a.Td_IdexpedienteActo = b.Td_IdExpedienteActo
  1526.                 inner join SN.vstInterviniente c on b.Tp_IdPersona = c.Tp_idpersona
  1527.                 inner join SYSMART.dbo.ta_tabla d on a.Ta_TipoLibroApertCierre = d.ta_codigo and d.ta_campo = 'Ta_TipoLibroApertCierre'
  1528.                 where year(a.Td_FechaRegistro) = @anho AND month(a.Td_FechaRegistro) = @mes
  1529.                 and isnull(d.ta_naturaleza, '') <> ''
  1530.                 --and a.Ta_TipoLibroApertCierre = '02'
  1531.                 --and convert(int, dbo.EXTRACT_YEAR(a.Td_observalibrocierre)) > 0
  1532.                 and ISNUMERIC ( c.Numero ) = 1
  1533.                 and LEN(c.Numero) = 11
  1534.                
  1535.                 order by a.Td_NumeroApertCierre
  1536.                
  1537.             open curLibroAperCierre
  1538.             fetch next from curLibroAperCierre  into @nroescritura,
  1539.             @fechaNum,
  1540.             @TipoLibro,
  1541.             @NumFolio,
  1542.             @TipoPer,
  1543.             @NroDoc,
  1544.             @ApellidoPat,
  1545.             @ApellidoMat,
  1546.             @Nombre1,
  1547.             @Nombre2,
  1548.             @RazonSocial
  1549.             while @@FETCH_STATUS = 0
  1550.                 begin
  1551.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'Ü', 'U' ), 'Ñ', 'N' )
  1552.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'Ú', 'U' ), 'Í', 'I' )
  1553.                     set @RazonSocial = REPLACE( replace( @RazonSocial, 'É', 'E' ), 'Ó', 'O' )
  1554.                     set @RazonSocial = replace( @RazonSocial, 'Á', 'A' )
  1555.                    
  1556.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'Ü', 'U' ), 'Ñ', 'N' )
  1557.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'Ú', 'U' ), 'Í', 'I' )
  1558.                     set @ApellidoPat = REPLACE( replace( @ApellidoPat, 'É', 'E' ), 'Ó', 'O' )
  1559.                     set @ApellidoPat = replace( @ApellidoPat, 'Á', 'A' )
  1560.                    
  1561.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'Ü', 'U' ) , 'Ñ', 'N')
  1562.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'Ú', 'U' ), 'Í', 'I' )
  1563.                     set @ApellidoMat = REPLACE( replace( @ApellidoMat, 'É', 'E' ), 'Ó', 'O' )
  1564.                     set @ApellidoMat = replace( @ApellidoMat, 'Á', 'A' )
  1565.                    
  1566.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'Ü', 'U' ) , 'Ñ', 'N')
  1567.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'Ú', 'U' ), 'Í', 'I' )
  1568.                     set @Nombre1 = REPLACE( replace( @Nombre1, 'É', 'E' ), 'Ó', 'O' )
  1569.                     set @Nombre1 = replace( @Nombre1, 'Á', 'A' )
  1570.                    
  1571.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'Ü', 'U' ) , 'Ñ', 'N')
  1572.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'Ú', 'U' ), 'Í', 'I' )
  1573.                     set @Nombre2 = REPLACE( replace( @Nombre2, 'É', 'E' ), 'Ó', 'O' )
  1574.                     set @Nombre2 = replace( @Nombre2, 'Á', 'A' )
  1575.                    
  1576.                     set @strContenidoPDT =
  1577.                     @nroescritura + '|' + @fechaNum + '|' + @TipoLibro + '|' + @NumFolio + '|' +
  1578.                     @TipoPer + '|' + @NroDoc + '|' + @ApellidoPat + '|' + @ApellidoMat + '|' +  @Nombre1 + '|' + @Nombre2 + '|' + @RazonSocial + '|'
  1579.                    
  1580.                     insert into @xPDT (NomArchivo, strContenido)
  1581.                     values (@nomArchivo, @strContenidoPDT)
  1582.                    
  1583.                     fetch next from curLibroAperCierre  into @nroescritura,
  1584.                     @fechaNum,
  1585.                     @TipoLibro,
  1586.                     @NumFolio,
  1587.                     @TipoPer,
  1588.                     @NroDoc,
  1589.                     @ApellidoPat,
  1590.                     @ApellidoMat,
  1591.                     @Nombre1,
  1592.                     @Nombre2,
  1593.                     @RazonSocial
  1594.                 end
  1595.         end
  1596.     PRINT @strContenidoPDT
  1597.     select * from @xPDT
  1598. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement