Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
1299 Näkymät

Dear community,


I'm trying to add register when i change the state of the invoice, the draft to posted.


I think that i have to catch the write event and the vals but i can't understand, how can i filter the vals for every register.


Example code :


    @api.model    def write(self, vals):        for invoice in self:            if (vals.get('state') == 'posted'):                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


Kind regards 

Avatar
Hylkää
Paras vastaus

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


Avatar
Hylkää
Tekijä

Dear Zinfin,

I can't undestant, how are you filter self with specific invoice, i think that self contain a lot of invoice but i think vals contain some values of specific invoice or all of invoice?

Kind regards, thanks for your help.

Hello Roger,
the loop is iterating over invoices represented by self. The condition within the loop checks if the key 'state' is present in the vals dictionary and if its value is 'posted'. Additionally, it checks if the state of the current invoice in the loop is 'draft'. If these conditions are met, the code inside the loop is executed.

The invoice variable in the loop represents the current invoice being processed. So, the code inside the loop is dealing with a specific invoice that satisfies the conditions specified in the if statement. The vals dictionary seems to contain information related to the state of invoices, and it's being checked for the 'posted' state.

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
elok. 24
1372
0
huhtik. 24
1396
1
maalisk. 24
1207
0
elok. 25
133
4
toukok. 25
2737