Fix note balances if needed

This commit is contained in:
Yohann D'ANELLO 2020-08-03 11:15:50 +02:00
parent 7d0c94c19b
commit f41a5a32f7
1 changed files with 7 additions and 3 deletions

View File

@ -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)