If a SFS treatment is billed in OpenVPMS, a Customer Order will be created.
If the treatment quantity decreases, and:
the Customer Order hasn't been invoiced, it is updated
the Customer Order has been invoiced, a Customer Return will be created with the difference in quantity
If the Customer Return in the second case is invoiced, a Credit will be created.
This is because it isn't associated with any Invoice Item.
To avoid this:
1. get the latest invoice for the customer
2. look for any line item(s) matching the Customer Return product that are uncorrelated (i.e. haven't generated their own orders)
3. decrease their quantities as appropriate
If the quantity available for return is less than the Customer Return, create the Credit as before.