ALTER TABLE TarifasFletes ADD TarifAfiliado DECIMAL(16,6) DEFAULT(0) NOT NULL
,TarifTercero DECIMAL(16,6) DEFAULT(0) NOT NULL
,TarifPropio DECIMAL(16,6) DEFAULT(0) NOT NULL
GO
INSERT INTO Sys_Roles (IdObj,IdRole,Funcion)
 VALUES ('FRMDFCT','FOC','Permitir Facturar remesas de otros clientes')
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTarifasFletes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTarifasFletes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTarifasFletes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTarifasFletes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTarifasFletes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTarifasFletes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTarifasFletesLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTarifasFletesLta]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [paInsTarifasFletes] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmNumero INT,@pmTipoTarifa VARCHAR(10),@pmTarifClie MONEY,@pmTarifPago MONEY
,@pmTarifTabla MONEY,@pmIdMneda VARCHAR(5),@pmUndMed VARCHAR(10),@pmNitCliente VARCHAR(16),@pmCdNat VARCHAR(4),@pmCdTipoVeh VARCHAR(4)
,@pmCdRango VARCHAR(4),@pmUndTarifClie VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmCdMercancia VARCHAR(16)
,@pmTipoAfiVehic VARCHAR(10),@pmTarifAfiliado DECIMAL(16,6),@pmTarifTercero DECIMAL(16,6),@pmTarifPropio DECIMAL(16,6),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TarifasFletes (Numero,IdOrigen,IdDestino,CdRuta,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed,NitCliente,CdNat,CdTipoVeh,CdRango
,UndTarifClie,UndTarifPago,TipoAfiVehic,TarifAfiliado,TarifTercero,TarifPropio,Inactivo,CdMercancia,FechaCrea,IdUsuario)
 VALUES (@pmNumero,@pmIdOrigen,@pmIdDestino,@pmCdRuta,@pmTipoTarifa,@pmTarifClie,@pmTarifPago,@pmTarifTabla,@pmIdMneda,@pmUndMed,@pmNitCliente
,@pmCdNat,@pmCdTipoVeh,@pmCdRango,@pmUndTarifClie,@pmUndTarifPago,@pmTipoAfiVehic,@pmTarifAfiliado,@pmTarifTercero,@pmTarifPropio,@pmInactivo,@pmCdMercancia,@pmFechaCrea,@pmIdUsuario)


GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [paUpTarifasFletes] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmNumero INT,@pmTipoTarifa VARCHAR(10),@pmTarifClie MONEY,@pmTarifPago MONEY,@pmTarifTabla MONEY,@pmIdMneda VARCHAR(5)
,@pmUndMed VARCHAR(10),@pmNitCliente VARCHAR(16),@pmCdNat VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmCdRango VARCHAR(4)
,@pmUndTarifClie VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmCdMercancia VARCHAR(16),@pmTipoAfiVehic VARCHAR(10)
,@pmTarifAfiliado DECIMAL(16,6),@pmTarifTercero DECIMAL(16,6),@pmTarifPropio DECIMAL(16,6)
 AS 
 UPDATE TarifasFletes SET TipoTarifa=@pmTipoTarifa,TarifClie=@pmTarifClie,TarifPago=@pmTarifPago,TarifTabla=@pmTarifTabla,IdMneda=@pmIdMneda,UndMed=@pmUndMed,NitCliente=@pmNitCliente
,CdNat=@pmCdNat,CdTipoVeh=@pmCdTipoVeh,CdRango=@pmCdRango,Inactivo=@pmInactivo,UndTarifClie=@pmUndTarifClie,UndTarifPago=@pmUndTarifPago,CdRuta=@pmCdRuta
,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,CdMercancia=@pmCdMercancia,TipoAfiVehic=@pmTipoAfiVehic
,TarifAfiliado=@pmTarifAfiliado,TarifTercero=@pmTarifTercero,TarifPropio=@pmTarifPropio
 WHERE  Numero=@pmNumero


GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [paQryTarifasFletes] @pmNumero INT
 AS 
 SELECT Numero,IdOrigen,IdDestino,CdRuta,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed
,NitCliente,CdNat,CdTipoVeh,CdRango,UndTarifClie,UndTarifPago
,Inactivo,CdMercancia,TipoAfiVehic,TarifAfiliado,TarifTercero,TarifPropio,FechaCrea,IdUsuario
 FROM TarifasFletes
 WHERE  Numero=@pmNumero


GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [paQryTarifasFletesLta] @pmTipoTarifa VARCHAR(10)=Null,@pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null
,@pmNitCliente VARCHAR(16)=Null,@pmInactivo BIT=Null,@pmCdRuta VARCHAR(4)=Null
 AS 
 SELECT Numero,IdOrigen,O.Localidad AS CiuOrigen,IdDestino,D.Localidad AS CiuDestino,CdRuta,TarifClie,TarifPago,TarifTabla,IdMneda,T.UndMed AS Und_Med,Unidad
,NitCliente,RazonSocial,CdNat,Natlzaprod,CdTipoVeh,TipoVehiculo,CdRango,DescripRango,T.Inactivo AS Inactvo,UndTarifClie,UndTarifPago,CdMercancia,TipoAfiVehic
,TarifAfiliado,TarifTercero,TarifPropio,FechaCrea,T.IdUsuario AS CdUsuario,Usuario
,O.IdDep AS CdDep,DPO.Departamento AS DptoOrigen,D.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,TipoTarifa
 FROM TarifasFletes AS T INNER JOIN Localidades AS O ON T.IdOrigen=O.IdLocal INNER JOIN Localidades AS D ON T.IdDestino=D.IdLocal
 INNER JOIN Departamentos AS DPO ON O.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON D.IdDep=DPD.IdDep
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS TC ON T.NitCliente=TC.IdTercero LEFT JOIN Sys_Um AS UM ON T.UndMed=UM.UndMed 
 LEFT JOIN TiposNat AS N ON T.CdNat=N.IdNat LEFT JOIN TiposVeh AS TV ON T.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN RangosPeso AS R ON T.CdRango=R.IdRango
 WHERE TipoTarifa LIKE ISNULL(@pmTipoTarifa,'%') AND  IdOrigen LIKE ISNULL(@pmIdOrigen,'%')
 AND IdDestino LIKE ISNULL(@pmIdDestino,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 AND CdRuta LIKE ISNULL(@pmCdRuta,'%')
 AND  (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY O.Localidad,D.Localidad,Numero


