Hello Roger Masias Font,
If you want to trigger a specific action when changing the state of an invoice from draft to posted, you can indeed use the write method.
However, there are a few issues in your provided code. First, the if condition should check if the current state is 'draft' and the new state is 'posted'. Second, you should use the super function to call the parent class's write method. Here's a corrected version of your code:
def write(self, vals):
result = super(YourInvoiceModel, self).write(vals)
for invoice in self:
if 'state' in vals and vals['state'] == 'posted' and invoice.state == 'draft':
# Your logic here
pos_id = invoice.line_ids.sale_line_ids.order_id.astore_tpv_id
invoice_date = invoice.invoice_date
amount_total = invoice.amount_total
account_move_id = invoice.id
# Perform your custom actions with the gathered data
return result
Make sure to replace YourInvoiceModel with the actual name of your invoice model. This code checks if the 'state' key is present in the vals dictionary and its value is 'posted', and if the current state of the invoice is 'draft'. If these conditions are met, you can access the required fields and perform your custom logic.
Remember to adapt the model and field names according to your actual data model