예제 #1
0
    def post(self):
        schema = EmployeeTypeSchema()
        employee_type, errors = schema.load(request.json)
        if errors:
            return errors, 422

        try:
            employee_type.created_by = get_jwt_identity()

            if not Entity.get(uuid=employee_type.entity_id):
                return {"msg": "Entity not found"}, 422
            if EmployeeType.get(name=employee_type.name,
                                entity_id=employee_type.entity_id):
                return {
                    "msg": "The supplied employee_type already exists"
                }, 409
            else:
                db.session.add(employee_type)
            db.session.commit()

            return (
                {
                    "msg": "employee_type created",
                    "employee_type": schema.dump(employee_type).data,
                },
                201,
            )
        except Exception as e:
            db.session.rollback()
            return {"msg": e.args[0]}, 500
예제 #2
0
    def get(self):
        schema = VendorSchema(many=True)

        if request.args.get("entity"):
            entity = Entity.get(uuid=request.args.get("entity"))
            if entity:
                query = entity.vendors.all()
            else:
                query = {}
        elif request.args.get("vendor"):
            vendor = Vendor.get(uuid=request.args.get("vendor"))
            if vendor:
                query = vendor.entities.all()
            else:
                query = {}
        else:
            from autoshop.commons.dbaccess import query

            sql = """ c.vendor_id, v.uuid as vendor_uuid, v.name as vendor,
            c.entity_id, e.uuid as entity_uuid, e.name as entity
            FROM entity_vendors c INNER JOIN entity e on e.id=c.entity_id
            INNER JOIN vendor v on v.id=c.vendor_id
            """

            data = query(sql)
            return jsonify(data)
            # return [] if data is None else data

        return schema.dump(query).data
예제 #3
0
    def post(self):
        schema = CommissionAccountSchema()
        comm_account, errors = schema.load(request.json)
        if errors:
            return errors, 422

        comm_account.created_by = get_jwt_identity()

        min_bal = request.json.get('minimum_balance')
        # remove commas
        min_bal = min_bal.replace(',', '')

        if CommissionAccount.get(code=comm_account.code):
            return {"msg": "Duplicate account code supplied"}
        if not Entity.get(uuid=comm_account.entity_id) and not Vendor.get(
                uuid=comm_account.entity_id):
            return {"msg": "The supplied entity id doesnt exist"}, 422

        try:
            comm_account.save(min_bal)

            return {
                "msg": "comm_account created",
                "comm_account": schema.dump(comm_account).data
            }, 201
        except Exception as e:
            db.session.rollback()

            return {"msg": e.args[0]}, 500
예제 #4
0
    def post(self):
        try:
            schema = EmployeeSchema()
            employee, errors = schema.load(request.json)
            if errors:
                return errors, 422

            if not employee.created_by:
                employee.created_by = get_jwt_identity()
            app.logger.info(employee.entity_id)

            if not Entity.get(uuid=employee.entity_id):
                return {"msg": "The supplied entity id does not exist"}, 422
            if not EmployeeType.get(
                uuid=employee.type_id, entity_id=employee.entity_id
            ):
                return {"msg": "The supplied employee type does not exist"}, 422
            if Employee.get(phone=employee.phone, entity_id=employee.entity_id):
                return {"msg": "The supplied employee phone already exists"}, 409
            else:

                db.session.add(employee)
                db.session.commit()

            return (
                {"msg": "employee created", "employee": schema.dump(employee).data},
                201,
            )

        except Exception as e:
            return {"msg": e.args}, 500
예제 #5
0
        def batch(self):
            """Execute multiple requests, submitted as a batch. :statuscode 207: Multi status"""
            try:
                requests = json.loads(request.data)
            except ValueError as e:
                return {"msg": e}, 400

            responses = []

            for index, req in enumerate(requests):
                name = req["name"]
                email = req["email"]
                phone = req["phone"]
                address = req["address"]

                entity = Entity(
                    name=name,
                    email=email,
                    phone=phone,
                    address=address,
                    created_by=get_jwt_identity(),
                )
                account = Account(
                    owner_id=entity.uuid,
                    acc_type="entity",
                    created_by=get_jwt_identity(),
                    group=entity.uuid,
                    minimum_balance=0.0,
                )

                if Entity.get(name=name):
                    return {"msg": "The supplied name already exists " + name}, 409
                else:
                    db.session.add(entity)
                    db.session.add(account)

            db.session.commit()

            responses.append({"msg": "entities created"})

            return json.dumps(responses), 207
예제 #6
0
    def post(self):
        try:
            schema = CustomerSchema()
            customer, errors = schema.load(request.json)
            if errors:
                return errors, 422

            if not customer.created_by:
                customer.created_by = get_jwt_identity()
            app.logger.info(customer.entity_id)

            if not Entity.get(uuid=customer.entity_id):
                return {"msg": "The supplied entity id does not exist"}, 422
            if not CustomerType.get(uuid=customer.type_id,
                                    entity_id=customer.entity_id):
                return {
                    "msg": "The supplied customer type does not exist"
                }, 422
            if Customer.get(phone=customer.phone,
                            entity_id=customer.entity_id):
                return {
                    "msg": "The supplied customer phone already exists"
                }, 409
            else:

                customer.log("A")

                account = Account(owner_id=customer.uuid,
                                  acc_type="customer",
                                  created_by=get_jwt_identity(),
                                  group=customer.entity_id,
                                  minimum_balance=10000000)
                db.session.add(account)
                db.session.commit()

            return (
                {
                    "msg": "customer created",
                    "customer": schema.dump(customer).data
                },
                201,
            )

        except Exception as e:
            return {"msg": e.args}, 500
예제 #7
0
    def post(self):
        entity_id = request.json["entity_id"]
        vendor_id = request.json["vendor_id"]

        entity = Entity.get(id=entity_id)
        vendor = Vendor.get(id=vendor_id)

        if not entity:
            return {"msg": "Entity not found"}, 422
        if not vendor:
            return {"msg": "Vendor not found"}, 422
        if entity.is_entity_vendor(vendor):
            return {"msg": "This partnership already exists"}, 409

        try:
            entity.add_vendor(vendor)
            db.session.commit()
            return {"msg": "operation successful"}, 201
        except Exception as e:
            db.session.rollback()
            return {"msg": e.args[0]}, 500
예제 #8
0
    def post(self):
        schema = ItemSchema()
        item, errors = schema.load(request.json)
        if errors:
            return errors, 422

        item.created_by = get_jwt_identity()

        if not Entity.get(uuid=item.entity_id):
            return {"msg": "The supplied entity id does not exist"}, 422
        if Item.get(name=item.name):
            return {"msg": "The supplied name already exists"}, 409
        if Item.get(code=item.code):
            return {"msg": "The supplied code already exists"}, 409
        if item.model_id != "ALL" and not VehicleModel.get(uuid=item.model_id):
            return {"msg": "The supplied vehicle model doesnt exist"}, 422
        if not ItemCategory.get(uuid=item.category_id):
            return {"msg": "The supplied item category doesnt exist"}, 422

        db.session.add(item)
        db.session.commit()

        return {"msg": "item created", "item": schema.dump(item).data}, 201
예제 #9
0
def seed():
    import datetime

    fileDir = os.path.dirname(os.path.realpath('__file__'))
    filename = os.path.join(fileDir, 'autoshop/sql/makes.sql')
    file = open(filename)
    execute_sql(file)

    entity = Entity(
        name='Floben Autoshop',
        email='*****@*****.**',
        phone='256770443322',
        address='Ntinda',
        created_by=1,
        date_created=datetime.datetime.now(),
    )

    account = Account(owner_id=entity.uuid,
                      acc_type='entity',
                      created_by=1,
                      group=entity.uuid)

    db.session.add(entity)
    db.session.add(account)

    ctype = CustomerType(uuid='in_fleet',
                         name='IN FLEET',
                         created_by=1,
                         entity_id=entity.uuid)
    ctype1 = CustomerType(uuid='out_fleet',
                          name='OUT FLEET',
                          created_by=1,
                          entity_id=entity.uuid)

    db.session.add(ctype1)
    db.session.add(ctype)
    db.session.commit()
예제 #10
0
    def post(self):
        schema = AccountSchema()
        account, errors = schema.load(request.json)
        if errors:
            return errors, 422

        account.created_by = get_jwt_identity()

        if (not Customer.get(uuid=account.owner_id)
                and not Entity.get(uuid=account.owner_id)
                and not Vendor.get(uuid=account.owner_id)):
            return {"msg": "The supplied owner id doesnt exist"}, 409

        try:
            db.session.add(account)
            db.session.commit()

            return {
                "msg": "account created",
                "account": schema.dump(account).data
            }, 201
        except Exception as e:
            db.session.rollback()
            return {"msg": e.args[0]}, 500
예제 #11
0
    def post(self):
        identity = get_jwt_identity()

        schema = TarriffSchema()
        tarriff, errors = schema.load(request.json)
        if errors:
            return errors, 422

        tarriff.created_by = identity

        try:
            if not TransactionType.get(uuid=tarriff.tran_type):
                return {"msg": "The supplied transaction type doesn't exist"}, 422
            if Tarriff.get(name=tarriff.name):
                return {"msg": "The supplied tarriff name already exists"}, 409
            if tarriff.entity_id != "ALL" and not Entity.get(uuid=tarriff.entity_id):
                return {"msg": "The entity code supplied doesn't exist"}, 422
            if Tarriff.get(
                payment_type=tarriff.payment_type,
                tran_type=tarriff.tran_type,
                entity_id=tarriff.entity_id,
            ):
                return (
                    {"msg": "A tarriff with the specified criteria already exists"},
                    409,
                )
            else:
                db.session.add(tarriff)
                db.session.commit()
                return (
                    {"msg": "tarriff created", "tarriff": schema.dump(tarriff).data},
                    201,
                )
        except Exception as e:
            db.session.rollback()
            return {"msg": e.args[0], "exception": e.args}, 500
예제 #12
0
 def entity(self):
     return Entity.get(uuid=self.entity_id).name