ciclos

ref: configuration

src/types/balance.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from . import db
from datetime import datetime as dt

class Balance(db.Model):
    __tablename__ = "balances"
    id = db.Column(db.Integer, primary_key = True)
    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, 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__()
        }