CREATE TABLE Trn_TraManifPrec ( 
TipDoc VARCHAR(3) DEFAULT ('MUC') NOT NULL,
Manifiesto INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
IdTipoPre VARCHAR(4) NOT NULL,
NumPrecinto VARCHAR(50) NOT NULL,
ColorPrecinto VARCHAR(50) NOT NULL,
Sellos VARCHAR(150)
 CONSTRAINT PK_Trn_TraManifPrec PRIMARY KEY CLUSTERED (TipDoc,Manifiesto,IdCia,Item),
 CONSTRAINT CK_Trn_TraManifPrecColorPrecinto CHECK ((len([ColorPrecinto])>(0))),
 CONSTRAINT CK_Trn_TraManifPrecIdTipoPre CHECK ((len([IdTipoPre])>(0))),
 CONSTRAINT CK_Trn_TraManifPrecNumPrecinto CHECK ((len([NumPrecinto])>(0))))
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_KdexPdc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_KdexPdc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_KdexOcc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_KdexOcc]
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].[paQrytm_KdexCom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_KdexCom]
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexPdc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal
,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmVrBruto,tmListaPrec
,tmUnidades,tmReferencia,tmDescripcion,tmCodTarDct,tmIdVend,tmComision,tmCodTarCom,tmCdOperario,tmComisnOper,tmCodTarCmc,tmServcios,Tanques,tmVrUnitario,tmVrBase
,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia,tmIdBodega,tmpVehiculo,tmIvaComb,tmBaseIvp
 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 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexGui] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmSalidas*tmVrPrecio AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd
,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom
,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
,tmUnidades,tmDescripcion,tmReferencia,tmReferencia2,tmListaPrec,tmVrBruto,tmVrUnitario,tmVrBase,tmServcios,Tanques,tmEsCombo
,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo,tmIdBodega,tmIvaComb,tmBaseIvp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexOcc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd
,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom
,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmUnidades,tmReferencia,tmDescripcion,tmListaPrec,tmVrBruto,tmVrUnitario,tmVrBase
,tmServcios,Tanques,tmEsCombo,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo
,tmReferencia2,tmIdBodega,tmIvaComb,tmBaseIvp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexCom] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrBruto,tmVrDcto,tmReferencia,tmCdProdEquiv,tmUnidades,tmServcios,tmEsCombo,tmEsProdBase,tmItemCbo,tmCdSubgrupo
,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmCodTarCom,tmCodTarCmc
--variables de ProMcias
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques,IvaDetCombo,LtaBaseIva
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Combo<>0 
 ORDER BY tmItem