Skip to content

Instantly share code, notes, and snippets.

@molekilla
Created January 8, 2019 00:35
Show Gist options
  • Save molekilla/3967e0c0f1a34e905ee12041ea0c138c to your computer and use it in GitHub Desktop.
Save molekilla/3967e0c0f1a34e905ee12041ea0c138c to your computer and use it in GitHub Desktop.
Deprecia MEF
package com.sigag.scheduler.jobs;
import io.reactivex.Observable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class DepreciaJob {
private static final Logger log = LoggerFactory.getLogger(DepreciaJob.class);
@Autowired
ActivosRepository activosRepository;
@Autowired
SubespRepository subespRepository;
@Autowired
DepreciacionRepository depreciacionRepository;
private Double round(Double value){
BigDecimal d = new BigDecimal(value);
return d.setScale(3, BigDecimal.ROUND_HALF_DOWN).doubleValue();
}
public void run(){
// Subespecificacion
Iterable<Subesp> subesps = subespRepository.findAll();
// Activos
Iterable<Activos> activos = activosRepository.findAll();
Observable<Activos> activos$ = Observable
.fromIterable(activos);//.filter(a -> a.getId() > 17910);
// Configura % de depreciaciones
List<Activos> items = new ArrayList<>();
for (Subesp subesp : subesps){
List<Activos> item = activos$
.filter(a -> {
if (a.getPorc_depreciacion() == null){
a.setPorc_depreciacion(0.0);
}
String esp = a.getCodigo_especificacion();
if (esp.length() == 0) {
esp = a.getId_especif();
}
String subesp1 = a.getCodigo_subespec();
if (subesp1.length() == 0) {
subesp1 = a.getId_sub_esp();
}
return a.getCodigo_cuenta().equals(subesp.getCUENTA())
&& a.getId_division().equals(subesp.getDIVISION())
&& esp.equals(subesp.getESPECIFICACION())
&& subesp1.equals(subesp.getSUBESPECIFICACION())
// && a.getPorc_depreciacion() < 0.01
&& a.getValor_original() >= 100;
}).toList().blockingGet();
if (item.size() > 0) {
for (Activos a : item) {
a.setPorc_depreciacion(subesp.getDEPRE());
items.add(a);
}
}
}
// update
for (Activos a : items) {
activosRepository.save(a);
}
// Actualiza activos de cuenta 055 - menores de 100
List<Activos> activos055 = activos$
.filter(a -> a.getValor_original() < 100 &&
a.getCodigo_cuenta() == "055" &&
a.getStatus() == 1)
.toList().blockingGet();
// Valor actual = Valor original
for (Activos a : activos055){
a.setPorc_depreciacion(0D);
a.setDeprec_anual(0D);
a.setDeprec_mensual(0D);
a.setDeprec_diario(0D);
a.setDepreciacion_total(0D);
a.setValor_actual(a.getValor_original());
activosRepository.save(a);
}
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(new Date());
List<Activos> activosList = activos$.toList().blockingGet();
// Activos a depreciar
for (Activos a : activosList){
Double depre = 0D;
Double depreAnual = 0D;
depreAnual = this.round(a.getValor_original() * (a.getPorc_depreciacion())/100);
log.info("depreciacion anual: " + depreAnual);
log.info("activo: " + a.getId().toString());
depre = this.round(depreAnual / 365);
Double depreMensual = this.round(depreAnual / 12);
Double depreAcum = 0D;
BigDecimal valor = new BigDecimal(0D);
Integer amountDays = 1;
// Si la fecha compra existe
if (a.getFecha_compra() != null) {
end.setTime(a.getFecha_compra());
long startTime = (new Date()).getTime();
long endTime = a.getFecha_compra().getTime();
Long diffTime = startTime - endTime;
Long diffDays = diffTime / (1000 * 60 * 60 * 24);
log.info(a.getId().toString());
amountDays = amountDays + diffDays.intValue();
for (Integer j = 0;j<amountDays;j++){
if (depreAcum < a.getValor_original()) {
depreAcum = this.round(depreAcum + depre);
Double v = a.getValor_original() - depreAcum;
valor = new BigDecimal(v);
valor = valor.setScale(2, BigDecimal.ROUND_HALF_DOWN);
// valor = Math.round(v.doubleValue()) + 0D;
} else {
valor = new BigDecimal(1D);
}
}
if (depreAcum >= a.getValor_original()) {
depreAcum = this.round(a.getValor_original() - 1);
}
// update
a.setDepreciacion_total(depreAcum);
a.setDeprec_diario(depre);
a.setDeprec_mensual(depreMensual);
a.setDeprec_anual(depreAnual);
a.setValor_actual(valor.doubleValue());
a.setTiempo_uso(amountDays.longValue());
activosRepository.save(a);
}
}
List<Activos> activosNeg = activos$
.filter(a -> a.getDepreciacion_total() < 0)
.toList().blockingGet();
for (Activos a : activosNeg){
a.setDepreciacion_total(0D);
activosRepository.save(a);
}
List<Activos> activosValor0 = activos$
.filter(a -> a.getValor_original() <= 0)
.toList().blockingGet();
for (Activos a : activosValor0){
a.setValor_actual(0D);
activosRepository.save(a);
}
Depreciacion row = new Depreciacion();
row.setFecha_depreciacion(new Date());
row.setFecha_sistema(new Date());
row.setUsuario("9999");
depreciacionRepository.save(row);
List<Depreciacion> depreciacionList = depreciacionRepository.findAllOrderByIdDesc();
if (depreciacionList.size() > 0){
// ?
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment