72 lignes
2.3 KiB
Python
72 lignes
2.3 KiB
Python
# PIAF - Portail Internet pour les Activités familiales
|
|
# Copyright (C) 2018 Cliss XXI <tech@cliss21.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from itertools import groupby
|
|
from functools import reduce
|
|
|
|
"""
|
|
Regroup prestation set by indivudal and by activity.
|
|
Compute the sum by individual.
|
|
"""
|
|
|
|
|
|
def regroup_prestations(prestation_set):
|
|
r = {}
|
|
|
|
def sort_regroup(l, k):
|
|
def lazy_id(x):
|
|
return x[k]['id'] if x[k] != None and 'id' in x[k] else 0
|
|
|
|
def lazy_object(id, l):
|
|
return l[0][k] if l else None
|
|
|
|
grouped = groupby(sorted(l, key=lazy_id), key=lazy_id)
|
|
listed = [(id, list(l)) for id, l in grouped]
|
|
return [(lazy_object(id, l), l) for id, l in listed]
|
|
|
|
r = []
|
|
for ind, ps in sort_regroup(prestation_set, 'individu'):
|
|
ps = sort_regroup(ps, 'activite')
|
|
total = sum([sum([p['montant'] for p in l]) for _, l in ps])
|
|
r.append([ind, ps, total])
|
|
return [p for p in r if p[0] != None] + [p for p in r if p[0] == None]
|
|
|
|
|
|
"""
|
|
Merge invoices into a single one.
|
|
"""
|
|
|
|
|
|
def merge_invoices(l):
|
|
r = {}
|
|
if l:
|
|
for k, v in l[0].items():
|
|
if k == "autres_factures":
|
|
continue
|
|
elif k == "numero":
|
|
r['numeros'] = sorted([f[k] for f in l])
|
|
elif k == "date_debut":
|
|
r[k] = min([f[k] for f in l])
|
|
elif k == "date_fin":
|
|
r[k] = max([f[k] for f in l])
|
|
elif k in ["montant", "regle", "solde"]:
|
|
r[k] = sum([f[k] for f in l])
|
|
elif k == "prestations_set":
|
|
r[k] = reduce(lambda x, y: x + y, [f[k] for f in l])
|
|
else:
|
|
r[k] = v
|
|
return r
|