Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

Even though there is default receivable account on partner i want to change it other account


if self.move_id.move_type == 'out_invoice' and self.account_id.user_type_id.id == '1': 

​so = self.env['sale.order'].search([('name','=',self.move_id.invoice_origin)]) 

​if so: 

​custom = self.env['custom'].search([('name','=',so.origin)]) 

​if custom: 

​custom_account = self.env['ir.config_parameter'].sudo().get_param('custom_account') 

​if not custom_account: 

​raise UserError('Configure the custom account!') 

​self.account_id = int(custom_account)


I want to insert this structure into account.move.line but can't figure out how. i tried to add it into onchange from account_id, but the function wasn't triggering

Awatar
Odrzuć
Najlepsza odpowiedź

If you want this code to be executed whenever the account_id changes, you can use the @api.onchange decorator. However, keep in mind that onchange methods are not stored in the database, they are mainly used for UI purposes.

Here’s an example of how you might use the @api.onchange decorator:

from odoo import api, models

class AccountMoveLine(models.Model):
    _inherit = 'account.move.line'

    @api.onchange('account_id')
    def _onchange_account_id(self):
        if self.move_id.move_type == 'out_invoice' and self.account_id.user_type_id.id == '1':
            so = self.env['sale.order'].search([('name','=',self.move_id.invoice_origin)])
            if so:
                custom = self.env['custom'].search([('name','=',so.origin)])
                if custom:
                    custom_account = self.env['ir.config_parameter'].sudo().get_param('custom_account')
                    if not custom_account:
                        raise UserError('Configure the custom account!')
                    self.account_id = int(custom_account)

This code will trigger whenever the account_id field is changed in the UI. If you want this logic to be applied every time a record is created or written to, regardless of whether it’s through the UI or not, you might want to override the create and write methods instead.

Remember to update the module after adding this code to apply the changes. If the function still isn’t triggering, there might be an issue with the conditions in your if statements. Make sure the conditions are being met when you’re testing the function.

Hope it helps.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
maj 24
1739
2
sty 23
4816
1
paź 22
4197
1
cze 22
3131
1
maj 22
3414