From f41a5a32f7417a874b497640373ea3911eb1e133 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 3 Aug 2020 11:15:50 +0200 Subject: [PATCH] Fix note balances if needed --- management/commands/check_consistency.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/management/commands/check_consistency.py b/management/commands/check_consistency.py index 67a40fb..72bbdfd 100644 --- a/management/commands/check_consistency.py +++ b/management/commands/check_consistency.py @@ -11,6 +11,7 @@ from note.templatetags.pretty_money import pretty_money class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument('--sum-all', '-s', action='store_true', help='Check if the global sum is equal to zero') + parser.add_argument('--fix', '-f', action='store_true', help='Fix note balances') def handle(self, *args, **options): if options["sum_all"]: @@ -29,11 +30,14 @@ class Command(BaseCommand): .annotate(total=F("quantity") * F("amount")).aggregate(Sum("total"))["total__sum"] or 0 outcoming = Transaction.objects.filter(valid=True, source=note)\ .annotate(total=F("quantity") * F("amount")).aggregate(Sum("total"))["total__sum"] or 0 - expected_balance = incoming - outcoming - if expected_balance != balance: + calculated_balance = incoming - outcoming + if calculated_balance != balance: self.stderr.write(self.style.NOTICE("LA SOMME DES TRANSACTIONS DE LA NOTE {} NE CORRESPOND PAS " "AVEC LE MONTANT RÉEL".format(str(note)))) self.stderr.write(self.style.NOTICE("Attendu : {}, calculé : {}" - .format(pretty_money(balance), pretty_money(expected_balance)))) + .format(pretty_money(balance), pretty_money(calculated_balance)))) + if options["fix"]: + note.balance = calculated_balance + note.save() error = True exit(1 if error else 0)