first commit
This commit is contained in:
45
Base/Metrics/DashboardAdmin/get_total_expense.py
Normal file
45
Base/Metrics/DashboardAdmin/get_total_expense.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from django.db.models import Sum
|
||||
from django.utils import timezone
|
||||
from collections import defaultdict
|
||||
from datetime import date
|
||||
from Base.models import (
|
||||
BankAccount,
|
||||
ChartOfAccount
|
||||
)
|
||||
from Movement.models import (
|
||||
ExpenseOrCredit,
|
||||
)
|
||||
|
||||
def metric():
|
||||
today = date.today()
|
||||
|
||||
expenses = ExpenseOrCredit.objects.filter(
|
||||
firm=True,
|
||||
debit=True,
|
||||
date__month=today.month,
|
||||
date__year=today.year
|
||||
).select_related('bank', 'chart_of_account').order_by('bank', 'date')
|
||||
|
||||
# Estrutura: { 'Nome do Banco': {'registros': [...], 'total_banco': 0} }
|
||||
grouped_data = {}
|
||||
total_geral = 0
|
||||
|
||||
for expense in expenses:
|
||||
bank_name = expense.bank.name
|
||||
|
||||
if bank_name not in grouped_data:
|
||||
grouped_data[bank_name] = {'registros': [], 'total_banco': 0}
|
||||
|
||||
grouped_data[bank_name]['registros'].append(expense)
|
||||
|
||||
# Soma os valores
|
||||
valor = float(expense.gross_value)
|
||||
grouped_data[bank_name]['total_banco'] += valor
|
||||
total_geral += valor
|
||||
|
||||
return {
|
||||
'grouped_data': grouped_data,
|
||||
'total_geral': total_geral,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user