diff options
| -rw-r--r-- | geldschieberbot.py | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py index 2c13ca8..27596cd 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -165,6 +165,7 @@ cars [cmd] - interact with the available cars cars [list | ls] - list available cars and their service charge cars add car-name service-charge - add new car cars new car-name service-charge - add new car +cars pay car-name amount - pay a bill for the specified car tanken amount [person] [car] [info] - calculate fuel costs, service charge and add them to the person's and car's balance respectively @@ -416,8 +417,72 @@ def cars(sender, args, msg): available_cars[car] = service_charge add_to_balance(car) return 'added "{}" as an available car'.format(car), None + # pay bill + elif args[1] in ["pay"]: + if len(args) < 4: + return None, 'not in form "{} {} car-name amount"'.format(args[0], args[1]) + + if not sender in num2name: + return None, "you must register first" + else: + sender_name = num2name[sender] + + car = args[2] + if car not in available_cars: + return None, 'car "{}" not known'.format(car) + + try: + amount = to_cent(args[3]) + amount_euro = to_euro(amount) + except: + return None, "amount must be a positive number" + + output = "" + + global record_changes + saved_record_changes = record_changes + record_changes = False + change = [args] + + total_available_charge = 0 + available_charges = [] + for person in balance[car]: + _amount = balance[car][person] + if _amount < 0: + total_available_charge -= _amount + available_charges.append((person, _amount)) + + proportion = -1 + if amount < total_available_charge: + proportion = -1 * (amount / total_available_charge) + + _, err = transaction(sender, f"!gib {car} {amount_euro}".split(), "") + assert(err is None) + output += f"{sender_name} payed {amount_euro}\n" + + # transfere money + for person, _amount in available_charges: + if person == sender_name or _amount >= 0: + continue + + to_move = _amount * proportion + to_move_euro = to_euro(to_move) + ret, err = transfer(sender, ["transfer", to_move_euro, car, person], "") + assert(err is None) + + output += "Transfere {} from {} to {}\n".format(to_move_euro, person, sender_name) + + output += "New Balances:\n" + output += create_summary(sender_name) + "\n" + output += create_summary(car) + + if record_changes and not dry_run: + changes[sender].append(change) + record_changes = saved_record_changes + + return output, None else: - return None, 'cmd not in form "{} [cmd]"'.format(args[0]) + return None, 'unknown car subcommand "{}".'.format(args[1]) cmds["cars"] = cars |
