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