36 lines
978 B
Python
36 lines
978 B
Python
from django.db.models import Sum,Count
|
|
from django.utils import timezone
|
|
from Base.models import (
|
|
ServiceList,
|
|
)
|
|
from Movement.models import (
|
|
Calendar,
|
|
)
|
|
|
|
def metric():
|
|
now = timezone.localtime(timezone.now())
|
|
current_month = now.month
|
|
current_year = now.year
|
|
|
|
# Criamos um queryset base para evitar repetição de filtros
|
|
base_queryset = Calendar.objects.filter(
|
|
date__month=current_month,
|
|
date__year=current_year
|
|
)
|
|
|
|
# Breakdown (Agrupado por profissional e serviço)
|
|
services_breakdown = base_queryset.values(
|
|
'professional__name',
|
|
'service__name'
|
|
).annotate(
|
|
quantidade_feitas=Count('id'),
|
|
lucro_liquido=Sum('value_cash')
|
|
).order_by('professional__name', 'service__name')
|
|
|
|
# Total geral de lucro no mês
|
|
total_geral = base_queryset.aggregate(total=Sum('value_cash'))['total'] or 0
|
|
|
|
return {
|
|
'services': services_breakdown,
|
|
'total': total_geral
|
|
} |