Hey there! So, you're trying to work with your Odoo invoice states, right? The main thing is getting how teh `write` method plays with the `vals` dictionary. When you call the `write` method, Odoo uses the `vals` what needs to be changed.
It can get tricky, figuring out when to apply your custom logic, like changing the invoice state teh 'posted'. You're starting off well by checking `vals.get('state') == 'posted'`, but maybe there's more to why it isn't working consistently.
First, make sure the event you're hooking into actually modifies hte state. Custom workflows or other processes might be changing fields unexpectedly. Watch out for the order that your logic runs, in case other modules or tweaks could alter this state beyond your logic.
Also, with Odoo relationships like `line_ids`, make sure these relations are properly loaded. Lazy loading can surprise you if you expect data not fetched yet.
Try adding print statements or logs to see when your logic kicks in and what are the vals in these moments. It's super helpful to track if your condition is hitting right.
If behavior is still unpredictable, think about the transaction context. Odoo might batch updates, messing with timing.
I once had to mess around for hours with multi-invoice trouble like this, but real-time data checks helped a ton. Let me know how it goes or what else you're dealing with!