mirror of
https://gitlab.crans.org/bde/nk20-scripts
synced 2025-10-20 20:09:35 +02:00
Compare commits
1 Commits
229531254b
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
ac93fec326 |
114
management/commands/steals.py
Normal file
114
management/commands/steals.py
Normal file
@@ -0,0 +1,114 @@
|
||||
# Copyright (C) 2018-2025 by BDE ENS-Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Sum
|
||||
|
||||
from note.models import RecurrentTransaction, TransactionTemplate
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = """
|
||||
Syntax of inventory file:\n
|
||||
DATE_START=YYYY-MM-DD HH:MM:SS\n
|
||||
button_id=quantity\n
|
||||
# some comment\n
|
||||
...\n
|
||||
DATE_END=YYYY-MM-DD\n
|
||||
button_id=quantity\n
|
||||
...\n
|
||||
GROCERY\n
|
||||
button_id=quantity\n
|
||||
...\n
|
||||
"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('file', type=str)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
if not kwargs['file']:
|
||||
kwargs['file'] = '/var/inventory/current.inv'
|
||||
file = open(kwargs['file'], 'r', encoding='utf-8')
|
||||
|
||||
inv_start, inv_end, inv_grocery = {}, {}, {}
|
||||
start = True
|
||||
grocery = False
|
||||
|
||||
for line in file:
|
||||
if line[0] == '#':
|
||||
continue
|
||||
if start:
|
||||
if 'DATE_START' in line:
|
||||
date_start = line.split('=')[1]
|
||||
elif 'DATE_END' in line:
|
||||
date_end = line.split('=')[1]
|
||||
start = False
|
||||
else:
|
||||
add_to_dict(line, inv_start)
|
||||
elif not grocery:
|
||||
if 'GROCERY' in line:
|
||||
grocery = True
|
||||
else:
|
||||
add_to_dict(line, inv_end)
|
||||
else:
|
||||
add_to_dict(line, inv_grocery)
|
||||
|
||||
delta_real = delta_from_inv(inv_start, inv_end, inv_grocery)
|
||||
delta_th = delta_from_note(date_start, date_end, delta_real.keys())
|
||||
|
||||
steal_dict = steal(delta_real, delta_th)
|
||||
|
||||
if kwargs['verbosity'] > 0:
|
||||
for button in steal_dict:
|
||||
text = "%.2f" % steal_dict[button] + \
|
||||
f"% of steal on button: {button.name} (pk={button.pk})"
|
||||
if steal_dict[button] > 20:
|
||||
self.stdout.write(self.style.ERROR(text))
|
||||
elif steal_dict[button] > 0:
|
||||
self.stdout.write(self.style.WARNING(text))
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS(text))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def add_to_dict(line, d):
|
||||
pk, quantity = line.split('=')
|
||||
button = TransactionTemplate.objects.get(pk=pk)
|
||||
d[button] = int(quantity)
|
||||
return
|
||||
|
||||
|
||||
def delta_from_note(date_start, date_end, keys):
|
||||
d = {}
|
||||
for button in keys:
|
||||
quantity = RecurrentTransaction.objects.filter(
|
||||
valid=True,
|
||||
created_at__gte=date_start,
|
||||
created_at__lte=date_end,
|
||||
template__pk=button.pk).aggregate(total=Sum('quantity'))['total']
|
||||
d[button] = quantity
|
||||
return d
|
||||
|
||||
|
||||
def delta_from_inv(s, e, g):
|
||||
d = {}
|
||||
for button in s:
|
||||
if button in g:
|
||||
if button in e:
|
||||
d[button] = s[button] + g[button] - e[button]
|
||||
else:
|
||||
d[button] = s[button] + g[button]
|
||||
else:
|
||||
if button in e:
|
||||
d[button] = s[button] - e[button]
|
||||
else:
|
||||
d[button] = s[button]
|
||||
return d
|
||||
|
||||
|
||||
def steal(real, th):
|
||||
s = {}
|
||||
for b in real:
|
||||
s[b] = 100 * (real[b] - th[b]) / real[b]
|
||||
return s
|
Reference in New Issue
Block a user