Skip to content

Commit

Permalink
Merge pull request #128 from gisce/imp_ajust_autoconsum
Browse files Browse the repository at this point in the history
Millores en les lectures i ajustos per autoconsum
  • Loading branch information
eberloso authored Jun 12, 2020
2 parents 0cb7bf2 + 84c4163 commit 1454bfa
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 13 deletions.
113 changes: 104 additions & 9 deletions gestionatr/input/messages/F1.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,25 +779,46 @@ class Lectura(object):

def __init__(self, data):
self.lectura_data = data
self._fecha = None
self._lectura = None
self._procedencia = None

@property
def fecha(self):
if self._fecha:
return self._fecha
if hasattr(self.lectura_data, 'Fecha'):
return self.lectura_data.Fecha.text.strip()
return None

@fecha.setter
def fecha(self, value):
self._fecha = value

@property
def procedencia(self):
if self._procedencia:
return self._procedencia
if hasattr(self.lectura_data, 'Procedencia'):
return self.lectura_data.Procedencia.text.strip()
return None

@procedencia.setter
def procedencia(self, value):
self._procedencia = value

@property
def lectura(self):
if self._lectura is not None:
return self._lectura
if hasattr(self.lectura_data, 'Lectura'):
return float(self.lectura_data.Lectura.text.strip())
return None

@lectura.setter
def lectura(self, value):
self._lectura = value


class Ajuste(object):

Expand Down Expand Up @@ -848,7 +869,11 @@ class Integrador(object):

def __init__(self, data):
self.integrador = data
self._lectura_desde = None
self._lectura_hasta = None
self._magnitud = None
self._periode = None
self._numero_ruedas_enteras = None
self._ajuste = None
self.comptador = None

Expand All @@ -857,10 +882,16 @@ def unique_name(self):

@property
def magnitud(self):
if self._magnitud:
return self._magnitud
if hasattr(self.integrador, 'Magnitud'):
return self.integrador.Magnitud.text.strip()
return None

@magnitud.setter
def magnitud(self, value):
self._magnitud = value

@property
def codigo_periodo(self):
if self._periode:
Expand All @@ -882,10 +913,16 @@ def constante_multiplicadora(self):

@property
def numero_ruedas_enteras(self):
if self._numero_ruedas_enteras:
return self._numero_ruedas_enteras
if hasattr(self.integrador, 'NumeroRuedasEnteras'):
return float(self.integrador.NumeroRuedasEnteras.text.strip())
return None

@numero_ruedas_enteras.setter
def numero_ruedas_enteras(self, value):
self._numero_ruedas_enteras = value

@property
def numero_ruedas_decimales(self):
if hasattr(self.integrador, 'NumeroRuedasDecimales'):
Expand All @@ -896,20 +933,32 @@ def numero_ruedas_decimales(self):
def consumo_calculado(self):
if hasattr(self.integrador, 'ConsumoCalculado'):
return float(self.integrador.ConsumoCalculado.text.strip())
return None
return 0

@property
def lectura_desde(self):
if self._lectura_desde:
return self._lectura_desde
if hasattr(self.integrador, 'LecturaDesde'):
return Lectura(self.integrador.LecturaDesde)
return None

@lectura_desde.setter
def lectura_desde(self, value):
self._lectura_desde = value

@property
def lectura_hasta(self):
if self._lectura_hasta:
return self._lectura_hasta
if hasattr(self.integrador, 'LecturaHasta'):
return Lectura(self.integrador.LecturaHasta)
return None

@lectura_hasta.setter
def lectura_hasta(self, value):
self._lectura_hasta = value

@property
def tipus(self):
return MAGNITUDS_OCSUM.get(self.magnitud)
Expand Down Expand Up @@ -943,8 +992,9 @@ def ometre(self):

class ModeloAparato(object):

def __init__(self, data):
def __init__(self, data, factura=None):
self.modelo_aparato = data
self.factura = factura

@property
def tipo_aparato(self):
Expand Down Expand Up @@ -1039,6 +1089,11 @@ def get_lectures(self, tipus=None):
lectures.append(integrador)
except AttributeError:
pass

if (not tipus or "S" in tipus) and self.factura and self.factura.get_consum_facturat(tipus='S', periode=None) and not self.factura.has_AS_lectures():
# Si no tenim lectures AS pero si que ens han cobrat excedents,
# creem unes lectures AS ficticies a 0 (puta ENDESA)
lectures.extend(self.factura.get_fake_AS_lectures())
return lectures

def get_lectures_activa(self):
Expand Down Expand Up @@ -1066,9 +1121,9 @@ class MultiModeloAparato(ModeloAparato):
repeat both the field Medidas and ModeloAparato despite the fact that they
are only providing two different periods for the same meter"""

def __init__(self, meter_list):
def __init__(self, meter_list, factura=None):
self.meters = meter_list
super(MultiModeloAparato, self).__init__(meter_list[0])
super(MultiModeloAparato, self).__init__(meter_list[0], factura=factura)

def _get_single_attribute(self, attribute):
for meter in self.meters:
Expand Down Expand Up @@ -1201,22 +1256,22 @@ def medidas(self):
data.append(Medida(d))
return data

def get_consum_facturat(self, tipus, periode):
def get_consum_facturat(self, tipus, periode=None):
if tipus not in ['A', 'S']:
return None

if tipus == 'A' and self.energia_activa:
res = []
for activa in self.energia_activa.terminos_energia_activa:
for periode_activa in activa.periodos:
if periode_activa.nombre == periode:
if periode_activa.nombre == periode or not periode:
res.append(periode_activa.cantidad)
return res

if tipus == 'S':
res = []
for concepte in self.conceptos_repercutibles:
if concepte.concepto_repercutible[0] == '7' and concepte.concepto_repercutible[1] == periode[1]:
if concepte.concepto_repercutible[0] == '7' and (not periode or concepte.concepto_repercutible[1] == periode[1]):
res.append(concepte.unidades)
return res

Expand All @@ -1243,6 +1298,46 @@ def periodes_facturats_agrupats(self, nperiodes_lectures, tipus='A'):

return len(res.keys()) != nperiodes_lectures

def has_AS_lectures(self):
for medida in self.medidas:
for aparell in medida.modelos_aparatos:
try:
for integrador in aparell.integradores:
if integrador.tipus == 'S':
return True
except AttributeError:
pass
return False

def get_fake_AS_lectures(self):
res = []
comptador_amb_lectures = None
for medida in self.medidas:
for c in medida.modelos_aparatos:
if c.get_lectures_activa_entrant():
comptador_amb_lectures = c
break
if comptador_amb_lectures:
base_info = comptador_amb_lectures.get_lectures_activa_entrant()[0]
for concepte in self.conceptos_repercutibles:
if concepte.concepto_repercutible[0] == '7':
l1 = Lectura(None)
l1.fecha = base_info.lectura_desde.fecha
l1.lectura = 0
l1.procedencia = base_info.lectura_desde.procedencia
l2 = Lectura(None)
l2.fecha = base_info.lectura_hasta.fecha
l2.lectura = 0
l2.procedencia = base_info.lectura_hasta.procedencia
new_integrador = Integrador(None)
new_integrador.magnitud = "AS"
new_integrador.numero_ruedas_enteras = base_info.numero_ruedas_enteras
new_integrador.codigo_periodo = base_info.codigo_periodo[0] + concepte.concepto_repercutible[1]
new_integrador.lectura_desde = l1
new_integrador.lectura_hasta = l2
res.append(new_integrador)
return res

def get_lectures_amb_ajust_quadrat_amb_consum(self, tipus='S', ajust_balancejat=True, motiu_ajust="98"):
lectures_per_periode = {}
for comptador in self.get_comptadors():
Expand Down Expand Up @@ -1277,7 +1372,7 @@ def get_lectures_amb_ajust_quadrat_amb_consum(self, tipus='S', ajust_balancejat=
lectura.ajuste = Ajuste(lectura.ajuste.ajuste)
old_ajust = lectura.ajuste and lectura.ajuste.ajuste_por_integrador or 0.0
new_val = consum - (lectura.lectura_hasta.lectura - lectura.lectura_desde.lectura + old_ajust)
if new_val != 0.0:
if new_val != (lectura.lectura_hasta.lectura - lectura.lectura_desde.lectura + old_ajust):
lectura.ajuste.ajuste_por_integrador = consum - (lectura.lectura_hasta.lectura - lectura.lectura_desde.lectura)
lectura.ajuste.codigo_motivo = motiu_ajust # normalment 98 - Autoconsumo
for l in lectures:
Expand All @@ -1304,7 +1399,7 @@ def get_comptadors(self):

comptadors = []
for llista_aparells in comptadors_agrupats.values():
aparell_multi = MultiModeloAparato(llista_aparells)
aparell_multi = MultiModeloAparato(llista_aparells, self)

di, df = aparell_multi.get_dates_inici_i_final()
comptadors.append((di, df, aparell_multi))
Expand Down
22 changes: 18 additions & 4 deletions gestionatr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,25 @@ def repartir_consums_entre_lectures(consums, lectures_xml):
if len(periodes) > 1:
consum_acumulat = 0
for l in lectures_xml:
res[l] = l.consumo_calculado
if l != lectures_xml[0]:
consum_acumulat += l.consumo_calculado
# Calculem el consum que te aquesta lectura
consumo_calculado = l.lectura_hasta.lectura + (l.ajuste and l.ajuste.ajuste_por_integrador or 0.0) - l.lectura_desde.lectura
if consumo_calculado < 0:
consumo_calculado += l.gir_comptador
res[l] = consumo_calculado
consum_acumulat += consumo_calculado
if res:
res[lectures_xml[0]] = consums[0] - consum_acumulat
if consums[0] >= consum_acumulat:
res[lectures_xml[0]] += consums[0] - consum_acumulat
else:
a_repartir = abs(consums[0] - consum_acumulat)
for l in sorted(res.keys(), key=lambda *a: a[0].periode):
if res[l] >= a_repartir:
res[l] -= a_repartir
break
else:
a_repartir -= res[l]
res[l] = 0

else:
consum = consums[0]
import math
Expand Down

0 comments on commit 1454bfa

Please sign in to comment.