1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Author: Nicolas Bessi. Copyright Camptocamp SA
5 # Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
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.
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.
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/>.
20 ##############################################################################
22 from datetime import datetime
23 from osv import fields, osv
24 from tools import mod10r
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"
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,
41 res.append(('bvr', 'BVR'))
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'''
57 for invoice in self.browse(cursor, user, ids, context=context):
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') \
64 res[invoice.id] += line.amount_to_pay
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'
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'),
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):
89 Function to validate a bvr reference like :
90 0100054150009>132000000000000000000000014+ 1300132412>
91 The validation is based on l10n_ch
93 invoices = self.browse(cr,uid,ids)
94 for invoice in invoices:
95 if invoice.reference_type == 'bvr':
96 if not invoice.reference:
98 ## I need help for this bug because in this case
99 # <010001000060190> 052550152684006+ 43435>
100 # the reference 052550152684006 do not match modulo 10
102 if mod10r(invoice.reference[:-1]) != invoice.reference and \
103 len(invoice.reference) == 15:
106 if mod10r(invoice.reference[:-1]) != invoice.reference:
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':
127 (_check_bvr, 'Error: Invalid Bvr Number (wrong checksum).',
129 (_check_reference_type, 'Error: BVR reference is required.',
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(
158 p = self.pool.get('res.partner').browse(cr, uid, partner_id)
160 bank_id = p.bank_ids[0].id
162 if type in ('in_invoice', 'in_refund'):
163 res['value']['partner_bank_id'] = bank_id
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'])
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"""
179 partner_bank_obj = self.pool.get('res.partner.bank')
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'
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"
194 'code': fields.char('Case Code', size=512),
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: