Issues with POS returns in multi-currency V12

We’ve just upgraded from V12.6(ish?) to V12.16 and we can no longer make POS returns on non-default currency.

Our ‘default’ currency is GBP, but we get sales in EUR (and other currencies) which are generally paid by PayPal, so we have accounts PayPal GBP, PayPal EUR etc. We create an SINV with currency EUR, mode of payment PayPal EUR, and all amounts in EUR. This works fine. Let’s suppose the sale is for €1000 which is £900 in ‘base’ currency.
When you go to create a return for the SINV, it gets an error ‘Paid amount + Write Off Amount can not be greater than Grand Total’.

The problem is that taxes_and_total.py, in calculate_outstanding_amount, is calculating a ‘total_amount_to_pay’ using the party_account_currency rather than the SINV currency, and thus calculating 900.00 instead of 1000.00. In update_paid_amount_for_return, this incorrect ‘total_amount_to_pay’ is compared to the (correct) total of payments of 1000.00, and since they don’t match all payments are deleted and (incorrect) payments are created with amount 900.00 and base amount 800-ish.

I’m not sure what’s changed - firstly, using ‘party_account_currency’ to set the ‘total_amount_to_pay’ seems to be wrong since it gives the wrong number. Secondly, there is nothing obvious set on the customer to suggest it should be giving a party_account_currency of GBP. It’s all a bit of a pain, and has led to some deep-diving in the code to no avail so far…

Pretty sure it was this:

https://github.com/frappe/erpnext/pull/20543

that broke it. The ‘helpfully update the Sales Invoice Payments if not correct’ thing is using a total_amount_to_pay calculated using the party currency, not the SINV currency, which will be wrong if they differ…