16bf2cf1c7
Конфиг счётчиков и день/ночь-тарифы, учёт энергосистем (генераторы, инверторы), доступ к разделу РРО/ПРРО. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
"""Допоміжні функції для комунальних показань."""
|
|
from __future__ import annotations
|
|
|
|
from apps.utilities.models import UtilityMeterConfig
|
|
|
|
READING_MODE_SINGLE = UtilityMeterConfig.READING_MODE_SINGLE
|
|
READING_MODE_DAY_NIGHT = UtilityMeterConfig.READING_MODE_DAY_NIGHT
|
|
|
|
|
|
def get_reading_mode(pharmacy_id, utility_type) -> str:
|
|
"""Режим зняття показань: спочатку конфіг аптеки, інакше extra_data типу."""
|
|
if not pharmacy_id or not utility_type:
|
|
return READING_MODE_SINGLE
|
|
|
|
utility_type_id = getattr(utility_type, 'pk', utility_type)
|
|
config = UtilityMeterConfig.objects.filter(
|
|
pharmacy_id=pharmacy_id,
|
|
utility_type_id=utility_type_id,
|
|
is_active=True,
|
|
).first()
|
|
if config:
|
|
return config.reading_mode
|
|
|
|
if hasattr(utility_type, 'get_extra_value'):
|
|
ut = utility_type
|
|
else:
|
|
from core.models.directories import Directory
|
|
ut = Directory.objects.filter(pk=utility_type_id).first()
|
|
if ut:
|
|
return ut.get_extra_value('reading_mode', READING_MODE_SINGLE) or READING_MODE_SINGLE
|
|
return READING_MODE_SINGLE
|
|
|
|
|
|
def meter_configs_map() -> dict[str, str]:
|
|
"""Ключ «pharmacy_id:utility_type_id» → reading_mode для JS."""
|
|
return {
|
|
f'{c.pharmacy_id}:{c.utility_type_id}': c.reading_mode
|
|
for c in UtilityMeterConfig.objects.filter(is_active=True).only(
|
|
'pharmacy_id', 'utility_type_id', 'reading_mode',
|
|
)
|
|
}
|
|
|
|
|
|
def utility_type_modes_map(utility_types) -> dict[str, str]:
|
|
"""Дефолтний режим з довідника типів (fallback без конфігу аптеки)."""
|
|
result = {}
|
|
for ut in utility_types:
|
|
result[str(ut.pk)] = ut.get_extra_value('reading_mode', READING_MODE_SINGLE) or READING_MODE_SINGLE
|
|
return result
|
|
|
|
|
|
def attach_reading_display(readings):
|
|
"""Додає reading_mode та споживання для шаблону."""
|
|
type_ids = {r.utility_type_id for r in readings}
|
|
pharmacy_ids = {r.pharmacy_id for r in readings}
|
|
configs = {
|
|
(c.pharmacy_id, c.utility_type_id): c.reading_mode
|
|
for c in UtilityMeterConfig.objects.filter(
|
|
pharmacy_id__in=pharmacy_ids,
|
|
utility_type_id__in=type_ids,
|
|
is_active=True,
|
|
)
|
|
}
|
|
for reading in readings:
|
|
reading.reading_mode = configs.get(
|
|
(reading.pharmacy_id, reading.utility_type_id),
|
|
reading.utility_type.get_extra_value('reading_mode', READING_MODE_SINGLE) or READING_MODE_SINGLE,
|
|
)
|