|
|
|
@ -6,7 +6,7 @@
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
import freitagslib.network as net
|
|
|
|
|
from freitagslib.commands import BuyCommand, DepositCommand, ToGoCommand
|
|
|
|
|
from freitagslib.commands import BuyCommand, DepositCommand
|
|
|
|
|
|
|
|
|
|
import colorama
|
|
|
|
|
from colorama import Fore, Style
|
|
|
|
@ -98,22 +98,14 @@ class Status:
|
|
|
|
|
def color(amount):
|
|
|
|
|
return COLOR_WITHDRAW if amount < 0 else COLOR_DEPOSIT
|
|
|
|
|
|
|
|
|
|
def color_command(command):
|
|
|
|
|
diff = command.difference()
|
|
|
|
|
if diff >= 0:
|
|
|
|
|
return COLOR_DEPOSIT
|
|
|
|
|
return COLOR_TO_GO if isinstance(command, ToGoCommand) else COLOR_WITHDRAW
|
|
|
|
|
|
|
|
|
|
def show_total(balance, plus=' '):
|
|
|
|
|
print('%3s %-40s %s%c %6.2f Euro%s' \
|
|
|
|
|
% ('', '', color(balance), sign(balance, plus),
|
|
|
|
|
abs(balance), COLOR_RESET))
|
|
|
|
|
|
|
|
|
|
def show_item(position, command):
|
|
|
|
|
diff = command.difference()
|
|
|
|
|
label = command.label()
|
|
|
|
|
def show_item(position, diff, label, color):
|
|
|
|
|
print('%2d) %-40s %s%c %6.2f Euro%s' \
|
|
|
|
|
% (position, label, color_command(command), sign(diff),
|
|
|
|
|
% (position, label, color, sign(diff),
|
|
|
|
|
abs(diff), COLOR_RESET))
|
|
|
|
|
|
|
|
|
|
def show_bar():
|
|
|
|
@ -127,8 +119,18 @@ class Status:
|
|
|
|
|
|
|
|
|
|
print('Geplante Änderungen:')
|
|
|
|
|
show_total(initial_balance)
|
|
|
|
|
for i, (command, balance_backup) in enumerate(self.transfers):
|
|
|
|
|
show_item(i + 1, command)
|
|
|
|
|
i = 1
|
|
|
|
|
for command, dummy in self.transfers:
|
|
|
|
|
if isinstance(command, BuyCommand):
|
|
|
|
|
if (command.includes_commodity()):
|
|
|
|
|
show_item(i, -command.commodity_value(), command.commodity_label(), COLOR_WITHDRAW)
|
|
|
|
|
i += 1
|
|
|
|
|
if (command.includes_deposit()):
|
|
|
|
|
show_item(i, -command.deposit_value(), command.deposit_label(), COLOR_TO_GO)
|
|
|
|
|
i += 1
|
|
|
|
|
else:
|
|
|
|
|
show_item(i, command.difference(), command.label(), COLOR_DEPOSIT)
|
|
|
|
|
i += 1
|
|
|
|
|
show_bar()
|
|
|
|
|
if len(self.transfers) > 1:
|
|
|
|
|
show_total(self.balance - initial_balance, plus='+')
|
|
|
|
@ -215,109 +217,78 @@ class Status:
|
|
|
|
|
|
|
|
|
|
if scope == TO_GO_ALL:
|
|
|
|
|
"""
|
|
|
|
|
Adds a ToGoCommands for each BuyCommands with deposit > 0 lacking a pair
|
|
|
|
|
Makes all BuyCommands with deposit > 0 include deposit
|
|
|
|
|
...and updates future balance accordingly.
|
|
|
|
|
"""
|
|
|
|
|
after = list()
|
|
|
|
|
dummy, initial_balance = self.transfers[0]
|
|
|
|
|
balance_before = initial_balance
|
|
|
|
|
|
|
|
|
|
def body(prev, command, balance_before):
|
|
|
|
|
if isinstance(prev, BuyCommand) \
|
|
|
|
|
and prev.deposit() > 0 \
|
|
|
|
|
and (not isinstance(command, ToGoCommand) \
|
|
|
|
|
or (isinstance(command, ToGoCommand) \
|
|
|
|
|
and prev.item() != command.item())):
|
|
|
|
|
to_go = ToGoCommand(prev.item())
|
|
|
|
|
prev.set_to_go(True)
|
|
|
|
|
after.append((to_go, balance_before))
|
|
|
|
|
balance_before += to_go.difference()
|
|
|
|
|
if command is not None:
|
|
|
|
|
after.append((command, balance_before))
|
|
|
|
|
balance_before += command.difference()
|
|
|
|
|
return balance_before
|
|
|
|
|
|
|
|
|
|
prev = None
|
|
|
|
|
for command, dummy in list(self.transfers):
|
|
|
|
|
balance_before = body(prev, command, balance_before)
|
|
|
|
|
prev = command
|
|
|
|
|
balance_before = body(prev, None, balance_before)
|
|
|
|
|
for command, dummy in self.transfers:
|
|
|
|
|
if isinstance(command, BuyCommand) \
|
|
|
|
|
and not command.includes_deposit() \
|
|
|
|
|
and command.deposit_value() > 0:
|
|
|
|
|
command.include_deposit(True)
|
|
|
|
|
balance_before += command.difference()
|
|
|
|
|
|
|
|
|
|
self.balance = balance_before
|
|
|
|
|
self.transfers = after
|
|
|
|
|
elif scope == TO_GO_NONE:
|
|
|
|
|
"""
|
|
|
|
|
Deletes all ToGoCommands
|
|
|
|
|
Makes all BuyCommands that include commodity
|
|
|
|
|
not include deposit any more
|
|
|
|
|
...and updates future balance accordingly.
|
|
|
|
|
"""
|
|
|
|
|
after = list()
|
|
|
|
|
first_command, initial_balance = self.transfers[0]
|
|
|
|
|
balance_before = initial_balance
|
|
|
|
|
|
|
|
|
|
for command, dummy in list(self.transfers):
|
|
|
|
|
for command, dummy in self.transfers:
|
|
|
|
|
if isinstance(command, BuyCommand) \
|
|
|
|
|
and command.is_to_go():
|
|
|
|
|
command.set_to_go(False)
|
|
|
|
|
elif isinstance(command, ToGoCommand):
|
|
|
|
|
continue
|
|
|
|
|
after.append((command, balance_before))
|
|
|
|
|
and command.includes_commodity():
|
|
|
|
|
command.include_deposit(False)
|
|
|
|
|
balance_before += command.difference()
|
|
|
|
|
|
|
|
|
|
self.balance = balance_before
|
|
|
|
|
self.transfers = after
|
|
|
|
|
elif scope == TO_GO_PREV:
|
|
|
|
|
"""
|
|
|
|
|
Adds a ToGoCommand after previous BuyCommand
|
|
|
|
|
Makes the last BuyCommand include deposit
|
|
|
|
|
...and updates future balance accordingly.
|
|
|
|
|
"""
|
|
|
|
|
prev, balance_backup = self.transfers[-1]
|
|
|
|
|
if not isinstance(prev, BuyCommand):
|
|
|
|
|
error_page(_PRODUCT_FIRST)
|
|
|
|
|
return
|
|
|
|
|
to_go = ToGoCommand(prev.item())
|
|
|
|
|
prev.set_to_go(True)
|
|
|
|
|
self.transfers.append((to_go, self.balance))
|
|
|
|
|
self.balance += to_go.difference()
|
|
|
|
|
if prev.includes_deposit():
|
|
|
|
|
error_page('FEHLER: Pfand für Produkt "%s" bereits aktiviert' % prev.item_name())
|
|
|
|
|
return
|
|
|
|
|
if prev.deposit_value() <= 0:
|
|
|
|
|
error_page('FEHLER: Produkt "%s" hat kein Pfand' % prev.item_name())
|
|
|
|
|
return
|
|
|
|
|
before = prev.difference()
|
|
|
|
|
prev.include_deposit(True)
|
|
|
|
|
after = prev.difference()
|
|
|
|
|
self.balance += (after - before)
|
|
|
|
|
elif scope == TO_GO_ONLY:
|
|
|
|
|
"""
|
|
|
|
|
Replace all BuyCommands with deposit > 0 by
|
|
|
|
|
a ToGoCommand. Resolve ToGoCommand duplicates.
|
|
|
|
|
Makes all BuyCommand that include commodity
|
|
|
|
|
be deposit only
|
|
|
|
|
...and updates future balance accordingly.
|
|
|
|
|
"""
|
|
|
|
|
after = list()
|
|
|
|
|
first_command, initial_balance = self.transfers[0]
|
|
|
|
|
dummy, initial_balance = self.transfers[0]
|
|
|
|
|
balance_before = initial_balance
|
|
|
|
|
|
|
|
|
|
for command, dummy in list(self.transfers):
|
|
|
|
|
if isinstance(command, ToGoCommand) \
|
|
|
|
|
and prev_item is not None \
|
|
|
|
|
and command.item() == prev_item:
|
|
|
|
|
"""
|
|
|
|
|
Skip this very duplicate
|
|
|
|
|
"""
|
|
|
|
|
prev_item = None
|
|
|
|
|
continue
|
|
|
|
|
elif isinstance(command, BuyCommand) \
|
|
|
|
|
and command.deposit() > 0:
|
|
|
|
|
"""
|
|
|
|
|
Replace by ToGoCommand
|
|
|
|
|
"""
|
|
|
|
|
item = command.item()
|
|
|
|
|
command = ToGoCommand(item)
|
|
|
|
|
prev_item = item
|
|
|
|
|
else:
|
|
|
|
|
prev_item = None
|
|
|
|
|
after.append((command, balance_before))
|
|
|
|
|
for command, dummy in self.transfers:
|
|
|
|
|
if isinstance(command, BuyCommand) \
|
|
|
|
|
and command.deposit_value() > 0 \
|
|
|
|
|
and command.includes_commodity():
|
|
|
|
|
command.include_commodity(False)
|
|
|
|
|
command.include_deposit(True)
|
|
|
|
|
balance_before += command.difference()
|
|
|
|
|
|
|
|
|
|
self.balance = balance_before
|
|
|
|
|
self.transfers = after
|
|
|
|
|
|
|
|
|
|
def undo(self):
|
|
|
|
|
assert(self.logged_in())
|
|
|
|
|
if self.transfers:
|
|
|
|
|
(last_command, balance_backup) = self.transfers[-1]
|
|
|
|
|
if isinstance(last_command, ToGoCommand) \
|
|
|
|
|
and self.transfers:
|
|
|
|
|
second_last_command = self.transfers[-2]
|
|
|
|
|
if isinstance(second_last_command, BuyCommand):
|
|
|
|
|
second_last_command.set_to_go(False)
|
|
|
|
|
dummy, balance_backup = self.transfers[-1]
|
|
|
|
|
self.transfers.pop()
|
|
|
|
|
self.balance = balance_backup
|
|
|
|
|
else:
|
|
|
|
|