From 214401a50966001cabbcb1a68c970774aeb66aa9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 22 Mar 2018 10:33:50 +0100 Subject: [PATCH] Fix some price bugs --- strings.py | 3 +++ utils.py | 16 ++++++++++------ worker.py | 16 ++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/strings.py b/strings.py index b42aade..de05966 100644 --- a/strings.py +++ b/strings.py @@ -92,6 +92,9 @@ menu_products = "📝️ Prodotti" # Admin menu: orders menu_orders = "📦 Ordini" +# Menu: transactions +menu_transactions = "💳 Transazioni" + # Admin menu: go to user mode menu_user_mode = "👤 Passa alla modalità utente" diff --git a/utils.py b/utils.py index 5e57d7c..2805739 100644 --- a/utils.py +++ b/utils.py @@ -19,6 +19,9 @@ class Price: # Copy self self.value = value.value + def __repr__(self): + return f"" + def __str__(self): return currency_format_string.format(symbol=currency_symbol, value="{0:.2f}".format(self.value / (10 ** int(config["Payments"]["currency_exp"])))) @@ -50,19 +53,19 @@ class Price: return Price(self.value - Price(other).value) def __mul__(self, other): - return Price(self.value * Price(other).value) + return Price(int(self.value * other)) def __floordiv__(self, other): - return Price(self.value // Price(other).value) + return Price(int(self.value // other)) def __radd__(self, other): - self.__add__(other) + return self.__add__(other) def __rsub__(self, other): return Price(Price(other).value - self.value) def __rmul__(self, other): - self.__mul__(other) + return self.__mul__(other) def __iadd__(self, other): self.value += Price(other).value @@ -73,9 +76,10 @@ class Price: return self def __imul__(self, other): - self.value *= Price(other).value + self.value *= other + self.value = int(self.value) return self def __ifloordiv__(self, other): - self.value //= Price(other).value + self.value //= other return self diff --git a/worker.py b/worker.py index 3fa3b2d..fd84f37 100644 --- a/worker.py +++ b/worker.py @@ -465,9 +465,8 @@ class ChatWorker(threading.Thread): # Create the price array prices = [telegram.LabeledPrice(label=strings.payment_invoice_label, amount=int(value))] # If the user has to pay a fee when using the credit card, add it to the prices list - # TODO: there's a bug here fee_percentage = float(configloader.config["Credit Card"]["fee_percentage"]) / 100 - fee_fixed = Price(configloader.config["Credit Card"]["fee_fixed"]) + fee_fixed = int(configloader.config["Credit Card"]["fee_fixed"]) total_fee = value * fee_percentage + fee_fixed if total_fee > 0: prices.append(telegram.LabeledPrice(label=strings.payment_invoice_fee_label, amount=int(total_fee))) @@ -525,10 +524,14 @@ class ChatWorker(threading.Thread): Administrative bot actions should be placed here.""" # Loop used to return to the menu after executing a command while True: - # Create a keyboard with the admin main menu - keyboard = [[telegram.KeyboardButton(strings.menu_products)], - [telegram.KeyboardButton(strings.menu_orders)], - [telegram.KeyboardButton(strings.menu_user_mode)]] + # Create a keyboard with the admin main menu based on the admin permissions specified in the db + keyboard = [[strings.menu_user_mode]] + if self.admin.edit_products: + keyboard.append([strings.menu_products]) + if self.admin.receive_orders: + keyboard.append([strings.menu_orders]) + if self.admin.view_transactions: + keyboard.append([strings.menu_transactions]) # Send the previously created keyboard to the user (ensuring it can be clicked only 1 time) self.bot.send_message(self.chat.id, strings.conversation_open_admin_menu, reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)) @@ -716,6 +719,7 @@ class ChatWorker(threading.Thread): if isinstance(update, CancelSignal): # Stop the listening mode self.listening_mode = False + break # If the user pressed the complete order button, complete the order elif update.data == "order_complete": # Find the order