# -*- coding: utf-8 -*- # Copyright 2021 Joan MarĂ­n # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import api, fields, models class AccountMove(models.Model): _inherit = "account.move" @api.depends('invoice_date', 'invoice_date_due', 'invoice_payment_term_id') def _get_payment_mean_id(self): for move in self: ir_model_data_obj = self.env['ir.model.data'] id_payment_term = ir_model_data_obj.get_object_reference( 'account', 'account_payment_term_immediate')[1] payment_term_immediate = self.env['account.payment.term'].browse( id_payment_term) if not move.invoice_date: id_payment_mean = False elif (not move.invoice_payment_term_id and move.invoice_date == move.invoice_date_due): id_payment_mean = ir_model_data_obj.get_object_reference( 'l10n_co_account_move_payment_mean', 'account_payment_mean_1')[1] elif move.invoice_payment_term_id == payment_term_immediate: id_payment_mean = ir_model_data_obj.get_object_reference( 'l10n_co_account_move_payment_mean', 'account_payment_mean_1')[1] else: id_payment_mean = ir_model_data_obj.get_object_reference( 'l10n_co_account_move_payment_mean', 'account_payment_mean_2')[1] move.payment_mean_id = self.env['account.payment.mean'].browse( id_payment_mean) @api.onchange('invoice_date', 'invoice_date_due', 'invoice_payment_term_id') def _onchange_invoice_dates_payment_term(self): if not self.payment_mean_code_id: self.payment_mean_code_id = self.env['account.payment.mean.code'].search( [('code', '=', '1')]).id payment_mean_id = fields.Many2one( comodel_name='account.payment.mean', compute="_get_payment_mean_id", string='Payment Method', store=False) payment_mean_code_id = fields.Many2one( comodel_name='account.payment.mean.code', string='Mean of Payment', copy=False) @api.model def create(self, vals): res = super(AccountMove, self).create(vals) for invoice in res: invoice._onchange_invoice_dates_payment_term() return res def write(self, vals): res = super(AccountMove, self).write(vals) if vals.get('invoice_date'): for invoice in self: invoice._onchange_invoice_dates_payment_term() return res