class Product(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    code = Column(String(50), nullable=False, unique=True)
    description = Column(Text, nullable=True, unique=False)
    price = Column(Integer, nullable=False, default=0)
    warehouse_id = Column(Integer, ForeignKey('warehouse.id'), nullable=False)
    sold_products = db.relationship(
        'SoldProduct',
        backref="product",
        cascade="all, delete-orphan"
    )
    finished_products = db.relationship(
        'FinishedProduct',
        backref="product",
        cascade="all, delete-orphan"
    )
    unit = "pz"

    def __repr__(self):
        return self.code

    def get(id):
        return Product.query.get(id)

    def get_all():
        return Product.query.all()

    def search(search_term):
        return Product.query.filter_by(code=search_term).first()

    @property
    def validation(self):
        from .validation import ProductValidation
        return ProductValidation(self)

    @property
    def request(self):
        from .request import ProductRequest
        return ProductRequest(self)

    @cached_property
    def inventory(self):
        inventory = {}
        for sold_product in self.sold_products:
            try:
                inventory[sold_product.unit]
            except KeyError:
                inventory[sold_product.unit] = 0
            inventory[sold_product.unit] -= sold_product.quantity
        for finished_product in self.finished_products:
            try:
                inventory[finished_product.unit]
            except KeyError:
                inventory[finished_product.unit] = 0
            inventory[finished_product.unit] += finished_product.quantity

        return inventory
class Permission(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    permission_name = Column(String(50), nullable=False, unique=True)
    description = Column(Text, nullable=True, unique=False)
    view_permissions = db.relationship("ViewPermission",
                                       backref="permission",
                                       cascade="all, delete-orphan")
    user_permissions = db.relationship('UserPermission',
                                       backref="permission",
                                       cascade="all, delete-orphan")

    def get(id):
        return Permission.query.get(id)

    def get_all():
        return Permission.query.all()

    def search(name):
        return Permission.query.filter_by(permission_name=name).first()

    @property
    def views(self):
        return [
            view_permission.view for view_permission in self.view_permissions
        ]

    @property
    def users(self):
        return [
            user_permission.user for user_permission in self.user_permissions
        ]

    @property
    def validation(self):
        from .validation import PermissionValidation
        return PermissionValidation(self)

    def is_admin(self):
        if self.permission_name == "Admin":
            return True

        return False

    def is_dev(self):
        if self.permission_name == "Dev":
            return True

        return False
class Warehouse(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    name = Column(String(200), nullable=False, unique=True)
    date = Column(DateTime, nullable=False, default=datetime.now)
    products = db.relationship('Product',
                               backref="warehouse",
                               cascade="all, delete-orphan")
    warehouse_expenses = db.relationship('WarehouseExpense',
                                         backref="warehouse",
                                         cascade="all, delete-orphan")

    def get(id):
        return Warehouse.query.get(id)

    def get_all():
        return Warehouse.query.all()

    def search(search_term):
        return Warehouse.query.filter_by(name=search_term).first()

    @property
    def request(self):
        from .request import WarehouseRequest
        return WarehouseRequest(self)

    @property
    def validation(self):
        from .validation import WarehouseValidation
        return WarehouseValidation(self)

    @property
    def expenses(self):
        return [
            warehouse_expense.expense
            for warehouse_expense in self.warehouse_expenses
        ]

    def add_expense(self, expense):
        from EnGo.models.expense import WarehouseExpense
        warehouse_expense = WarehouseExpense(warehouse_id=self.id,
                                             expense_id=expense.id)
        warehouse_expense.add()

    def search_expenses(self, search_term):
        return [
            expense for expense in self.expenses
            if expense.concept == search_term
        ]
Exemple #4
0
class Expense(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    concept = Column(String(200), nullable=False, unique=False)
    type_id = Column(Integer, ForeignKey('expense_type.id'), nullable=False)
    cost = Column(Integer, nullable=False, default=0)
    unit = Column(String(20), nullable=False, default="pz")
    quantity = Column(Integer, nullable=False, default=0)
    date = Column(DateTime, nullable=False, default=datetime.now)
    warehouse_expenses = db.relationship('WarehouseExpense',
                                         backref="expense",
                                         cascade="all, delete-orphan")

    def __repr__(self):
        return self.concept

    def get(id):
        return Expense.query.get(id)

    def get_all():
        return Expense.query.all()

    def search_all(search_term):
        return Expense.query.filter_by(concept=search_term).all()

    @property
    def validation(self):
        from .validation import ExpenseValidation
        return ExpenseValidation(self)

    @property
    def request(self):
        from .request import ExpenseRequest
        return ExpenseRequest(self)
class Receipt(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False, default=datetime.now)
    customer_id = Column(Integer, ForeignKey('customer.id'), nullable=False)
    sold_products = db.relationship('SoldProduct',
                                    backref="receipt",
                                    cascade="all, delete-orphan")

    def __repr__(self):
        return self.folio

    def get(id):
        return Receipt.query.get(id)

    def get_all():
        return Receipt.query.all()

    @property
    def total(self):
        total = 0
        for sold_product in self.sold_products:
            total += sold_product.total

        return total

    @property
    def folio(self):
        id = str(self.id)
        folio = "REM "
        num_of_zeros = 5 - len(id)
        for _ in range(num_of_zeros):
            folio += "0"
        folio += id

        return folio

    @property
    def validation(self):
        from .validation import ReceiptValidation
        return ReceiptValidation(self)

    @property
    def request(self):
        from .request import ReceiptRequest
        return ReceiptRequest(self)

    @property
    def products(self):
        return [sold_product.product for sold_product in self.sold_products]

    def add_product(self, product):
        from EnGo.models.product import SoldProduct
        sold_product = SoldProduct(receipt_id=self.id, product_id=product.id)
        sold_product.add()
class View(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    view_name = Column(String(100), nullable=False, unique=True)
    view_permissions = db.relationship('ViewPermission',
                                       backref="view",
                                       cascade="all, delete-orphan")

    @property
    def permissions(self):
        return [
            view_permission.permission
            for view_permission in self.view_permissions
        ]

    def get(id):
        return View.query.get(id)

    def get_all():
        return View.query.all()

    def search(name):
        return View.query.filter_by(view_name=name).first()

    def requires_dev(self):
        for permission in self.permissions:
            if permission.is_dev():
                return True

        return False

    def add_permissions(self, permissions):
        for permission in permissions:
            self.add_permission(permission)

    def add_permission(self, permission):
        view_permission = ViewPermission(view_id=self.id,
                                         permission_id=permission.id)
        view_permission.add()

    def update_permissions(self, permissions):
        self.delete_permissions()
        self.add_permissions(permissions)

    def delete_permissions(self):
        for view_permission in self.view_permissions:
            view_permission.delete()
Exemple #7
0
class ExpenseType(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False, unique=True)
    expenses = db.relationship('Expense',
                               backref='type',
                               cascade='all, delete-orphan')

    def search(search_term):
        return ExpenseType.query.filter_by(name=search_term).first()

    @property
    def validation(self):
        from .validation import ExpenseTypeValidation
        return ExpenseTypeValidation(self)

    @property
    def request(self):
        from .request import ExpenseTypeRequest
        return ExpenseTypeRequest(self)
Exemple #8
0
class Customer(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    customer_name = Column(String(100), nullable=False, unique=False)
    address = Column(String(200), nullable=False, unique=False)
    phone = Column(String(15), nullable=False, unique=False, default="")
    email = Column(String(200), nullable=False, unique=False, default="")
    rfc = Column(String(20), nullable=True, unique=False)
    date = Column(DateTime, nullable=False, default=datetime.now)
    receipts = db.relationship('Receipt',
                               backref="customer",
                               cascade="all, delete-orphan")

    def get(id):
        return Customer.query.get(id)

    def get_all():
        return Customer.query.all()

    def search_all(search_term):
        customers = Customer.query.filter_by(customer_name=search_term).all()
        if customers == []:
            customers = Customer.query.filter_by(address=search_term).all()
        if customers == []:
            customers = Customer.query.filter_by(phone=search_term).all()
        if customers == []:
            customers = Customer.query.filter_by(rfc=search_term).all()

        return customers

    @property
    def validation(self):
        from .validation import CustomerValidation
        return CustomerValidation(self)

    @property
    def request(self):
        from .request import CustomerRequest
        return CustomerRequest(self)
Exemple #9
0
class User(db.Model, MyModel):
    id = Column(Integer, primary_key=True)
    username = Column(String(100), nullable=False, unique=True)
    password = Column(String(100), nullable=False)
    salary = Column(Integer, nullable=False, default=0)
    contract = db.relationship(
        "Contract",
        backref="user",
        cascade="all, delete-orphan",
        uselist=False,
    )
    observations = db.relationship('UserObservation',
                                   backref="user",
                                   cascade="all, delete-orphan")
    activities = db.relationship('UserActivity',
                                 backref="user",
                                 cascade="all, delete-orphan")
    orders = db.relationship('Order',
                             backref="user",
                             cascade="all, delete-orphan")
    user_permissions = db.relationship('UserPermission',
                                       backref="user",
                                       cascade="all, delete-orphan")
    production = db.relationship('UserProduction',
                                 backref="user",
                                 cascade="all, delete-orphan")

    def get(id):
        return User.query.get(id)

    def get_all():
        return User.query.all()

    def search(username):
        return User.query.filter_by(username=username).first()

    @property
    def permissions(self):
        return [
            user_permission.permission
            for user_permission in self.user_permissions
        ]

    @property
    def validation(self):
        from .validation import UserValidation
        return UserValidation(self)

    @property
    def request(self):
        from .request import UserRequest
        return UserRequest(self)

    @property
    def schedule(self):
        from .schedule import UserSchedule
        return UserSchedule(self)

    def has_view_permissions(self, view_name):
        view = View.search(view_name)
        if self.is_dev() or not view:
            return True
        if self.is_admin() and not view.requires_dev():
            return True
        for permission in self.permissions:
            if permission in set(view.permissions):
                return True

        return False

    def is_admin(self):
        return self.has_permissions(["Admin", "Dev"])

    def is_dev(self):
        return self.has_permissions(["Dev"])

    def has_permissions(self, permission_names):
        for permission in self.permissions:
            if permission.permission_name in set(permission_names):
                return True

        return False

    def add_permission(self, permission):
        user_permission = UserPermission(user_id=self.id,
                                         permission_id=permission.id)
        user_permission.add()

    def add_permissions(self, permissions):
        for permission in permissions:
            self.add_permission(permission)

    def remove_permissions(self):
        for user_permission in self.user_permissions:
            user_permission.delete()

    def update_permissions(self, permissions):
        self.remove_permissions()
        self.add_permissions(permissions)