如何在OpenERP(odoo V16)中允许借方/贷方出现负值?

huangapple go评论57阅读模式
英文:

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

问题

我需要在Odoo 16中的借方/贷方中输入负值。以下是源代码:

源文件:/addons/account/account_move_line.py

字段:

debit = fields.Monetary(
    string='借方',
    compute='_compute_debit_credit', inverse='_inverse_debit', store=True, precompute=True,
    currency_field='company_currency_id',
)
credit = fields.Monetary(
    string='贷方',
    compute='_compute_debit_credit', inverse='_inverse_credit', store=True, precompute=True,
    currency_field='company_currency_id',
)
balance = fields.Monetary(
    string='余额',
    compute='_compute_balance', store=True, readonly=False, precompute=True,
    currency_field='company_currency_id',
)

逻辑代码:

防止此函数中出现负值

@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

还有反向函数:

@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

我将上述代码修改如下:首先,将字段"debit, credit, balance"的默认值设置为0,以避免空值错误,然后从上述函数中删除所有代码,完整代码如下:

debit = fields.Monetary(
    string='借方',
    compute='_compute_debit_credit', inverse='_inverse_debit', store=True, precompute=True,
    currency_field='company_currency_id', default=0
)
credit = fields.Monetary(
    string='贷方',
    compute='_compute_debit_credit', inverse='_inverse_credit', store=True, precompute=True,
    currency_field='company_currency_id', default=0
)
balance = fields.Monetary(
    string='余额',
    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

但是它无法正常工作。修改后,我可以成功输入负值,但是当点击"提交"按钮时,所有值都变为0。如何修改代码以使其正常工作?提前感谢。

英文:

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=&#39;Debit&#39;,
    compute=&#39;_compute_debit_credit&#39;, inverse=&#39;_inverse_debit&#39;, store=True, precompute=True,
    currency_field=&#39;company_currency_id&#39;,
)
credit = fields.Monetary(
    string=&#39;Credit&#39;,
    compute=&#39;_compute_debit_credit&#39;, inverse=&#39;_inverse_credit&#39;, store=True, precompute=True,
    currency_field=&#39;company_currency_id&#39;,
)
balance = fields.Monetary(
    string=&#39;Balance&#39;,
    compute=&#39;_compute_balance&#39;, store=True, readonly=False, precompute=True,
    currency_field=&#39;company_currency_id&#39;,
)

logic code:

it prevent negative value from this function,

@api.depends(&#39;balance&#39;, &#39;move_id.is_storno&#39;)
def _compute_debit_credit(self):
    for line in self:
        if not line.is_storno:
            line.debit = line.balance if line.balance &gt; 0.0 else 0.0
            line.credit = -line.balance if line.balance &lt; 0.0 else 0.0
        else:
            line.debit = line.balance if line.balance &lt; 0.0 else 0.0
            line.credit = -line.balance if line.balance &gt; 0.0 else 0.0

also the inverse function:

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

@api.onchange(&#39;credit&#39;)
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=&#39;Debit&#39;,
    compute=&#39;_compute_debit_credit&#39;, inverse=&#39;_inverse_debit&#39;, store=True, precompute=True,
    currency_field=&#39;company_currency_id&#39;, default=0
)
credit = fields.Monetary(
    string=&#39;Credit&#39;,
    compute=&#39;_compute_debit_credit&#39;, inverse=&#39;_inverse_credit&#39;, store=True, precompute=True,
    currency_field=&#39;company_currency_id&#39;, default=0
)
balance = fields.Monetary(
    string=&#39;Balance&#39;,
    compute=&#39;_compute_balance&#39;, store=True, readonly=False, precompute=True,
    currency_field=&#39;company_currency_id&#39;, default=0
)



@api.onchange(&#39;debit&#39;)
def _inverse_debit(self):
    pass

@api.onchange(&#39;credit&#39;)
def _inverse_credit(self):
    pass

@api.depends(&#39;balance&#39;, &#39;move_id.is_storno&#39;)
def _compute_debit_credit(self):
    pass

@api.depends(&#39;move_id&#39;)
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.

答案1

得分: 0

这个功能已经在Odoo 16中可用了。您只需要在公司设置中激活"Storno Accounting",就可以直接使用负账务。

英文:

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

huangapple
  • 本文由 发表于 2023年7月27日 17:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定