You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k4ever/client-barcode/freitagskasse.py

182 lines
4.2 KiB

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GPL v3 or later
from __future__ import print_function
import freitagslib.network as net
import sys
import decimal
import os
import time
def clear():
os.system('clear')
CODES = {
'UNDO':('undo',),
'COMMIT':('commit',),
'DEPOSIT 0.01':('deposit', decimal.Decimal('0.01')),
'DEPOSIT 0.05':('deposit', decimal.Decimal('0.05')),
'DEPOSIT 0.10':('deposit', decimal.Decimal('0.10')),
'DEPOSIT 0.50':('deposit', decimal.Decimal('0.50')),
'DEPOSIT 1.00':('deposit', decimal.Decimal('1.00')),
'DEPOSIT 5.00':('deposit', decimal.Decimal('5.00')),
'DEPOSIT 10.00':('deposit', decimal.Decimal('10.00')),
'DEPOSIT 50.00':('deposit', decimal.Decimal('50.00')),
'731453722620':('buy', 'CD Eagle Eye-Cherry - Desireless', decimal.Decimal('8.00')),
# http://www.gearfuse.com/wp-content/uploads/2008/07/mate.png
'4029764001807':('buy', 'Club Mate 0.5l', decimal.Decimal('0.60')),
'4013595054149':('buy', 'Vita Cola 1.0l', decimal.Decimal('0.79')),
}
def delay(what, seconds):
for i in xrange(seconds, 0, -1):
if i < seconds:
sys.stdout.write('\r')
sys.stdout.write('%s in %d Sekunden... ' % (what, i))
sys.stdout.flush()
time.sleep(1)
def warn_balance():
print('Kontostrand im Minus, NICHT cool!')
def error_page(message):
clear()
print(message)
print()
delay('Weiter', 3)
class Status:
def __init__(self):
self._reset()
def _reset(self):
self.login_code = None
self.login_name = None
self.balance = None
self.transfers = None
def dump(self):
if self.logged_in():
print('Eingeloggt als: %s' % (self.login_name))
print()
if self.transfers:
print('Geplante Änderungen:')
initial_balance = self.transfers[0][2]
print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if initial_balance > 0 else '-', abs(initial_balance)))
for i, (label, difference, balance_backup) in enumerate(self.transfers):
print(('%2d) %-40s %c %6.2f Euro') % (i + 1, label, '+' if difference > 0 else '-', abs(difference)))
print(('%3s %-40s %8s') % ('', '', '============='))
print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if self.balance > 0 else '-', abs(self.balance)))
if self.balance < 0:
warn_balance()
print()
print('Committen nicht vergessen!')
else:
print('Kontostand beträgt: %.2f Euro' % (self.balance))
if self.balance < 0:
print()
warn_balance()
else:
print('Bitte einloggen.')
print()
print('Scanne dazu deine ID-Karte mit dem Barcode-Leser.')
print()
def logged_in(self):
return self.login_code is not None
def login(self, code):
assert(not self.logged_in())
self.login_code = code
self.login_name = 'Sebastian Pipping' ############
self.balance = decimal.Decimal('0.20') ######################
self.transfers = list()
def commit(self):
assert(self.logged_in())
# TODO save balance
self.transfers = None
# Show final balance for some time
clear()
self.dump()
delay('Logout', 3)
# Logout
self._reset()
def _do_money(self, label, difference):
log_entry = (label, difference, self.balance)
self.transfers.append(log_entry)
self.balance = self.balance + difference
def buy(self, title, price):
assert(self.logged_in())
self._do_money(title, -price)
def deposit(self, amount):
assert(self.logged_in())
self._do_money('%.2f Euro einzahlen' % amount, amount)
def undo(self):
assert(self.logged_in())
if self.transfers:
(label, difference, balance_backup) = self.transfers[-1]
self.transfers.pop()
self.balance = balance_backup
else:
error_page('FEHLER: Nichts da, was ich rückgängig machen könnte.')
def print_prompt():
sys.stdout.write(">>> ")
def handle(line, status):
if line == 'exit':
clear()
sys.exit(0)
if status.logged_in():
if line in CODES:
call = CODES[line]
method = call[0]
params = call[1:]
getattr(status, method)(*params)
else:
error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line)
else:
status.login(line)
def main():
status = Status()
while True:
clear()
status.dump()
print_prompt()
l = sys.stdin.readline()
if not l:
break
line = l.rstrip()
handle(line, status)
if __name__ == '__main__':
main()