ALTER TABLE Trn_ReqDetalle ADD Cant_Orden DECIMAL(14,4) DEFAULT(0) NOT NULL
GO
ALTER TABLE tm_ReqDetalle ADD tmCantOrden DECIMAL(14,4) DEFAULT(0) NOT NULL
GO
ALTER TABLE Trn_ProdOrden ADD Num_Entrega INT DEFAULT(0) NOT NULL,CdCiaEnt CHAR(2)
GO
ALTER TABLE Trn_ProdOrdenDet ADD ItemEnt INT DEFAULT(0) NOT NULL
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdOrden_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdOrden_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsProdOrden]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsProdOrden]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsReqDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsReqDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ReqDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_ReqDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ReqDetalle_Mtto]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_ReqDetalle_Mtto]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ReqDetalle_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_ReqDetalle_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdEntrega_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdEntrega_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdEntregaTallasFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdEntregaTallasFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdOrden]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdOrden]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdOrdenEntFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdOrdenEntFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdOrdenSalFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdOrdenSalFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdOrdenTallasFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryProdOrdenTallasFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryReqDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryReqDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryRequisicion_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryRequisicion_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryRequisicionDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryRequisicionDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_ReqDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_ReqDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_ReqDetalleDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_ReqDetalleDso]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpProdOrden]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpProdOrden]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenSalFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS
SELECT PS.TipDoc,PS.NumOrden,PS.IdCia,Compania,PS.Item,PS.TipSal,PS.NumSalida,PS.IdCiaSal,D.Item AS DetItem,D.IdProducto,D.Descripcion,D.Entradas,D.Salidas,D.VrUnitario
,D.IdBodega,Bodega,D.Referencia AS SalReferencia,SA.Fecha,SA.TipOrd,SA.NumOrden AS Num_OrdenSA,SA.IdCiaOrd,SA.Modalidad,SA.Anulado,SA.Observacion,SA.TipCom,SA.Comprobante,SA.IdCiaCom
,P.DescripProd,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.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdOrdenSal AS PS INNER JOIN Trn_Kardex AS D ON PS.TipSal=D.TipDoc AND PS.NumSalida=D.Documento AND PS.IdCiaSal=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 UndMed AS UD ON P.IdUnd=UD.IdUnd
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN Companias AS CN ON PS.IdCiaSal=CN.IdCia
LEFT JOIN (SELECT TipDoc,Salida,IdCia,Fecha,TipOrd,NumOrden,IdCiaOrd,Modalidad,TipCom,Comprobante,IdCiaCom,Anulado,Observacion,IdEstado FROM Trn_Salidas WHERE TipDoc='SAI'
UNION ALL SELECT TipDev,Devolucion,IdCia,Fecha,TipOrd,NumOrden,IdCiaOrd,Modalidad,TipCom,Comprobante,IdCiaCom,1,Observacion,IdEstado
FROM Trn_DevSal WHERE TipDev='DSI') AS SA ON PS.TipSal=SA.TipDoc AND PS.NumSalida=SA.Salida AND PS.IdCiaSal=SA.IdCia
WHERE PS.TipDoc=@pmTipDoc AND PS.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND PS.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenEntFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS 
SELECT O.TipDoc,O.NumEntrega,O.IdCia,Compania,O.Fecha,O.FecEntrega,O.TipoEntrega,O.NitTercero,RazonSocial,O.NomContacto,O.TelContacto
,O.TipOrden,O.NumOrden,O.IdCiaOrden,O.EdoOrden,O.Anulado,O.FecDev,O.Observacion,O.TimeSys AS FechaCrea,O.IdUsuario,Usuario
,D.Item,D.ItemOrden,D.CantOrden,D.Cantidad,D.Defectuoso,D.Rechazado,D.CostoUnit,D.VrDcto,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed
,D.IdProducto,P.DescripProd,D.Descripcion,D.CdTipProc,TipoProceso,D.Tallas,D.ProcFinal,D.Observacion AS MotivoRchzo,P.Tallaje
FROM Trn_ProdEntrega AS O INNER JOIN Trn_ProdEntregaDet AS D ON O.TipDoc=D.TipDoc AND O.NumEntrega=D.NumEntrega AND O.IdCia=D.IdCia
INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
LEFT JOIN TiposProceso AS TP ON D.CdTipProc=TP.IdTipProc LEFT JOIN Terceros AS T ON O.NitTercero=T.IdTercero
WHERE O.TipOrden=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin  AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrden_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS 
SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia,Compania,O.Fecha,O.TipCons,O.NumCons,O.IdCiaCons,C.Fecha AS FecCons,O.Modalidad,O.IdTipProc,TP.TipoProceso,TP.IndOrden
,O.IdTaller,TL.NomTaller,TL.Direccion,TL.IdLocal,LC.Localidad,TL.Telefono,TL.TelCelular,TL.TipoTaller,O.NitTercero,RazonSocial,O.NomContacto,O.TelContacto
,O.CostoTotal,O.ConPago,O.EdoOrden,O.Num_Entrega,O.CdCiaEnt,O.FecEntrega,O.ProcFinal,O.Anulado,O.FecDev,O.Observacion
,O.IdEstado,Estado,O.OrigenAdd,O.TimeSys AS FecCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea,O.IdUsuario,Usuario
--datos detalle
,D.Item,DT.TipPed,DT.Pedido,DT.IdCiaPed,DT.ItemPed,D.ItemCons,D.IdProducto,P.DescripProd,D.Descripcion,D.Cantidad,D.CostoEst,D.Terminado,D.Defectuoso,D.Rechazado
,DT.IdBodega AS CodBodega,B.Bodega,DT.CantAprob,DT.Existencias,DT.CantSaldo,DT.CantTdo,DT.Cantidad AS CantProceso,DT.Tallas
,DT.IdTipProc AS DetCdTipProc,PS.TipoProceso AS DetTipoProceso,DT.ProcFinal AS DetProcFinal,D.MotivoDev,D.ItemEnt
,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.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdOrden AS O INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller INNER JOIN Localidades AS LC ON TL.IdLocal=LC.IdLocal
INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
INNER JOIN Trn_ProdOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
INNER JOIN Trn_ProdConsolida AS C ON O.TipCons=C.TipDoc AND O.NumCons=C.NumCons AND O.IdCiaCons=C.IdCia
INNER JOIN Trn_ProdConsProc AS DT ON D.TipCons=DT.TipDoc AND D.NumCons=DT.NumCons AND D.IdCiaCons=DT.IdCia AND D.ItemCons=DT.Item
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 UndMed AS UD ON P.IdUnd=UD.IdUnd
INNER JOIN Bodegas AS B ON DT.IdBodega=B.IdBodega
LEFT JOIN TiposProceso AS PS ON DT.IdTipProc=PS.IdTipProc
LEFT JOIN Terceros AS T ON O.NitTercero=T.IdTercero
WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntrega_Cr] @pmTipDoc VARCHAR(3),@pmNumEntregaIni INT,@pmNumEntregaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipDoc,TipoDoc,E.NumEntrega,E.IdCia,Compania,E.Fecha,E.FecEntrega,E.TipOrden,E.NumOrden,E.IdCiaOrden,E.CostoTotal,E.Descuento,E.CantTotal
,E.TipoEntrega,E.NitTercero,RazonSocial,E.NomContacto,E.TelContacto,E.EdoOrden AS EdoEntrega
,O.Fecha AS FecOrden,O.TipCons,O.NumCons,O.IdCiaCons,O.Modalidad,O.IdTipProc,TP.TipoProceso,TP.IndOrden,O.IdTaller,TL.NomTaller,TL.Direccion,TL.IdLocal,LC.Localidad,TL.Telefono,TL.TelCelular,TL.TipoTaller 
,O.ConPago,O.ProcFinal,O.Observacion AS ObservOrden,O.EdoOrden
,E.Anulado,E.FecDev,E.Observacion,E.IdEstado,Estado,E.OrigenAdd,E.TimeSys AS FecCrea,E.FecUpdate AS FecActualiza,E.IdCiaCrea,E.IdUsuario,Usuario
--detalles
,D.Item,D.ItemOrden,D.CantOrden,D.Cantidad,D.Defectuoso,D.Rechazado,D.CostoUnit,D.VrDcto,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto,P.DescripProd,D.Descripcion
,D.CdTipProc AS DetCdProc,PS.TipoProceso AS DetProceso,D.Tallas,D.ProcFinal AS DetProcFinal,D.Observacion AS MotivoRechz
,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.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdEntrega AS E INNER JOIN Trn_ProdEntregaDet AS D ON E.TipDoc=D.TipDoc AND E.NumEntrega=D.NumEntrega AND E.IdCia=D.IdCia
INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON E.TipDoc=TD.IdDoc
INNER JOIN Trn_ProdOrden AS O ON E.TipOrden=O.TipDoc AND E.NumOrden=O.NumOrden AND E.IdCiaOrden=O.IdCia
INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller INNER JOIN Localidades AS LC ON TL.IdLocal=LC.IdLocal
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 UndMed AS UD ON P.IdUnd=UD.IdUnd
LEFT JOIN TiposProceso AS PS ON D.CdTipProc=PS.IdTipProc 
LEFT JOIN Terceros AS T ON E.NitTercero=T.IdTercero
WHERE E.TipDoc=@pmTipDoc AND E.NumEntrega BETWEEN @pmNumEntregaIni AND @pmNumEntregaFin AND E.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_ReqDetalle_Sel] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad
,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 SELECT  @pmtmNumero,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal
,(CantSalida-ISNULL(CantDevSal,0)),'0','','0',PlacaVehDet,'',0,Referencia,'',0,CodCCtoDet,CodSCtoDet,Cant_Orden
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_ReqDetalleDso] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmCantidad,tmIdUnd,Unidad,tmVrUnitario,tmCantidad*tmVrUnitario AS ValorTotal
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmCantOrden,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmCantSalida,tmIdSubgrupo,tmCdBodega,tmObservacion
,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmNumVehic,tmNumero
 FROM tm_ReqDetalle AS D INNER JOIN UndMed AS U ON D.tmIdUnd=U.IdUnd
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_ReqDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmCdProducto VARCHAR(16),@pmtmDescripcion VARCHAR(250)
,@pmtmIdSubgrupo VARCHAR(8),@pmtmCdBodega VARCHAR(4),@pmtmCantidad DECIMAL(14,4),@pmtmIdUnd VARCHAR(4),@pmtmVrUnitario MONEY
,@pmtmTipOrd VARCHAR(3),@pmtmNumOrden INT,@pmtmIdCiaOrd CHAR(2),@pmtmTipSal VARCHAR(3),@pmtmNSalida INT,@pmtmIdCiaSal CHAR(2),@pmtmFechaSal SMALLDATETIME
,@pmtmCantSalida DECIMAL(14,4),@pmtmIdOperario VARCHAR(16),@pmtmCdCenServ VARCHAR(4),@pmtmNitTercero VARCHAR(16),@pmtmNumVehic VARCHAR(10),@pmtmNumParte VARCHAR(20)
,@pmtmEstado INT,@pmtmObservacion VARCHAR(1000),@pmtmCodConc VARCHAR(4),@pmtmVrPrecio MONEY,@pmtmCodCenCto VARCHAR(16),@pmtmCodSubCto VARCHAR(16),@pmtmCantOrden DECIMAL(14,4)
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd
,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmCdProducto,@pmtmDescripcion,@pmtmIdSubgrupo,@pmtmCdBodega,@pmtmCantidad,@pmtmIdUnd,@pmtmVrUnitario
,@pmtmTipOrd,@pmtmNumOrden,@pmtmIdCiaOrd,@pmtmTipSal,@pmtmNSalida,@pmtmIdCiaSal,@pmtmFechaSal,@pmtmCantSalida,@pmtmIdOperario,@pmtmCdCenServ
,@pmtmNitTercero,@pmtmNumVehic,@pmtmNumParte,@pmtmEstado,@pmtmObservacion,@pmtmCodConc,@pmtmVrPrecio,@pmtmCodCenCto,@pmtmCodSubCto,@pmtmCantOrden)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ReqDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion
,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden,tmNumero
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_ReqDetalle_Mtto] @pmTipReq VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad
,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 SELECT  @pmtmNumero,Item,R.IdProducto,R.Descripcion,ISNULL(P.IdSubgrupo,'0'),R.CdBodega,R.Cantidad,R.IdUnd,CAST (VrUnitario AS MONEY),TipOdt,NumOrden,IdCiaOdt
,TipSal,NumSalida,IdCiaSal,Null,CantSalida,IdOperario,CdCenServ,NitTercero,NumVehic,NumParte,R.EstadoReq,R.Observacion,R.CdConcServ,VrPrecioUnd,'','',0
 FROM Trn_MttoOrdenReq AS R LEFT JOIN ProdMcias AS P ON R.IdProducto=P.IdProducto
 WHERE TipReq=@pmTipReq AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicion_Cr] @pmTipDoc VARCHAR(3),@pmRequisicionIni INT,@pmRequisicionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS CdTipo,TipoDoc,R.Requisicion AS NumRequis,R.IdCia AS CdCia,Compania,Fecha,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdRespons,T.RazonSocial AS NomResponsable
,R.IdCCosto AS CdCentCosto,CC.CCosto,R.IdSubCos AS CdSubCent,SC.SubCosto,R.IdDep AS CdDep,Dependencia,VrSubTotal,R.Cantidad AS CantTotal,R.NContrato AS NumContrato,IdCiaCont
,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Agencia,CodAgencia,Modalidad,DirEntrega,IdLocEnt,Localidad,Departamento,R.TipSal AS Tip_Sal,NumSalida,R.IdCiaSal AS CdCiaSal
,R.FechaSal AS Fec_Salida,NumAprob,FecAprob,CdUsuAprob,NivelAprob,OrigenAdd,Anulado,NomContacto,TelsContacto,EmailContacto,Num_Vehic,Num_Trailer,TipoVigencia,FecDev,R.Observacion AS Observ
,R.IdEstado AS CdEstado,Estado,R.TimeSys,R.FecUpdate,R.IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
,D.Item,CdProducto,Descripcion,D.Cantidad AS CantArt,D.IdUnd AS CdUnid,Unidad,D.IdSubgrupo AS CdSubgrupo,Subgrupo,VrUnitario,TipOrd,NumOrden,IdCiaOrd,D.TipSal AS DetTipoSal,NSalida,D.IdCiaSal AS DetCiaSal
,D.FechaSal AS DetFecSalida,CdBodega,Bodega,D.Referencia,D.CantSalida,D.CantDevSal,D.Cant_Orden,CodCCtoDet,CCD.CCosto AS DetCentro,CodSCtoDet,SCD.SubCosto AS DetSubCosto,D.PlacaVehDet
 FROM Trn_Requisicion AS R INNER JOIN Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero
 INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON D.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Localidades AS L ON R.IdLocEnt=L.IdLocal
 INNER JOIN Departamentos AS DT ON L.IdDep=DT.IdDep
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero
 LEFT JOIN Bodegas AS BG ON D.CdBodega =BG.IdBodega
 LEFT JOIN Agencias AS A ON R.CdAgencia=A.IdAgencia
 LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN CentroCosto AS CCD ON D.CodCCtoDet=CCD.IdCCosto
 LEFT JOIN SubCentros AS SCD ON D.CodSCtoDet=SCD.IdSubCos
 WHERE R.TipDoc=@pmTipDoc AND R.Requisicion BETWEEN  @pmRequisicionIni AND @pmRequisicionFin 
 AND R.IdCia=@pmIdCia 
 ORDER BY  R.Requisicion,Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaTallasFmt] @pmTipDoc VARCHAR(3),@pmNumEntregaIni INT,@pmNumEntregaFin INT,@pmIdCia CHAR(2)
AS
SELECT TipDoc,NumEntrega,IdCia,Item,ItemDet,NumTalla,Cantidad,Defectuoso,Rechazado
FROM Trn_ProdEntregaTallas
WHERE TipDoc=@pmTipDoc AND NumEntrega BETWEEN @pmNumEntregaIni AND @pmNumEntregaFin
AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenTallasFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS
SELECT TipDoc,NumOrden,IdCia,Item,ItemOrden,NumTalla,Cantidad,Terminado,Defectuoso,Rechazado
FROM Trn_ProdOrdenTallas
WHERE TipDoc=@pmTipDoc AND NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin
AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,TipCons,NumCons,IdCiaCons,Modalidad,IdTipProc,IdTaller,CostoTotal,ConPago,EdoOrden,Num_Entrega,CdCiaEnt,FecEntrega
,NitTercero,NomContacto,TelContacto,ProcFinal,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_ProdOrden
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipCons VARCHAR(3),@pmNumCons INT,@pmIdCiaCons CHAR(2),@pmModalidad VARCHAR(10),@pmIdTipProc VARCHAR(4),@pmIdTaller VARCHAR(4),@pmCostoTotal MONEY,@pmConPago BIT,@pmEdoOrden INT
,@pmFecEntrega SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNitTercero VARCHAR(16),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmProcFinal BIT,@pmNum_Entrega INT,@pmCdCiaEnt CHAR(2),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ProdOrden SET Fecha=@pmFecha,TipCons=@pmTipCons,NumCons=@pmNumCons,IdCiaCons=@pmIdCiaCons,Modalidad=@pmModalidad,IdTipProc=@pmIdTipProc,IdTaller=@pmIdTaller,CostoTotal=@pmCostoTotal,ConPago=@pmConPago,EdoOrden=@pmEdoOrden,FecEntrega=@pmFecEntrega
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NitTercero=@pmNitTercero,NomContacto=@pmNomContacto,TelContacto=@pmTelContacto,ProcFinal=@pmProcFinal,Num_Entrega=@pmNum_Entrega,CdCiaEnt=@pmCdCiaEnt,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipCons VARCHAR(3),@pmNumCons INT,@pmIdCiaCons CHAR(2),@pmModalidad VARCHAR(10),@pmIdTipProc VARCHAR(4),@pmIdTaller VARCHAR(4),@pmCostoTotal MONEY,@pmConPago BIT,@pmEdoOrden INT
,@pmFecEntrega SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNitTercero VARCHAR(16),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmProcFinal BIT,@pmNum_Entrega INT,@pmCdCiaEnt CHAR(2)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ProdOrden (TipDoc,NumOrden,IdCia,Fecha,TipCons,NumCons,IdCiaCons,Modalidad,IdTipProc,IdTaller,CostoTotal,ConPago,EdoOrden,FecEntrega,NitTercero,NomContacto,TelContacto,ProcFinal,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,Num_Entrega,CdCiaEnt)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmTipCons,@pmNumCons,@pmIdCiaCons,@pmModalidad,@pmIdTipProc,@pmIdTaller,@pmCostoTotal,@pmConPago,@pmEdoOrden,@pmFecEntrega,@pmNitTercero,@pmNomContacto,@pmTelContacto,@pmProcFinal,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNum_Entrega,@pmCdCiaEnt)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsReqDetalle] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdProducto VARCHAR(16),@pmDescripcion VARCHAR(250),@pmIdSubgrupo VARCHAR(8)
,@pmCdBodega VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmIdUnd VARCHAR(4),@pmVrUnitario MONEY,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmTipSal VARCHAR(3),@pmNSalida INT,@pmIdCiaSal CHAR(2),@pmFechaSal SMALLDATETIME,@pmReferencia VARCHAR(250),@pmCantSalida DECIMAL(14,4),@pmCantDevSal DECIMAL(14,4)
,@pmCodCCtoDet VARCHAR(16),@pmCodSCtoDet VARCHAR(16),@pmPlacaVehDet VARCHAR(10),@pmCant_Orden DECIMAL(14,4)
AS 
 INSERT INTO Trn_ReqDetalle (TipDoc,Requisicion,IdCia,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal
,Referencia,CantSalida,CantDevSal,CodCCtoDet,CodSCtoDet,PlacaVehDet,Cant_Orden)
 VALUES (@pmTipDoc,@pmRequisicion,@pmIdCia,@pmItem,@pmCdProducto,@pmDescripcion,@pmIdSubgrupo,@pmCdBodega,@pmCantidad,@pmIdUnd,@pmVrUnitario,@pmTipOrd
,@pmNumOrden,@pmIdCiaOrd,@pmTipSal,@pmNSalida,@pmIdCiaSal,@pmFechaSal,@pmReferencia,@pmCantSalida,@pmCantDevSal,@pmCodCCtoDet,@pmCodSCtoDet,@pmPlacaVehDet,@pmCant_Orden)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicionDet] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Fecha,IdRespons,IdCCosto,IdSubCos,IdDep,NContrato,IdCiaCont,NitCliente,CdAgencia,Modalidad
,DirEntrega,IdLocEnt,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev,Observacion,IdEstado
,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,D.Cantidad AS CantArt,IdUnd,VrUnitario,D.Referencia
,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.Cant_Orden,D.CantSalida,D.CantDevSal,D.CodCCtoDet,D.CodSCtoDet,D.PlacaVehDet
 FROM Trn_Requisicion AS R INNER JOIN Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion
 AND R.IdCia=D.IdCia
 WHERE R.TipDoc=@pmTipDoc AND R.Requisicion=@pmRequisicion AND R.IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryReqDetalle] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,CdProducto,Descripcion,Cantidad,IdUnd,IdSubgrupo,VrUnitario
,TipOrd,NumOrden,IdCiaOrd,Cant_Orden,TipSal,NSalida,IdCiaSal,FechaSal,CdBodega,Referencia
,CantSalida,CantDevSal,CodCCtoDet,CodSCtoDet,PlacaVehDet
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
 ORDER BY  Item
GO
