Skip to content

Instantly share code, notes, and snippets.

@molekilla
Created November 22, 2019 20:02
Show Gist options
  • Save molekilla/afeb316dd717da97047cd6ea1403d07b to your computer and use it in GitHub Desktop.
Save molekilla/afeb316dd717da97047cd6ea1403d07b to your computer and use it in GitHub Desktop.
CREATE DEFINER=`root`@`localhost` PROCEDURE `calc_depreciacion_2`()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE depre INTEGER DEFAULT 0;
DECLARE valor_original DECIMAL(10,2);
DECLARE porc_depreciacion DECIMAL(10,2);
DECLARE codigo_cuenta VARCHAR(25);
DECLARE estado INTEGER DEFAULT 0;
DECLARE id_activo INTEGER DEFAULT 0;
DECLARE fecha_compra DATE;
DECLARE _depre DECIMAL(10,2) DEFAULT 0;
DECLARE _depreAnual DECIMAL(10,2) DEFAULT 0;
DECLARE _depreMensual DECIMAL(10,2) DEFAULT 0;
DECLARE _depreAcumulada DECIMAL(10,2) DEFAULT 0;
DECLARE _valor DECIMAL(10,2) DEFAULT 0;
DECLARE _amountDays INTEGER DEFAULT 1;
DECLARE start_time DATE DEFAULT current_date();
DECLARE end_time DATE DEFAULT current_date();
DECLARE diff_temp DATE DEFAULT current_date();
DECLARE diff_temp2 DATE DEFAULT current_date();
-- declare cursor for activos
DEClARE cursorActivos
CURSOR FOR
SELECT a.id_activo id_activo, a.status estado, a.codigo_cuenta codigo_cuenta, a.valor_original valor_original, sub.depre depre FROM infoplaza_dts.activos a INNER JOIN infoplaza_dts.subespecificacion sub
ON a.codigo_cuenta = sub.cuenta AND
a.id_division = sub.division AND
a.codigo_especificacion = sub.especificacion AND
a.codigo_subespec = sub.subespecificacion AND
a.valor_original >= 100;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN cursorActivos;
getActivos: LOOP
FETCH cursorActivos INTO fecha_compra, porc_depreciacion, id_activo, estado, codigo_cuenta, valor_original, depre;
IF finished = 1 THEN
LEAVE getActivos;
END IF;
-- debugger
select fecha_compra, porc_depreciacion, id_activo, estado, codigo_cuenta, valor_original, depre;
SET _depreAnual = valor_original * porc_depreciacion/100;
SET _depre = _depreAnual / 365;
SET _depreMensual = _depreAnual / 12;
SET _valor = 0;
SET _amountDays = 1;
IF ISNULL(fecha_compra) IS false THEN
SET end_time = fecha_compra;
SET diff_temp = unix_timestamp(current_date()) - unix_timestamp(fecha_compra);
SET diff_temp2 = diff_temp / (1000 * 60 * 60 * 24);
SET _amountDays = _amountDays + diff_temp2;
sum: LOOP
IF _depreAcumulada < valor_original THEN
SET _depreAcumulada = _depreAcumulada + _depre;
SET _valor = valor_original - _depreAcumulada;
ELSE
SET _valor = 1;
END IF;
SET _amountDays = _amountDays - 1;
IF _amountDays < 0 THEN
LEAVE sum;
END IF;
ITERATE sum;
END LOOP;
IF _depreAcumulada >= valor_original THEN
SET _depreAcumulada = valor_original - 1;
END IF;
-- missing tiempo uso
UPDATE activos SET deprec_anual = _depreAnual,
deprec_mensual = _depreMensual,
deprec_diario = _depre,
depreciacion_total = _depreAcumulada,
valor_actual = _valor
WHERE activos.id_activo = id_activo;
END IF;
END LOOP getActivos;
CLOSE cursorActivos;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment