Merge lp:~camptocamp/account-invoicing/account-invoicing_payment_term_rounding into lp:~account-core-editors/account-invoicing/7.0

Proposed by Yannick Vaucher @ Camptocamp
Status: Merged
Merged at revision: 21
Proposed branch: lp:~camptocamp/account-invoicing/account-invoicing_payment_term_rounding
Merge into: lp:~account-core-editors/account-invoicing/7.0
Diff against target: 198 lines (+174/-0)
4 files modified
payment_term_rounding/__init__.py (+21/-0)
payment_term_rounding/__openerp__.py (+34/-0)
payment_term_rounding/account.py (+104/-0)
payment_term_rounding/account_view.xml (+15/-0)
To merge this branch: bzr merge lp:~camptocamp/account-invoicing/account-invoicing_payment_term_rounding
Reviewer Review Type Date Requested Status
Frederic Clementi - Camptocamp (community) Approve
Guewen Baconnier @ Camptocamp code review, no test Approve
Review via email: mp+174949@code.launchpad.net

Description of the change

Add module payment_term_rounding

To post a comment you must log in.
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

s/rouding/rounding/ in __openerp__.py

In this code:

    def compute_line_amount(self, cr, uid, id, total_amount, remaining_amount, context=None):
        if isinstance(id, list):
            id = id[0]

I observed that tuple is usually an accepted type for the ids too.
When 'id' is a list, you should assert that the received length if 'id' is 1. Otherwise the programmer using your method might have unpredictable effects if he want to call it on multiple ids.

Proposal:
    def compute_line_amount(self, cr, uid, id, total_amount, remaining_amount, context=None):
        if isinstance(id, (tuple, list)):
            assert len(id) == 1, "compute_line_amount accepts only 1 ID"
            id = id[0]

When you format the dates, '%Y-%m-%d' should be replaced by openerp.tools.DEFAULT_SERVER_DATE_FORMAT

At l.157, the whole branch
    if amt:
        ....
Can be replace by
    if not amt:
        continue
    ...
This will avoid to nest all this part of code and you'll gain some width.

review: Needs Fixing
Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

I made the corrections

Thanks for the review

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Thanks for the change

review: Approve (code review, no test)
Revision history for this message
Frederic Clementi - Camptocamp (frederic-clementi) wrote :

On functional aspect, it works great.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'payment_term_rounding'
2=== added file 'payment_term_rounding/__init__.py'
3--- payment_term_rounding/__init__.py 1970-01-01 00:00:00 +0000
4+++ payment_term_rounding/__init__.py 2013-07-23 09:55:33 +0000
5@@ -0,0 +1,21 @@
6+# -*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# Author: Yannick Vaucher
10+# Copyright 2013 Camptocamp SA
11+#
12+# This program is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU Affero General Public License as
14+# published by the Free Software Foundation, either version 3 of the
15+# License, or (at your option) any later version.
16+#
17+# This program is distributed in the hope that it will be useful,
18+# but WITHOUT ANY WARRANTY; without even the implied warranty of
19+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+# GNU Affero General Public License for more details.
21+#
22+# You should have received a copy of the GNU Affero General Public License
23+# along with this program. If not, see <http://www.gnu.org/licenses/>.
24+#
25+##############################################################################
26+from . import account
27
28=== added file 'payment_term_rounding/__openerp__.py'
29--- payment_term_rounding/__openerp__.py 1970-01-01 00:00:00 +0000
30+++ payment_term_rounding/__openerp__.py 2013-07-23 09:55:33 +0000
31@@ -0,0 +1,34 @@
32+# -*- coding: utf-8 -*-
33+##############################################################################
34+#
35+# Author: Yannick Vaucher
36+# Copyright 2013 Camptocamp SA
37+#
38+# This program is free software: you can redistribute it and/or modify
39+# it under the terms of the GNU Affero General Public License as
40+# published by the Free Software Foundation, either version 3 of the
41+# License, or (at your option) any later version.
42+#
43+# This program is distributed in the hope that it will be useful,
44+# but WITHOUT ANY WARRANTY; without even the implied warranty of
45+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46+# GNU Affero General Public License for more details.
47+#
48+# You should have received a copy of the GNU Affero General Public License
49+# along with this program. If not, see <http://www.gnu.org/licenses/>.
50+#
51+##############################################################################
52+{'name' : 'Rounding on payment term',
53+ 'version' : '1.0',
54+ 'category': 'Accounting',
55+ 'description': """Giving rounding functionality at payment term line level """,
56+ 'author' : 'Camptocamp',
57+ 'maintainer': 'Camptocamp',
58+ 'website': 'http://www.camptocamp.com/',
59+ 'depends' : ['account'],
60+ 'data': ['account_view.xml'],
61+ 'test': [],
62+ 'installable': True,
63+ 'auto_install': False,
64+ 'application': True,
65+ }
66
67=== added file 'payment_term_rounding/account.py'
68--- payment_term_rounding/account.py 1970-01-01 00:00:00 +0000
69+++ payment_term_rounding/account.py 2013-07-23 09:55:33 +0000
70@@ -0,0 +1,104 @@
71+# -*- coding: utf-8 -*-
72+##############################################################################
73+#
74+# Author: Yannick Vaucher
75+# Copyright 2013 Camptocamp SA
76+#
77+# This program is free software: you can redistribute it and/or modify
78+# it under the terms of the GNU Affero General Public License as
79+# published by the Free Software Foundation, either version 3 of the
80+# License, or (at your option) any later version.
81+#
82+# This program is distributed in the hope that it will be useful,
83+# but WITHOUT ANY WARRANTY; without even the implied warranty of
84+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85+# GNU Affero General Public License for more details.
86+#
87+# You should have received a copy of the GNU Affero General Public License
88+# along with this program. If not, see <http://www.gnu.org/licenses/>.
89+#
90+##############################################################################
91+from datetime import datetime
92+from dateutil.relativedelta import relativedelta
93+import time
94+
95+from openerp.osv import orm, fields
96+from openerp.tools.float_utils import float_round
97+
98+import openerp.addons.decimal_precision as dp
99+from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
100+
101+
102+class AccountPaymentTermLine(orm.Model):
103+ _inherit = "account.payment.term.line"
104+ _columns = {
105+ 'amount_round': fields.float('Amount Rounding',
106+ digits_compute=dp.get_precision('Account'),
107+ help="Sets the amount so that it is a multiple of this value.\n"
108+ "To have amounts that end in 0.99, set rounding 1, surcharge -0.01"
109+ ),
110+ }
111+
112+ def compute_line_amount(self, cr, uid, id, total_amount, remaining_amount, context=None):
113+ """
114+ Compute the amount for a payment term line.
115+ In case of procent computation, use the payment
116+ term line rounding if defined
117+
118+ :param total_amount: total balance to pay
119+ :param remaining_amount: total amount minus sum of previous lines
120+ computed amount
121+ :returns: computed amount for this line
122+ """
123+ if isinstance(id, (tuple, list)):
124+ assert len(id) == 1, "compute_line_amount accepts only 1 ID"
125+ id = id[0]
126+ obj_precision = self.pool.get('decimal.precision')
127+ prec = obj_precision.precision_get(cr, uid, 'Account')
128+ line = self.browse(cr, uid, id, context=context)
129+ if line.value == 'fixed':
130+ return float_round(line.value_amount, precision_digits=prec)
131+ elif line.value == 'procent':
132+ amt = total_amount * line.value_amount
133+ if line.amount_round:
134+ amt = float_round(amt, precision_rounding=line.amount_round)
135+ return float_round(amt, precision_digits=prec)
136+ elif line.value == 'balance':
137+ amt = float_round(remaining_amount, precision_digits=prec)
138+ return None
139+
140+
141+class AccountPaymentTerm(orm.Model):
142+ _inherit = "account.payment.term"
143+
144+ def compute(self, cr, uid, id, value, date_ref=False, context=None):
145+ """
146+ Complete overwrite of compute method to add rounding on line computing
147+ """
148+
149+ obj_precision = self.pool.get('decimal.precision')
150+ prec = obj_precision.precision_get(cr, uid, 'Account')
151+ if not date_ref:
152+ date_ref = datetime.now().strftime(DEFAULT_SERVER_DATE_FORMAT)
153+ pt = self.browse(cr, uid, id, context=context)
154+ amount = value
155+ result = []
156+ for line in pt.line_ids:
157+ amt = line.compute_line_amount(value, amount)
158+ if not amt:
159+ continue
160+ next_date = (datetime.strptime(date_ref, DEFAULT_SERVER_DATE_FORMAT) + relativedelta(days=line.days))
161+ if line.days2 < 0:
162+ next_first_date = next_date + relativedelta(day=1,months=1) #Getting 1st of next month
163+ next_date = next_first_date + relativedelta(days=line.days2)
164+ if line.days2 > 0:
165+ next_date += relativedelta(day=line.days2, months=1)
166+ result.append( (next_date.strftime(DEFAULT_SERVER_DATE_FORMAT), amt) )
167+ amount -= amt
168+
169+ amount = reduce(lambda x,y: x+y[1], result, 0.0)
170+ dist = round(value-amount, prec)
171+ if dist:
172+ result.append( (time.strftime(DEFAULT_SERVER_DATE_FORMAT), dist) )
173+ return result
174+
175
176=== added file 'payment_term_rounding/account_view.xml'
177--- payment_term_rounding/account_view.xml 1970-01-01 00:00:00 +0000
178+++ payment_term_rounding/account_view.xml 2013-07-23 09:55:33 +0000
179@@ -0,0 +1,15 @@
180+<?xml version="1.0" encoding="utf-8"?>
181+<openerp>
182+ <data>
183+ <record id="view_payment_term_line_form" model="ir.ui.view">
184+ <field name="name">account.payment.term.line.form.add.rounding</field>
185+ <field name="model">account.payment.term.line</field>
186+ <field name="inherit_id" ref="account.view_payment_term_line_form"/>
187+ <field name="arch" type="xml">
188+ <div attrs="{'invisible':[('value','=','balance')]}" position="after">
189+ <field name="amount_round" attrs="{'invisible':[('value','=','balance')]}"/>
190+ </div>
191+ </field>
192+ </record>
193+ </data>
194+</openerp>
195
196=== added directory 'payment_term_rounding/documentation'
197=== added file 'payment_term_rounding/documentation/DOC_payment_terms_rounding.ods'
198Binary files payment_term_rounding/documentation/DOC_payment_terms_rounding.ods 1970-01-01 00:00:00 +0000 and payment_term_rounding/documentation/DOC_payment_terms_rounding.ods 2013-07-23 09:55:33 +0000 differ

Subscribers

People subscribed via source and target branches