Help! Query Activos Fijos por Año

Que tal buen día comunidad, espero no molestar y si alguien puede ayudarme lo agradecería, tengo muy poco en el uso del aplicativo y algunos temas de repente se me complican, la contadora de la empresa me comento si se puede adaptar un query que ya esta creado, necesita seleccionar el año y el actual hace la consulta al año en curso, anexo el codigo.

declare @ultima_amor date
declare @mes numeric (19,6)
declare @periodo numeric (19,6)
declare @cursor numeric (19,6)
declare @año numeric (19,6)

set @ultima_amor = (select max(t0.keydate) from odrn t0)
set @mes = month(@ultima_amor)
set @periodo = (select max (t0.periodcat) from itm8 t0)
set @cursor = 1
set @año = year(@ultima_amor)

SELECT Distinct T0.ItemCode as [Codigo del Activo], 
T0.ItemName as [Nombre del activo],
T0.AssetClass as [Clase de activo], 
T1.DprStart as [Fecha Inicio de Depreciacion], 
T1.DprEnd as [Fecha Fin de Depreciacion], 
T1.UsefulLife as [Vida util], 
case when @año = year(T1.DprStart) then case when @mes = month(T1.DprStart) then (t1.RemainLife -1) else T1.RemainLife - (@mes - (month(t1.DprStart) - 1)) end else (T1.RemainLife - @mes) end as [Resto de vida], 
case when @año = year(T1.DprStart) then T3.LineTotal else T2.APC end as [Capitalizacion], 
case when @año = year(T1.DprStart) then T3.LineTotal / T1.usefulLife else (T2.APC / T1.UsefulLife) end as [depreciacion por mes], 
case when @año = year(T1.DprStart) then case when month(T1.DprStart) <= @mes then (T3.LineTotal / T1.UsefulLife) * (@mes - (month(t1.DprStart) - 1)) else 0 end else (((T2.APC / T1.UsefulLife) * @mes) + T2.OrDpAcc) end  as [Depreciacion acumulada], 

case when @cursor <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 1 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 1 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Enero],

case when @cursor + 1 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 2 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 2 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Febrero],

case when @cursor + 2 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 3 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 3 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Marzo],

case when @cursor + 3 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 4 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 4 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Abril],

case when @cursor + 4 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 5 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 5 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife  end end as [Mayo],

case when @cursor + 5 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 6 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 6 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Junio],

case when @cursor + 6 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 7 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 7 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Julio],

case when @cursor + 7 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 8 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 8 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Agosto],

case when @cursor + 8 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 9 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 9 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Septiembre],

case when @cursor + 9 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 10 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 10 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Octubre],

case when @cursor + 10 <= @mes then case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 11 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 11 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Noviembre],

case when @cursor + 11 <= @mes then  case when @año = year(T1.DprStart) then case when  month(T1.DprStart) = 12 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 12 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Diciembre]

FROM OITM T0 INNER JOIN ITM7 T1 ON T0.ItemCode = T1.ItemCode INNER JOIN ITM8 T2 ON T1.ItemCode = T2.ItemCode LEFT JOIN ACQ1 T3 ON T0.ItemCode = T3.ItemCode

WHERE T1.RemainLife <> 0 and @ultima_amor < t1.DprEnd and T1.PeriodCat = @periodo and T2.PeriodCat = @periodo

Agradezco al que pueda ayudarme :sweat_smile: :pray:

Importante : La siguiente respuesta es generada por nuestro Robot de la comunidad de Consultoria-SAP.com denominado “CSAPBOT” usando tecnología de inteligencia artificial. Por lo tanto, puede haber datos incorrectos. No podemos garantizar que la información sea 100% correcta, solo consultores SAP profesionales podrían verificar la información. Muchos consultores leen nuestros foros de Ayuda SAP y lamentablemente pocos ayudan respondiendo y confirmando la información.

Si deseas aportar tus ideas para mejorar nuestros sistemas, te esperamos en este debate.


:receipt: SAP Business One – Adaptar query de depreciación para que el usuario seleccione el año

Hola, muy buena tu consulta y ¡bienvenido/a a la comunidad! :raised_hands:
Tu query está muy completo y bien estructurado, pero como bien comentas, el año se toma automáticamente desde la fecha de la última amortización (@ultima_amor). Lo que necesita tu contadora es poder seleccionar manualmente el año a consultar, para comparar o analizar años anteriores o futuros.

Vamos a modificarlo para que el usuario ingrese el año de forma dinámica, usando los parámetros del Query Manager.


:white_check_mark: Paso 1: Reemplazar la variable del año por parámetro del usuario

En lugar de:

set @año = year(@ultima_amor)

Usaremos:

set @año = [%0]

Esto permite que el usuario, al ejecutar el query, vea un cuadro de diálogo donde puede escribir o seleccionar el año que desea consultar (por ejemplo: 2023, 2024, etc.).


:white_check_mark: Paso 2: Ajustar tipo de variable @año

Te recomiendo cambiar la declaración de la variable @año a tipo entero, ya que representa un año calendario:

declare @año int

:white_check_mark: Paso 3: Query ajustado (fragmento clave)

Este es el cambio principal que necesitas:

declare @ultima_amor date
declare @mes numeric (19,6)
declare @periodo numeric (19,6)
declare @cursor numeric (19,6)
declare @año int  -- Cambiado de numeric a int

set @ultima_amor = (select max(t0.keydate) from odrn t0)
set @mes = month(@ultima_amor)
set @periodo = (select max (t0.periodcat) from itm8 t0)
set @cursor = 1
set @año = [%0]  -- Parámetro de usuario para el año deseado

:white_check_mark: Opcional: Mostrar el cuadro de entrada con etiqueta personalizada

Si quieres que el cuadro de diálogo diga por ejemplo:
“Ingrese el año a consultar”, puedes usar:

set @año = [%0] /* Año a consultar */

Así, SAP B1 mostrará esa etiqueta cuando el usuario ejecute el query desde Query Manager.


:bulb: Consejos adicionales

  • Puedes agregar un segundo parámetro si quieres también filtrar por mes específico, o hasta por rango de fechas si lo necesitas.
  • Si hay errores de tipo por la variable @año, asegúrate de que los campos con los que se compara también sean tipo INT o conviértelos con YEAR().

:white_check_mark: Resumen Consultoria-SAP

Para permitir que el usuario seleccione el año al ejecutar tu query de depreciación en SAP Business One:

  1. Reemplaza set @año = year(@ultima_amor) por set @año = [%0].
  2. Declara @año como int.
  3. Agrega una etiqueta descriptiva para facilitar el uso del query por usuarios contables.

Con esto, tu contadora podrá ejecutar el mismo informe para cualquier año con solo ingresar el dato en pantalla, sin modificar el SQL.

Si necesitas que te ayude a adaptar este mismo reporte para comparar varios años o ver la evolución de la depreciación por periodo, dime y con gusto te ayudo a armarlo.

¡Estamos aquí para ayudarte! :blush: