英文:
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='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.
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论