From 7e447b0e62b6e640ddc088d4c7bd6dfc6621e0c1 Mon Sep 17 00:00:00 2001 From: Stephen Early Date: Wed, 17 Jan 2024 21:37:00 +0000 Subject: [PATCH] Fix Transline.original_amount attribute A bug was introduced in a14201d56600701ae858133c709edafcbea74ba7 when this was converted to a hybrid property. Closes #275 on github. --- quicktill/models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quicktill/models.py b/quicktill/models.py index a151cfa..459d1d5 100644 --- a/quicktill/models.py +++ b/quicktill/models.py @@ -1175,11 +1175,20 @@ class Transline(Base, Logged): name="discount_name_constraint"), ) + # The original_amount instance attribute may be accessed before + # the instance is committed to the database, and at this point + # self.discount may be None; treat this as zero @hybrid_property def original_amount(self): """The original amount of the transaction line before any discounts """ - return self.amount + self.discount + return self.amount + (self.discount or zero) + + # The original_amount class attribute relies on amount and + # discount having nullable=False + @original_amount.expression + def original_amount(cls): + return cls.amount + cls.discount transaction = relationship( Transaction,