feat: address QA analytics and report upload feedback
All checks were successful
All checks were successful
This commit is contained in:
@@ -77,6 +77,16 @@ def _delta_percent(current, previous) -> float:
|
||||
)
|
||||
|
||||
|
||||
def _direction(current, previous) -> str:
|
||||
current_value = _dec(current)
|
||||
previous_value = _dec(previous)
|
||||
if current_value > previous_value:
|
||||
return "up"
|
||||
if current_value < previous_value:
|
||||
return "down"
|
||||
return "unchanged"
|
||||
|
||||
|
||||
def _period_rank(report_quarter: int | None) -> int:
|
||||
return 5 if report_quarter is None else report_quarter
|
||||
|
||||
@@ -216,14 +226,30 @@ class OrganizationAnalyticsService:
|
||||
*,
|
||||
organization: Organization,
|
||||
report_year: int,
|
||||
report_quarter: int | None,
|
||||
report_quarter: int | None = None,
|
||||
) -> dict[str, object]:
|
||||
f2_records = cls._f2_records(organization)
|
||||
f1_records = cls._f1_records(organization)
|
||||
current_f2 = cls._require_record(
|
||||
_pick_record(f2_records, report_year, report_quarter),
|
||||
entity="Financial summary",
|
||||
)
|
||||
current_f2 = _pick_record(f2_records, report_year, report_quarter)
|
||||
if current_f2 is None:
|
||||
empty_metric = {
|
||||
"amount": 0,
|
||||
"previous_amount": 0,
|
||||
"delta_percent": 0.0,
|
||||
}
|
||||
return {
|
||||
"organization_id": str(organization.id),
|
||||
"report_period": {
|
||||
"year": report_year,
|
||||
"quarter": report_quarter,
|
||||
},
|
||||
"data_available": False,
|
||||
"message": "Данные отсутствуют",
|
||||
"revenue": empty_metric,
|
||||
"net_profit": empty_metric.copy(),
|
||||
"taxes_paid": empty_metric.copy(),
|
||||
"insurance_contributions": empty_metric.copy(),
|
||||
}
|
||||
current_f1 = _pick_record(f1_records, report_year, report_quarter)
|
||||
previous_f2 = _pick_record(f2_records, report_year - 1, report_quarter)
|
||||
previous_f1 = _pick_record(f1_records, report_year - 1, report_quarter)
|
||||
@@ -252,6 +278,8 @@ class OrganizationAnalyticsService:
|
||||
"year": report_year,
|
||||
"quarter": report_quarter,
|
||||
},
|
||||
"data_available": True,
|
||||
"message": None,
|
||||
"revenue": {
|
||||
"amount": _amount(current_f2.revenue),
|
||||
"previous_amount": _amount(revenue_previous),
|
||||
@@ -279,7 +307,12 @@ class OrganizationAnalyticsService:
|
||||
@staticmethod
|
||||
def _economics_metric_groups() -> dict[str, tuple[str, ...]]:
|
||||
return {
|
||||
"efficiency": ("revenue", "ebitda", "net_profit"),
|
||||
"efficiency": (
|
||||
"revenue",
|
||||
"ebitda",
|
||||
"net_profit",
|
||||
"revenue_per_employee",
|
||||
),
|
||||
"profitability": ("gross_profit", "net_profit", "operating_profit"),
|
||||
"debt": ("net_debt", "loans", "assets"),
|
||||
"investment": ("capex", "rd_expenses", "assets"),
|
||||
@@ -298,6 +331,7 @@ class OrganizationAnalyticsService:
|
||||
"assets": "rub_thousands",
|
||||
"capex": "rub_thousands",
|
||||
"rd_expenses": "rub_thousands",
|
||||
"revenue_per_employee": "rub_per_employee",
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
@@ -341,6 +375,17 @@ class OrganizationAnalyticsService:
|
||||
primary, fallback = field_map.get(metric, (ZERO, ZERO))
|
||||
return _dec(primary or fallback)
|
||||
|
||||
@classmethod
|
||||
def _economics_display_value(cls, metric: str, f2, f4, f1) -> int:
|
||||
if metric == "revenue_per_employee":
|
||||
employees = _dec(getattr(f1, "avg_payroll_employees", ZERO))
|
||||
if employees == ZERO:
|
||||
employees = _dec(getattr(f1, "avg_employees", ZERO))
|
||||
if employees == ZERO:
|
||||
return 0
|
||||
return _amount(cls._economics_metric_value("revenue", f2, f4) / employees)
|
||||
return _amount_thousands(cls._economics_metric_value(metric, f2, f4))
|
||||
|
||||
@classmethod
|
||||
def get_economics(
|
||||
cls,
|
||||
@@ -356,46 +401,58 @@ class OrganizationAnalyticsService:
|
||||
f4_by_year = _best_records_by_year(
|
||||
cls._f4_records(organization), from_year, to_year
|
||||
)
|
||||
f1_by_year = _best_records_by_year(
|
||||
cls._f1_records(organization), from_year, to_year
|
||||
)
|
||||
|
||||
periods = sorted(set(f2_by_year) | set(f4_by_year))
|
||||
periods = sorted(set(f2_by_year) | set(f4_by_year) | set(f1_by_year))
|
||||
if not periods:
|
||||
latest_year = max(
|
||||
filter(
|
||||
None,
|
||||
(
|
||||
_latest_report_year(cls._f2_records(organization)),
|
||||
_latest_report_year(cls._f4_records(organization)),
|
||||
),
|
||||
),
|
||||
default=None,
|
||||
)
|
||||
if latest_year is None:
|
||||
raise NotFoundError(message="Economics data is not available")
|
||||
|
||||
f2_by_year = _best_records_by_year(
|
||||
cls._f2_records(organization), latest_year, latest_year
|
||||
)
|
||||
f4_by_year = _best_records_by_year(
|
||||
cls._f4_records(organization), latest_year, latest_year
|
||||
)
|
||||
periods = sorted(set(f2_by_year) | set(f4_by_year))
|
||||
return {
|
||||
"organization_id": str(organization.id),
|
||||
"group": group,
|
||||
"periods": [],
|
||||
"data_available": False,
|
||||
"message": "Данные отсутствуют",
|
||||
"kpis": {},
|
||||
"series": [],
|
||||
"ratios": [],
|
||||
"ratio_normatives": ECONOMICS_RATIO_NORMATIVES,
|
||||
}
|
||||
|
||||
metric_units = cls._economics_metric_units()
|
||||
selected_metrics = cls._economics_metric_groups()[group]
|
||||
last_period = periods[-1]
|
||||
previous_period = periods[-2] if len(periods) > 1 else None
|
||||
|
||||
def metric_value(metric: str, period: int | None) -> int:
|
||||
if period is None:
|
||||
return 0
|
||||
return cls._economics_display_value(
|
||||
metric,
|
||||
f2_by_year.get(period),
|
||||
f4_by_year.get(period),
|
||||
f1_by_year.get(period),
|
||||
)
|
||||
|
||||
return {
|
||||
"organization_id": str(organization.id),
|
||||
"group": group,
|
||||
"periods": periods,
|
||||
"data_available": True,
|
||||
"message": None,
|
||||
"kpis": {
|
||||
metric: {
|
||||
"value": _amount_thousands(
|
||||
cls._economics_metric_value(
|
||||
metric,
|
||||
f2_by_year.get(last_period),
|
||||
f4_by_year.get(last_period),
|
||||
)
|
||||
"value": metric_value(metric, last_period),
|
||||
"previous_value": metric_value(metric, previous_period),
|
||||
"delta_value": metric_value(metric, last_period)
|
||||
- metric_value(metric, previous_period),
|
||||
"delta_percent": _delta_percent(
|
||||
metric_value(metric, last_period),
|
||||
metric_value(metric, previous_period),
|
||||
),
|
||||
"direction": _direction(
|
||||
metric_value(metric, last_period),
|
||||
metric_value(metric, previous_period),
|
||||
),
|
||||
"unit": metric_units[metric],
|
||||
}
|
||||
@@ -408,13 +465,7 @@ class OrganizationAnalyticsService:
|
||||
"points": [
|
||||
{
|
||||
"period": report_year,
|
||||
"value": _amount_thousands(
|
||||
cls._economics_metric_value(
|
||||
metric,
|
||||
f2_by_year.get(report_year),
|
||||
f4_by_year.get(report_year),
|
||||
)
|
||||
),
|
||||
"value": metric_value(metric, report_year),
|
||||
}
|
||||
for report_year in periods
|
||||
],
|
||||
@@ -462,6 +513,9 @@ class OrganizationAnalyticsService:
|
||||
current_f1 = _pick_record(cls._f1_records(organization), report_year)
|
||||
years = list(range(report_year - history_years + 1, report_year + 1))
|
||||
history_records = _best_records_by_year(f3_records, years[0], years[-1])
|
||||
history_f1_records = _best_records_by_year(
|
||||
cls._f1_records(organization), years[0], years[-1]
|
||||
)
|
||||
|
||||
average_employees = int(_dec(current_f3.avg_employees))
|
||||
under_30 = int(average_employees * 0.29)
|
||||
@@ -494,6 +548,43 @@ class OrganizationAnalyticsService:
|
||||
{
|
||||
"year": year,
|
||||
"average_employees": int(_dec(history_records[year].avg_employees)),
|
||||
"avg_payroll_employees": int(
|
||||
_dec(
|
||||
getattr(
|
||||
history_f1_records.get(year),
|
||||
"avg_payroll_employees",
|
||||
ZERO,
|
||||
)
|
||||
)
|
||||
),
|
||||
"average_age": _weighted_average_age(
|
||||
[
|
||||
{
|
||||
"age_group": "under_30",
|
||||
"employees_count": int(
|
||||
_dec(history_records[year].avg_employees)
|
||||
* Decimal("0.29")
|
||||
),
|
||||
},
|
||||
{
|
||||
"age_group": "30_50",
|
||||
"employees_count": int(
|
||||
_dec(history_records[year].avg_employees)
|
||||
* Decimal("0.49")
|
||||
),
|
||||
},
|
||||
{
|
||||
"age_group": "over_50",
|
||||
"employees_count": int(
|
||||
_dec(history_records[year].avg_employees)
|
||||
* Decimal("0.22")
|
||||
),
|
||||
},
|
||||
]
|
||||
),
|
||||
"payroll_fund": _amount(
|
||||
getattr(history_f1_records.get(year), "payroll_fund", ZERO)
|
||||
),
|
||||
}
|
||||
for year in years
|
||||
if year in history_records
|
||||
@@ -592,6 +683,61 @@ class OrganizationAnalyticsService:
|
||||
},
|
||||
]
|
||||
|
||||
bucket_fields = {
|
||||
"under_5_years": "age_under_5",
|
||||
"5_10_years": "age_5_10",
|
||||
"10_15_years": "age_10_15",
|
||||
"15_20_years": "age_15_20",
|
||||
"over_20_years": "age_over_20",
|
||||
}
|
||||
total_age_units = sum(item["units_count"] for item in age_distribution)
|
||||
for item in age_distribution:
|
||||
bucket = item["bucket"]
|
||||
if f6_records:
|
||||
bucket_units_by_record = [
|
||||
int(_dec(getattr(record, bucket_fields[bucket], ZERO)))
|
||||
for record in f6_records
|
||||
]
|
||||
imported_equipment = sum(
|
||||
round(
|
||||
int(_dec(record.imported_equipment))
|
||||
* units
|
||||
/ max(1, int(_dec(record.total_equipment)))
|
||||
)
|
||||
for record, units in zip(
|
||||
f6_records, bucket_units_by_record, strict=True
|
||||
)
|
||||
)
|
||||
weighted_units = sum(bucket_units_by_record)
|
||||
utilization_rate = sum(
|
||||
_dec(record.utilization_rate) * units
|
||||
for record, units in zip(
|
||||
f6_records, bucket_units_by_record, strict=True
|
||||
)
|
||||
) / max(1, weighted_units)
|
||||
weighted_wear = sum(
|
||||
_dec(record.physical_wear_percent) * units
|
||||
for record, units in zip(
|
||||
f6_records, bucket_units_by_record, strict=True
|
||||
)
|
||||
) / max(1, weighted_units)
|
||||
else:
|
||||
imported_equipment = round(
|
||||
int(_dec(current_f3.imported_equipment))
|
||||
* item["units_count"]
|
||||
/ max(1, total_age_units)
|
||||
)
|
||||
utilization_rate = _dec(current_f3.utilization_rate)
|
||||
weighted_wear = _dec(current_f3.physical_wear_percent)
|
||||
item.update(
|
||||
{
|
||||
"imported_equipment": imported_equipment,
|
||||
"utilization_rate": round(float(utilization_rate / 100), 2),
|
||||
"lease_share_itn_percent": None,
|
||||
"weighted_wear_percent": _ratio(weighted_wear),
|
||||
}
|
||||
)
|
||||
|
||||
if f6_records:
|
||||
category_rows = [
|
||||
{
|
||||
@@ -661,6 +807,7 @@ class OrganizationAnalyticsService:
|
||||
"report_year": report_year,
|
||||
"summary": {
|
||||
"total_equipment": int(_dec(current_f3.total_equipment)),
|
||||
"machine_tools_and_equipment": int(_dec(current_f3.total_equipment)),
|
||||
"domestic_equipment": int(_dec(current_f3.domestic_equipment)),
|
||||
"imported_equipment": int(_dec(current_f3.imported_equipment)),
|
||||
"physical_wear_percent": _ratio(current_f3.physical_wear_percent),
|
||||
@@ -1185,7 +1332,7 @@ class DashboardAnalyticsService:
|
||||
def get_dashboard(
|
||||
cls, *, corporation_scope: str | None = None
|
||||
) -> dict[str, object]:
|
||||
queryset = Organization.objects.all()
|
||||
queryset = Organization.objects.filter(mostovik_uid__isnull=False)
|
||||
if corporation_scope:
|
||||
queryset = filter_queryset_by_scopes(queryset, [corporation_scope])
|
||||
|
||||
@@ -1234,23 +1381,38 @@ class DashboardAnalyticsService:
|
||||
1 for org in cluster_organizations if not org.bankruptcy_messages_found
|
||||
)
|
||||
|
||||
cluster_years = sorted(
|
||||
{
|
||||
year
|
||||
for organization in cluster_organizations
|
||||
for year in f3_best_by_organization.get(str(organization.id), {})
|
||||
}
|
||||
)
|
||||
from_year = None
|
||||
to_year = None
|
||||
growth_values = []
|
||||
for organization in cluster_organizations:
|
||||
organization_f3_by_year = f3_best_by_organization.get(
|
||||
str(organization.id), {}
|
||||
)
|
||||
report_years = sorted(organization_f3_by_year)
|
||||
if len(report_years) < 2:
|
||||
continue
|
||||
previous_record = organization_f3_by_year[report_years[-2]]
|
||||
current_record = organization_f3_by_year[report_years[-1]]
|
||||
if current_record is None or previous_record is None:
|
||||
continue
|
||||
growth_values.append(
|
||||
_delta_percent(
|
||||
current_record.avg_employees, previous_record.avg_employees
|
||||
year_pairs = list(zip(cluster_years, cluster_years[1:], strict=False))
|
||||
for candidate_from, candidate_to in reversed(year_pairs):
|
||||
candidate_growth_values = []
|
||||
for organization in cluster_organizations:
|
||||
organization_f3_by_year = f3_best_by_organization.get(
|
||||
str(organization.id), {}
|
||||
)
|
||||
)
|
||||
previous_record = organization_f3_by_year.get(candidate_from)
|
||||
current_record = organization_f3_by_year.get(candidate_to)
|
||||
if current_record is None or previous_record is None:
|
||||
continue
|
||||
candidate_growth_values.append(
|
||||
_delta_percent(
|
||||
current_record.avg_employees,
|
||||
previous_record.avg_employees,
|
||||
)
|
||||
)
|
||||
if candidate_growth_values:
|
||||
from_year = candidate_from
|
||||
to_year = candidate_to
|
||||
growth_values = candidate_growth_values
|
||||
break
|
||||
|
||||
distribution_by_cluster.append(
|
||||
{
|
||||
@@ -1270,6 +1432,8 @@ class DashboardAnalyticsService:
|
||||
headcount_growth_by_cluster.append(
|
||||
{
|
||||
"cluster": cluster_code,
|
||||
"from_year": from_year,
|
||||
"to_year": to_year,
|
||||
"growth_percent": round(sum(growth_values) / len(growth_values), 1)
|
||||
if growth_values
|
||||
else 0.0,
|
||||
|
||||
@@ -39,7 +39,10 @@ class OrganizationAnalyticsBaseView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_organization(self, organization_id) -> Organization:
|
||||
return get_object_or_404(Organization, id=organization_id)
|
||||
return get_object_or_404(
|
||||
Organization.objects.filter(mostovik_uid__isnull=False),
|
||||
id=organization_id,
|
||||
)
|
||||
|
||||
|
||||
class AnalyticsDashboardView(APIView):
|
||||
|
||||
@@ -69,14 +69,15 @@ class OrganizationViewSet(ClassicReadOnlyViewSet[Organization]):
|
||||
"""
|
||||
ViewSet для просмотра организаций.
|
||||
|
||||
Только чтение - организации создаются автоматически при загрузке форм.
|
||||
Только чтение. В каталог попадают организации, подтвержденные обменным
|
||||
пакетом Моставика; строки форм сами по себе каталог не расширяют.
|
||||
|
||||
Эндпоинты:
|
||||
GET /organizations/ - список организаций
|
||||
GET /organizations/{id}/ - детали организации
|
||||
"""
|
||||
|
||||
queryset = Organization.objects.all()
|
||||
queryset = Organization.objects.filter(mostovik_uid__isnull=False)
|
||||
serializer_class = OrganizationCatalogDetailSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
filterset_class = OrganizationFilter
|
||||
|
||||
@@ -33,6 +33,8 @@ class FinancialSummaryReportPeriodSerializer(serializers.Serializer):
|
||||
class FinancialSummaryResponseSerializer(serializers.Serializer):
|
||||
organization_id = serializers.UUIDField()
|
||||
report_period = FinancialSummaryReportPeriodSerializer()
|
||||
data_available = serializers.BooleanField()
|
||||
message = serializers.CharField(allow_null=True)
|
||||
revenue = FinancialSummaryMetricSerializer()
|
||||
net_profit = FinancialSummaryMetricSerializer()
|
||||
taxes_paid = FinancialSummaryMetricSerializer()
|
||||
@@ -41,6 +43,10 @@ class FinancialSummaryResponseSerializer(serializers.Serializer):
|
||||
|
||||
class EconomicsKpiSerializer(serializers.Serializer):
|
||||
value = serializers.IntegerField()
|
||||
previous_value = serializers.IntegerField()
|
||||
delta_value = serializers.IntegerField()
|
||||
delta_percent = serializers.FloatField()
|
||||
direction = serializers.ChoiceField(choices=("up", "down", "unchanged"))
|
||||
unit = serializers.CharField()
|
||||
|
||||
|
||||
@@ -48,6 +54,7 @@ class EconomicsKpisSerializer(serializers.Serializer):
|
||||
revenue = EconomicsKpiSerializer(required=False)
|
||||
ebitda = EconomicsKpiSerializer(required=False)
|
||||
net_profit = EconomicsKpiSerializer(required=False)
|
||||
revenue_per_employee = EconomicsKpiSerializer(required=False)
|
||||
gross_profit = EconomicsKpiSerializer(required=False)
|
||||
operating_profit = EconomicsKpiSerializer(required=False)
|
||||
net_debt = EconomicsKpiSerializer(required=False)
|
||||
@@ -87,6 +94,8 @@ class EconomicsResponseSerializer(serializers.Serializer):
|
||||
organization_id = serializers.UUIDField()
|
||||
group = serializers.CharField()
|
||||
periods = serializers.ListField(child=serializers.IntegerField())
|
||||
data_available = serializers.BooleanField()
|
||||
message = serializers.CharField(allow_null=True)
|
||||
kpis = EconomicsKpisSerializer()
|
||||
series = EconomicsMetricSeriesSerializer(many=True)
|
||||
ratios = EconomicsRatioSerializer(many=True)
|
||||
@@ -111,6 +120,9 @@ class PersonnelAgeDistributionSerializer(serializers.Serializer):
|
||||
class PersonnelHistorySerializer(serializers.Serializer):
|
||||
year = serializers.IntegerField()
|
||||
average_employees = serializers.IntegerField()
|
||||
avg_payroll_employees = serializers.IntegerField()
|
||||
average_age = serializers.FloatField()
|
||||
payroll_fund = serializers.IntegerField()
|
||||
|
||||
|
||||
class PersonnelResponseSerializer(serializers.Serializer):
|
||||
@@ -124,6 +136,7 @@ class PersonnelResponseSerializer(serializers.Serializer):
|
||||
|
||||
class EquipmentSummarySerializer(serializers.Serializer):
|
||||
total_equipment = serializers.IntegerField()
|
||||
machine_tools_and_equipment = serializers.IntegerField()
|
||||
domestic_equipment = serializers.IntegerField()
|
||||
imported_equipment = serializers.IntegerField()
|
||||
physical_wear_percent = serializers.FloatField()
|
||||
@@ -139,6 +152,10 @@ class EquipmentSummarySerializer(serializers.Serializer):
|
||||
class EquipmentAgeDistributionSerializer(serializers.Serializer):
|
||||
bucket = serializers.CharField()
|
||||
units_count = serializers.IntegerField()
|
||||
imported_equipment = serializers.IntegerField()
|
||||
utilization_rate = serializers.FloatField()
|
||||
lease_share_itn_percent = serializers.FloatField(allow_null=True)
|
||||
weighted_wear_percent = serializers.FloatField()
|
||||
|
||||
|
||||
class EquipmentCategorySerializer(serializers.Serializer):
|
||||
@@ -320,6 +337,8 @@ class DashboardExecutorsSerializer(serializers.Serializer):
|
||||
|
||||
class DashboardHeadcountGrowthSerializer(serializers.Serializer):
|
||||
cluster = serializers.CharField()
|
||||
from_year = serializers.IntegerField(allow_null=True)
|
||||
to_year = serializers.IntegerField(allow_null=True)
|
||||
growth_percent = serializers.FloatField()
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,9 @@ TEST_COMPANY_IDENTITIES = tuple(
|
||||
for index in range(1, TEST_COMPANY_COUNT + 1)
|
||||
)
|
||||
DEMO_REPORT_FILE_NAMES = (
|
||||
"Ф-1_2022-12.xlsx",
|
||||
"Ф-1_2023-12.xlsx",
|
||||
"Ф-1_2024-12.xlsx",
|
||||
"Ф-1_2025-09.xlsx",
|
||||
"Ф-1_2025-12.xlsx",
|
||||
"Ф-1_2026-03.xlsx",
|
||||
|
||||
Reference in New Issue
Block a user