Ejemplo n.º 1
0
class Product(Base):
    name = Column(String(80))
    amount = Column(Integer)
    value = Column(Integer)

    allow_negative = Column(Boolean)
    value_constant = Column(Boolean)
    hidden = Column(Boolean)

    # TODO: losemods and gainmods should not just be many_to_many since they have an order to be executed in!
    losemods = many_to_many('losemods', 'Product', 'Mod')
    gainmods = many_to_many('gainmods', 'Product', 'Mod')

    group = Column(String(80))

    def __init__(self):
        pass

    def lose(self, amount):
        if amount <= 0:
            raise IllegalProductAdaption(
                "Incorrect call of function 'lose', amount cannot be zero/negative."
            )

        if amount > self.amount and not self.allow_negative:
            raise IllegalProductAdaption(
                str(self) + " would have negative stock.")

        if self.value_constant:
            dvalue = amount * self.value
        else:
            dvalue = round(amount / self.amount * self.value)

        self.value -= dvalue
        self.amount -= amount
        return dvalue

    def gain(self, amount, value):
        if amount <= 0 or value < 0:
            raise IllegalProductAdaption(
                "Incorrect call of function 'gain', amount cannot be zero/negative."
            )

        if self.value_constant:
            if value != amount * self.value:
                raise IllegalProductAdaption(
                    str(self) + " has a constant price that is not matched.")
            self.amount += amount
        else:
            self.value += value
            self.amount += amount
        return value
Ejemplo n.º 2
0
class TransactionRow(Base):
    transaction_id = Column(ForeignKey('transaction.id'))

    product = relationship('Product', lazy='joined')
    product_id = Column(ForeignKey('product.id'))

    amount = Column(Integer)
    prevalue = Column(Integer)
    value = Column(Integer)

    includes_mods = many_to_many('includemods', 'TransactionRow', 'Mod')

    def __init__(self, transaction, product, mods):
        self.transaction = transaction
        transaction.rows.append(self)
        self.includes_mods = mods
        self.product = product
        self.value = 0
        self.amount = 0

    def lose(self, amount):  # selling more, buying less
        dvalue = self.product.lose(amount)
        self.prevalue -= dvalue
        self.amount -= amount
        rowapplymods(self)
        return dvalue

    def gain(self, amount, value):  # selling less, buying more
        self.amount += amount
        self.prevalue += value
        currvalue = self.value
        rowapplymods(self)
        self.product.gain(amount, self.value - currvalue)
        return value

    def delete(self):
        if self.value < 0:
            self.product.gain(-self.amount, -self.value)
        elif self.value > 0:
            self.product.change_amount(-self.amount)
            self.product.change_value(-self.value)
        elif self.amount != 0:
            self.product.change_amount(-self.amount)
        self.amount = 0
        self.value = 0
Ejemplo n.º 3
0
class OutputRecipe(Base):
    """"Make a special output that needs multiple products. Note: will do some ugly rounding."""
    output = Column(String(80))
    inputs = many_to_many('inputs', 'OutputRecipe', 'RecipePart')
Ejemplo n.º 4
0
class InputRecipe(Base):
    """"Make a special input that creates multiple products. Note: only one item may have variable cost."""
    input = Column(String(80))
    outputs = many_to_many('outputs', 'InputRecipe', 'RecipePart')
Ejemplo n.º 5
0
class PosInstance(Base):
    name = Column(String(80))
    posusers = many_to_many('posusers', 'PosInstance', 'User')