SET QUOTED_IDENTIFIER OFF
IF OBJECT_ID('dbo.sp_fco_crea_riesgos_mm') IS NOT NULL
    DROP PROCEDURE dbo.sp_fco_crea_riesgos_mm
GO
CREATE PROCEDURE dbo.sp_fco_crea_riesgos_mm
(
   	@pvc_compania	varchar(4),
   	@pvc_anio		varchar(4),
   	@pvc_mes		varchar(2),
   	@pvc_suc		varchar(10)
)
/**********************************************************************/
/*** Modulo:      Modulo Fiscal Colombiano Version FCO - ADAM V.3   ***/
/*** Descripcion: Crea archivo plano para Riesgos                   ***/
/*** Autor:       Juan Carlos Leguizamon                            ***/
/*** Fecha MF:    Julio 10 de 2001                                  ***/
/*** Fecha FCO:   Abril 01 de 2006                                  ***/
/**********************************************************************/
AS
   /* VARIABLES ESTRUCTURA MEDIO MAGNETICO */
DECLARE 	
   @CI01_06 Char(58),
   @CI07_18 Char(148),
   @CI19_27 Char(57),

   @REN_31  Char(26),
   @REN_32  Char(26),
   @REN_34  Char(26),
   @REN_35  Char(26),
   @REN_36  Char(26),
   @REN_37  Char(26),
   @REN_38  Char(26),
   @REN_39  Char(26),

   @C4_R31  Numeric(10,0),
   @C4_R32  Numeric(10,0),
   @C4_R35  Numeric(10,0),
   @C4_R37  Numeric(10,0),
   @C4_R39  Numeric(10,0)

   /* VARIABLES AUXILIARES */
   /* Para armar sentencias dinamicamente */
   DECLARE @sql  varChar(3000),
	   @sql2 varchar(3000),
	   @sql3 varchar(3000)

   /* Para controlar puntos y rayas en el registro_fiscal */
   DECLARE 	@nit	Char(9)
   /* Forma de Presentacion de la Autoliquidacion para ARP */
   DECLARE 	@fpres  Char(1)
   /* Sucursal */
   DECLARE	@cod_SUC Char(10),
	   	@control_SUC varchar(10)

   /* Tabla donde estan los datos */
   DECLARE @tabla  VarChar(30)

   /* Variables para descontar incapacidades, saldos a favor y pagar intereses por mora */
   DECLARE      @nro_aut_in  char(10),
                @vlr_aut_in  numeric(10,0),
                @vlr_otr_rp  numeric(10,0),
                @nro_meses   char(10),
                @vlr_mor_rp  numeric(10,0),
                @nro_rad_saf char(10),
                @vlr_saf_rp  numeric(10,0)

BEGIN

    SET @tabla = 'FCO_ARP_' + RTrim(@pvc_compania) + '_' + RTrim(@pvc_anio) + '_' + RTrim(@pvc_mes)
    DELETE fco_arp_mm

   /***********************************************************************/
   /*****          CONSTRUCCION DEL REGISTRO TIPO I                   *****/
   /***********************************************************************/

   SELECT @CI01_06 = '00000102' + 
	LTrim(RTrim(SubString(nombre_ARP,1,40))) + Space(40-Len(LTrim(RTrim(SubString(nombre_ARP,1,40))))) + 
        Replicate('0',9-Len(LTrim(RTrim(SubString(nit_ARP,1,9))))) + LTrim(RTrim(SubString(nit_ARP,1,9))) +
        SubString(nit_ARP,11,1)
   FROM vw_fco_companias
   WHERE compania = @pvc_compania 

   SELECT @nit=registro_fiscal
   FROM vw_fco_companias
   WHERE compania=@pvc_compania 

   IF CharIndex('.', @nit)>0
   BEGIN
      SELECT @nit=SubString(registro_fiscal,1,3) + SubString(registro_fiscal,5,3) + SubString(registro_fiscal,9,3)
      FROM vw_fco_companias WHERE compania=@pvc_compania 
   END
   ELSE
   BEGIN
      SELECT @nit=SubString(registro_fiscal,1,9)
      FROM vw_fco_companias WHERE compania=@pvc_compania 
   END

   /* Control del codigo de la sucursal */
   SET @control_SUC = IsNull(@pvc_suc,'0')
   IF Len(@control_SUC)=0
	SET @control_SUC='0'

   IF @control_SUC = '0'
   BEGIN
       SELECT @CI07_18 = LTrim(RTrim(SubString(C.nombre_cia,1,40))) + Space(40-Len(LTrim(RTrim(SubString(C.nombre_cia,1,40))))) + 
		'N' + 
        Replicate('0',9-Len(LTrim(RTrim(@nit)))) + LTrim(RTrim(@nit)) +
        SubString(C.registro_fiscal, Len(RTrim(C.registro_fiscal)),1) +
		SubString(C.clase_aportante,1,1) + Space(1-Len(SubString(C.clase_aportante,1,1))) + 
		LTrim(RTrim(SubString(C.domicilio,1,40))) + Space(40-Len(LTrim(RTrim(SubString(C.domicilio,1,40))))) + 
		LTrim(RTrim(SubString(M.nombre,1,15))) + Space(15-Len(LTrim(RTrim(SubString(M.nombre,1,15))))) + 
		Replicate('0',4-Len(LTrim(RTrim(SubString(LTrim(M.codigo),1,4))))) + LTrim(RTrim(SubString(LTrim(M.codigo),1,4))) +
		LTrim(RTrim(SubString(D.nombre,1,15))) + Space(15-Len(LTrim(RTrim(SubString(D.nombre,1,15))))) + 
		Replicate('0',2-Len(LTrim(RTrim(SubString(LTrim(D.codigo),1,2))))) + LTrim(RTrim(SubString(LTrim(D.codigo),1,2))) +
		LTrim(RTrim(SubString(C.telefono,1,10))) + Space(10-Len(LTrim(RTrim(SubString(C.telefono,1,10))))) + 
		LTrim(RTrim(SubString(C.fax,1,10))) + Space(10-Len(LTrim(RTrim(SubString(C.fax,1,10)))))
       FROM vw_fco_companias C,
	    fco_CIUDADES M,
            fco_DEPARTAMENTOS D
       WHERE M.codigo   = C.cod_ciudad And
	 D.codigo   = C.cod_dpto   And
	 M.cod_dpto = D.codigo 	   And
	 C.compania = @pvc_compania
   END
   ELSE
   BEGIN
       SELECT @CI07_18 = LTrim(RTrim(SubString(C.nombre_cia,1,40))) + Space(40-Len(LTrim(RTrim(SubString(C.nombre_cia,1,40))))) + 
		'N' + 
   	        Replicate('0',9-Len(LTrim(RTrim(@nit)))) + LTrim(RTrim(@nit)) +
   	        SubString(C.registro_fiscal, Len(RTrim(C.registro_fiscal)),1) +
		SubString(C.clase_aportante,1,1) + Space(1-Len(SubString(C.clase_aportante,1,1))) + 
		LTrim(RTrim(SubString(RSP.direccion,1,40))) + Space(40-Len(LTrim(RTrim(SubString(RSP.direccion,1,40))))) + 
		LTrim(RTrim(SubString(M.nombre,1,15))) + Space(15-Len(LTrim(RTrim(SubString(M.nombre,1,15))))) + 
		Replicate('0',4-Len(LTrim(RTrim(SubString(LTrim(M.codigo),1,4))))) + LTrim(RTrim(SubString(LTrim(M.codigo),1,4))) +
		LTrim(RTrim(SubString(D.nombre,1,15))) + Space(15-Len(LTrim(RTrim(SubString(D.nombre,1,15))))) + 
		Replicate('0',2-Len(LTrim(RTrim(SubString(LTrim(D.codigo),1,2))))) + LTrim(RTrim(SubString(LTrim(D.codigo),1,2))) +
		LTrim(RTrim(SubString(RSP.telefono,1,10))) + Space(10-Len(LTrim(RTrim(SubString(RSP.telefono,1,10))))) + 
		LTrim(RTrim(SubString(RSP.fax,1,10))) + Space(10-Len(LTrim(RTrim(SubString(RSP.fax,1,10)))))
       FROM vw_fco_companias C,
            fco_sucursales RSP, 
	    fco_CIUDADES M,
            fco_DEPARTAMENTOS D
       WHERE 
         RSP.compania = C.compania And
	 M.codigo   = RSP.cod_ciudad And
	 D.codigo   = RSP.cod_dpto   And
	 M.cod_dpto = D.codigo 	   And
	 C.compania = @pvc_compania  And
         RSP.sucursal = @pvc_suc
   END

   /* Forma de Presentación de la Autoliquidación de Aportes */
   IF @control_SUC = '0'
   BEGIN
	SELECT @fpres = IsNull(forma_presentacion,'U')
	FROM   vw_fco_companias
	WHERE  compania = @pvc_compania

        SET @cod_SUC = ' '
   END
   ELSE
   BEGIN
	SET @fpres='S';
	SET @cod_SUC = @control_SUC
   END

    /* Numero de trabajadores */
   SET @sql=
   " SELECT Total = Count(COD_TRAB)" +
   " INTO ##tmp_trabajadores" +
   " FROM " + @tabla +
   " WHERE COD_TRAB IN" +
   " (SELECT trabajador FROM trabajadores_grales" + 
   " WHERE compania = '" + @pvc_compania + "' AND sit_trabajador = 1" +
   Case @control_SUC WHEN '0' THEN "" ELSE " AND COD_SUC='" + @control_SUC + "'" END + ")"

   EXECUTE (@sql)

   SELECT @CI19_27 = LTrim(RTrim(@pvc_anio)) + Replicate('0',2-Len(LTrim(RTrim(@pvc_mes)))) + LTrim(RTrim(@pvc_mes)) +
		Space(1) + Space(6) + Space(8) +
		Space(10) + Space(10) + @fpres + @cod_suc +
		Replicate('0',5-Len(LTrim(RTrim(Convert(Char,total))))) + LTrim(RTrim(Convert(Char, total)))
   FROM ##tmp_trabajadores
   DROP TABLE ##tmp_trabajadores

   INSERT INTO fco_arp_mm VALUES (0, @CI01_06 + @CI07_18 + @CI19_27)

   /***********************************************************************/
   /*****          CONSTRUCCION DEL REGISTRO TIPO II                  *****/
   /***********************************************************************/


   SET @sql="SELECT Identity(Int,1,1) Sec, " +
	"'2' + LTrim(RTrim(SubString(C11A,1,1))) + Space(1-Len(LTrim(RTrim(SubString(C11A,1,1))))) + " +
	"Replicate('0', 12 - Len(RTrim(LTrim(Convert(char(12),C11B))))) + RTrim(LTrim(Convert(char(12),C11B))) + " +
	"RTrim(LTrim(SubString(SubString(C12ABC, 1, CharIndex('/', C12ABC)-1),1,15))) + Space(15-Len(RTrim(LTrim(SubString(SubString(C12ABC, 1, CharIndex('/', C12ABC)-1),1,15))))) +" +
	"RTrim(LTrim(SubString(SubString(C12ABC, CharIndex('/', C12ABC) + 1, CharIndex('/', C12ABC, CharIndex('/', C12ABC)+1) - CharIndex('/', C12ABC)-1),1,15))) + Space(15-Len(RTrim(LTrim(SubString(SubString(C12ABC, CharIndex('/', C12ABC) + 1, CharIndex('/', C12ABC, CharIndex('/', C12ABC)+1) - CharIndex('/', C12ABC)-1),1,15))))) +" +
	"RTrim(LTrim(SubString(SubString(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)), 1, Case When CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)))>0 Then CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) Else Len(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) End),1,15))) + Space(15-Len(RTrim(LTrim(SubString(SubString(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)), 1, Case When CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)))>0 Then CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) Else Len(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) End),1,15))))) +" +
	"Case When CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) = 0 Then space(15-Len('')) Else RTrim(LTrim(SubString(SubString(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)), CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) + 1, Len(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) - CharIndex(' ', Reverse(LTrim(RTrim(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)))))),1,15))) + Space(15-Len(RTrim(LTrim(SubString(SubString(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)), CharIndex(' ', Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) + 1, Len(Reverse(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1))) - CharIndex(' ', Reverse(LTrim(RTrim(SubString(Reverse(C12ABC),1,CharIndex('/', Reverse(C12ABC))-1)))))),1,15))))) End +" +
	"Case When RTrim(LTrim(IsNull(C13A,''))) = '' Then Space(1) Else C13A End +" +
	"Case When RTrim(LTrim(IsNull(C13B,''))) = '' Then Space(1) Else C13B End +"
   SET @sql2=
	"Case When RTrim(LTrim(IsNull(C13C,''))) = '' Then Space(1) Else C13C End +" +
	"Case When RTrim(LTrim(IsNull(C13D,''))) = '' Then Space(1) Else C13D End +" +
	"Case When RTrim(LTrim(IsNull(C13E,''))) = '' Then Space(1) Else C13E End +" +
	"Case When RTrim(LTrim(IsNull(C13F,''))) = '' Then Space(1) Else C13F End +" +
	"Case When RTrim(LTrim(IsNull(C13G,''))) = '' Then Space(1) Else C13G End +" +
	"Case When RTrim(LTrim(IsNull(C13H,''))) = '' Then Space(1) Else C13H End +" +
	"Case When RTrim(LTrim(IsNull(C13I,''))) = '' Then Space(1) Else C13I End +" +
	"Case When RTrim(LTrim(IsNull(C13J,''))) = '' Then Space(1) Else C13J End +" +
	"Replicate('0',2 - Len(LTrim(RTrim(Convert(Char(2),Floor(C13K)))))) + LTrim(RTrim(Convert(Char(2),Floor(C13K)))) +" +
	"Replicate('0',2 - Len(LTrim(RTrim(Convert(Char(2),Floor(C14A)))))) + LTrim(RTrim(Convert(Char(2),Floor(C14A)))) +" +	
	"Replicate('0',9 - Len(LTrim(RTrim(Convert(Char(9),Floor(C15A)))))) + LTrim(RTrim(Convert(Char(9),Floor(C15A)))) +" 
   SET @sql3=
	"Replicate('0',9) +" +
	"Replicate('0',9 - Len(LTrim(RTrim(Convert(Char(9),Floor(C17A)))))) + LTrim(RTrim(Convert(Char(9),Floor(C17A)))) +" +
	"Replicate('0',9) +" +
	"Replicate('0',9 - Len(LTrim(RTrim(Convert(Char(9),Floor(C28B)))))) + LTrim(RTrim(Convert(Char(9),Floor(C28B)))) +" +
	"Replicate('0',9 - Len(LTrim(RTrim(Convert(Char(9),Floor(C29A)))))) + LTrim(RTrim(Convert(Char(9),Floor(C29A)))) +" +
	"LTrim(RTrim(SubString(C30A,1,5))) + Space(5-Len(LTrim(RTrim(SubString(C30A,1,5))))) + " +
	"LTrim(RTrim(SubString(C30B,1,5))) + Space(5-Len(LTrim(RTrim(SubString(C30B,1,5))))) As Registro2" +
	" INTO ##TMP_DETALLE" +
	" FROM " + @tabla +
	" WHERE CIA='" + @pvc_compania + "'" +
   	Case @control_SUC WHEN '0' THEN "" ELSE " AND COD_SUC='" + @control_SUC + "'" END +
	" AND (C29A > 0 OR C13F = 'X' OR C13G = 'X' OR C13H = 'X' OR C13K > 0)"

   EXECUTE (@sql + @sql2 + @sql3)

   INSERT INTO fco_arp_mm 
   SELECT Sec, Replicate('0', 5- Len(RTrim(LTrim(Convert(Char(5),Sec))))) + RTrim(LTrim(Convert(Char(5),Sec))) + Registro2
   FROM ##TMP_DETALLE

   DROP TABLE ##TMP_DETALLE

   /***********************************************************************/
   /*****          CONSTRUCCION DEL REGISTRO TIPO III                 *****/
   /***********************************************************************/

   /* Totales para los campos 4 de los renglones 31 y 32*/
   SELECT @sql = 
	" Select IsNull(Floor(Sum(C29A)),0) As C29A, IsNull(Floor(Sum(C32B)),0) As C32B " +
	" INTO ##TMP_TOTALES_F02" +
	" From " + @tabla +
	" Where CIA='" + @pvc_compania + "'" +
   	Case @control_SUC WHEN '0' THEN "" ELSE " AND COD_SUC='" + @control_SUC + "'" END
   EXECUTE (@sql)

   SELECT @C4_R31 = C29A, @C4_R32 = 0 --C32B Se pone en comentario a raiz de pruebas con Grasco en Jun.09/06 - JCL
   FROM ##TMP_TOTALES_F02

   DROP TABLE ##TMP_TOTALES_F02

   /*******	RENGLON 31	******/
   SELECT @REN_31 = '000313' + Replicate('0', 10) + 
	Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @C4_R31))))) + LTrim(RTrim(Convert(Char(10), @C4_R31)))
   INSERT INTO fco_arp_mm VALUES (999931,@REN_31)

   /*******	RENGLON 32	******/
   SET @nro_aut_in = Space(10)
   SET @vlr_aut_in = 0

   IF @C4_R32 <= 0 
   BEGIN
       IF @control_SUC = '0' 
           SELECT @nro_aut_in = IsNull(A.referencia, ' '), @vlr_aut_in = IsNull(A.valor_1,0)
           FROM fco_ala_datos_adi A, fco_rel_sgss_da B
           WHERE A.sgss = B.sgss 
             AND A.item = B.ala_datos_adi
             AND A.tabla = @tabla
             AND A.cod_adm = 'ARP'
             AND b.secuencia = 1
        ELSE
           SELECT @nro_aut_in = IsNull(A.referencia, ' '), @vlr_aut_in = IsNull(A.valor_1,0)
           FROM fco_ala_datos_adi A, fco_rel_sgss_da B
           WHERE A.sgss = B.sgss 
             AND A.item = B.ala_datos_adi
             AND A.tabla = @tabla
             AND A.cod_adm = 'ARP'
             AND b.secuencia = 1
             AND cod_suc = @control_SUC

       SET @C4_R32 = @vlr_aut_in
   END

   SELECT @REN_32 = '000323' + @nro_aut_in + 
	Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @C4_R32))))) + LTrim(RTrim(Convert(Char(10), @C4_R32)))
   INSERT INTO fco_arp_mm VALUES (999932,@REN_32)

   /*******	RENGLON 34	******/
   SET @vlr_otr_rp = 0

   IF @control_SUC = '0' 
       SELECT @vlr_otr_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 2
    ELSE
       SELECT @vlr_otr_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss 
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 2
         AND cod_suc = @control_SUC

   SELECT @REN_34 = '000343' + Replicate('0', 10) + 
   Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @vlr_otr_rp))))) + LTrim(RTrim(Convert(Char(10), @vlr_otr_rp)))

   INSERT INTO fco_arp_mm VALUES (999934,@REN_34)

   /*******	RENGLON 35	******/
   SELECT @C4_R35 = @C4_R31 - @C4_R32 - @vlr_otr_rp
   SELECT @REN_35 = '000353' + Replicate('0', 10) + 
	Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @C4_R35))))) + LTrim(RTrim(Convert(Char(10), @C4_R35)))
   INSERT INTO fco_arp_mm VALUES (999935,@REN_35)

   /*******	RENGLON 36	******/
   SET @nro_meses = Space(10)
   SET @vlr_mor_rp = 0

   IF @control_SUC = '0' 
       SELECT @nro_meses = IsNull(A.referencia, ' '), @vlr_mor_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss 
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 3
    ELSE
       SELECT @nro_meses = IsNull(A.referencia, ' '), @vlr_mor_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss 
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 3
         AND cod_suc = @control_SUC

   SELECT @REN_36 = '000363' + Replicate('0', 10) + 
   Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @vlr_mor_rp))))) + LTrim(RTrim(Convert(Char(10), @vlr_mor_rp)))
   INSERT INTO fco_arp_mm VALUES (999936,@REN_36)

   /*******	RENGLON 37	******/
   SELECT @C4_R37 = @C4_R35 + @vlr_mor_rp
   SELECT @REN_37 = '000373' + 
        Replicate('0', 10-Len(LTrim(RTrim(@nro_meses)))) + LTrim(RTrim(@nro_meses)) +
	Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @C4_R37))))) + LTrim(RTrim(Convert(Char(10), @C4_R37)))
   INSERT INTO fco_arp_mm VALUES (999937,@REN_37)

   /*******	RENGLON 38	******/
   SET @nro_rad_saf = Space(10)
   SET @vlr_saf_rp = 0

   IF @control_SUC = '0' 
       SELECT @nro_rad_saf = IsNull(A.referencia, ' '), @vlr_saf_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 4
    ELSE
       SELECT @nro_rad_saf = IsNull(A.referencia, ' '), @vlr_saf_rp = IsNull(A.valor_1,0)
       FROM fco_ala_datos_adi A, fco_rel_sgss_da B
       WHERE A.sgss = B.sgss 
         AND A.item = B.ala_datos_adi
         AND A.tabla = @tabla
         AND A.cod_adm = 'ARP'
         AND b.secuencia = 4
         AND cod_suc = @control_SUC

   SELECT @REN_38 = '000383' + @nro_rad_saf + 
   Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @vlr_saf_rp))))) + LTrim(RTrim(Convert(Char(10), @vlr_saf_rp)))

   INSERT INTO fco_arp_mm VALUES (999938,@REN_38)

   /*******	RENGLON 39	******/
   SELECT @C4_R39 = @C4_R37 - @vlr_saf_rp
   SELECT @REN_39 = '000393' + Replicate('0', 10) +
	Replicate('0', 10-Len(LTrim(RTrim(Convert(Char(10), @C4_R39))))) + LTrim(RTrim(Convert(Char(10), @C4_R39)))
   INSERT INTO fco_arp_mm VALUES (999939,@REN_39)

END
GO
GRANT EXECUTE ON dbo.sp_fco_crea_riesgos_mm TO PUBLIC
GO
