if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdInsumosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdInsumosLta]
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].[paQryProdConsInsuFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdConsInsuFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdConsolida_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdConsolida_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdConsPedFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdConsPedFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdConsProcFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdConsProcFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdConsTallasFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdConsTallasFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdProcesosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdProcesosLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTallasMovDoc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTallasMovDoc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_KdexSai]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_KdexSai]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsProcFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd,D.IdBodega,B.Bodega
,D.CdUbic,D.CantAprob,D.Existencias,D.CantSaldo,D.CantTdo,D.IdTipProc,TP.TipoProceso,TP.IndOrden,D.ProcFinal,D.Cantidad,D.Tallas
,D.VrCostoEst,D.EdoOrden,D.TipOrden,D.NumOrden,D.CdCiaOrden,D.CantOrden
--datos del producto
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong AS ProComentarios
FROM Trn_ProdConsProc AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN TiposProceso AS TP ON D.IdTipProc=TP.IdTipProc
INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdProcesosLta] 
AS
SELECT PS.IdProducto AS CodProducto,P.DescripProd,PS.Item,PS.IdTipProc,TipoProceso,PC.Descripcion AS DescProceso,PC.IndOrden
,PS.CostoEst,PS.UltCosto,PS.ProcFinal,PS.Id,PC.Inactivo AS ProcInactivo
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad AS UndadMed,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM ProdProcesos AS PS INNER JOIN TiposProceso AS PC ON PS.IdTipProc=PC.IdTipProc
INNER JOIN ProdMcias AS P ON PS.IdProducto=P.IdProducto
INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsInsuFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd
,D.IdInsumo,IM.DescripProd AS Insumo,D.IdTipProc,TP.TipoProceso,TP.IndOrden,D.IdBodega,B.Bodega,CdBodTdo,BD.Bodega AS BodegaTdo
,D.Cantidad,D.CantPed,D.Existencias,D.CantSaldo,D.CantTdo,D.CantOrden,D.CantSalidas
--datos del insumo
,IM.Referencia,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,IM.IdSubgrupo AS CdSubgpo,Subgrupo
,IM.IdMarca,Marca,IM.IdUnd,UM.Unidad AS UndadMed,IM.Color,IM.Tamano,IM.DescripLong AS InsDescripcion
FROM Trn_ProdConsInsu AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN TiposProceso AS TP ON D.IdTipProc=TP.IdTipProc
INNER JOIN ProdMcias AS IM ON D.IdInsumo=IM.IdProducto INNER JOIN SubGrupos AS S ON IM.IdSubgrupo=S.IdSubgrupo
INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
INNER JOIN Marcas AS M ON IM.IdMarca=M.IdMarca INNER JOIN UndMed AS UM ON IM.IdUnd=UM.IdUnd
LEFT JOIN Bodegas AS BD ON D.CdBodTdo=BD.IdBodega
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdInsumosLta] 
AS
SELECT PN.IdProducto AS CodProducto,P.DescripProd,PN.Item,PN.IdInsumo AS CodInsumo,IM.DescripProd AS DescInsumo
,PN.IdTipProc,TipoProceso,PC.Descripcion AS DescProceso,PC.IndOrden,PN.Cantidad,PN.Inactivo,PN.Id
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad AS UndadMed,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
--inf. insumos
,IM.IdUnd AS InsIdUnd,UMI.Unidad AS InsUndad
FROM ProdInsumos AS PN INNER JOIN TiposProceso AS PC ON PN.IdTipProc=PC.IdTipProc
INNER JOIN ProdMcias AS P ON PN.IdProducto=P.IdProducto INNER JOIN ProdMcias AS IM ON PN.IdInsumo=IM.IdProducto
INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd INNER JOIN UndMed AS UMI ON IM.IdUnd=UMI.IdUnd
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsolida_Cr] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
 SELECT C.TipDoc,TipoDoc,C.NumCons,C.IdCia,Compania,C.Fecha,C.IdBodega,B.Bodega,C.CdBodDest,BD.Bodega AS BodDestino,C.IdBodIns,BU.Bodega AS BodInsumo,C.CdBodInsDest,BDI.Bodega AS BodDestInsumo
,C.Anulado,C.FecDev,C.CritPedidos,PT.NumTdoProd,PT.NumTdoInsu,C.Observacion,C.IdEstado,Estado,C.TimeSys AS FecCrea,C.FecUpdate AS FecActualiza,C.OrigenAdd,C.IdCiaCrea,C.IdUsuario,Usuario
--Detalles
,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd,D.IdBodega AS CodBodega,BG.Bodega AS DetBodega,D.CdUbic,D.CantPedido,D.CantAprob,D.Existencias
,D.CantSaldo,D.CantTdo,D.CdBodTdo,BT.Bodega AS DetBodegaDest,D.Tallas,D.IdUnd,UD.Unidad
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdConsolida AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
INNER JOIN Bodegas AS B ON C.IdBodega=B.IdBodega
INNER JOIN Trn_ProdConsDet AS D ON C.TipDoc=D.TipDoc AND C.NumCons=D.NumCons AND C.IdCia=D.IdCia
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo
INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca INNER JOIN Bodegas AS BG ON D.IdBodega=BG.IdBodega
INNER JOIN UndMed AS UD ON D.IdUnd=UD.IdUnd 
LEFT JOIN Bodegas AS BD ON C.CdBodDest=BD.IdBodega
LEFT JOIN Bodegas AS BU ON C.IdBodIns=BU.IdBodega LEFT JOIN Bodegas AS BDI ON C.CdBodInsDest=BDI.IdBodega
LEFT JOIN Bodegas AS BT ON D.CdBodTdo=BT.IdBodega
LEFT JOIN (SELECT TipDoc,NumCons,IdCia,MAX(Traslado) AS NumTdoProd,MAX(NumTdoIns) AS NumTdoInsu FROM Trn_ProdConsPed GROUP BY TipDoc,NumCons,IdCia)
AS PT ON C.TipDoc=PT.TipDoc AND C.NumCons=PT.NumCons AND C.IdCia=PT.IdCia
WHERE C.TipDoc=@pmTipDoc AND C.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTallasMovDoc] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
AS 
SELECT TM.TipDoc,TM.Documento,TM.IdCia,Compania,TM.Item,TM.Fecha,TM.ItemKdx,TM.IdProducto AS CdProducto,DescripProd
,TM.IdBodega AS CdBodega,Bodega,TM.IdUbic,TM.NumTalla,TM.Entradas,TM.Salidas
,TL.Cantidad AS CantSaldo,TL.Pedidos AS CantPed
FROM Trn_TallasMov AS TM INNER JOIN ProdMcias AS P ON TM.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON TM.IdBodega=B.IdBodega INNER JOIN Companias AS CN ON TM.IdCia=CN.IdCia
LEFT JOIN Trn_Tallajes AS TL ON TM.IdProducto=TL.IdProducto AND TM.IdCia=TL.IdCia AND TM.IdBodega=TL.IdBodega
AND TM.NumTalla=TL.NumTalla
WHERE TM.TipDoc=@pmTipDoc AND TM.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND TM.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexSai] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmSalidas,tmVrUnitario,tmSalidas*tmVrUnitario AS CostoTotal
,tmRemision,tmIdCiaRem,tmTipDoc,tmDocumento,tmIdCia,tmIdUnd,Unidad,tmUnidades,tmReferencia,tmDescripcion
,tmCdCCosto,tmCdSubCos,tmIdTercero,tmIdVend,tmpVehiculo,tmVrPrecio,tmSalidas*tmVrPrecio AS VrTotal,tmTarifaIva,tmVrIva
,tmVrBruto,tmNumLote,tmFecVceLote,Tanques,tmServcios,tmEsCombo,tmCdTanque,tmVrImvCosto,tmItemCbo,tmgalsbruto,tmReferencia2
 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
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsPedFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT PD.TipDoc,PD.NumCons,PD.IdCia,PD.Item,PD.TipPed,PD.Pedido,PD.IdCiaPed,Compania,PD.TipTdo,PD.Traslado,PD.IdCiaTdo,PD.NumTdoIns,PD.CdCiaIns
,O.Fecha,FechaVence,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia,Agencia,VrSubTotal,VrDescuento,VrImpuesto,VrNeto,O.IdVend,VN.RazonSocial AS Vendedor
,O.IdLocEnv,LE.Localidad AS CiudadEnvio,DirEnvio,DiasEntraga,O.NomContac,O.TelContac,O.emlContac,O.RefPedido,O.Modalidad,O.Vigencia,NumAprob,IdCiaApr,FecAprob,DetalleAprob
,O.TipFac,O.Factura,O.IdCiaFac,TipRem,Remision,IdCiaRem,O.NumAutSicom AS NumOrdenComp,O.IdEstado,Estado,O.Anulado,O.Observacion
FROM Trn_ProdConsPed AS PD INNER JOIN dbo.Trn_Opedido AS O ON PD.TipPed=O.TipDoc AND PD.Pedido=O.Pedido AND PD.IdCiaPed=O.IdCia
INNER JOIN Companias AS CN ON PD.IdCiaPed=CN.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal
WHERE PD.TipDoc=@pmTipDoc AND PD.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND PD.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmRec_Costo,tmMgenCont
,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,CASE WHEN CantObseq>0 AND (TipDoc='DVE' OR TipDoc='DEI' OR SUBSTRING(TipDoc,1,2)='FC' OR SUBSTRING(TipDoc,1,2)='FO') THEN Salidas-CantObseq ELSE Salidas END
,CASE WHEN CantObseq>0 AND (TipDoc='ENT' OR TipDoc='COM' OR SUBSTRING(TipDoc,1,2)='DF' OR SUBSTRING(TipDoc,1,2)='DO') THEN Entradas-CantObseq ELSE Entradas END
,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,CASE WHEN CantObseq>0 AND VrIvaObseq>0 THEN (VrIvaEnt+VrIvaSal)-VrIvaObseq ELSE VrIvaEnt+VrIvaSal END,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
,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN 'OPP' ELSE 'REM' END
,Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,Rec_Costo,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq
,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsTallasFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
,@pmTipoReg VARCHAR(3)=Null
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipoReg,D.ItemDet,D.NumTalla,D.Cantidad,D.CantPed AS CantAprob
FROM Trn_ProdConsTallas AS D
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
AND (D.TipoReg=@pmTipoReg OR @pmTipoReg IS NULL)
