Payment date for tax cash

Following Issue 8965: can't validate bank statement 31/12 without creating period for january - Tryton issue tracker

For now, we use today as default payment date but when recording past events (like with a statement, it may not be the best default behavior.
The biggest challenge is that the payment recorded may be in a closed period so we could no more register the tax for this period.

My proposal would be to use the greatest date (but before today) of the payment lines on the invoice and if the period linked is closed, we fallback to today. And we still allow to force a specific date with the payment_date context.

This design will be a little bit less performant as it will require to make a period search for each date involved (on statement posting, it could be one per line).

This is about cash basis tax accounting.
Therefore, each payment must be accounted for in the VAT declarative period of the actual payment received (or made) - individually.

If the VAT declaration has already been made and the period closed, then there is need for regularisation if the payments were not manually taken into consideration, naturally following the customary/legal rules applicable for such regularisations… which may be via a rectification for the erroneous period or taking account of the payments in the declaration of the period discovering such omission…