[FIX] better coding standard
[odoo/odoo.git] / addons / l10n_ch / invoice.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    Author: Nicolas Bessi. Copyright Camptocamp SA
5 #    Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from datetime import datetime
23 from osv import fields, osv
24 from tools import mod10r
25
26 class account_invoice(osv.osv):
27     """Inherit account.invoice in order to add bvr
28     printing functionnalites. BVR is a Swiss payment vector"""
29     _inherit = "account.invoice"
30
31     ## @param self The object pointer.
32     ## @param cursor a psycopg cursor
33     ## @param user res.user.id that is currently loged
34     ## @parma context a standard dict
35     ## @return a list of tuple (name,value)
36     def _get_reference_type(self, cursor, user, context=None):
37         """Function use by the function field reference_type in order to initalise available
38         BVR Reference Types"""
39         res = super(account_invoice, self)._get_reference_type(cursor, user,
40                 context=context)
41         res.append(('bvr', 'BVR'))
42         return res
43
44     ## @param self The object pointer.
45     ## @param cursor a psycopg cursor
46     ## @param user res.user.id that is currently loged
47     ## @parma context a standard dict
48     ## @param name of the files
49     ## @param args a list of diverse argument
50     ## @parma context a standard dict
51     ## @return a  dict (invoice id,amount to pay)
52     def _amount_to_pay(self, cursor, user, ids, name, args, context=None):
53         '''Return the amount still to pay regarding all the payment orders'''
54         if not ids:
55             return {}
56         res = {}
57         for invoice in self.browse(cursor, user, ids, context=context):
58             res[invoice.id] = 0.0
59             if invoice.move_id:
60                 for line in invoice.move_id.line_id:
61                     if not line.date_maturity or \
62                             datetime.strptime(line.date_maturity, '%Y-%m-%d') \
63                             < datetime.now():
64                         res[invoice.id] += line.amount_to_pay
65         return res
66
67     _columns = {
68         ### BVR reference type BVR or FREE
69         'reference_type': fields.selection(_get_reference_type,
70             'Reference Type', required=True),
71         ### Partner bank link between bank and partner id
72         'partner_bank_id': fields.many2one('res.partner.bank', 'Bank Account',
73             help='The partner bank account to pay\nKeep empty to use the default'
74             ),
75         ### Amount to pay
76         'amount_to_pay': fields.function(_amount_to_pay, method=True,
77             type='float', string='Amount to be paid',
78             help='The amount which should be paid at the current date\n' \
79                     'minus the amount which is already in payment order'),
80     }
81
82     ## @param self The object pointer.
83     ## @param cursor a psycopg cursor
84     ## @param user res.user.id that is currently loged
85     ## @parma ids invoices id
86     ## @return a boolean True if valid False if invalid
87     def _check_bvr(self, cr, uid, ids):
88         """
89         Function to validate a bvr reference like :
90         0100054150009>132000000000000000000000014+ 1300132412>
91         The validation is based on l10n_ch
92         """
93         invoices = self.browse(cr,uid,ids)
94         for invoice in invoices:
95             if invoice.reference_type == 'bvr':
96                 if not invoice.reference:
97                     return False
98                 ## I need help for this bug because in this case
99                 # <010001000060190> 052550152684006+ 43435>
100                 # the reference 052550152684006 do not match modulo 10
101                 #
102                 if mod10r(invoice.reference[:-1]) != invoice.reference and \
103                     len(invoice.reference) == 15:
104                     return True
105                 #
106                 if mod10r(invoice.reference[:-1]) != invoice.reference:
107                     return False
108         return True
109     ## @param self The object pointer.
110     ## @param cursor a psycopg cursor
111     ## @param user res.user.id that is currently loged
112     ## @parma ids invoices id
113     ## @return a boolean True if valid False if invalid
114     def _check_reference_type(self, cursor, user, ids):
115         """Check the customer invoice reference type depending
116         on the BVR reference type and the invoice partner bank type"""
117         for invoice in self.browse(cursor, user, ids):
118             if invoice.type in 'in_invoice':
119                 if invoice.partner_bank_id and \
120                         invoice.partner_bank_id.state in \
121                         ('bvrbank', 'bvrpost') and \
122                         invoice.reference_type != 'bvr':
123                             return False
124         return True
125
126     _constraints = [
127         (_check_bvr, 'Error: Invalid Bvr Number (wrong checksum).',
128             ['reference']),
129         (_check_reference_type, 'Error: BVR reference is required.',
130             ['reference_type']),
131     ]
132
133     ## @param self The object pointer.
134     ## @param cr a psycopg cursor
135     ## @param uid res.user.id that is currently loged
136     ## @parma ids invoices id
137     ## @parma type the invoice type
138     ## @param partner_id the partner linked to the invoice
139     ## @parma date_invoice date of the invoice
140     ## @parma payment_term inoice payment term
141     ## @param partner_bank_id the partner linked invoice bank
142     ## @return the dict of values with the partner_bank value updated
143     def onchange_partner_id(self, cr, uid, ids, type, partner_id,
144             date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
145         """ Function that is call when the partner of the invoice is changed
146         it will retriev and set the good bank partner bank"""
147         res = super(account_invoice, self).onchange_partner_id(
148                                                                 cr,
149                                                                  uid,
150                                                                  ids,
151                                                                  type,
152                                                                  partner_id,
153                                                                  date_invoice,
154                                                                  payment_term
155                                                             )
156         bank_id = False
157         if partner_id:
158             p = self.pool.get('res.partner').browse(cr, uid, partner_id)
159             if p.bank_ids:
160                 bank_id = p.bank_ids[0].id
161
162         if type in ('in_invoice', 'in_refund'):
163             res['value']['partner_bank_id'] = bank_id
164
165         if partner_bank_id != bank_id:
166             to_update = self.onchange_partner_bank(cr, uid, ids, bank_id)
167             res['value'].update(to_update['value'])
168         return res
169
170     ## @param self The object pointer.
171     ## @param cursor a psycopg cursor
172     ## @param user res.user.id that is currently loged
173     ## @parma ids invoices id
174     ## @param partner_bank_id the partner linked invoice bank
175     ## @return the dict of values with the reference type  value updated
176     def onchange_partner_bank(self, cursor, user, ids, partner_bank_id):
177         """update the reference type depending of the partner bank"""
178         res = {'value': {}}
179         partner_bank_obj = self.pool.get('res.partner.bank')
180         if partner_bank_id:
181             partner_bank = partner_bank_obj.browse(cursor, user, partner_bank_id)
182             if partner_bank.state in ('bvrbank', 'bvrpost'):
183                 res['value']['reference_type'] = 'bvr'
184         return res
185
186 account_invoice()
187
188 class account_tax_code(osv.osv):
189     """Inherit account tax code in order
190     to add a Case code"""
191     _name = 'account.tax.code'
192     _inherit = "account.tax.code"
193     _columns = {
194         'code': fields.char('Case Code', size=512),
195     }
196
197 account_tax_code()
198
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: