if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_MovClientes_Dfc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_MovClientes_Dfc]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_MovClientes_Dfc] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,TimeSys,FecDoc,IdConcepto,Modalidad,Observacion,ModdDev
,TipDoc,Factura,IdCiaDoc,IdLocEnv,'','0','','0',0,0,0,TipCom,Comprobante,IdCiaCom,0,0,Fecha,IdUsuario,0,0,0,0
 FROM Trn_DevFcr
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DevMasivo<=0 AND VrNeto>0 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')