Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Add Reserved and allow to update balance of product
src/types/balance.py | 17 +++++++++++++++-- src/types/product.py | 2 +-
diff --git a/src/types/balance.py b/src/types/balance.py index ac55ac258d06b9b1e9cac44b313ac7fc1d1848f5..f52e74c9cfe5c7266e93fe0a13635eb57928a723 100644 --- a/src/types/balance.py +++ b/src/types/balance.py @@ -4,22 +4,35 @@ class Balance(db.Model): __tablename__ = "balances" id = db.Column(db.Integer, primary_key = True) - value = db.Column(db.Numeric(10, 2), nullable = False) + value = db.Column(db.Numeric(10, 2), default = 0.0, nullable = False) + reserved = db.Column(db.Numeric(10, 2), default = 0.0, nullable = False) product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable = False) created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow) - def __init__(self, value, product_id): + def __init__(self, value, reserved, product_id): self.value = value + self.reserved = reserved self.product_id = product_id def create(self): db.session.add(self) db.session.commit() + def update(self, value): + self.value = value + db.session.commit() + + def rollback_reserved(self): + self.value += self.reserved + self.reserved = 0.0 + + db.session.commit() + def to_json(self): return { + "reserved": float(self.reserved), "value": float(self.value), "updated_at": self.updated_at.__str__() } diff --git a/src/types/product.py b/src/types/product.py index c365f9463a68cfb96f85966510793f4d143b4964..75dab0884b580a4938997fe841628e9ead848f16 100644 --- a/src/types/product.py +++ b/src/types/product.py @@ -26,7 +26,7 @@ def create(self): db.session.add(self) db.session.commit() - Balance(0.0, self.id).create() + Balance(0.0, 0.0, self.id).create() return self