ComptaClub/src/comptabilite/tools.py

282 lines
8.4 KiB
Python

# coding=utf-8
from decimal import Decimal
from django.db.models import Sum
from .models import (
Transaction,
TransactionType,
Patrimony,
PatrimonyType,
RightEngagementType,
RightEngagement,
)
from .utils import zero_or_value
def get_transactions_and_sums_for_year_and_type(accounting_year, transaction_type_id):
"""
Récupère les transactions associées à une année et un type et additionne les valeurs obtenues.
Args:
accounting_year (int): année comptable
transaction_type_id (int): type de transaction
Returns:
(dict)
"""
transactions_list = (
Transaction.objects.by_year(accounting_year)
.filter(transaction_type__transaction_type=transaction_type_id)
.order_by("registrationDate")
)
sum_bank_amount = 0
sum_box_amount = 0
sum_total_amount = 0
for transaction in transactions_list:
sum_bank_amount += transaction.bkAmount
sum_box_amount += transaction.bxAmount
sum_total_amount += transaction.totalAmount
return {
"transactions_list": transactions_list,
"sum_bank_amount": sum_bank_amount,
"sum_box_amount": sum_box_amount,
"sum_total_amount": sum_total_amount,
}
def get_transactions_totalamount_sum_value_for_type(accounting_year, transaction_type):
"""
Récupère les transactions associées à une année et un type et additionne le montant total des
records obtenus.
Args:
accounting_year (int): année comptable
transaction_type (id): type de transaction
Returns:
(int): somme totale
"""
return zero_or_value(
list(
Transaction.objects.by_year(accounting_year)
.filter(
transaction_type=transaction_type, is_simulated=False, is_done=True,
)
.aggregate(Sum("totalAmount"))
.values()
)[0]
)
def get_transactions_totalamount_sum_value_for_parenttype(
accounting_year, transaction_type
):
"""
Récupère les transactions associées à une année et un type et additionne le montant total des
records obtenus.
Args:
accounting_year (int): année comptable
transaction_type (id): type de transaction
Returns:
(int): somme totale
"""
return zero_or_value(
list(
Transaction.objects.by_year(accounting_year)
.filter(
transaction_type__parent=transaction_type,
is_simulated=False,
is_done=True,
)
.aggregate(Sum("totalAmount"))
.values()
)[0]
)
def get_transactiontypes_and_total_amount_transactions(
accounting_year, transactiontype_id
):
"""
Récupère tous les types de transactions qui existent pour, pour chacun d'eux récupère toutes
les transactions qui sont `effectuées` et non `simulées` et va additionner les montants totaux
par type de transaction.
Args:
accounting_year (int): année comptable
transactiontype_id (int): type de transaction
Returns:
(dict): ensemble des types de transaction et somme totale
"""
i = 0
total_transactiontypes_value = 0
transactiontypes_info_list = []
transactiontypes_list = TransactionType.objects.filter(
transaction_type=transactiontype_id, parent=None
).order_by("order")
for transaction_type in transactiontypes_list:
j = 0
sum_value = get_transactions_totalamount_sum_value_for_type(
accounting_year, transaction_type
)
transactiontypes_info_list.append(
{
"label": transaction_type.label,
"category": transaction_type.category,
"parent": None,
"total_transactiontypes_value": sum_value,
"id": transaction_type.id,
}
)
transactiontype_subtype = TransactionType.objects.filter(
parent=transaction_type
).order_by("order")
for subtype in transactiontype_subtype:
subtype_sum_value = get_transactions_totalamount_sum_value_for_type(
accounting_year, subtype
)
transactiontypes_info_list.append(
{
"label": subtype.label,
"category": subtype.category,
"parent": subtype.parent,
"total_transactiontypes_value": subtype_sum_value,
"id": subtype.id,
}
)
transactiontypes_info_list[i][
"total_transactiontypes_value"
] += subtype_sum_value
j += 1
total_transactiontypes_value += transactiontypes_info_list[i][
"total_transactiontypes_value"
]
i += j + 1
return {
"transactiontypes_info_list": transactiontypes_info_list,
"total": total_transactiontypes_value,
}
def get_transactiontype_and_sum_for_spf_export(accounting_year, transaction_type_id):
"""
Récupère tous les types de transactions sans parents pour une année et un type donnés. Ensuite,
pour chacun d'eux elle va sélectionner toutes les transactions qui sont `effectuées` et `non
simulées` appartenant à ce type de transactions et va ensuite additionner les montants totaux
du type de transaction.
Args:
accounting_year (int): année comptable
transaction_type_id (int): type de transaction
Returns:
(dict): ensemble des types de transaction et somme totale
"""
sum_total_transaction = 0
transaction_type_info = []
transaction_type_list = TransactionType.objects.filter(
transaction_type=transaction_type_id, parent=None
).order_by("order")
for transaction_type in transaction_type_list:
sum_total_amount = get_transactions_totalamount_sum_value_for_type(
accounting_year, transaction_type
) + get_transactions_totalamount_sum_value_for_parenttype(
accounting_year, transaction_type
)
sum_total_transaction += sum_total_amount
transaction_type_info.append(
{
"label": transaction_type.label,
"sum_total_amount": sum_total_amount
if sum_total_amount != 0
else Decimal(0.00),
"category": transaction_type.category,
}
)
return {
"transaction_type_info": transaction_type_info,
"sum_total_transaction": sum_total_transaction,
}
def get_right_engagement_and_sump_spf(accounting_year, category):
"""
Récupère tous les types de droits et d'engagements et l'ensemble lignes correspondantes et
additionne les montants.
Args:
accounting_year (int): année comptable
category (int): choix de la catégorie (i.e. "droits" ou "engagements")
Returns:
(int): somme totale
"""
right_engagement_info = []
right_engagement_list = RightEngagementType.objects.filter(
category=category
).order_by("order")
for right_engagement in right_engagement_list:
tmp = list(
RightEngagement.objects.by_year(accounting_year)
.filter(r_e_type=right_engagement)
.aggregate(Sum("amount"))
.values()
)[0]
right_engagement_info.append(
[right_engagement.label, tmp if tmp is not None else Decimal(0.00)]
)
return right_engagement_info
def get_asset_liability_and_sump_spf(accounting_year, category):
"""
Récupère tous les types de droits et dettes. Pour chacun de ces types, va chercher l'ensemble
lignes correspondantes et additionne les montants.
Args:
accounting_year (int): année comptable
category (int): choix de la catégorie (i.e. "droits" ou "engagements")
Returns:
(int): somme totale
"""
asset_liability_info = []
asset_liability_list = PatrimonyType.objects.filter(category=category).order_by(
"order"
)
for item in asset_liability_list:
tmp = list(
Patrimony.objects.by_year(accounting_year)
.filter(patrimony_type=item)
.aggregate(Sum("totalAmount"))
.values()
)[0]
asset_liability_info.append(
[item.label, tmp if tmp is not None else Decimal(0.00)]
)
return asset_liability_info