changeset d49c9dbffce2 in modules/analytic_account:5.0 details: https://hg.tryton.org/modules/analytic_account?cmd=changeset&node=d49c9dbffce2 description: Distribute remainder to not increase total amount
issue10911 review385311002 (grafted from 2c0eccf95df01aad3e1b8efc26cd4f3eb987fb86) diffstat: account.py | 5 +++-- tests/test_analytic_account.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diffs (52 lines): diff -r fb376006397e -r d49c9dbffce2 account.py --- a/account.py Wed Jul 21 08:30:03 2021 +0200 +++ b/account.py Sun Oct 31 10:40:06 2021 +0100 @@ -313,11 +313,12 @@ if remainder: i = 0 while remainder: - account, amount = result[i] + account, current_amount = result[i] rounding = self.currency.rounding.copy_sign(remainder) - result[i] = (account, amount - rounding) + result[i] = (account, current_amount + rounding) remainder -= rounding i = (i + 1) % len(result) + assert sum(a for _, a in result) == amount return result diff -r fb376006397e -r d49c9dbffce2 tests/test_analytic_account.py --- a/tests/test_analytic_account.py Wed Jul 21 08:30:03 2021 +0200 +++ b/tests/test_analytic_account.py Sun Oct 31 10:40:06 2021 +0100 @@ -232,6 +232,30 @@ account.distribute(Decimal('100.03')), [(account1, Decimal('70.02')), (account2, Decimal('30.01'))]) + @with_transaction() + def test_account_distribute_remainder(self): + "Test account distribute remainder" + pool = Pool() + Account = pool.get('analytic_account.account') + Distribution = pool.get('analytic_account.account.distribution') + + currency = create_currency('usd') + account1 = Account(type='normal', currency=currency) + account2 = Account(type='normal', currency=currency) + account3 = Account(type='normal', currency=currency) + account = Account(type='distribution', currency=currency) + account.distributions = [ + Distribution(account=account1, ratio=Decimal('0.5')), + Distribution(account=account2, ratio=Decimal('0.375')), + Distribution(account=account3, ratio=Decimal('0.125')), + ] + + self.assertListEqual( + account.distribute(Decimal('65.35')), [ + (account1, Decimal('32.67')), + (account2, Decimal('24.51')), + (account3, Decimal('8.17'))]) + def suite(): suite = trytond.tests.test_tryton.suite()