Пример #1
0
    def pay_bill(self, paying_user_id: int, bill: CoffeeBill) -> str:
        """
        Pay a bill by creating an expense for `paying_user` with respect to logged in splitwise user.
        :param paying_user_id:  Splitwise id of paying user
        :param bill:            CoffeeBill to be payed
        :return:                Splitwise transaction id
        """
        # Convert amount to string
        amount_str = f"{bill.sum:.2f}"

        # User which will pay money
        paying_user = ExpenseUser()
        paying_user.setId(paying_user_id)
        paying_user.setOwedShare(amount_str)

        # User which will receive money
        my_user = ExpenseUser()
        my_user.setId(self.user.getId())
        my_user.setPaidShare(amount_str)

        # Create Expense/Transaction
        expense = Expense()
        expense.setCost(amount_str)
        expense.setDescription(f"CoffeeTracker, Rechnung {bill.id}")
        expense.addUser(paying_user)
        expense.addUser(my_user)

        # Execute transaction
        expense_response, errors = self.s.createExpense(expense)

        if errors is not None:
            raise RuntimeError(
                "An error occured while processing the transaction with Splitwise: "
                f"{errors.errors}")

        return f"{expense_response.getId()}"
Пример #2
0
Файл: app.py Проект: omijn/tmo
def add_group_expense(exp_data: dict):
    """
    Utility function to add multiple expenses at once.
    Pass in a dict of user_ids -> amount owed
    """
    try:
        total = float(exp_data['total'])
        group_id = exp_data['group_id']
        desc = exp_data['desc']
    except KeyError:
        print("total, group_id and desc are required fields in the expense")
        return

    expense = Expense()
    expense.setCost(total)
    expense.setDescription(desc)
    expense.setGroupId(group_id)

    sum = 0

    for user_id, details in exp_data['users'].items():
        try:
            amt = float(details["amt"])
        except KeyError:
            print("'amt' is a required field")
            return
        except ValueError:
            print(f"invalid amount {details['amt']}")
            return

        user = ExpenseUser()
        user.setId(user_id)
        user.setPaidShare(0)
        user.setOwedShare(amt)
        sum += amt

        expense.addUser(user)

    me = ExpenseUser()
    me.setId(s.getCurrentUser().getId())
    me.setPaidShare(total)
    me.setOwedShare(total - sum)
    expense.addUser(me)

    if DRY_RUN:
        print("--- dry run ---")
        print_expense_details(expense)
        return

    resp, err = s.createExpense(expense)

    # handle err
    if err is not None:
        print(err.getErrors())
        return

    print("Expense created!")
    timestamp_comment = f"added at {datetime.datetime.now()} :)"
    add_comment_to_expense(resp.getId(), timestamp_comment)

    # extra comments
    if "comment" in exp_data and exp_data["comment"] != "":
        add_comment_to_expense(resp.getId(), comment)