Grant.S
Grant.S

Reputation: 31

how to allow negative value in debit/credit in OpenERP(odoo V16)?

I need to input negative value in debit/credit in odoo, version16, the source code as below:

source file: /addons/account/account_move_line.py

fields:

debit = fields.Monetary(
    string='Debit',
    compute='_compute_debit_credit', inverse='_inverse_debit', store=True, precompute=True,
    currency_field='company_currency_id',
)
credit = fields.Monetary(
    string='Credit',
    compute='_compute_debit_credit', inverse='_inverse_credit', store=True, precompute=True,
    currency_field='company_currency_id',
)
balance = fields.Monetary(
    string='Balance',
    compute='_compute_balance', store=True, readonly=False, precompute=True,
    currency_field='company_currency_id',
)

logic code:

it prevent negative value from this function,

@api.depends('balance', 'move_id.is_storno')
def _compute_debit_credit(self):
    for line in self:
        if not line.is_storno:
            line.debit = line.balance if line.balance > 0.0 else 0.0
            line.credit = -line.balance if line.balance < 0.0 else 0.0
        else:
            line.debit = line.balance if line.balance < 0.0 else 0.0
            line.credit = -line.balance if line.balance > 0.0 else 0.0

also the inverse function:

@api.onchange('debit')
def _inverse_debit(self):
    for line in self:
        if line.debit:
            line.credit = 0
        line.balance = line.debit - line.credit

@api.onchange('credit')
def _inverse_credit(self):
    for line in self:
        if line.credit:
            line.debit = 0
        line.balance = line.debit - line.credit

I revised above code as below, firstly ,set fields "debit, credit, balance" the default=0 to avoid null error,then remove all code from above function, full code as below:

debit = fields.Monetary(
    string='Debit',
    compute='_compute_debit_credit', inverse='_inverse_debit', store=True, precompute=True,
    currency_field='company_currency_id', default=0
)
credit = fields.Monetary(
    string='Credit',
    compute='_compute_debit_credit', inverse='_inverse_credit', store=True, precompute=True,
    currency_field='company_currency_id', default=0
)
balance = fields.Monetary(
    string='Balance',
    compute='_compute_balance', store=True, readonly=False, precompute=True,
    currency_field='company_currency_id', default=0
)



@api.onchange('debit')
def _inverse_debit(self):
    pass

@api.onchange('credit')
def _inverse_credit(self):
    pass

@api.depends('balance', 'move_id.is_storno')
def _compute_debit_credit(self):
    pass

@api.depends('move_id')
def _compute_balance(self):
    self.balance = self.debit - self.credit

but it cannot work, after the modification, I can successfully input negative value, but when click the "post" button, all value become 0, how to revise the code to have it work properly? thanks advanced.

Upvotes: 0

Views: 211

Answers (1)

aekis.dev
aekis.dev

Reputation: 2764

That feature it's available already in Odoo 16. You just need to activate Storno Accounting on your Company Settings and you could have negative accounting out-of-the-box

Upvotes: 0

Related Questions