How do I find them:
With this SQL:
select origin, reference, number, ai.state, date_invoice::date, account_period.name as wrong_period_name, account_period.id as wrong_period_id, account_period.date_start, account_period.date_stop,
(select account_period.id from account_period,account_invoice where account_invoice.id = ai.id and account_invoice.date_invoice::date >= date_start::date and account_invoice.date_invoice::date <= date_stop::date) as right_period_id,
(select account_period.name from account_period,account_invoice where account_invoice.id = ai.id and account_invoice.date_invoice::date >= date_start::date and account_invoice.date_invoice::date <= date_stop::date) as right_period_name
from account_invoice ai, account_period where ai.period_id = account_period.id and ( date_invoice::date < account_period.date_start::date or date_invoice::date > account_period.date_stop::date);
How do you fix them:
With this SQL:
update account_invoice ai set period_id = (select account_period.id from account_period where ai.date_invoice::date >= date_start::date and ai.date_invoice <= date_stop::date) from account_period where ai.period_id = account_period.id and ( date_invoice::date < account_period.date_start::date or date_invoice::date > account_period.date_stop::date);
You can prevent them by making the period field readonly, and letting Odoo calculate the value based on the date (instead of giving the user the ability to edit it).