ALTER TABLE Trn_Kardex ADD galsbruto DECIMAL(14,4) DEFAULT(0),galsneto DECIMAL(14,4) DEFAULT(0),Temperatura DECIMAL(14,4) DEFAULT(0)
,UmTemp VARCHAR(3) DEFAULT('F'),Densidad  DECIMAL(14,4) DEFAULT(0)
GO
ALTER TABLE tm_Kdex ADD tmgalsbruto DECIMAL(14,4) DEFAULT(0),tmgalsneto DECIMAL(14,4) DEFAULT(0),tmTemperatura DECIMAL(14,4) DEFAULT(0)
,tmUmTemp VARCHAR(3) DEFAULT('F'),tmDensidad  DECIMAL(14,4) DEFAULT(0)
GO
ALTER TABLE Sys_Cnxextna ADD CnxDriver VARCHAR(100),CnxPort VARCHAR(30)
GO
ALTER TABLE Sys_Cnxextna DROP CONSTRAINT CK_Sys_CnxextnaCnxEngine
GO
ALTER TABLE Sys_Cnxextna ALTER COLUMN CnxEngine VARCHAR(10) NOT NULL
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsCnxextna]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsCnxextna]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsKardex]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsKardex]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Kdex]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_Kdex]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Kdex_Nts]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_Kdex_Nts]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Kdex_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_Kdex_Sel]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Kdex_SelDev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_Kdex_SelDev]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCnxextna]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryCnxextna]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCnxextnaLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryCnxextnaLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCnxextnaTip]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryCnxextnaTip]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryKardex]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryKardex]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_Kdex]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_Kdex]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_KdexDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_KdexDet]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_KdexGui]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_KdexGui]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpCnxextna]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpCnxextna]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCnxextna @pmIdReg INT,@pmCnxEngine VARCHAR(10),@pmCnxDso VARCHAR(250),@pmCnxDbname VARCHAR(50),@pmCnxAutentic VARCHAR(3)
,@pmCnxUid VARCHAR(50),@pmCnxPwd VARCHAR(50),@pmCnxOwner VARCHAR(20),@pmCnxObserv VARCHAR(250),@pmCnxTipo VARCHAR(10),@pmCnxPpties VARCHAR(250)
,@pmCnxDriver VARCHAR(100),@pmCnxPort VARCHAR(30)
 AS 
 INSERT INTO Sys_Cnxextna (IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxPpties,CnxObserv,CnxDriver,CnxPort)
 VALUES (@pmIdReg,@pmCnxTipo,@pmCnxEngine,@pmCnxDso,@pmCnxDbname,@pmCnxAutentic,@pmCnxUid,@pmCnxPwd,@pmCnxOwner,@pmCnxPpties,@pmCnxObserv,@pmCnxDriver,@pmCnxPort)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsKardex @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCdTanque VARCHAR(4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmIdUnd VARCHAR(4),@pmVrUnitario MONEY,@pmVrPrecio MONEY,@pmVrCostProm MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIvaEnt MONEY,@pmVrIvaSal MONEY,@pmTarifaDct DECIMAL(14,4),@pmVrDctoEnt MONEY,@pmVrDctoSal MONEY,@pmVrCostoEnt MONEY
,@pmVrCostoSal MONEY,@pmTarifaRet DECIMAL(14,4),@pmVrReteEnt MONEY,@pmVrReteSal MONEY,@pmTarifaIca DECIMAL(14,4),@pmVrIcaEnt MONEY,@pmVrIcaSal MONEY,@pmVrBruto MONEY,@pmCdUbic VARCHAR(10),@pmNumLote VARCHAR(30),@pmFechLote SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmCdLocal VARCHAR(8),@pmCdSzona VARCHAR(4),@pmpVehiculo VARCHAR(10),@pmIdVend VARCHAR(16),@pmComision DECIMAL(14,4)
,@pmCdOperario VARCHAR(16),@pmComisnOper DECIMAL(14,4),@pmReferencia VARCHAR(50),@pmDescripcion VARCHAR(250),@pmComptmntos VARCHAR(50),@pmCdProdEquiv VARCHAR(16),@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFactura VARCHAR(15),@pmTipDocDev VARCHAR(3),@pmNumDocDev INT,@pmCdMngra VARCHAR(10),@pmNumInicial DECIMAL(14,4),@pmNumFinal DECIMAL(14,4),@pmSobretasa DECIMAL(14,4),@pmTasaNac DECIMAL(14,4),@pmTasaDep DECIMAL(14,4)
,@pmTasaMun DECIMAL(14,4),@pmSoldicom DECIMAL(14,4),@pmImpGlobal DECIMAL(14,4),@pmOtroImpto DECIMAL(14,4),@pmUnidades DECIMAL(14,4),@pmItemCombo INT,@pmServcios BIT,@pmNoVentas INT,@pmEsCombo BIT,@pmEsProdBase INT,@pmCodTarDct VARCHAR(4),@pmCodTarIva VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarCom VARCHAR(4),@pmCodTarCmc VARCHAR(4),@pmListaPrec CHAR(1),@pmVrBase MONEY,@pmCdMoneda VARCHAR(5),@pmVrTasaCamb MONEY,@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY
,@pmVrDivisa3 MONEY,@pmReferencia2 VARCHAR(50),@pmFecOrden SMALLDATETIME,@pmgalsbruto DECIMAL(14,4),@pmgalsneto DECIMAL(14,4), @pmTemperatura DECIMAL(14,4),@pmUmTemp VARCHAR(3),@pmDensidad DECIMAL(14,4),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Kardex (TipDoc,Documento,IdCia,Item,Fecha,IdProducto,IdBodega,CdTanque,Entradas,Salidas,IdUnd,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,IdConcepto,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend,Comision,CdOperario,ComisnOper,Referencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,CdMngra,NumInicial
,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,TimeSys,IdUsuario)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmIdProducto,@pmIdBodega,@pmCdTanque,@pmEntradas,@pmSalidas,@pmIdUnd,@pmVrUnitario,@pmVrPrecio,@pmVrCostProm,@pmTarifaIva,@pmVrIvaEnt,@pmVrIvaSal,@pmTarifaDct,@pmVrDctoEnt,@pmVrDctoSal,@pmVrCostoEnt,@pmVrCostoSal,@pmTarifaRet,@pmVrReteEnt,@pmVrReteSal,@pmTarifaIca,@pmVrIcaEnt,@pmVrIcaSal,@pmVrBruto,@pmCdUbic,@pmNumLote,@pmFechLote,@pmIdConcepto,@pmIdTercero,@pmCdAgencia,@pmCdCCosto,@pmCdSubCos,@pmCdLocal,@pmCdSzona,@pmpVehiculo,@pmIdVend
,@pmComision,@pmCdOperario,@pmComisnOper,@pmReferencia,@pmDescripcion,@pmComptmntos,@pmCdProdEquiv,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmCotizacion,@pmIdCiaCot,@pmRemision,@pmIdCiaRem,@pmFactura,@pmTipDocDev,@pmNumDocDev,@pmCdMngra,@pmNumInicial,@pmNumFinal,@pmSobretasa,@pmTasaNac,@pmTasaDep,@pmTasaMun,@pmSoldicom,@pmImpGlobal,@pmOtroImpto,@pmUnidades,@pmItemCombo,@pmServcios,@pmNoVentas,@pmEsCombo,@pmEsProdBase,@pmCodTarDct,@pmCodTarIva,@pmCodTarIca,@pmCodTarRet,@pmCodTarCom,@pmCodTarCmc
,@pmListaPrec,@pmVrBase,@pmCdMoneda,@pmVrTasaCamb,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmReferencia2,@pmFecOrden,@pmgalsbruto,@pmgalsneto,@pmTemperatura,@pmUmTemp,@pmDensidad,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmCdTanque VARCHAR(4),@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmIdUnd VARCHAR(4),@pmtmVrUnitario MONEY,@pmtmVrPrecio MONEY,@pmtmVrCostProm MONEY,@pmtmVrProm MONEY
,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifaDct DECIMAL(14,4),@pmtmVrDcto MONEY,@pmtmTarifaRet DECIMAL(14,4),@pmtmVrRete MONEY,@pmtmTarifaIca DECIMAL(14,4),@pmtmVrIca MONEY,@pmtmVrBruto MONEY,@pmtmCdUbic VARCHAR(10),@pmtmNumLote VARCHAR(30),@pmtmFechLote SMALLDATETIME,@pmtmFecVceLote SMALLDATETIME
,@pmtmIdTercero VARCHAR(16),@pmtmCdAgencia VARCHAR(16),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmCdLocal VARCHAR(8),@pmtmCdSzona VARCHAR(4),@pmtmpVehiculo VARCHAR(10),@pmtmIdVend VARCHAR(16),@pmtmComision DECIMAL(14,4),@pmtmCdOperario VARCHAR(16),@pmtmComisnOper DECIMAL(14,4),@pmtmReferencia VARCHAR(50)
,@pmtmDescripcion VARCHAR(250),@pmtmComptmntos VARCHAR(50),@pmtmCdProdEquiv VARCHAR(16),@pmtmCdMngra VARCHAR(10),@pmtmNumInicial DECIMAL(14,4),@pmtmNumFinal DECIMAL(14,4),@pmtmSobretasa DECIMAL(14,4),@pmtmTasaNac DECIMAL(14,4),@pmtmTasaDep DECIMAL(14,4),@pmtmTasaMun DECIMAL(14,4),@pmtmSoldicom DECIMAL(14,4)
,@pmtmImpGlobal DECIMAL(14,4),@pmtmOtroImpto DECIMAL(14,4),@pmtmUnidades DECIMAL(14,4),@pmtmServcios BIT,@pmtmNoVentas INT,@pmtmEsCombo BIT,@pmtmEsProdBase INT,@pmtmItemCbo INT,@pmtmCodTarDct VARCHAR(4),@pmtmCodTarIva VARCHAR(4),@pmtmCodTarIca VARCHAR(4),@pmtmCodTarRet VARCHAR(4),@pmtmCodTarCom VARCHAR(4)
,@pmtmCodTarCmc VARCHAR(4),@pmtmCdSubgrupo VARCHAR(8),@pmtmListaPrec CHAR(1),@pmtmVrBase MONEY,@pmtmCdMoneda VARCHAR(5),@pmtmVrTasaCamb MONEY,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmTipRem VARCHAR(3),@pmtmRemision INT,@pmtmIdCiaRem CHAR(2),@pmtmReferencia2 VARCHAR(50),@pmtmFecOrden SMALLDATETIME
,@pmtmgalsbruto DECIMAL(14,4),@pmtmgalsneto DECIMAL(14,4),@pmtmTemperatura DECIMAL(14,4),@pmtmUmTemp VARCHAR(3),@pmtmDensidad DECIMAL(14,4)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmCdTanque,@pmtmEntradas,@pmtmSalidas,@pmtmIdUnd,@pmtmVrUnitario,@pmtmVrPrecio,@pmtmVrCostProm,@pmtmVrProm,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaDct,@pmtmVrDcto,@pmtmTarifaRet,@pmtmVrRete,@pmtmTarifaIca,@pmtmVrIca,@pmtmVrBruto,@pmtmCdUbic,@pmtmNumLote,@pmtmFechLote,@pmtmFecVceLote
,@pmtmIdTercero,@pmtmCdAgencia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmCdLocal,@pmtmCdSzona,@pmtmpVehiculo,@pmtmIdVend,@pmtmComision,@pmtmCdOperario,@pmtmComisnOper,@pmtmReferencia,@pmtmDescripcion,@pmtmComptmntos,@pmtmCdProdEquiv,@pmtmCdMngra,@pmtmNumInicial,@pmtmNumFinal,@pmtmSobretasa,@pmtmTasaNac,@pmtmTasaDep,@pmtmTasaMun,@pmtmSoldicom
,@pmtmImpGlobal,@pmtmOtroImpto,@pmtmUnidades,@pmtmServcios,@pmtmNoVentas,@pmtmEsCombo,@pmtmEsProdBase,@pmtmItemCbo,@pmtmCodTarDct,@pmtmCodTarIva,@pmtmCodTarIca,@pmtmCodTarRet,@pmtmCodTarCom,@pmtmCodTarCmc,@pmtmCdSubgrupo,@pmtmListaPrec,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia
,@pmtmVrBase,@pmtmCdMoneda,@pmtmVrTasaCamb,@pmtmTipRem,@pmtmRemision,@pmtmIdCiaRem,@pmtmReferencia2,@pmtmFecOrden,@pmtmgalsbruto,@pmtmgalsneto,@pmtmTemperatura,@pmtmUmTemp,@pmtmDensidad)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_Nts @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Entradas,Salidas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipDoc,Documento,IdCia,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,0,0,0,'',0
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND EsProdBase=0 AND Salidas>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_Sel @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Entradas,Salidas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipOrd,NumOrden,IdCiaOrd,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND EsProdBase=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_SelDev @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Salidas,Entradas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipOrd,NumOrden,IdCiaOrd,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia  AND  EsProdBase<=1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextna @pmIdReg INT
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd
,CnxOwner,CnxObserv,CnxPpties,CnxDriver,CnxPort
 FROM Sys_Cnxextna
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextnaLta @pmCnxTipo VARCHAR(10)=Null
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort,CnxPwd
 FROM Sys_Cnxextna
 WHERE  CnxTipo LIKE ISNULL(@pmCnxTipo,'%')
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextnaTip @pmCnxTipo VARCHAR(10)
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort
 FROM Sys_Cnxextna
 WHERE  CnxTipo=@pmCnxTipo
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardex @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,IdProducto,IdBodega,CdTanque,Entradas,Salidas,IdUnd,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,IdConcepto,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona
,pVehiculo,IdVend,Comision,CdOperario,ComisnOper,Referencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev
,NumDocDev,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,CodTarDct
,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,TimeSys,IdUsuario
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Kdex @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb
,tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDet @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia
,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
--variables de productos
,DescripProd,TipoRef,IdSubgrupo,Seriales,Lotes,Combo,Tanques
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGui @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmSalidas*tmVrPrecio AS VrSubTotal,tmComptmntos
,tmUnidades,tmReferencia,tmDescripcion,tmIdBodega,Bodega,tmVrBruto,tmVrUnitario,tmListaPrec
,tmServcios,Tanques,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo,tmEsCombo
,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCnxextna @pmIdReg INT,@pmCnxEngine VARCHAR(10),@pmCnxDso VARCHAR(250),@pmCnxDbname VARCHAR(50),@pmCnxAutentic VARCHAR(3)
,@pmCnxUid VARCHAR(50),@pmCnxPwd VARCHAR(50),@pmCnxOwner VARCHAR(20),@pmCnxObserv VARCHAR(250),@pmCnxTipo VARCHAR(10),@pmCnxPpties VARCHAR(250)
,@pmCnxDriver VARCHAR(100),@pmCnxPort VARCHAR(30)
 AS 
 UPDATE Sys_Cnxextna SET CnxEngine=@pmCnxEngine,CnxDso=@pmCnxDso,CnxDbname=@pmCnxDbname,CnxAutentic=@pmCnxAutentic,CnxUid=@pmCnxUid,CnxPwd=@pmCnxPwd
,CnxOwner=@pmCnxOwner,CnxObserv=@pmCnxObserv,CnxTipo=@pmCnxTipo,CnxPpties=@pmCnxPpties,CnxDriver=@pmCnxDriver,CnxPort=@pmCnxPort
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



