Exemplo n.º 1
0
    def post(self):

        params = request.json
        errors = product_validator.BatchCreationRequest().validate(params,
                                                                   many=False)
        if errors:
            return {
                "errors": [{
                    "code": "BAD_INPUT",
                    "message": "\n".join(errors)
                }]
            }, 400

        deliveryitem = product_repository.get_deliveryitem_by_reference_and_type(
            params.get('reference'), params.get('delivery_type'))

        if deliveryitem is None:
            return {
                "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
            }, 404

        if len(deliveryitem.batches) > 0:
            return {
                "errors": [
                    get_error_messages(constant.DELIVERYITEM_ALREADY_EXPORTED,
                                       params.get('reference'),
                                       params.get('delivery_type'))
                ]
            }, 400

        product_repository.generate_batch_from_delivery_item(
            deliveryitem, params.get("batch_size"))

        return {}, 204
Exemplo n.º 2
0
def check_organisation(params):
    ref_fields = {
        "role": "orga_role",
        "status": "orga_status",
        "availability": "orga_availability"
    }

    obj, errors = _check_params(
        params,
        OrganisationSerializer.resource_fields,
        "organization",
        required_fields=OrganisationSerializer.required,
        ref_fields=ref_fields)

    roles = ref_repository.get_roles()
    check_data = {
        "tra": "transporter",
        "cus": "customer",
        "pro": "provider",
        "man": "manufactor"
    }
    check_func = {
        "user": check_user,
        "address": check_address,
        "tra": check_transporter,
        "cus": check_customer,
        "pro": check_provider,
        "man": check_manufactor
    }

    # Mandatory fields
    for field in ["user", "address"]:
        if params.get(field) is None:
            errors.append(
                get_error_messages(constant.FIELD_REQUIRED, field,
                                   "organisation"))
            continue

        obj_data, errors_data = check_func[field](params.get(field))
        obj[field] = obj_data
        errors.extend(errors_data)

    if obj["role"] is None:
        return obj, errors

    # conditional field
    data_code = roles[obj["role"]]
    if params.get(check_data[data_code]) is None:
        errors.append(
            get_error_messages(constant.FIELD_REQUIRED, check_data[data_code],
                               "organisation"))
        return obj, errors

    obj_data, errors_data = check_func[data_code](params.get(
        check_data[data_code]))
    obj[check_data[data_code]] = obj_data
    errors.extend(errors_data)
    return obj, errors
Exemplo n.º 3
0
    def post(self):
        params = request.json
        errors = product_validator.DeliveryItemCreationRequest().validate(
            params, many=False)
        if errors:
            return {
                "errors": [{
                    "code": f"BAD_PARAM_{k.upper()}",
                    "message": "\n".join(v)
                } for k, v in errors.items()]
            }, 400

        product = product_repository.get_product_reference_by_reference(
            params.get('reference'))
        if product is None:
            return {
                "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
            }, 404

        errors = []
        prod_type = product_repository.ProductType
        if product.type == prod_type.materials:
            errors.append(
                get_error_messages(constant.CANNOT_DELIVER_MATERIAL_PRODUCT))

        manufactor = orga_repository.get_organisation(
            params.get("manufactor_vid"))
        if not manufactor:
            errors.append(
                get_error_messages(constant.UNKNOWN_ORGANISATION,
                                   params.get("manufactor_vid")))
        elif manufactor.role_obj.code != "man":
            errors.append(
                get_error_messages(constant.NOT_A_MANUFACTOR,
                                   params.get("manufactor_vid")))

        if errors:
            return {"errors": errors}, 400

        if params.get("kit") == 1:
            # check the material availability
            try:
                product_repository.check_kit_delivery_creation(
                    product, params.get("count"))
            except product_repository.ProductException as exc:
                return {"errors": exc.details}, 400

        func = product_repository.create_kit_delivery_stock if params.get("delivery_type") == "kit" \
            else product_repository.create_final_delivery_stock
        func(product, params.get("count"), manufactor)

        return {}, 204
Exemplo n.º 4
0
def _check_params(params,
                  resource_fields,
                  model_name,
                  required_fields=None,
                  ref_fields=None):
    errors = []
    obj = {}

    req_fields = required_fields or []
    for field in resource_fields:
        if params.get(field) is None and field in req_fields:
            errors.append(
                get_error_messages(constant.FIELD_REQUIRED, field, model_name))

        if not ref_fields or field not in ref_fields:
            # no ref variables ou field not belong to ref variables
            obj[field] = params.get(field, None)
            continue

        obj[field] = params.get(field, None)
        if params.get(field) is not None:
            value = _check_ref(params.get(field), field, errors)
            obj[field] = value

    return obj, errors
Exemplo n.º 5
0
def _check_ref(input_value, field, errors):
    ref = ref_repository.get_reference_from_code_or_libelle(input_value)
    if not ref:
        error = get_error_messages(constant.UNKNOWN_VALUE, input_value, field)
        errors.append(error)
        return
    return ref.id
Exemplo n.º 6
0
 def get(self, reference):
     obj = product_repository.get_batch_by_reference(reference)
     if obj is None:
         return {
             "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
         }, 404
     return obj.to_json(), 200
Exemplo n.º 7
0
 def get(self, reference):
     obj = product_repository.get_deliveryitem_by_reference(reference)
     if obj is None:
         return {
             "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
         }, 404
     return obj, 200
Exemplo n.º 8
0
    def post(self):
        params = request.json
        errors = product_validator.StockCreationRequest().validate(params,
                                                                   many=False)
        if errors:
            return {
                "errors": [{
                    "code": f"BAD_PARAM_{k.upper()}",
                    "message": "\n".join(v)
                } for k, v in errors.items()]
            }, 400

        product = product_repository.get_product_reference_by_reference(
            params.get('reference'))
        if product is None:
            return {
                "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
            }, 404

        prod_type = product_repository.ProductType
        if product.type == prod_type.final:
            # check the material availability
            try:
                product_repository.check_kit_stock_creation(
                    product, params.get("count"))
            except product_repository.ProductException as exc:
                return {"errors": exc.details}, 400

        func = product_repository.create_kit_stock if product.type == prod_type.final \
            else product_repository.create_material_stock
        obj = func(product, params.get("count"))

        return obj.to_json(), 200
Exemplo n.º 9
0
def check_kit_delivery_creation(ref, expected):
    assert isinstance(ref, Product) == True
    assert ref.type == ProductType.final

    # checker si le stock des materiaux existe.
    # checker si il est en nombre suffisant.
    errors = []
    stock = count_stock_by_reference(ref.reference)
    if not stock:
        errors.append(get_error_messages(constant.NO_STOCK, ref.reference))

    if expected > stock:
        errors.append(get_error_messages(constant.NOT_ENOUGH_STOCK, ref.reference))
    if errors:
        raise ProductException("BAD_STOCK", errors)

    return True
Exemplo n.º 10
0
    def put(self, reference):

        product = product_repository.get_product_reference_by_reference(
            reference)
        if product is None:
            return {
                "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)]
            }, 404

        params = request.json
        errors = product_validator.ProductEquivalenceList().validate(
            params, many=False)
        if errors:
            return {
                "errors": [{
                    "code": "BAD_INPUT",
                    "message": "\n".join(errors)
                }]
            }, 400

        errors = []
        if product.type != product_repository.ProductType.final:
            errors.append(
                get_error_messages(constant.BAD_FINAL_PRODUCT,
                                   product.reference))

        materials = params.get("materials")
        for i, mat in enumerate(materials):
            material = product_repository.get_product_reference_by_reference(
                mat.get("reference"))
            if not material:
                errors.append(
                    get_error_messages(constant.BAD_MATERIAL_PRODUCT,
                                       mat.get("reference")))

        if errors:
            return {"errors": errors}, 400

        for mat in materials:
            product_repository.create_equivalence(product,
                                                  mat.get("reference"),
                                                  mat.get("count"))

        product.refresh()

        return product.to_json(), 200
Exemplo n.º 11
0
def check_kit_stock_creation(ref, count):
    assert isinstance(ref, Product) == True
    assert ref.type == ProductType.final

    # checker si le stock des materiaux existe.
    # checker si il est en nombre suffisant.
    errors = []
    for item in ref.materials:
        mat_stock = count_stock_by_reference(item.reference)
        if not mat_stock:
            errors.append(get_error_messages(constant.NO_STOCK, item.reference))
            continue
        if item.material[0].count * count > mat_stock:
            errors.append(get_error_messages(constant.NOT_ENOUGH_STOCK, item.reference))
            continue
    if errors:
        raise ProductException("BAD_STOCK", errors)

    return True