dbo.clientes_condiciones_facturacion_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

19
Name Data Type Length Description
@action varchar 30
@p_cliente int 4
@p_producto int 4
@user_id int 4
@producto_no_se_factura bit 1
@prod_codigo_factura varchar 10
@tipo_facturacion char 1
@Lista_PrecioEspecial_Cuotas char 1
@lista_precios int 4
@precio_especial_tipo char 1
@precio_especial_moneda varchar 3
@precio_especial decimal 9
@precio_especial_excedentes decimal 9
@cantidad_cuotas int 4
@moneda_cuotas varchar 3
@valor_cuota decimal 9
@bonificacion_porcentaje decimal 5
@bonificacion_desde date 3
@bonificacion_hasta date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
select * from clientes_condiciones_facturacion
*/



CREATE OR ALTER PROCEDURE dbo.clientes_condiciones_facturacion_
 @action  varchar(30),
 @p_cliente int,
 @p_producto int, 
 @user_id int        = NULL,
 @producto_no_se_factura bit     = NULL,
 @prod_codigo_factura varchar(10)   = NULL,
 @tipo_facturacion char(1)     = NULL,
 @Lista_PrecioEspecial_Cuotas char(1)  = NULL,
 @lista_precios int       = NULL,
 @precio_especial_tipo char(1)    = NULL,
 @precio_especial_moneda varchar(3)   = NULL,
 @precio_especial decimal(18, 2)    = NULL,
 @precio_especial_excedentes decimal(18, 2) = NULL,
 @cantidad_cuotas int      = NULL,
 @moneda_cuotas varchar(3)     = NULL,
 @valor_cuota decimal(18, 2)     = NULL,
 @bonificacion_porcentaje decimal(5, 2)  = NULL,
 @bonificacion_desde date     = NULL,
 @bonificacion_hasta date     = NULL

AS
BEGIN
 
 SET NOCOUNT ON;

 IF (@action = 'SELECT') 
 BEGIN
 SELECT  cliente,
   producto,
   prod_codigo_factura,
   producto_no_se_factura,
   tipo_facturacion,
   lista_precios,
   precio_especial_tipo,
   precio_especial_moneda,
   precio_especial,
   precio_especial_excedentes,
   Lista_PrecioEspecial_Cuotas,
   moneda_cuotas,
   cantidad_cuotas,
   valor_cuota,
   cuotas_cobradas,
   bonificacion_porcentaje,
   bonificacion_desde,
   bonificacion_hasta,
   fecha_ultimo_cierre,
   fecha_ultima_factura
   from clientes_condiciones_facturacion
   WHERE cliente = @p_cliente
   AND producto = @p_producto
   and visible = 1

 END 


 IF (@action = 'GRABAR') 
 BEGIN
  declare @grabar bit = 0
  if not exists (select * from clientes_condiciones_facturacion WHERE cliente = @p_cliente AND producto = @p_producto and visible = 1)
  begin
   set @grabar = 1
  end 
  else 
  begin
  -- Vemos si hubo cambios 
   declare @id_registro int = NULL, 
           @producto_no_se_factura_anterior bit    = NULL,
     @tipo_facturacion_anterior char(1)     = NULL,
     @prod_codigo_factura_anterior varchar(10)   = NULL,
     @Lista_PrecioEspecial_Cuotas_anterior char(1)  = NULL,
     @lista_precios_anterior int       = NULL,
     @precio_especial_tipo_anterior char(1)    = NULL,
     @precio_especial_moneda_anterior varchar(3)   = NULL,
     @precio_especial_anterior decimal(18, 2)   = NULL,
     @precio_especial_excedentes_anterior decimal(18, 2) = NULL,
     @cantidad_cuotas_anterior int      = NULL,
     @moneda_cuotas_anterior varchar(3)     = NULL,
     @valor_cuota_anterior decimal(18, 2)    = NULL,
     @cuotas_cobradas_anterior int                       = NULL,
     @bonificacion_porcentaje_anterior decimal(5, 2)  = NULL,
     @bonificacion_desde_anterior date     = NULL,
     @bonificacion_hasta_anterior date     = NULL,
     @fecha_ultimo_cierre date       = NULL,
     @fecha_ultima_factura date       = NULL
   SELECT  top 1 
     @id_registro = id_registro,
     @producto_no_se_factura_anterior = producto_no_se_factura,
     @tipo_facturacion_anterior = tipo_facturacion,
     @prod_codigo_factura_anterior = prod_codigo_factura,
     @Lista_PrecioEspecial_Cuotas_anterior = Lista_PrecioEspecial_Cuotas,
     @lista_precios_anterior = lista_precios,
     @precio_especial_tipo_anterior = precio_especial_tipo,
     @precio_especial_moneda_anterior = precio_especial_moneda,
     @precio_especial_anterior = precio_especial,
     @precio_especial_excedentes_anterior = precio_especial_excedentes,
     @cantidad_cuotas_anterior = cantidad_cuotas,
     @moneda_cuotas_anterior = moneda_cuotas,
     @valor_cuota_anterior = valor_cuota,
     @bonificacion_porcentaje_anterior = bonificacion_porcentaje,
     @bonificacion_desde_anterior = bonificacion_desde,
     @bonificacion_hasta_anterior = bonificacion_hasta,
     @fecha_ultimo_cierre = fecha_ultimo_cierre,
     @fecha_ultima_factura = fecha_ultima_factura
    from clientes_condiciones_facturacion
    WHERE cliente = @p_cliente
     AND producto = @p_producto
     and visible = 1
     order by created desc
   if (isnull(@producto_no_se_factura_anterior,'0')   != isnull(@producto_no_se_factura,'0') or 
    isnull(@tipo_facturacion_anterior,'')     != isnull(@tipo_facturacion,'') or 
    isnull(@prod_codigo_factura_anterior,'')    != isnull(@prod_codigo_factura,'') or 
    isnull(@Lista_PrecioEspecial_Cuotas_anterior,'')  != isnull(@Lista_PrecioEspecial_Cuotas,'') or 
    isnull(@lista_precios_anterior,'0')     != isnull(@lista_precios,'0') or 
    isnull(@precio_especial_tipo_anterior,'')    != isnull(@precio_especial_tipo,'') or 
    isnull(@precio_especial_moneda_anterior,'')   != isnull(@precio_especial_moneda,'') or 
    isnull(@precio_especial_anterior,'0')     != isnull(@precio_especial,'0') or 
    isnull(@precio_especial_excedentes_anterior,'0')  != isnull(@precio_especial_excedentes,'0') or 
    isnull(@cantidad_cuotas_anterior,'0')     != isnull(@cantidad_cuotas,'0') or 
    isnull(@moneda_cuotas_anterior,'')      != isnull(@moneda_cuotas,'') or 
    isnull(@valor_cuota_anterior,'0')      != isnull(@valor_cuota,'0') or 
    isnull(@bonificacion_porcentaje_anterior,'0')   != isnull(@bonificacion_porcentaje,'0') or 
    isnull(@bonificacion_desde_anterior,'2999-12-31')  != isnull(@bonificacion_desde,'2999-12-31') or 
    isnull(@bonificacion_hasta_anterior,'2999-12-31')  != isnull(@bonificacion_hasta,'2999-12-31'))
   begin
    UPDATE clientes_condiciones_facturacion SET visible = 0, modifed_by = @user_id, modified = getdate()
    WHERE cliente = @p_cliente
     AND producto = @p_producto
     and visible = 1
    set @grabar = 1
   end
   else
   begin
    if @id_registro is not null -- No deberia haber, pero por las dudas veo si hay otro visible = 1 y lo paso a visible = 0
    begin
     if exists (select * from clientes_condiciones_facturacion WHERE cliente = @p_cliente AND producto = @p_producto and visible = 1 and id_registro != @id_registro)
     begin
      UPDATE clientes_condiciones_facturacion SET visible = 0, modifed_by = @user_id, modified = getdate()
      WHERE cliente = @p_cliente
       AND producto = @p_producto
       and visible = 1
       and id_registro != @id_registro
     end
    end
   end 
  end
  if (@grabar = 1)
  begin
  
   insert into clientes_condiciones_facturacion(cliente,
              producto,
              prod_codigo_factura,
              producto_no_se_factura,
              tipo_facturacion,
              Lista_PrecioEspecial_Cuotas,
              lista_precios,
              precio_especial_tipo,
              precio_especial_moneda,
              precio_especial,
              precio_especial_excedentes,
              cantidad_cuotas,
              moneda_cuotas,
              valor_cuota,
              bonificacion_porcentaje,
              bonificacion_desde,
              bonificacion_hasta,
              fecha_ultimo_cierre,
              fecha_ultima_factura,
              visible, 
              created,
              created_by)
           values  ( @p_cliente,
              @p_producto,
              @prod_codigo_factura,
              @producto_no_se_factura,
              @tipo_facturacion,
              @Lista_PrecioEspecial_Cuotas,
              @lista_precios,
              @precio_especial_tipo,
              @precio_especial_moneda,
              @precio_especial,
              @precio_especial_excedentes,
              @cantidad_cuotas,
              @moneda_cuotas,
              @valor_cuota,
              @bonificacion_porcentaje,
              @bonificacion_desde,
              @bonificacion_hasta,
              @fecha_ultimo_cierre,
              @fecha_ultima_factura,
              1,
              getdate(),
              @user_id)
  end
  select 'ok'
 END 

      
END

GO

Used By

No items found