SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[paQrytm_FactNetoDet] @pmtmEst CHAR(2)
 AS 
 SELECT tmTipFac,tmFactura,tmIdCia,Compania,F.Fecha,F.FechaVence,T.TipoId,F.IdCliente,T.Dv,T.RazonSocial AS NomCliente,F.IdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,tmItem,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,CdTanque,tmEntradas,tmSalidas,tmSubTotal,tmDescuento,tmImpuesto,tmRetencion,tmReteICA,tmReteIVA,tmReteCREE,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun
,tmImpGlobal,tmOtros,tmRec_Costo,tmMgenCont,tmIvaInf,tmCargos,tmOtrDcto,tmFletes,tmCostos,tmFaltantes,tmAnticipos,tmImpCons,tmNeto AS VrNeto
,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,K.VrPrecio,K.Unidades,K.Referencia,K.Referencia2,K.CdLocal AS KarCdLocal,LE.Localidad AS KarLocalidad,LE.IdDep AS KarCodDep,DE.Departamento AS KarDpto
,K.CdCCosto,CCosto,K.CdSubCos,SubCosto,ListaPrec,K.VrBruto,K.VrBase,K.Servcios,K.NoVentas,K.EsCombo,K.EsProdBase,K.ItemCombo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo
,G.IdLinea AS CdLinea,Linea,PM.CodBarras,DescripLong,DescripAbrv
,F.BaseImp,F.BaseRet,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,F.TarifaRtc,F.TipPed,F.Pedido,F.IdCiaPed,F.TipRem,F.Remision,F.IdCiaRem,F.TipCot,F.Cotizacion,F.IdCiaCot,F.FecPedido
,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,F.TarifaCom,F.pVehiculo AS PlacaVeh,F.CdConductor,TC.RazonSocial AS Conductor,F.NitEmpTrans,F.EmpTrans,F.DirEnvio,DiasEntraga,F.IdForma AS CdForma,FormaPago,DetallePago
,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,Modalidad,F.IdConcepto AS CdConcepto,Concepto,F.TipCom,F.Comprobante,F.IdCiaCom
,F.Observacion,F.NumDev,F.FecDev,F.TimeSys AS FechaCrea,F.FecUpdate AS FechaAct,F.IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
,K.Remision AS KarNumRemision,K.IdCiaRem AS KarCdCiaRem
--Información del tercero
,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,D.Departamento AS TercDpto
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.TelMovil AS TercCelular,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.TipoCliente,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo
,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
,A.CdFntePago AS CdFuente,FuentePago,TipoZona,A.CodSicom
,V.Observacion AS VehObservacion,V.Descripcion AS VehDescripcion
--informacion de la guia
,TG.NumVerifica
FROM tm_FactNeto AS TF INNER JOIN Trn_Facturas AS F ON TF.tmTipFac=F.TipDoc AND TF.tmFactura=F.Factura AND TF.tmIdCia=F.IdCia
INNER JOIN Trn_Kardex AS K ON TF.tmTipFac=K.TipDoc AND TF.tmFactura=K.Documento AND TF.tmIdCia=K.IdCia AND TF.tmItem=K.Item
INNER JOIN Companias AS CN ON TF.tmIdCia=CN.IdCia INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.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 K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea 
LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Vehiculos AS V ON F.pVehiculo=V.IdVehiculo 
LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos 
LEFT JOIN Localidades AS LE ON K.CdLocal=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
LEFT JOIN trn_guia AS TG ON TG.TipFac=F.TipDoc AND TG.Factura=F.Factura AND TG.IdCiaFac=F.IdCia AND K.Remision=TG.Guia AND K.IdCiaRem=TG.IdCia
WHERE tmEst=@pmtmEst
