Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
440 Lượt xem

Hi Odoo community,

I'm currently customizing the account.payment.register model in Odoo 17. Specifically, I want to override an existing computed field (amount) defined with @api.depends to remove one specific field (date_payments) from its dependency list.

Base model : module account of odoo


amount = fields.Monetary(currency_field='currency_id', store=True, readonly=False, compute='_compute_amount')

@api.depends('can_edit_wizard', 'source_amount', 'source_amount_currency', 'source_currency_id', 'company_id', 'currency_id', 'payment_date')
def _compute_amount(self):
    for wizard in self:
        if not wizard.journal_id or not wizard.currency_id or not wizard.payment_date:
            wizard.amount = wizard.amount
        elif wizard.source_currency_id and wizard.can_edit_wizard:
            batch_result = wizard._get_batches()[0]
            wizard.amount = wizard._get_total_amount_in_wizard_currency_to_full_reconcile(batch_result)[0]
        else:
            # The wizard is not editable so no partial payment allowed and then, 'amount' is not used.
            wizard.amount = None

i want to inherit this from custom module

class AccountPaymentRegisterInherit(models.TransientModel):
    _inherit = "account.payment.register"

@api.depends('can_edit_wizard', 'source_amount', 'source_amount_currency', 'source_currency_id', 'company_id',
             'currency_id')
def _compute_amount(self):
    """Custom compute method for 'amount' that excludes 'payment_date' dependance
    to prevent unwanted recomputation after modifying the payment date.
    """
    return super()._compute_amount()

The problem is that it's not working; it's still using the parent module's API dependency fields

please help me understand this weird behaviour

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

please try this code 
from odoo import api, fields, models


class AccountPaymentRegisterInherit(models.TransientModel):

    _inherit = "account.payment.register"


    amount = fields.Monetary(

        currency_field='currency_id',

        store=True,

        readonly=False,

        compute='_custom_compute_amount',  # Note the new compute method

    )


    @api.depends('can_edit_wizard', 'source_amount', 'source_amount_currency',

                 'source_currency_id', 'company_id', 'currency_id')  # <-- payment_date removed here

    def _custom_compute_amount(self):

        for wizard in self:

            if not wizard.journal_id or not wizard.currency_id:  # Removed `wizard.payment_date` check

                wizard.amount = wizard.amount

            elif wizard.source_currency_id and wizard.can_edit_wizard:

                batch_result = wizard._get_batches()[0]

                wizard.amount = wizard._get_total_amount_in_wizard_currency_to_full_reconcile(batch_result)[0]

            else:

                wizard.amount = None


i hope it is use full

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

You must redefine the field in your inherited model with the new @api.depends.


Please refer to the code below:



from odoo import models, fields, api

class AccountPaymentRegisterInherit(models.TransientModel):
_inherit = "account.payment.register"

amount = fields.Monetary(
currency_field='currency_id',
store=True,
readonly=False,
compute='_compute_amount'
)

@api.depends(
'can_edit_wizard',
'source_amount',
'source_amount_currency',
'source_currency_id',
'company_id',
'currency_id'
)
def _compute_amount(self):
for wizard in self:
if not wizard.journal_id or not wizard.currency_id:
wizard.amount = wizard.amount
elif wizard.source_currency_id and wizard.can_edit_wizard:
batch_result = wizard._get_batches()[0]
wizard.amount = wizard._get_total_amount_in_wizard_currency_to_full_reconcile(batch_result)[0]
else:
wizard.amount = None


Hope it helps.

Ảnh đại diện
Huỷ bỏ