if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelOilCostoVen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paDelOilCostoVen]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsOilCostoVen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsOilCostoVen]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsOilCostoVenProd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsOilCostoVenProd]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsOilDevFacProd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsOilDevFacProd]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevFcrFmtb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDevFcrFmtb]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilCostoVen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilCostoVen]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilCostoVenFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilCostoVenFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpOilCostoVen]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpOilCostoVen]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilDevFacProd] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdTanque VARCHAR(4),@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(500),@pmCantidad DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmVrUnitario DECIMAL(16,6),@pmTarifaDct DECIMAL(16,6),@pmVrDcto DECIMAL(16,6),@pmTarifaIva DECIMAL(16,6),@pmVrIva DECIMAL(16,6),@pmTarifaRet DECIMAL(16,6),@pmVrRetFte DECIMAL(16,6),@pmTarifaIca DECIMAL(16,6),@pmVrRetIca DECIMAL(16,6)
,@pmFleteUnd DECIMAL(16,6),@pmCostoUnit DECIMAL(16,6),@pmItemDet INT,@pmTipoProd VARCHAR(10),@pmTipGuia VARCHAR(3),@pmNumGuia INT,@pmIdCiaGuia CHAR(2),@pmCantGuia DECIMAL(16,6),@pmIndMezcla INT,@pmItemMezcla INT,@pmFactorMezcla DECIMAL(16,6),@pmCodTarIva VARCHAR(4),@pmCodTarDct VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodSicomPro VARCHAR(20),@pmCodBodega VARCHAR(4)
 AS 
 INSERT INTO Trn_OilDevFacProd (TipDev,Devolucion,IdCia,Item,IdTanque,IdProducto,Descripcion,Cantidad,UndMed,VrUnitario,TarifaDct,VrDcto,TarifaIva,VrIva,TarifaRet,VrRetFte,TarifaIca,VrRetIca,FleteUnd,CostoUnit,ItemDet,TipoProd,TipGuia,NumGuia,IdCiaGuia,CantGuia,IndMezcla,ItemMezcla,FactorMezcla,CodTarIva,CodTarDct,CodTarRet,CodTarIca,CodSicomPro,CodBodega)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmItem,@pmIdTanque,@pmIdProducto,@pmDescripcion,@pmCantidad,@pmUndMed,@pmVrUnitario,@pmTarifaDct,@pmVrDcto,@pmTarifaIva,@pmVrIva,@pmTarifaRet,@pmVrRetFte,@pmTarifaIca,@pmVrRetIca,@pmFleteUnd,@pmCostoUnit,@pmItemDet,@pmTipoProd,@pmTipGuia,@pmNumGuia,@pmIdCiaGuia,@pmCantGuia,@pmIndMezcla,@pmItemMezcla,@pmFactorMezcla,@pmCodTarIva,@pmCodTarDct,@pmCodTarRet,@pmCodTarIca,@pmCodSicomPro,@pmCodBodega)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevFcrFmtb] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDev,TipoDoc,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipFact,D.Factura AS NumFactura
,D.IdCiaDoc,D.FecDoc,D.IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrRetencion
,D.VrReteICA,D.VrReteIVA,D.VrFletes,D.VrOtros,D.VrCargos,D.VrOtrDcto,D.VrCostos,D.VrImpCarbono,D.VrIvaIngProd,D.VrNeto,D.Cantidad AS CantTotal,D.BaseImp,D.BaseRet,D.BaseIvaIgp
,D.VrReteCREE,D.TarifaRtc,D.CodTarRtc,D.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,D.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,D.IdVend AS NitVend,VN.RazonSocial AS Vendedor
,D.TarifaCom,D.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,Pedido,IdCiaPed,D.Remision,D.IdCiaRem
,D.FecPedido,D.Modalidad,D.ModdDev,D.PlacaVehic,D.ZonaFrontera,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,D.CdMotDev,MotivoDev
,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,D.IdCiaCrea,D.IdUsuario,Usuario,D.OrigenAdd
--Información 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,DT.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
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,ExcIva,Autoret,VrCupo,VrSaldo
--Detalles
,DP.Item,DP.IdTanque,DP.IdProducto,PM.DescripProd,DP.Descripcion,DP.Cantidad,DP.UndMed,UM.Unidad,DP.VrUnitario,DP.TarifaDct,DP.VrDcto,DP.TarifaIva,DP.VrIva,DP.TarifaRet,DP.VrRetFte,DP.TarifaIca,DP.VrRetIca,DP.FleteUnd,DP.CostoUnit
,DP.ItemDet,DP.TipoProd,DP.TipGuia,DP.NumGuia,DP.IdCiaGuia,DP.CantGuia,DP.IndMezcla,DP.ItemMezcla,DP.FactorMezcla,DP.CodTarIva,DP.CodTarDct,DP.CodTarRet,DP.CodTarIca,DP.CodSicomPro,DP.CodBodega,Bodega
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 FROM Trn_DevFcr AS D INNER JOIN Trn_OilDevFacProd AS DP ON D.TipDev=DP.TipDev AND D.Devolucion=DP.Devolucion AND D.IdCia=DP.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN Agencias AS A ON D.IdAgencia=A.IdAgencia
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DT ON L.IdDep=DT.IdDep 
 INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON DP.IdProducto=PM.IdProducto INNER JOIN Sys_Um AS UM ON DP.UndMed=UM.UndMed
 LEFT JOIN Localidades AS LE ON D.IdLocEnv=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos 
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom LEFT JOIN TiposMotdev AS MD ON D.CdMotDev=MD.IdMotDev
 LEFT JOIN Tanques AS TQ ON DP.IdTanque=TQ.IdTanque  LEFT JOIN Bodegas AS B ON DP.CodBodega=B.IdBodega
 LEFT JOIN Trn_Face AS FE ON D.TipDev=FE.TipDoc AND D.Devolucion=FE.Documento AND D.IdCia=FE.IdCia
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 AND DP.IndMezcla<>2
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCostoVenFmt] @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc,TipoDoc,N.NumNota,N.IdCia,Compania,N.Fecha,N.FechaInicio,N.FechaFinal,N.IdConcepto,Concepto,N.Cantidad AS CantTotal,N.ValorTotal,N.CantDev,N.VrDevFact
,N.TipCom,N.Comprobante,N.IdCiaCom,N.Anulado,N.NumDev,N.FecDev,N.Observacion,N.IdEstado,ED.Estado,N.FechaCrea,N.IdCiaCrea,N.OrigenAdd,N.IdUsuario,Usuario
,D.Item,D.IdProducto,D.Descripcion,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.TipFact,D.NumFactura,D.IdCiaFact,D.ItemFact,D.FechaFact
,D.TipGuia,D.NumGuia,D.IdCiaGuia,D.FechaGuia,D.IdCliente,T.RazonSocial,D.IdAgencia,D.IdVehiculo,D.CdCCosto,CCosto,D.CdSubCos,SubCosto,D.TipoProd,P.DescripProd,P.IdSubgrupo
 FROM Trn_OilCostoVen AS N INNER JOIN Trn_OilCostoVenProd AS D ON N.TipDoc=D.TipDoc AND N.NumNota=D.NumNota AND N.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON N.IdConcepto=CT.IdConcepto
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 WHERE N.TipDoc=@pmTipDoc AND N.NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilCostoVen] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmCantidad DECIMAL(16,6),@pmValorTotal DECIMAL(16,6),@pmCantDev DECIMAL(16,6),@pmVrDevFact DECIMAL(16,6),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilCostoVen (TipDoc,NumNota,IdCia,Fecha,FechaInicio,FechaFinal,IdConcepto,Cantidad,ValorTotal,CantDev,VrDevFact,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmFechaInicio,@pmFechaFinal,@pmIdConcepto,@pmCantidad,@pmValorTotal,@pmCantDev,@pmVrDevFact,@pmCdCCosto,@pmCdSubCos,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilCostoVen] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmCantidad DECIMAL(16,6),@pmValorTotal DECIMAL(16,6),@pmCantDev DECIMAL(16,6),@pmVrDevFact DECIMAL(16,6),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OilCostoVen SET Fecha=@pmFecha,FechaInicio=@pmFechaInicio,FechaFinal=@pmFechaFinal,IdConcepto=@pmIdConcepto,Cantidad=@pmCantidad,ValorTotal=@pmValorTotal,CantDev=@pmCantDev,VrDevFact=@pmVrDevFact,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCostoVen] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,FechaInicio,FechaFinal,IdConcepto,Cantidad,ValorTotal,CantDev,VrDevFact,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilCostoVen
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilCostoVen] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilCostoVen WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilCostoVenProd] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(500),@pmCantidad DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmVrUnitario DECIMAL(16,6),@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmIdCiaFact CHAR(2),@pmItemFact INT,@pmFechaFact SMALLDATETIME,@pmTipGuia VARCHAR(3),@pmNumGuia INT,@pmIdCiaGuia CHAR(2),@pmFechaGuia SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipoProd VARCHAR(10)
 AS 
 INSERT INTO Trn_OilCostoVenProd (TipDoc,NumNota,IdCia,Item,IdProducto,Descripcion,Cantidad,UndMed,VrUnitario,TipFact,NumFactura,IdCiaFact,ItemFact,FechaFact,TipGuia,NumGuia,IdCiaGuia,FechaGuia,IdCliente,IdAgencia,IdVehiculo,CdCCosto,CdSubCos,TipoProd)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmItem,@pmIdProducto,@pmDescripcion,@pmCantidad,@pmUndMed,@pmVrUnitario,@pmTipFact,@pmNumFactura,@pmIdCiaFact,@pmItemFact,@pmFechaFact,@pmTipGuia,@pmNumGuia,@pmIdCiaGuia,@pmFechaGuia,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmCdCCosto,@pmCdSubCos,@pmTipoProd)
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilTrasladoFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilTrasladoFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsOilTrasladoProd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsOilTrasladoProd]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilTrasladoFmt] @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc,TipoDoc,N.NumNota,N.IdCia,Compania,N.Fecha,N.FechaInicio,N.FechaFinal,N.IdTercero,T.RazonSocial,N.CantMP,N.CantProducido,N.CantConsumo,N.CantCrudoCom
,N.Total_MP,VU_MP,CantBrutoMP,CantNetoMP,CantFactMP,Total_TRA,VU_TRA,CantNetoTRA,CantFactTRA,Total_TVacio,VU_TVacio,CantNetoTv,CantFactTv,Total_Standby,VU_Standby,Cant_Standby,Total_MOD,Total_CIF
,N.IdConcepto,Concepto,N.CdCCosto,CCosto,N.CdSubCos,SubCosto,N.TipCom,N.Comprobante,N.IdCiaCom,N.Anulado,N.NumDev,N.FecDev,N.Observacion,N.IdEstado,ED.Estado,N.FechaCrea,N.IdCiaCrea,N.OrigenAdd,N.IdUsuario,Usuario
--Detalles
,D.Item,D.IdProducto,D.Descripcion,D.TipoProd,D.SaldoInicial,D.SaldoFinal,D.Entradas,D.Despachos,D.DespMezcla,D.Salidas,D.AjuSobrante,D.AjuFaltante,D.Cantidad,D.UndMed,UM.Unidad
,D.PorcRend,D.CostoAnt,D.VrUnitario,D.VrTotal,D.SaldoMPAnt,D.CantProdAnt,D.CostoMPAnt,D.CostoMPMes,D.CostoUndMP,D.CostoUndTra,D.CostoTotalTra,D.CostoTotalSB,D.CostoUndVco,D.CostoTotalVco
,D.CostoTotalMod,D.CostoTotalCif,P.DescripProd,P.IdSubgrupo,D.CodMezcla,D.CdoSubMezcla,D.Producido,D.CantVehTnsto,D.CantBrutoDesp,D.CantNetoDesp,D.CantTraDesp
FROM Trn_OilTraslado AS N INNER JOIN Trn_OilTrasladoProd AS D ON N.TipDoc=D.TipDoc AND N.NumNota=D.NumNota AND N.IdCia=D.IdCia
INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero
INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON N.IdConcepto=CT.IdConcepto
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
LEFT JOIN CentroCosto AS CC ON N.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON N.CdSubCos=SC.IdSubCos
WHERE N.TipDoc=@pmTipDoc AND N.NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilTrasladoProd] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(500),@pmTipoProd VARCHAR(10),@pmSaldoInicial DECIMAL(16,6),@pmSaldoFinal DECIMAL(16,6),@pmEntradas DECIMAL(16,6),@pmDespachos DECIMAL(16,6),@pmDespMezcla DECIMAL(16,6),@pmSalidas DECIMAL(16,6),@pmAjuSobrante DECIMAL(16,6),@pmAjuFaltante DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmPorcRend DECIMAL(16,6)
,@pmCostoAnt DECIMAL(16,6),@pmVrUnitario DECIMAL(16,6),@pmVrTotal DECIMAL(16,6),@pmSaldoMPAnt DECIMAL(16,6),@pmCantProdAnt DECIMAL(16,6),@pmCostoMPAnt DECIMAL(16,6),@pmCostoMPMes DECIMAL(16,6),@pmCostoUndMP DECIMAL(16,6),@pmCostoUndTra DECIMAL(16,6),@pmCostoTotalTra DECIMAL(16,6),@pmCostoTotalSB DECIMAL(16,6),@pmCostoUndVco DECIMAL(16,6),@pmCostoTotalVco DECIMAL(16,6),@pmCostoTotalMod DECIMAL(16,6),@pmCostoTotalCif DECIMAL(16,6),@pmCodMezcla VARCHAR(16),@pmCdoSubMezcla VARCHAR(8)
,@pmProducido DECIMAL(16,6),@pmCantVehTnsto DECIMAL(16,6),@pmCantBrutoDesp DECIMAL(16,6),@pmCantNetoDesp DECIMAL(16,6),@pmCantTraDesp DECIMAL(16,6)
 AS 
 INSERT INTO Trn_OilTrasladoProd (TipDoc,NumNota,IdCia,Item,IdProducto,Descripcion,TipoProd,SaldoInicial,SaldoFinal,Entradas,Despachos,DespMezcla,Salidas,AjuSobrante,AjuFaltante,Cantidad,UndMed,PorcRend,CostoAnt,VrUnitario,VrTotal,SaldoMPAnt,CantProdAnt,CostoMPAnt,CostoMPMes,CostoUndMP,CostoUndTra,CostoTotalTra,CostoTotalSB,CostoUndVco,CostoTotalVco,CostoTotalMod,CostoTotalCif,CodMezcla,CdoSubMezcla,Producido,CantVehTnsto,CantBrutoDesp,CantNetoDesp,CantTraDesp)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmItem,@pmIdProducto,@pmDescripcion,@pmTipoProd,@pmSaldoInicial,@pmSaldoFinal,@pmEntradas,@pmDespachos,@pmDespMezcla,@pmSalidas,@pmAjuSobrante,@pmAjuFaltante,@pmCantidad,@pmUndMed,@pmPorcRend,@pmCostoAnt,@pmVrUnitario,@pmVrTotal,@pmSaldoMPAnt,@pmCantProdAnt,@pmCostoMPAnt,@pmCostoMPMes,@pmCostoUndMP,@pmCostoUndTra,@pmCostoTotalTra,@pmCostoTotalSB,@pmCostoUndVco,@pmCostoTotalVco,@pmCostoTotalMod
,@pmCostoTotalCif,@pmCodMezcla,@pmCdoSubMezcla,@pmProducido,@pmCantVehTnsto,@pmCantBrutoDesp,@pmCantNetoDesp,@pmCantTraDesp)
GO
