@@ -1703,15 +1703,16 @@ def get_fake_lectures(self, lectures, tipus=None):
17031703 generacio_facturada = self .factura and self .factura .get_consum_facturat (tipus = 'S' , periode = None )
17041704 if (not tipus or "S" in tipus ) and self .factura and not self .factura .has_AS_lectures ():
17051705 # El mètode get_consum_facturat retorna [0.0] si no hi ha consum
1706- if generacio_facturada and len (generacio_facturada ) > 1 or generacio_facturada [0 ] != 0.0 :
1706+ if generacio_facturada and ( len (generacio_facturada ) > 1 or generacio_facturada [0 ] != 0.0 ) :
17071707 # Si no tenim lectures AS pero si que ens han cobrat excedents,
17081708 # creem unes lectures AS ficticies a 0 (puta ENDESA)
17091709 lectures .extend (self .factura .get_fake_AS_lectures (comptador_base = self ))
17101710 if (not tipus or "S" in tipus ) and self .factura and self .has_AS_lectures_only_p0 () \
17111711 and generacio_facturada and len (generacio_facturada ) > 1 :
17121712 # Si nomes ens envien el P0 de excedents pero ens cobren varis periodes
17131713 # creem una lectura e P2 AS ficticies a 0 (puta FENOSA)
1714- lectures .extend (self .factura .get_fake_AS_p2_lectures (comptador_base = self ))
1714+ # Setejem el period_start a 2 perque com a lectura de P1 agafem el totalitzador
1715+ lectures .extend (self .factura .get_fake_AS_lectures (period_start = 2 , comptador_base = self ))
17151716
17161717 if (
17171718 (not tipus or "A" in tipus )
@@ -1733,16 +1734,10 @@ def get_fake_lectures(self, lectures, tipus=None):
17331734
17341735 def get_fake_pX_lectures (self , lectura_ae_totalitzador , tipus = None ):
17351736 # Si te un totalitzador amb el codi de les noves tarifes, creem les lectures fake
1736- tarifa_atr = self .factura .datos_factura .tarifa_atr_fact
1737- nperiodes_td = PERIODES_PER_TARIFA .get (tarifa_atr , {}).get (tipus , None )
17381737 # Obtenim els periodes de les lectures segons la tarifa
1738+ lectures_per_periode = self .factura .get_periodes_from_tarifa_and_tipus (tipus = tipus )
17391739 res = []
1740- if nperiodes_td :
1741- lectures_per_periode = {}
1742- for periode in range (1 , nperiodes_td + 1 ):
1743- pname = "P" + str (periode )
1744- lectures_per_periode [pname ] = []
1745-
1740+ if lectures_per_periode :
17461741 # Agafem el totalitzador com a lectura "base"
17471742 base_lectura = lectura_ae_totalitzador
17481743 # Marquem el totalitzador com a lectura de P1
@@ -2339,44 +2334,6 @@ def has_AS_lectures_only_p0(self):
23392334 pass
23402335 return has_p0
23412336
2342- def get_fake_AS_lectures (self , comptador_base = None ):
2343- res = []
2344- comptador_amb_lectures = None
2345- for medida in self .medidas :
2346- for c in medida .modelos_aparatos :
2347- if c .get_lectures_activa_entrant ():
2348- comptador_amb_lectures = c
2349- break
2350- if comptador_amb_lectures :
2351- te_autoconsum = (self .autoconsumo and self .autoconsumo .energia_excedentaria ) or ([x for x in (self .conceptos_repercutibles or []) if '7' == x .concepto_repercutible [0 ]])
2352- if not te_autoconsum :
2353- return res
2354- base_info = comptador_amb_lectures .get_lectures_activa_entrant ()[0 ]
2355- i = 0
2356- for consum in self .get_consum_facturat (tipus = 'S' ):
2357- i += 1
2358- l1 = Lectura (None )
2359- l1 .fecha = base_info .lectura_desde .fecha
2360- l1 .lectura = 0
2361- l1 .procedencia = base_info .lectura_desde .procedencia
2362- l2 = Lectura (None )
2363- l2 .fecha = base_info .lectura_hasta .fecha
2364- l2 .lectura = 0
2365- l2 .procedencia = base_info .lectura_hasta .procedencia
2366- new_integrador = Integrador (base_info .integrador )
2367- if comptador_base :
2368- new_integrador .comptador = comptador_base
2369- new_integrador .comptador = base_info .comptador
2370- new_integrador .magnitud = "AS"
2371- new_integrador .numero_ruedas_enteras = base_info .numero_ruedas_enteras
2372- new_integrador .codigo_periodo = base_info .codigo_periodo [0 ] + str (i )
2373- if not new_integrador .periode :
2374- new_integrador .codigo_periodo = base_info .codigo_periodo
2375- new_integrador .lectura_desde = l1
2376- new_integrador .lectura_hasta = l2
2377- res .append (new_integrador )
2378- return res
2379-
23802337 def get_fake_pX_lectura (self , tipus , periode , base_info , lectura_desde = 0 , lectura_hasta = 0 , comptador_base = None ):
23812338 l1 = Lectura (None )
23822339 l1 .fecha = base_info .lectura_desde .fecha
@@ -2412,24 +2369,48 @@ def get_fake_pX_lectura(self, tipus, periode, base_info, lectura_desde=0, lectur
24122369
24132370 return new_integrador
24142371
2415- def get_fake_AS_p2_lectures (self , comptador_base = None ):
2416- res = []
2372+ def get_comptador_amb_lectures (self , tipus = None ):
2373+ """
2374+ Retorna el primer comptador que tingui les lectures del tipus inidcat
2375+ Si cap comptador té lectures del primer tipus, es repeteix el procés amb el seguent tipus
2376+ """
2377+ if tipus is None :
2378+ tipus = ['S' , 'A' ]
24172379 comptador_amb_lectures = None
24182380 for medida in self .medidas :
2419- for c in medida .modelos_aparatos :
2420- if c .get_lectures_activa_sortint ():
2421- comptador_amb_lectures = c
2381+ for t in tipus :
2382+ for c in medida .modelos_aparatos :
2383+ if c .get_lectures (tipus = t ):
2384+ comptador_amb_lectures = c
2385+ break
2386+ if comptador_amb_lectures :
24222387 break
2388+ if comptador_amb_lectures :
2389+ break
2390+
2391+ return comptador_amb_lectures
2392+
2393+ def get_fake_AS_lectures (self , period_start = 1 , comptador_base = None ):
2394+ res = []
2395+ # Si no te autoconsum no creem lectures d'excedents
2396+ conceptes_repercutibles_excedents = [x for x in (self .conceptos_repercutibles or []) if '7' == x .concepto_repercutible [0 ]]
2397+ te_autoconsum = (self .autoconsumo and self .autoconsumo .energia_excedentaria ) or conceptes_repercutibles_excedents
2398+ if not te_autoconsum :
2399+ return res
2400+ comptador_amb_lectures = self .get_comptador_amb_lectures ()
24232401 if comptador_amb_lectures :
2424- te_autoconsum = (self .autoconsumo and self .autoconsumo .energia_excedentaria ) or ([x for x in (self .conceptos_repercutibles or []) if '7' == x .concepto_repercutible [0 ]])
2425- if not te_autoconsum :
2426- return res
24272402 base_info = comptador_amb_lectures .get_lectures_activa_entrant ()[0 ]
2428- i = 0
2429- for consum in self .get_consum_facturat (tipus = 'S' ):
2430- i += 1
2431- if i <= 1 :
2432- continue
2403+ num_periodes = self .get_num_periodes_from_tarifa_and_tipus (tipus = 'S' )
2404+ # Mandanga per les tarifes antigues. Els excedents s'informaven com a conceptes repercutibles.
2405+ # Si no hem pogut obtenir el num periodes perque son tarifes antigues, i posem tants periodes com
2406+ # conceptes repercutibles tingui
2407+ if not num_periodes :
2408+ num_periodes = len (conceptes_repercutibles_excedents )
2409+ if not num_periodes :
2410+ return res
2411+ # Depending on period_start, P1 is skipped. If we have totalizer, it will be skipped, otherwise we need to
2412+ # create readings for each tariff period
2413+ for i in range (period_start , num_periodes + 1 ): # num_periodes not included, so we add 1
24332414 l1 = Lectura (None )
24342415 l1 .fecha = base_info .lectura_desde .fecha
24352416 l1 .lectura = 0
@@ -2723,6 +2704,22 @@ def get_maximetres_consumidor(self):
27232704 num_periode += 1
27242705 return data
27252706
2707+ def get_num_periodes_from_tarifa_and_tipus (self , tipus = 'A' ):
2708+ tarifa_atr = self .datos_factura .tarifa_atr_fact
2709+ nperiodes_td = PERIODES_PER_TARIFA .get (tarifa_atr , {}).get (tipus , 0 )
2710+ return nperiodes_td
2711+
2712+ def get_periodes_from_tarifa_and_tipus (self , tipus = 'A' ):
2713+ lectures_per_periode = {}
2714+ nperiodes_td = self .get_num_periodes_from_tarifa_and_tipus (tipus = tipus )
2715+ # Obtenim els periodes de les lectures segons la tarifa
2716+ if nperiodes_td :
2717+ for periode in range (1 , nperiodes_td + 1 ):
2718+ pname = "P" + str (periode )
2719+ lectures_per_periode [pname ] = []
2720+
2721+ return lectures_per_periode
2722+
27262723
27272724class ConceptoRepercutible (object ):
27282725
0 commit comments