diff --git a/database.py b/database.py index c307b4e..b778dd8 100644 --- a/database.py +++ b/database.py @@ -163,7 +163,8 @@ class Transaction(TableDeclarativeBase): payment_email = Column(String) # Order ID - order_id = Column(Integer) + order_id = Column(Integer, ForeignKey("orders.order_id")) + order = relationship("Order") # Extra table parameters __tablename__ = "transactions" @@ -216,6 +217,8 @@ class Order(TableDeclarativeBase): items = relationship("OrderItem") # Extra details specified by the purchasing user notes = Column(Text) + # Linked transaction + transaction = relationship("Transaction", uselist=False) # Extra table parameters __tablename__ = "orders" @@ -223,7 +226,8 @@ class Order(TableDeclarativeBase): def __repr__(self): return f"" - def __str__(self): + def get_text(self, session): + joined_self = session.query(Order).filter_by(order_id=self.order_id).join(Transaction).one() items = "" for item in self.items: items += str(item) + "\n" @@ -232,7 +236,7 @@ class Order(TableDeclarativeBase): date=self.creation_date.isoformat(), items=items, notes=self.notes if self.notes is not None else "", - value=#TODO) + value=str(Price(-joined_self.transaction.value))) class OrderItem(TableDeclarativeBase): @@ -250,7 +254,7 @@ class OrderItem(TableDeclarativeBase): __tablename__ = "orderitems" def __str__(self): - return f"{self.product.name} - {self.product.price}" + return f"{self.product.name} - {str(Price(self.product.price))}" def __repr__(self): return f"" diff --git a/worker.py b/worker.py index 98c5abc..54d54ea 100644 --- a/worker.py +++ b/worker.py @@ -363,7 +363,8 @@ class ChatWorker(threading.Thread): return # Create a new transaction and add it to the session transaction = db.Transaction(user=self.user, - value=value) + value=value, + order_id=order.order_id) self.session.add(transaction) # Subtract credit from the user self.user.credit += value @@ -375,7 +376,7 @@ class ChatWorker(threading.Thread): admins = self.session.query(db.Admin).filter_by(receive_orders=True).all() # Notify them of the new placed order for admin in admins: - self.bot.send_message(admin.user_id, f"{strings.notification_order_placed.format(order=str(order))}") + self.bot.send_message(admin.user_id, f"{strings.notification_order_placed.format(order=order.get_text(self.session))}") def __order_status(self): raise NotImplementedError() @@ -457,7 +458,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 - fee_percentage = Price(configloader.config["Credit Card"]["fee_percentage"]) // 100 + # 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"]) total_fee = value * fee_percentage + fee_fixed if total_fee > 0: