if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelNiifComp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paDelNiifComp]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentas_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentas_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasAge]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasAge]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasAge_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasAge_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasAux]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasAux]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasCc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasCc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasCc_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasCc_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasNit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasNit]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasNit_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasNit_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasVeh]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasVeh]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuFisCuentasVeh_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuFisCuentasVeh_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentas_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentas_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasAge]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasAge]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasAge_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasAux]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasAux]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasCc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasCc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasCc_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasCc_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasNit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasNit]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasNit_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasNit_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasVeh]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasVeh]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAcuNiifCuentasVeh_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAfAmtComp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAfAmtComp]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsAfDeprecCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsAfDeprecCom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsComprobantes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsComprobantes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsConceptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsConceptos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsNiifComp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsNiifComp]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsNiifDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsNiifDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsPuc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsPuc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsPuc_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsPuc_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsPucNiif]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsPucNiif]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTiposCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTiposCom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Comp_Niif]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_Comp_Niif]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_NiifCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_NiifCom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_NiifGen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_NiifGen]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_NiifMov]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_NiifMov]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_NiifMov_Acc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_NiifMov_Acc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_NiifMov_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_NiifMov_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryComprobantes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryComprobantes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryComprobantesDoc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryComprobantesDoc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryComprobantesDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryComprobantesDso]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryConceptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryConceptos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryConceptosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryConceptosLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryNiifComp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryNiifComp]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryNiifComp_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryNiifComp_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPuc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPuc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPucAju]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPucAju]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPucNiif]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPucNiif]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPucNiifAju]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPucNiifAju]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposCom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComConsLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposComConsLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComDsa]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposComDsa]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposComDso]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposComLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComNiif]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposComNiif]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_NiifCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_NiifCom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_NiifGen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_NiifGen]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_NiifMov]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_NiifMov]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpComprobantes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpComprobantes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpConceptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpConceptos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpNiifComp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpNiifComp]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpNiifCompAnu]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpNiifCompAnu]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpPuc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpPuc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpPucNiif]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpPucNiif]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTiposCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpTiposCom]
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Comp_Niif] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce
,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo
,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmTipCom,tmComprobante,tmIdCia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut)
 SELECT @pmtmNumero,Item,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,IdVehiculo,D.IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia
,TipCom,Comprobante,IdCia,SUBSTRING(NomCuenta,1,100),SUBSTRING(CCosto,1,100),'',SUBSTRING(RazonSocial,1,150),TipDocRef,DocRef,IdCiaRef,NitDoc,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza
 FROM Trn_NiifDetalle AS D INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 LEFT JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposComLta] @pmEsEgreso BIT=Null,@pmInactivo BIT=Null
 AS 
 SELECT TC.IdCom AS CdCom,TipoCom,TC.IdDiario AS CodDiario,Diario,EsEgreso,ConsUnico
,N.IdCia AS CdCia,Compania,LDesde,LHasta,Numero,NumManual,Formato
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,ConfigFecha
,TC.Inactivo AS Inactvo,ClaseNiif,TC.FechaAdd AS Fec_Add,TC.FechaUpdate AS Fec_Update
 FROM TiposCom AS TC INNER JOIN Diarios AS  D ON TC.IdDiario=D.IdDiario
 LEFT JOIN  TiposComCons  AS N ON TC.IdCom=N.IdCom
 LEFT JOIN Companias AS C ON N.IdCia=C.IdCia 
 WHERE (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 AND  (TC.Inactivo=ISNULL(@pmInactivo,0) or TC.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY TipoCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposComConsLta] @pmIdCom VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT N.IdCom AS CdTipo,TipoCom,N.IdCia AS CdCia,Compania,LDesde,LHasta,Numero,NumManual,Formato
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,ConfigFecha,EsEgreso,ConsUnico
,T.IdDiario AS CdDiario,Diario,ClaseNiif,N.FechaAdd AS Fec_Add,N.FechaUpdate AS Fec_Update
 FROM TiposComCons AS N INNER JOIN TiposCom AS T ON N.IdCom=T.IdCom
 INNER JOIN Diarios AS D ON T.IdDiario=D.IdDiario INNER JOIN Companias AS C ON N.IdCia=C.IdCia
 WHERE T.Inactivo=0  AND N.IdCom LIKE ISNULL(@pmIdCom,'%') AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY N.IdCom,N.IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifComp_Cr] @pmTipCom VARCHAR(3),@pmComprobanteIni INT,@pmComprobanteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipCom AS IdTipoComp,TipoCom,C.Comprobante AS NumComprob,C.IdCia AS CdCia,Compania,C.Fecha AS FechaComp
,C.IdTercero AS ComNitTercero,T.RazonSocial AS ComNomTercero,VrTotal,C.IdCta AS IdCtaCte,NumeroCta,CTA.IdBanco AS CdBanco,Banco
,EnEfectivo,C.NumCheque AS ComNumCheque,FecCheque,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,C.VehPropio AS VehEsPropio
,CedCondtor,CD.RazonSocial AS NomConductor,C.CodConce AS CodConcepto,CN.Concepto,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3
,PermEditar,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TipoNiif
,Propddes,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut
,CtbTipo,CtbNumero,FisTipo,FisNumero
--detalle
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo,D.VehPropio AS DetVehPropio
,D.IdCCosto AS CdCentCosto,CCosto,D.IdSubCos AS CdSubCentro,SubCosto,VrBase,TarifaBase,D.TipDoc AS TipoDoc,D.Documento AS NumDoc,D.IdCiaDoc AS CdCiaDoc
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,D.NumCheque AS DetNumCheque,TipoAplica,Consolida,CodCargo
,NitOtros,DO.RazonSocial AS NombreOtros,CodSubgpo,Subgrupo,CiuOrigen,DL.Localidad AS NomCiudadOrigen,CodAgncia,DA.Agencia AS NomAgencia,CodAgencia,Referencia
,Referncia,NitDoc,DND.RazonSocial AS NombreNitDoc,TipDocRef,DocRef,IdCiaRef,CdConcTrib,TB.Concepto AS DescConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CodCuentaCG
--informacion del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_NiifComp AS C INNER JOIN Trn_NiifDetalle AS D ON C.TipCom=D.TipCom AND C.Comprobante=D.Comprobante AND C.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero  LEFT JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 LEFT JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Terceros AS CD ON C.CedCondtor=CD.IdTercero LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS DO ON D.NitOtros=DO.IdTercero
 LEFT JOIN SubGrupos AS S ON D.CodSubgpo=S.IdSubgrupo
 LEFT JOIN Localidades AS DL ON D.CiuOrigen=DL.IdLocal
 LEFT JOIN Agencias AS DA ON D.CodAgncia=DA.IdAgencia
 LEFT JOIN Terceros AS DND ON D.NitDoc=DND.IdTercero LEFT JOIN ConceptosTrib AS TB ON D.CdConcTrib=TB.IdConcepto
 WHERE C.TipCom=@pmTipCom AND C.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifMov] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT C.IdCuenta AS CdCuenta,NomCuenta,TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Item,Fecha,Detalle,VrDebito,VrCredito,C.IdTercero AS NitTercero,T.RazonSocial AS RaznSocial
,IdVehiculo,C.IdCCosto AS CdCenCosto,CCosto,C.IdSubCos AS CdSubcentro,SubCosto,VrBase,TarifaBase,TipDoc,TipoDoc,Documento,IdCiaDoc,CodConce,Concepto
,NitDoc,ND.RazonSocial AS NomNitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumeroCta,CTE.IdBanco AS CodBanco,Banco
,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,NIO.RazonSocial AS NomNitOtros,CodSubgpo,Subgrupo,CiuOrigen,LD.Localidad AS NomCiudad
,CodAgncia,A.Agencia AS NomAgencia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG,VrSanCue,VrSanCueCia,VrSanNit,VrSanNitCia,VrSanVeh,VrSanVehCia
,VrSanCc,VrSanCcCia,VrSanCcSub,VrSanCcSubCia,VrSanNitCc,VrSanNitCcCia,VrSanNitCcSub,VrSanNitCcSubCia,VrSanNitAge,VrSanNitAgeCia
,VrSanNitAgeCc,VrSanNitAgeCcCia,VrSanNitAgeCcSub,VrSanNitAgeCcSubCia,VrSanNitVeh
,VrSanNitVehCia,VrSanNitVehCc,VrSanNitVehCcCia,VrSanVehCc,VrSanVehCcCia,VrSanVehCcSub,VrSanVehCcSubCia,tmEst
,TC.IdDiario AS CdDiario,Diario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono,T.TipEnte AS TercTipEnte
,CodAgencia,DirAgncia,Referencia
 FROM tm_NiifMov AS C INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN PucNiif AS P ON C.IdCuenta=P.IdCuenta
 INNER JOIN CentroCosto AS O ON C.IdCCosto=O.IdCCosto
 LEFT JOIN TiposCom AS TC ON C.TipCom=TC.IdCom LEFT JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 LEFT JOIN Companias AS CI ON C.IdCia=CI.IdCia
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NIO ON C.NitOtros=NIO.IdTercero
 LEFT JOIN Terceros AS ND ON C.NitDoc=ND.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN Localidades AS LD ON C.CiuOrigen=LD.IdLocal
 LEFT JOIN SubGrupos AS SG ON  C.CodSubgpo=SG.IdSubgrupo
 LEFT JOIN Agencias AS A ON C.CodAgncia=A.IdAgencia
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTE ON C.CodCta=CTE.IdCta LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 WHERE tmEst=@pmtmEst AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND C.IdTercero LIKE ISNULL(@pmIdTercero ,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 ORDER BY C.IdCuenta,Fecha,C.IdCia,TipCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComprobantesDso] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME, @pmTipCom VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmAnulado BIT=Null,@pmIntegrado BIT=Null,@pmEsEgreso BIT=Null,@pmAnticipo BIT=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT TipCom,Comprobante,C.IdCia AS CodCia,Compania,Fecha,C.IdTercero AS NitTercero,RazonSocial,VrTotal,C.IdCta AS CodCta,NumeroCta,Tipo_Pago,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS IdEstad,Estado
,NiifTipo,NiifNumero,FisTipo,FisNumero,Propddes,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,CdSustento,Establec,PEmision,Autorizac,FechaAut
 FROM Trn_Comprobantes AS C INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipCom LIKE ISNULL(@pmTipCom,'%') AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1)) 
AND (Anticipo=ISNULL(@pmAnticipo,0) or Anticipo=ISNULL(@pmAnticipo,1)) 
 ORDER BY TipCom,C.IdCia,Comprobante
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifCom] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCia,Compania,BC.tmIdCuenta AS IdCuenta,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo,tmSaldoAnterior2,tmTotalDebitos2,tmTotalCreditos2
,tmSaldoAnterior2+tmTotalDebitos2-tmTotalCreditos2 AS NuevoSaldo2
,tmIdTercero,RazonSocial,tmCodAgncia,A.Agencia AS NomAgencia,tmIdVehiculo,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
--información del tercero
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono
,EsAccnista,EsCliente,EsPropietario,CodAgencia,Referencia 
,tmItem,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,P.Tercero,P.CentroCosto
 FROM tm_NiifCom AS BC INNER JOIN PucNiif AS P ON BC.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON BC.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON BC.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON BC.tmIdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON BC.tmIdCuenta=NV.tmIdCuenta
 LEFT JOIN Agencias AS A ON BC.tmCodAgncia=A.IdAgencia
 LEFT JOIN Companias AS CN ON BC.tmIdCia=CN.IdCia
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifGen] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCia,Compania,BG.tmIdCuenta AS CdCuenta,NomCuenta,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,tmEst
 FROM tm_NiifGen AS BG INNER JOIN PucNiif AS P ON BG.tmIdCuenta=P.IdCuenta
 LEFT JOIN Companias AS CI ON BG.tmIdCia=CI.IdCia
 LEFT JOIN tm_NivCue AS NV ON BG.tmIdCuenta=NV.tmIdCuenta
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptosLta] @pmIdDoc VARCHAR(3)=Null,@pmIdCom VARCHAR(3)=Null,@pmPdterminado INT=Null
,@pmInactivo  BIT=Null
 AS 
 SELECT IdConcepto,Concepto,C.IdDoc AS CdTipo,TipoDoc,C.IdCom AS CdTcmp,TipoCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado
,TrfaSanc,RndTotal,DcmtoEquiv,TipoContab,CdComNiif,CdComFiscal,C.FechaAdd AS Fec_Add,C.FechaUpdate AS Fec_Upd,C.IdUsuario AS IdUsuari,Usuario,C.Inactivo AS Inactiv
 FROM Conceptos AS C INNER JOIN Sys_TiposDoc AS T ON C.IdDoc=T.IdDoc
 INNER JOIN TiposCom AS TC ON C.IdCom=TC.IdCom
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE C.IdDoc LIKE ISNULL(@pmIdDoc,'%') AND C.IdCom LIKE ISNULL(@pmIdCom,'%')
 AND (Pdterminado>=ISNULL(@pmPdterminado,-1)  AND Pdterminado<=ISNULL(@pmPdterminado,2147483647))
 AND  (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY C.IdDoc,Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposComDsa] @pmEsEgreso BIT
 AS
 SELECT   IdCom,TipoCom +'  ' + IdCom  AS DsTip  FROM  TiposCom 
 WHERE  Inactivo=0 AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 AND (ISNULL(ClaseNiif,'')='CONTAB' OR LEN(ISNULL(ClaseNiif,''))=0)
 UNION
 SELECT   IdDoc AS IdCom,TipoDoc+ ' '+ IdDoc AS DsTip   FROM   Sys_TiposDoc
 WHERE  IdDoc IN ('DVE','DEI','DOP','DEC','DVO','DRV','DRR') AND Inactivo=0
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_Sel]  @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 --nit-veh
  INSERT INTO AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo,'NV',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Tercero=1 AND P.Vehiculo=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Sel]  @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 --nit-veh
  INSERT INTO AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo,'NV',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Tercero=1 AND P.Vehiculo=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAux] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmnReg INT,@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmSubAuxiliar VARCHAR(16),@pmTipoAcum VARCHAR(4),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY,@pmTipoAfiVehic INT
 AS 
 INSERT INTO AcuFisCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoAfiVehic)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifCom] @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmIdTercero VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16)
,@pmtmIdSubCos VARCHAR(16),@pmtmCodAgncia VARCHAR(16),@pmtmTipo VARCHAR(4),@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY
,@pmtmSaldoAnterior2 MONEY,@pmtmTotalDebitos2 MONEY,@pmtmTotalCreditos2 MONEY,@pmtmNuevoSaldo2 MONEY
 AS 
 INSERT INTO tm_NiifCom (tmEst,tmIdCuenta,tmIdCia,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmNuevoSaldo,tmSaldoAnterior2,tmTotalDebitos2,tmTotalCreditos2,tmNuevoSaldo2)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmIdCia,@pmtmItem,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos
,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSaldoAnterior2,@pmtmTotalDebitos2,@pmtmTotalCreditos2,@pmtmNuevoSaldo2)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPuc_Sel] @pmIdCuenta VARCHAR(16),@pmNewCuenta VARCHAR(16)
AS 
 INSERT INTO Puc (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif)
 SELECT @pmNewCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
 FROM Puc WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPucAju] @pmIdCuenta VARCHAR(16)=Null,@pmCueIni VARCHAR(16)=Null
,@pmCueFin VARCHAR(16)=Null
AS
 IF @pmCueIni IS NULL --NO RANGOS
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
 ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos,IdCueNiif
  FROM  Puc
  WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta like ISNULL(@pmIdCuenta,'%')
  ORDER BY IdCuenta
 ELSE
  BEGIN
  IF @pmCueIni='LIKE'
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos,IdCueNiif
   FROM  Puc
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta LIKE @pmIdCuenta
   ORDER BY IdCuenta
  ELSE
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos,IdCueNiif
   FROM  Puc
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta BETWEEN @pmCueIni AND @pmCueFin
   ORDER BY IdCuenta
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPuc] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(100),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmSubCentro BIT,@pmVehiculo BIT,@pmCentroCosto BIT,@pmVencimiento BIT
,@pmBaseGravable BIT,@pmPorcntaje DECIMAL(10,4),@pmNaturaleza CHAR(1),@pmAjustable BIT,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferncias BIT
,@pmAgencia BIT,@pmFmtDian INT,@pmConDian INT,@pmRenDec INT,@pmCdTipDoc VARCHAR(3),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmInactivo BIT,@pmAfijos INT
,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmIdCueNiif VARCHAR(16)
 AS 
 INSERT INTO Puc (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif)
 VALUES (@pmIdCuenta,@pmNomCuenta,@pmMayor,@pmMovimiento,@pmTercero,@pmSubCentro,@pmVehiculo,@pmCentroCosto,@pmVencimiento,@pmBaseGravable,@pmPorcntaje,@pmNaturaleza,@pmAjustable,@pmCuentaAjuDeb,@pmCuentaAjuCre
,@pmCheques,@pmReferncias,@pmAgencia,@pmFmtDian,@pmConDian,@pmRenDec,@pmCdTipDoc,@pmFechaAdd,@pmIdUsuario,@pmInactivo,@pmAfijos,@pmListarConc,@pmReqAutorizac,@pmAntProveedor,@pmIdCueNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpPuc] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(100),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmSubCentro BIT,@pmVehiculo BIT,@pmCentroCosto BIT,@pmVencimiento BIT,@pmBaseGravable BIT
,@pmPorcntaje DECIMAL(10,4),@pmNaturaleza CHAR(1),@pmAjustable BIT,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferncias BIT
,@pmAgencia BIT,@pmFmtDian INT,@pmConDian INT,@pmRenDec INT,@pmCdTipDoc VARCHAR(3),@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT,@pmAfijos INT,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmIdCueNiif VARCHAR(16)
 AS 
 UPDATE Puc SET NomCuenta=@pmNomCuenta,Mayor=@pmMayor,Movimiento=@pmMovimiento,Tercero=@pmTercero,SubCentro=@pmSubCentro,Vehiculo=@pmVehiculo,CentroCosto=@pmCentroCosto,Vencimiento=@pmVencimiento,BaseGravable=@pmBaseGravable
,Porcntaje=@pmPorcntaje,Naturaleza=@pmNaturaleza,Ajustable=@pmAjustable,CuentaAjuDeb=@pmCuentaAjuDeb,CuentaAjuCre=@pmCuentaAjuCre,Cheques=@pmCheques,Referncias=@pmReferncias,Agencia=@pmAgencia
,FmtDian=@pmFmtDian,ConDian=@pmConDian,RenDec=@pmRenDec,CdTipDoc=@pmCdTipDoc,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo,Afijos=@pmAfijos,ListarConc=@pmListarConc,ReqAutorizac=@pmReqAutorizac,AntProveedor=@pmAntProveedor,IdCueNiif=@pmIdCueNiif 
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuc] @pmIdCuenta VARCHAR(16)
 AS 
 IF @pmIdCuenta IS NULL
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,FechaAdd,FechaUpdate,IdUsuario
,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
  FROM Puc ORDER BY IdCuenta
 ELSE
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,FechaAdd,FechaUpdate,IdUsuario
,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
  FROM Puc
  WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Tercero=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Tercero=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'AcuNiifCuentasCc'
  BEGIN TRANSACTION @TrnNombre
  INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,'-1','CC',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.CentroCosto=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto
 --centro de costo-subcentro
  INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos,'CS',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND CentroCosto=1 AND SubCentro=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNiifDetalle] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmVrBase MONEY,@pmTarifaBase DECIMAL(14,4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4),@pmNitDoc VARCHAR(16)
,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME,@pmCodCta VARCHAR(4),@pmNumCheque VARCHAR(20),@pmIntegrado BIT,@pmTipoAplica CHAR(1),@pmConsolida BIT,@pmCodCargo VARCHAR(4),@pmNitOtros VARCHAR(16),@pmCodSubgpo VARCHAR(8),@pmCiuOrigen VARCHAR(8),@pmCodAgncia VARCHAR(16),@pmVehPropio INT,@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmTimeSys SMALLDATETIME
,@pmFecUpdate SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11),@pmCdConcTrib VARCHAR(4),@pmCdTarifTrib VARCHAR(4),@pmNumEstablec VARCHAR(20),@pmPtoEmision VARCHAR(20),@pmNum_Autoriza VARCHAR(50),@pmFechAutoriza SMALLDATETIME,@pmCodCuentaCG VARCHAR(16)
 AS 
 INSERT INTO Trn_NiifDetalle (TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CodCuentaCG)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmIdVehiculo,@pmIdCCosto,@pmIdSubCos,@pmVrBase,@pmTarifaBase,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmCodConce,@pmNitDoc,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCodCta,@pmNumCheque,@pmIntegrado,@pmTipoAplica,@pmConsolida,@pmCodCargo,@pmNitOtros,@pmCodSubgpo,@pmCiuOrigen,@pmCodAgncia,@pmVehPropio,@pmReferncia
,@pmTipDocRef,@pmDocRef,@pmIdCiaRef,@pmTimeSys,@pmFecUpdate,@pmIdCiaCrea,@pmIdUsuario,@pmCdConcTrib,@pmCdTarifTrib,@pmNumEstablec,@pmPtoEmision,@pmNum_Autoriza,@pmFechAutoriza,@pmCodCuentaCG)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'AcuFisCuentasCc'
  BEGIN TRANSACTION @TrnNombre
  INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,'-1','CC',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.CentroCosto=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto
 --centro de costo-subcentro
  INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos,'CS',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND CentroCosto=1 AND SubCentro=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov_Sel] @pmtmEst CHAR(2),@pmTipoNiif VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdSubCos VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null
 AS 
 IF @pmCueIni IS NULL
  INSERT INTO tm_NiifMov (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,D.Item,D.Fecha,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,VrBase,TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,D.CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_NiifMov (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,Item,D.Fecha,D.IdCuenta,Detalle,D.VrDebito,D.VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,D.VrBase,D.TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,D.FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCuenta BETWEEN @pmCueIni AND @pmCueFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov_Acc] @pmtmEst CHAR(2),@pmTipoNiif VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdSubCos VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null
 AS 
 IF @pmCueIni IS NULL
  INSERT INTO tm_NiifMov (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,D.Item,D.Fecha,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,VrBase,TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,D.CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND EsAccnista<>0 AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_NiifMov (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,Item,D.Fecha,D.IdCuenta,Detalle,D.VrDebito,D.VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,D.VrBase,D.TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,D.FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
   INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCuenta BETWEEN @pmCueIni AND @pmCueFin AND EsAccnista<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdVehiculo,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Vehiculo=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdVehiculo,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Vehiculo=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAux] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmnReg INT,@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmSubAuxiliar VARCHAR(16),@pmTipoAcum VARCHAR(4),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY,@pmTipoAfiVehic INT
 AS 
 INSERT INTO AcuNiifCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoAfiVehic)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos
,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAfDeprecCom] @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmTipComAju VARCHAR(3),@pmCompAjustes INT
,@pmTipoCompNiif VARCHAR(3),@pmNumCompNiif INT,@pmTipoCompFis VARCHAR(3),@pmNumCompFis INT
 AS 
 INSERT INTO Trn_AfDeprecCom (Numero,IdCia,TipCom,Comprobante,TipComAju,CompAjustes,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante,@pmTipComAju,@pmCompAjustes,@pmTipoCompNiif,@pmNumCompNiif,@pmTipoCompFis,@pmNumCompFis)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov] @pmtmEst CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmVrBase MONEY,@pmTarifaBase DECIMAL(14,4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4),@pmNitDoc VARCHAR(16),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME,@pmCodCta VARCHAR(4)
,@pmNumCheque VARCHAR(20),@pmIntegrado BIT,@pmTipoAplica CHAR(1),@pmConsolida BIT,@pmCodCargo VARCHAR(4),@pmNitOtros VARCHAR(16),@pmCodSubgpo VARCHAR(8),@pmCiuOrigen VARCHAR(8),@pmCodAgncia VARCHAR(16),@pmVehPropio INT,@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
,@pmVrSanCue MONEY,@pmVrSanCueCia MONEY,@pmVrSanNit MONEY,@pmVrSanNitCia MONEY,@pmVrSanVeh MONEY,@pmVrSanVehCia MONEY,@pmVrSanCc MONEY,@pmVrSanCcCia MONEY,@pmVrSanCcSub MONEY,@pmVrSanCcSubCia MONEY,@pmVrSanNitCc MONEY,@pmVrSanNitCcCia MONEY,@pmVrSanNitCcSub MONEY,@pmVrSanNitCcSubCia MONEY
,@pmVrSanNitAge MONEY,@pmVrSanNitAgeCia MONEY,@pmVrSanNitAgeCc MONEY,@pmVrSanNitAgeCcCia MONEY,@pmVrSanNitAgeCcSub MONEY,@pmVrSanNitAgeCcSubCia MONEY,@pmVrSanNitVeh MONEY,@pmVrSanNitVehCia MONEY,@pmVrSanNitVehCc MONEY,@pmVrSanNitVehCcCia MONEY,@pmVrSanVehCc MONEY,@pmVrSanVehCcCia MONEY,@pmVrSanVehCcSub MONEY,@pmVrSanVehCcSubCia MONEY,@pmCodCuentaCG VARCHAR(16)
 AS 
 INSERT INTO tm_NiifMov (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen
,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,VrSanCue,VrSanCueCia,VrSanNit,VrSanNitCia,VrSanVeh,VrSanVehCia,VrSanCc,VrSanCcCia,VrSanCcSub,VrSanCcSubCia,VrSanNitCc,VrSanNitCcCia,VrSanNitCcSub,VrSanNitCcSubCia,VrSanNitAge,VrSanNitAgeCia,VrSanNitAgeCc,VrSanNitAgeCcCia,VrSanNitAgeCcSub,VrSanNitAgeCcSubCia,VrSanNitVeh,VrSanNitVehCia,VrSanNitVehCc,VrSanNitVehCcCia,VrSanVehCc,VrSanVehCcCia,VrSanVehCcSub,VrSanVehCcSubCia,CodCuentaCG)
 VALUES (@pmtmEst,@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmIdVehiculo,@pmIdCCosto,@pmIdSubCos,@pmVrBase,@pmTarifaBase,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmCodConce,@pmNitDoc,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCodCta,@pmNumCheque
,@pmIntegrado,@pmTipoAplica,@pmConsolida,@pmCodCargo,@pmNitOtros,@pmCodSubgpo,@pmCiuOrigen,@pmCodAgncia,@pmVehPropio,@pmReferncia,@pmTipDocRef,@pmDocRef,@pmIdCiaRef,@pmVrSanCue,@pmVrSanCueCia,@pmVrSanNit,@pmVrSanNitCia,@pmVrSanVeh,@pmVrSanVehCia,@pmVrSanCc,@pmVrSanCcCia,@pmVrSanCcSub,@pmVrSanCcSubCia,@pmVrSanNitCc,@pmVrSanNitCcCia,@pmVrSanNitCcSub,@pmVrSanNitCcSubCia,@pmVrSanNitAge,@pmVrSanNitAgeCia,@pmVrSanNitAgeCc,@pmVrSanNitAgeCcCia
,@pmVrSanNitAgeCcSub,@pmVrSanNitAgeCcSubCia,@pmVrSanNitVeh,@pmVrSanNitVehCia,@pmVrSanNitVehCc,@pmVrSanNitVehCcCia,@pmVrSanVehCc,@pmVrSanVehCcCia,@pmVrSanVehCcSub,@pmVrSanVehCcSubCia,@pmCodCuentaCG)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAfAmtComp] @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmTipoCompNiif VARCHAR(3),@pmNumCompNiif INT,@pmTipoCompFis VARCHAR(3),@pmNumCompFis INT
 AS 
 INSERT INTO Trn_AfAmtComp (Numero,IdCia,TipCom,Comprobante,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante,@pmTipoCompNiif,@pmNumCompNiif,@pmTipoCompFis,@pmNumCompFis)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos
,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifGen] @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2),@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY
,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 INSERT INTO tm_NiifGen (tmEst,tmIdCuenta,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmIdCia,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSalAnt2,@pmtmDebitos2,@pmtmCreditos2,@pmtmSaldo2)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposCom] @pmIdCom VARCHAR(3)
 AS 
 SELECT IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd,FechaUpdate,ClaseNiif
 FROM TiposCom
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCom] @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmClaseNiif VARCHAR(10),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposCom (IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd,ClaseNiif)
 VALUES (@pmIdCom,@pmTipoCom,@pmIdDiario,@pmEsEgreso,@pmConsUnico,@pmInactivo,@pmFechaAdd,@pmClaseNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCom] @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmClaseNiif VARCHAR(10),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposCom SET TipoCom=@pmTipoCom,IdDiario=@pmIdDiario,EsEgreso=@pmEsEgreso
,ConsUnico=@pmConsUnico,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate,ClaseNiif=@pmClaseNiif
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComDso] @pmEsEgreso BIT
AS
 SELECT IdCom,TipoCom,IdCom +'  ' + TipoCom  AS DsTip
 FROM TiposCom 
 WHERE Inactivo=0 AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 AND (ISNULL(ClaseNiif,'')='CONTAB' OR LEN(ISNULL(ClaseNiif,''))=0) 
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComNiif] 
AS
 SELECT IdCom,TipoCom,IdCom + ' '+TipoCom+' ('+ISNULL(ClaseNiif,'')+')' AS DsTip
 FROM TiposCom 
 WHERE ISNULL(ClaseNiif,'') IN ('NIIF','FISCAL') AND Inactivo=0
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComprobantesDoc]  @pmTipDoc VARCHAR(3), @pmDocumento INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,Observacion,IdEstado
,Propddes,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero
 FROM Trn_Comprobantes
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento  AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY  TipCom,IdCia, Comprobante
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpComprobantes] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(150),@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY,@pmVrDivisa3 MONEY,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME,@pmNiifTipo VARCHAR(3),@pmNiifNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmPropddes VARCHAR(50),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Comprobantes SET Fecha=@pmFecha,IdTercero=@pmIdTercero,VrTotal=@pmVrTotal,IdCta=@pmIdCta,EnEfectivo=@pmEnEfectivo,NumCheque=@pmNumCheque,FecCheque=@pmFecCheque,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,TipEgr=@pmTipEgr,NEgreso=@pmNEgreso,pVehiculo=@pmpVehiculo,VehPropio=@pmVehPropio
,CedCondtor=@pmCedCondtor,CodConce=@pmCodConce,Beneficiario=@pmBeneficiario,PermEditar=@pmPermEditar,Integrado=@pmIntegrado,EsEgreso=@pmEsEgreso,Anticipo=@pmAnticipo,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Propddes=@pmPropddes
,VrDivisa1=@pmVrDivisa1,VrDivisa2=@pmVrDivisa2,VrDivisa3=@pmVrDivisa3,FecUpdate=@pmFecUpdate,CdSustento=@pmCdSustento,Tipo_Pago=@pmTipo_Pago,Establec=@pmEstablec,PEmision=@pmPEmision,Autorizac=@pmAutorizac,FechaAut=@pmFechaAut,NiifTipo=@pmNiifTipo,NiifNumero=@pmNiifNumero,FisTipo=@pmFisTipo,FisNumero=@pmFisNumero
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsComprobantes] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(150),@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY,@pmVrDivisa3 MONEY, @pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME,@pmNiifTipo VARCHAR(3),@pmNiifNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmPropddes VARCHAR(50),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Comprobantes (TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,PermEditar,Integrado,EsEgreso,Anticipo,VrDivisa1,VrDivisa2,VrDivisa3,Observacion,IdEstado,Propddes
,TimeSys,IdCiaCrea,IdUsuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmFecha,@pmIdTercero,@pmVrTotal,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmFecCheque,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipEgr,@pmNEgreso,@pmpVehiculo,@pmVehPropio,@pmCedCondtor,@pmCodConce,@pmBeneficiario,@pmPermEditar,@pmIntegrado,@pmEsEgreso
,@pmAnticipo,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmObservacion,@pmIdEstado,@pmPropddes,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCdSustento,@pmTipo_Pago,@pmEstablec,@pmPEmision,@pmAutorizac,@pmFechaAut,@pmNiifTipo,@pmNiifNumero,@pmFisTipo,@pmFisNumero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComprobantes] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,Observacion
,IdEstado,Propddes,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero
 FROM Trn_Comprobantes
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNiifComp] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(250),@pmVrDivisa1 DECIMAL(16,6),@pmVrDivisa2 DECIMAL(16,6),@pmVrDivisa3 DECIMAL(16,6)
,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipoNiif VARCHAR(10),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME
,@pmCtbTipo VARCHAR(3),@pmCtbNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmPropddes VARCHAR(50),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NiifComp (TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso
,Anticipo,Observacion,IdEstado,Propddes,TimeSys,IdCiaCrea,IdUsuario,TipoNiif,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,CtbTipo,CtbNumero,FisTipo,FisNumero)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmFecha,@pmIdTercero,@pmVrTotal,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmFecCheque,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipEgr,@pmNEgreso,@pmpVehiculo,@pmVehPropio,@pmCedCondtor,@pmCodConce,@pmBeneficiario
,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmPermEditar,@pmIntegrado,@pmEsEgreso,@pmAnticipo,@pmObservacion,@pmIdEstado,@pmPropddes,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoNiif,@pmCdSustento,@pmTipo_Pago,@pmEstablec,@pmPEmision,@pmAutorizac,@pmFechaAut,@pmCtbTipo,@pmCtbNumero,@pmFisTipo,@pmFisNumero)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNiifComp] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(250),@pmVrDivisa1 DECIMAL(16,6),@pmVrDivisa2 DECIMAL(16,6),@pmVrDivisa3 DECIMAL(16,6)
,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipoNiif VARCHAR(10),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME
,@pmCtbTipo VARCHAR(3),@pmCtbNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NiifComp SET Fecha=@pmFecha,IdTercero=@pmIdTercero,VrTotal=@pmVrTotal,IdCta=@pmIdCta,EnEfectivo=@pmEnEfectivo,NumCheque=@pmNumCheque,FecCheque=@pmFecCheque,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,TipEgr=@pmTipEgr,NEgreso=@pmNEgreso
,pVehiculo=@pmpVehiculo,VehPropio=@pmVehPropio,CedCondtor=@pmCedCondtor,CodConce=@pmCodConce,Beneficiario=@pmBeneficiario,VrDivisa1=@pmVrDivisa1,VrDivisa2=@pmVrDivisa2,VrDivisa3=@pmVrDivisa3,PermEditar=@pmPermEditar,Integrado=@pmIntegrado,EsEgreso=@pmEsEgreso,Anticipo=@pmAnticipo,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipoNiif=@pmTipoNiif,CdSustento=@pmCdSustento,Tipo_Pago=@pmTipo_Pago,Establec=@pmEstablec,PEmision=@pmPEmision,Autorizac=@pmAutorizac,FechaAut=@pmFechaAut,CtbTipo=@pmCtbTipo,CtbNumero=@pmCtbNumero,FisTipo=@pmFisTipo,FisNumero=@pmFisNumero
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNiifComp] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,Observacion,IdEstado
,Propddes,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,TipoNiif,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,CtbTipo,CtbNumero,FisTipo,FisNumero
 FROM Trn_NiifComp
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNiifComp] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_NiifComp
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpNiifCompAnu] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2), @pmAnulado BIT, @pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmPermEditar BIT,@pmObservacion VARCHAR(1000)
AS
 UPDATE Trn_NiifComp SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,IdEstado=@pmIdEstado
,Observacion=@pmObservacion,PermEditar=@pmPermEditar
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdVehiculo,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpConceptos] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmIdDoc VARCHAR(3),@pmIdCom VARCHAR(3),@pmVcmto BIT,@pmImptos BIT,@pmRetfte BIT
,@pmRetIca BIT,@pmObservacion VARCHAR(150),@pmPdterminado INT,@pmTrfaSanc DECIMAL(14,4),@pmRndTotal INT,@pmDcmtoEquiv BIT,@pmInactivo BIT
,@pmTipoContab VARCHAR(10),@pmCdComNiif VARCHAR(3),@pmCdComFiscal VARCHAR(3),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Conceptos SET Concepto=@pmConcepto,IdDoc=@pmIdDoc,IdCom=@pmIdCom,Vcmto=@pmVcmto,Imptos=@pmImptos,Retfte=@pmRetfte,RetIca=@pmRetIca,Observacion=@pmObservacion
,Pdterminado=@pmPdterminado,TrfaSanc=@pmTrfaSanc,RndTotal=@pmRndTotal,Inactivo=@pmInactivo,DcmtoEquiv=@pmDcmtoEquiv
,TipoContab=@pmTipoContab,CdComNiif=@pmCdComNiif,CdComFiscal=@pmCdComFiscal,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptos] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,IdDoc,IdCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado
,TrfaSanc,RndTotal,Inactivo,DcmtoEquiv,TipoContab,CdComNiif,CdComFiscal,FechaAdd,FechaUpdate,IdUsuario
 FROM Conceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsConceptos] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmIdDoc VARCHAR(3),@pmIdCom VARCHAR(3),@pmVcmto BIT,@pmImptos BIT
,@pmRetfte BIT,@pmRetIca BIT,@pmObservacion VARCHAR(150),@pmPdterminado INT,@pmTrfaSanc DECIMAL(14,4),@pmRndTotal INT,@pmDcmtoEquiv BIT
,@pmInactivo BIT,@pmTipoContab VARCHAR(10),@pmCdComNiif VARCHAR(3),@pmCdComFiscal VARCHAR(3),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Conceptos (IdConcepto,Concepto,IdDoc,IdCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado,TrfaSanc,RndTotal,DcmtoEquiv
,Inactivo,FechaAdd,IdUsuario,TipoContab,CdComNiif,CdComFiscal)
 VALUES (@pmIdConcepto,@pmConcepto,@pmIdDoc,@pmIdCom,@pmVcmto,@pmImptos,@pmRetfte,@pmRetIca,@pmObservacion,@pmPdterminado,@pmTrfaSanc
,@pmRndTotal,@pmDcmtoEquiv,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmTipoContab,@pmCdComNiif,@pmCdComFiscal)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdVehiculo,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsPucNiif] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(250),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmVehiculo BIT,@pmAgencia BIT,@pmCentroCosto BIT,@pmSubCentro BIT,@pmVencimiento BIT,@pmBaseGravable BIT,@pmTarifaPred DECIMAL(14,4),@pmNaturaleza CHAR(1),@pmAjustable BIT
,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferencias BIT,@pmCdTipDoc VARCHAR(3),@pmInactivo BIT,@pmAfijos INT,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO PucNiif (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias,CdTipDoc,FechaCrea,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor)
 VALUES (@pmIdCuenta,@pmNomCuenta,@pmMayor,@pmMovimiento,@pmTercero,@pmVehiculo,@pmAgencia,@pmCentroCosto,@pmSubCentro,@pmVencimiento,@pmBaseGravable,@pmTarifaPred,@pmNaturaleza,@pmAjustable,@pmCuentaAjuDeb,@pmCuentaAjuCre,@pmCheques,@pmReferencias,@pmCdTipDoc,@pmFechaCrea,@pmIdUsuario,@pmInactivo,@pmAfijos,@pmListarConc,@pmReqAutorizac,@pmAntProveedor)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpPucNiif] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(250),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmVehiculo BIT,@pmAgencia BIT,@pmCentroCosto BIT,@pmSubCentro BIT,@pmVencimiento BIT,@pmBaseGravable BIT,@pmTarifaPred DECIMAL(14,4),@pmNaturaleza CHAR(1),@pmAjustable BIT
,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferencias BIT,@pmCdTipDoc VARCHAR(3),@pmInactivo BIT,@pmAfijos INT,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmFechaAct SMALLDATETIME
 AS 
 UPDATE PucNiif SET NomCuenta=@pmNomCuenta,Mayor=@pmMayor,Movimiento=@pmMovimiento,Tercero=@pmTercero,Vehiculo=@pmVehiculo,Agencia=@pmAgencia,CentroCosto=@pmCentroCosto,SubCentro=@pmSubCentro,Vencimiento=@pmVencimiento,BaseGravable=@pmBaseGravable,TarifaPred=@pmTarifaPred,Naturaleza=@pmNaturaleza
,Ajustable=@pmAjustable,CuentaAjuDeb=@pmCuentaAjuDeb,CuentaAjuCre=@pmCuentaAjuCre,Cheques=@pmCheques,Referencias=@pmReferencias,CdTipDoc=@pmCdTipDoc,FechaAct=@pmFechaAct,Inactivo=@pmInactivo,Afijos=@pmAfijos
,ListarConc=@pmListarConc,ReqAutorizac=@pmReqAutorizac,AntProveedor=@pmAntProveedor
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPucNiif] @pmIdCuenta VARCHAR(16)
 AS 
 SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias,CdTipDoc,FechaCrea,FechaAct,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor
 FROM PucNiif
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPucNiifAju] @pmIdCuenta VARCHAR(16)=Null,@pmCueIni VARCHAR(16)=Null
,@pmCueFin VARCHAR(16)=Null
AS
 IF @pmCueIni IS NULL --NO RANGOS
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred AS Porcntaje,Naturaleza
 ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
  FROM  PucNiif
  WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta like ISNULL(@pmIdCuenta,'%')
  ORDER BY IdCuenta
 ELSE
  BEGIN
  IF @pmCueIni='LIKE'
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred AS Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
   FROM PucNiif
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta LIKE @pmIdCuenta
   ORDER BY IdCuenta
  ELSE
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred AS Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
   FROM PucNiif
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta BETWEEN @pmCueIni AND @pmCueFin
   ORDER BY IdCuenta
 END
GO
