Fusionne deux fonctions chunkify
On rajoute de l'agrégation optionnelle dans la fonction.
This commit is contained in:
parent
26bcd729bb
commit
ef35f45ad2
2 changed files with 18 additions and 116 deletions
|
@ -2465,7 +2465,7 @@ class AccountStatOperation(UserAccountMixin, ScaleMixin, JSONDetailView):
|
|||
context_object_name = "account"
|
||||
id_prefix = ""
|
||||
|
||||
def get_operations(self, scale, types=None):
|
||||
def get_operations(self, types=None):
|
||||
# On selectionne les opérations qui correspondent
|
||||
# à l'article en question et qui ne sont pas annulées
|
||||
# puis on choisi pour chaques intervalle les opérations
|
||||
|
@ -2477,28 +2477,20 @@ class AccountStatOperation(UserAccountMixin, ScaleMixin, JSONDetailView):
|
|||
)
|
||||
if types is not None:
|
||||
all_operations = all_operations.filter(type__in=types)
|
||||
chunks = scale.get_by_chunks(
|
||||
all_operations,
|
||||
field_db="group__at",
|
||||
field_callback=(lambda d: d["group__at"]),
|
||||
)
|
||||
return chunks
|
||||
return all_operations
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
old_ctx = super().get_context_data(*args, **kwargs)
|
||||
context = {"labels": old_ctx["labels"]}
|
||||
scale = self.scale
|
||||
context = super().get_context_data(*args, **kwargs)
|
||||
|
||||
types = self.request.GET.get("types", None)
|
||||
if types is not None:
|
||||
types = ast.literal_eval(types)
|
||||
|
||||
operations = self.get_operations(types=types, scale=scale)
|
||||
operations = self.get_operations(types=types)
|
||||
# On compte les opérations
|
||||
nb_ventes = []
|
||||
for chunk in operations:
|
||||
ventes = sum(ope["article_nb"] for ope in chunk)
|
||||
nb_ventes.append(ventes)
|
||||
nb_ventes = self.scale.chunkify_qs(
|
||||
operations, field="group__at", aggregate=Sum("article_nb")
|
||||
)
|
||||
|
||||
context["charts"] = [
|
||||
{
|
||||
|
@ -2558,23 +2550,13 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
|
|||
liq_only = all_purchases.filter(group__on_acc__trigramme="LIQ")
|
||||
liq_exclude = all_purchases.exclude(group__on_acc__trigramme="LIQ")
|
||||
|
||||
chunks_liq = scale.get_by_chunks(
|
||||
liq_only, field_db="group__at", field_callback=lambda d: d["group__at"]
|
||||
nb_liq = scale.chunkify_qs(
|
||||
liq_only, field="group__at", aggregate=Sum("article_nb")
|
||||
)
|
||||
chunks_no_liq = scale.get_by_chunks(
|
||||
liq_exclude, field_db="group__at", field_callback=lambda d: d["group__at"]
|
||||
nb_accounts = scale.chunkify_qs(
|
||||
liq_exclude, field="group__at", aggregate=Sum("article_nb")
|
||||
)
|
||||
|
||||
# On compte les opérations
|
||||
nb_ventes = []
|
||||
nb_accounts = []
|
||||
nb_liq = []
|
||||
for chunk_liq, chunk_no_liq in zip(chunks_liq, chunks_no_liq):
|
||||
sum_accounts = sum(ope["article_nb"] for ope in chunk_no_liq)
|
||||
sum_liq = sum(ope["article_nb"] for ope in chunk_liq)
|
||||
nb_ventes.append(sum_accounts + sum_liq)
|
||||
nb_accounts.append(sum_accounts)
|
||||
nb_liq.append(sum_liq)
|
||||
nb_ventes = [n1 + n2 for n1, n2 in zip(nb_liq, nb_accounts)]
|
||||
|
||||
context["charts"] = [
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue