If the 'Auto Apply Credits' option is enabled for a customer (through their customer class) and customer places new order then already existing credit documents (if any) are being automatically applied to newly created order. It works the way described below.
When PBA processes customer's order it 'requests a payment' for the order. If the 'Auto Apply Credits' option is disabled for a customer PBA just creates a new payment and wait while real payment is received. If customer paid order by credit card then card will be charged, payment will be released (in case charge was successful) and order will be processed. If a customer chose offline payment (check/cash) then payment will be sitting in the 'Hold' status until Provider staff member release it manually.
If the 'Auto Apply Credits' option is enabled for a customer then PBA first checks if Provider already owes some money to customer, i.e. PBA checks if a customer has existing open credit memo or payment with non-zero balance.
For example, last month customer paid more than necessary, therefore part of their existing payment can be used to apply to the new order. And customer pays not the whole amount of the new order, but only difference between order amount and sum of existing open credit documents. Usually oldest documents are used first to apply to the order.
Invoices are always created as a result of order provisioning, and when the invoice is being created the information about all the documents which are applied to order is being copied to invoice. This means that documents replacement happens: before invoice is created all payments are applied directly to the order, after invoice is created it is being applied to the order, and payments are being applied to the invoice.
The 'Auto Apply Credits' option does not work for debit memos, no documents are automatically applied to a debit memo.