This is a classic example of the providing an allocation re-usable service via Business Logic Extensibility. Here we add a Payment to a Customer, and its amount is allocated to that Customers' outstanding unpaid orders.
Please see the database structure.
allocateFromTo
rule creates payment_order_allocation rows that represent the amount of the payment disbursed to each recipient Order. payment_order_allocation
logic determines the exact amount allocated, orders.amount_paid
ustomers.balance
.