2
0

Handle negative amounts

This commit is contained in:
Andrea 2019-01-30 10:03:52 +01:00 committed by Andrea Stirpe
parent 61e1c5ad75
commit cecde22f67
3 changed files with 25 additions and 17 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2018 Onestein (<https://www.onestein.eu>) # Copyright 2018-2019 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import calendar import calendar
@ -382,9 +382,6 @@ class AccountSpread(models.Model):
In case checks pass, invoke "def _compute_spread_board()" method. In case checks pass, invoke "def _compute_spread_board()" method.
""" """
for spread in self: for spread in self:
if spread.total_amount < 0.0:
raise UserError(
_("Cannot spread negative amounts of invoice lines"))
if spread.total_amount: if spread.total_amount:
spread._compute_spread_board() spread._compute_spread_board()
@ -466,16 +463,28 @@ class AccountSpread(models.Model):
spread_mls = self.line_ids.mapped('move_id.line_ids') spread_mls = self.line_ids.mapped('move_id.line_ids')
if created_moves: if created_moves:
spread_mls |= created_moves.mapped('line_ids') spread_mls |= created_moves.mapped('line_ids')
if self.invoice_type in ('in_invoice', 'out_refund'):
spread_sign = True if self.total_amount >= 0.0 else False
in_invoice_or_out_refund = ('in_invoice', 'out_refund')
if self.invoice_type in in_invoice_or_out_refund and spread_sign:
spread_mls = spread_mls.filtered(lambda x: x.credit != 0.) spread_mls = spread_mls.filtered(lambda x: x.credit != 0.)
else: elif self.invoice_type in in_invoice_or_out_refund:
spread_mls = spread_mls.filtered(lambda x: x.debit != 0.) spread_mls = spread_mls.filtered(lambda x: x.debit != 0.)
elif spread_sign:
spread_mls = spread_mls.filtered(lambda x: x.debit != 0.)
else:
spread_mls = spread_mls.filtered(lambda x: x.credit != 0.)
invoice_mls = self.invoice_id.move_id.mapped('line_ids') invoice_mls = self.invoice_id.move_id.mapped('line_ids')
if self.invoice_id.type in ('in_invoice', 'out_refund'): if self.invoice_id.type in in_invoice_or_out_refund and spread_sign:
invoice_mls = invoice_mls.filtered(lambda x: x.debit != 0.) invoice_mls = invoice_mls.filtered(lambda x: x.debit != 0.)
else: elif self.invoice_id.type in in_invoice_or_out_refund:
invoice_mls = invoice_mls.filtered(lambda x: x.credit != 0.) invoice_mls = invoice_mls.filtered(lambda x: x.credit != 0.)
elif spread_sign:
invoice_mls = invoice_mls.filtered(lambda x: x.credit != 0.)
else:
invoice_mls = invoice_mls.filtered(lambda x: x.debit != 0.)
to_be_reconciled = self.env['account.move.line'] to_be_reconciled = self.env['account.move.line']
if len(invoice_mls) > 1: if len(invoice_mls) > 1:

View File

@ -1,4 +1,4 @@
# Copyright 2016-2018 Onestein (<https://www.onestein.eu>) # Copyright 2016-2019 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models from odoo import _, api, fields, models
@ -84,8 +84,8 @@ class AccountInvoiceSpreadLine(models.Model):
line_ids = [(0, 0, { line_ids = [(0, 0, {
'name': spread.name.split('\n')[0][:64], 'name': spread.name.split('\n')[0][:64],
'account_id': spread.debit_account_id.id, 'account_id': spread.debit_account_id.id,
'debit': amount, 'debit': amount if amount > 0.0 else 0.0,
'credit': 0.0, 'credit': -amount if amount < 0.0 else 0.0,
'analytic_account_id': analytic.id, 'analytic_account_id': analytic.id,
'analytic_tag_ids': analytic_tags, 'analytic_tag_ids': analytic_tags,
'currency_id': not_same_curr and current_currency.id or False, 'currency_id': not_same_curr and current_currency.id or False,
@ -93,8 +93,8 @@ class AccountInvoiceSpreadLine(models.Model):
}), (0, 0, { }), (0, 0, {
'name': spread.name.split('\n')[0][:64], 'name': spread.name.split('\n')[0][:64],
'account_id': spread.credit_account_id.id, 'account_id': spread.credit_account_id.id,
'credit': amount, 'credit': amount if amount > 0.0 else 0.0,
'debit': 0.0, 'debit': -amount if amount < 0.0 else 0.0,
'analytic_account_id': analytic.id, 'analytic_account_id': analytic.id,
'analytic_tag_ids': analytic_tags, 'analytic_tag_ids': analytic_tags,
'currency_id': not_same_curr and current_currency.id or False, 'currency_id': not_same_curr and current_currency.id or False,

View File

@ -1,4 +1,4 @@
# Copyright 2017-2018 Onestein (<https://www.onestein.eu>) # Copyright 2017-2019 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import datetime import datetime
@ -501,11 +501,10 @@ class TestComputeSpreadBoard(common.TransactionCase):
'period_type': 'month', 'period_type': 'month',
'spread_date': datetime.date(2017, 1, 7) 'spread_date': datetime.date(2017, 1, 7)
}) })
with self.assertRaises(UserError): self.spread.compute_spread_board()
self.spread.compute_spread_board()
spread_lines = self.spread.line_ids spread_lines = self.spread.line_ids
self.assertFalse(spread_lines) self.assertTrue(spread_lines)
def test_15_compute_spread_board_line_account_deprecated(self): def test_15_compute_spread_board_line_account_deprecated(self):
self.spread.debit_account_id.deprecated = True self.spread.debit_account_id.deprecated = True