...
Perform the validation if Device request has quantity
Validate quantity value
Check that dispensed device quantity is equal to prescribed quantity in Device Request
sum($.details.quantity.value) = device_request.quantity
in case of error - return 422 ('Dispensed quantity must be equal to prescribed quantity in Device Request')
Validate quantity units
Check that quantity.code matches with packaging_unit of the device
in case of error - return 422 (Does not match the packaging unit of the prescribed device)
Validate discount
Validate sell_price
Check it is present if medical program is present
in case of error - return 422 ('Required property sell_price was not present')
Validate discount_amount
Check it is present if medical program is present
in case of error - return 422 ('Required property discount_amount was not present')
Check it is absent if medical program is absent
in case of error - return 422 ('Property discount_amount shouldn’t be submitted if medical program is absent')
Calculate allowed reimbursement amount:
If reimbursement_type of program_device is PERCENTAGE:
If reimbursement_percentage_discount of program_device is zero, then check $.discount_amount is equal to zero too
in case of error - return 422 ('Requested discount amount must be equal to 0')
Calculate allowed_reimbursement_amount = $.sell_price * program_device.reimbursement_percentage_discount/100
If reimbursement_type of program_device is FIXED:
Get allowed_reimbursement_amount = program_device.reimbursement_amount
Check $.discount_amount <= allowed_reimbursement_amount * ($.details.quantity.value/device_definition.packaging_count) + DEVICE_DISPENSE_TOLERANCE
in case of error - return 422 ('Requested discount amount must be less or equal to allowed reimbursement amount')
Check the ratio of discount amount to reimbursement amount as $.discount_amount/ (allowed_reimbursement_amount * ($.details.quantity.value/device_definition.packaging_count)) >= 1 - DEVICE_DISPENSE_DEVIATION
In case of error - return 422 ('The ratio of requested discount amount to allowed reimbursement amount must be greater or equal to <1 - DEVICE_DISPENSE_DEVIATION>')
...