ALTER PROCEDURE [dbo].[sp_remFiniquito_Ins]
(
    @periodo AS int,
	@mes AS int,
	@ficha AS int,
	@numfiniquito AS int,
	@fechaemision AS datetime,
	@fechatermino AS datetime,
	@cargo AS int,
	@idcausal AS Int,
	@comentario AS varchar(500),
	@formato AS Int,
	@cantannos AS int,
	@montoannos AS decimal(18,0),
	@cantmes AS int ,
	@montomes AS decimal(18,0),
	@cantdias AS int,
	@montodias AS decimal(18,0),
	@totaldiastomados AS decimal(18,0),
	@diashabiles AS decimal(18,0),
	@tomados AS decimal(18,0),
	@saldodiashabiles AS decimal(18,0),
	@saldodiasinhabiles AS decimal(18,0),
	@totaldiasvacaportomar AS decimal(18,0),
	@tiposueldo AS int,
	@numdias_mes1 AS int,
	@sueldobase_mes1 AS decimal(18,4),
	@tratos_mes1 AS decimal(18,4),
	@bonos_mes1 AS decimal(18,4),
	@gratificacion_mes1 AS decimal(18,4),
	@colacion_mes1 AS decimal(18,4),
	@movilizacion_mes1 AS decimal(18,4),
	@total_mes1 AS decimal(18,4),
	@numdias_mes2 AS int,
	@sueldobase_mes2 AS decimal(18,4),
	@tratos_mes2 AS decimal(18,4),
	@bonos_mes2 AS decimal(18,4),
	@gratificacion_mes2 AS decimal(18,4),
	@colacion_mes2 AS decimal(18,4),
	@movilizacion_mes2 AS decimal(18,4),
	@total_mes2 AS decimal(18,4),
	@numdias_mes3 AS int,
	@sueldobase_mes3 AS decimal(18,4),
	@tratos_mes3 AS decimal(18,4),
	@bonos_mes3 AS decimal(18,4),
	@gratificacion_mes3 AS decimal(18,4),
	@colacion_mes3 AS decimal(18,4),
	@movilizacion_mes3 AS decimal(18,4),
	@total_mes3 AS decimal(18,4),
	@totalpromdias AS decimal(18,4),
	@valordiasvaca AS decimal(18,4),
	@indem90uf AS decimal(18,4),
	@totalpromvaca AS decimal(18,4),
	@cantvacapropor AS decimal(18,4),
	@montovacapropor AS decimal(18,4),
	@cantsueldomes AS decimal(18,4),
	@montosueldomes AS decimal(18,4),
	@cantindemavisoprevio AS decimal(18,4),
	@montoindemavisoprevio AS decimal(18,4),
	@cantindemannosservicio AS decimal(18,4),
	@montoindemannosservicio AS decimal(18,4),
	@subtotalpago AS decimal(18,4),
	@descAFC AS decimal(18,4),
	@descprestamoCCAF AS decimal(18,4),
	@descotros AS decimal(18,4),
	@subtotaldesc AS decimal(18,4),
	@totalfiniquito AS decimal(18,4),
	@loging AS varchar,
	@fecing AS smalldatetime,
	@logmod AS varchar,
	@fecmod AS smalldatetime
)
AS 

IF exists(SELECT 1 FROM remFiniquito WHERE periodo = @periodo AND mes = @mes AND ficha = @ficha) 
BEGIN

UPDATE remFiniquito
   SET periodo = @periodo,
		mes = @mes,
		ficha = @ficha,
		numfiniquito = @numfiniquito,
		fechaemision = @fechaemision,
		fechatermino = @fechatermino,
		cargo = @cargo,
		idcausal = @idcausal,
		comentario = @comentario,
		formato = @formato,
		cantannos =	@cantannos,
		montoannos = @montoannos,
		cantmes = @cantmes,
		montomes =	@montomes,
		cantdias = @cantdias,
		montodias =	@montodias,
		totaldiastomados =	@totaldiastomados,
		diashabiles =	@diashabiles,
		tomados = @tomados,
		saldodiashabiles = 	@saldodiashabiles,
		saldodiasinhabiles = @saldodiasinhabiles,
		totaldiasvacaportomar =	@totaldiasvacaportomar,
		tiposueldo = @tiposueldo,
		numdias_mes1 =	@numdias_mes1,
		sueldobase_mes1 = @sueldobase_mes1,
		tratos_mes1 = @tratos_mes1,
		bonos_mes1 = @bonos_mes1,
		gratificacion_mes1 = @gratificacion_mes1,
		colacion_mes1 = @colacion_mes1,
		movilizacion_mes1 = @movilizacion_mes1,
		total_mes1 = @total_mes1,
		numdias_mes2 = @numdias_mes2,
		sueldobase_mes2 = @sueldobase_mes2,
		tratos_mes2 = @tratos_mes2,
		bonos_mes2 = @bonos_mes2,
		gratificacion_mes2 = @gratificacion_mes2,
		colacion_mes2 = @colacion_mes2,
		movilizacion_mes2 = @movilizacion_mes2,
		total_mes2 = @total_mes2,
		numdias_mes3 = @numdias_mes3,
		sueldobase_mes3 = @sueldobase_mes3,
		tratos_mes3 = @tratos_mes3,
		bonos_mes3 = @bonos_mes3,
		gratificacion_mes3 = @gratificacion_mes3,
		colacion_mes3 = @colacion_mes3,
		movilizacion_mes3 = @movilizacion_mes3,
		total_mes3 = @total_mes3,
		totalpromdias = @totalpromdias,
		valordiasvaca = @valordiasvaca,
		indem90uf = @indem90uf,
		totalpromvaca = @totalpromvaca,
		cantvacapropor = @cantvacapropor,
		montovacapropor = @montovacapropor,
		cantsueldomes = @cantsueldomes,
		montosueldomes = @montosueldomes,
		cantindemavisoprevio = @cantindemavisoprevio,
		montoindemavisoprevio = @montoindemavisoprevio,
		cantindemannosservicio = @cantindemannosservicio,
		montoindemannosservicio = @montoindemannosservicio,
		subtotalpago = @subtotalpago,
		descAFC = @descAFC,
		descprestamoCCAF = @descprestamoCCAF,
		descotros = @descotros,
		subtotaldesc = @subtotaldesc,
		totalfiniquito = @totalfiniquito,
		loging = @loging,
		fecing = GETDATE(),
		logmod = @logmod,
		fecmod = GETDATE()

		WHERE periodo = @periodo AND mes = @mes AND ficha = @ficha

END ELSE BEGIN

	INSERT INTO remFiniquito
           ([periodo]
		   , [mes]
		   , [ficha]
		   , [numfiniquito]
		   , [fechaemision]
		   , [fechatermino]
		   , [cargo]
		   , [idcausal]
		   , [comentario]
		   , [formato]
		   , [cantannos]
		   , [montoannos]
		   , [cantmes]
		   , [montomes]
		   , [cantdias]
		   , [montodias]
		   , [totaldiastomados]
		   , [diashabiles]
		   , [tomados]
		   , [saldodiashabiles]
		   , [saldodiasinhabiles]
		   , [totaldiasvacaportomar]
		   , [tiposueldo]
		   , [numdias_mes1]
		   , [sueldobase_mes1]
		   , [tratos_mes1]
		   , [bonos_mes1]
		   , [gratificacion_mes1]
		   , [colacion_mes1]
		   , [movilizacion_mes1]
		   , [total_mes1]
		   , [numdias_mes2]
		   , [sueldobase_mes2]
		   , [tratos_mes2]
		   , [bonos_mes2]
		   , [gratificacion_mes2]
		   , [colacion_mes2]
		   , [movilizacion_mes2]
		   , [total_mes2]
		   , [numdias_mes3]
		   , [sueldobase_mes3]
		   , [tratos_mes3]
		   , [bonos_mes3]
		   , [gratificacion_mes3]
		   , [colacion_mes3]
		   , [movilizacion_mes3]
		   , [total_mes3]
		   , [totalpromdias]
		   , [valordiasvaca]
		   , [indem90uf]
		   , [totalpromvaca]
		   , [cantvacapropor]
		   , [montovacapropor]
		   , [cantsueldomes]
		   , [montosueldomes]
		   , [cantindemavisoprevio]
		   , [montoindemavisoprevio]
		   , [cantindemannosservicio]
		   , [montoindemannosservicio]
		   , [subtotalpago]
		   , [descAFC]
		   , [descprestamoCCAF]
		   , [descotros]
		   , [subtotaldesc]
		   , [totalfiniquito]
		   , [loging]
		   , [fecing]
		   , [logmod]
		   , [fecmod])
     VALUES
           (@periodo,
			@mes,
			@ficha,
			@numfiniquito,
			@fechaemision,
			@fechatermino,
			@cargo,
			@idcausal,
			@comentario,
			@formato,
			@cantannos,
			@montoannos,
			@cantmes,
			@montomes,
			@cantdias,
			@montodias,
			@totaldiastomados,
			@diashabiles,
			@tomados,
			@saldodiashabiles,
			@saldodiasinhabiles,
			@totaldiasvacaportomar,
			@tiposueldo,
			@numdias_mes1,
			@sueldobase_mes1,
			@tratos_mes1,
			@bonos_mes1,
			@gratificacion_mes1,
			@colacion_mes1,
			@movilizacion_mes1,
			@total_mes1,
			@numdias_mes2,
			@sueldobase_mes2,
			@tratos_mes2,
			@bonos_mes2,
			@gratificacion_mes2,
			@colacion_mes2,
			@movilizacion_mes2,
			@total_mes2,
			@numdias_mes3,
			@sueldobase_mes3,
			@tratos_mes3,
			@bonos_mes3,
			@gratificacion_mes3,
			@colacion_mes3,
			@movilizacion_mes3,
			@total_mes3,
			@totalpromdias,
			@valordiasvaca,
			@indem90uf,
			@totalpromvaca,
			@cantvacapropor,
			@montovacapropor,
			@cantsueldomes,
			@montosueldomes,
			@cantindemavisoprevio,
			@montoindemavisoprevio,
			@cantindemannosservicio,
			@montoindemannosservicio,
			@subtotalpago,
			@descAFC,
			@descprestamoCCAF,
			@descotros,
			@subtotaldesc,
			@totalfiniquito,
			@loging,
			GETDATE(),
			@logmod,
			GETDATE())

END