[PYTHON][VIEW] siren-->siret
[burette/lhc.git] / lhc.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # lhc module for OpenERP, Customize OpenERP for L'Heureux Cyclage Copyright
5 # (C) 2013-2023 L'Heureux Cyclage (<http://www.heureux-cyclage.org>)
6 #
7 # This file is a part of lhc_custom_oe
8 #
9 # lhc_custom_oe is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # lhc_custom_oe is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 from openerp.osv import osv
25 from openerp.osv import orm
26 from openerp.osv import fields
27 from openerp.tools.translate import _
28 import openerp.addons.decimal_precision as dp
29 import datetime
30
31
32 class res_partner(orm.Model):
33 _inherit = 'res.partner'
34
35 def _get_county(self, cr, uid, ids, name, args, context=None):
36 """docstring for _county_get_fnc"""
37 res = {}
38 for obj in self.browse(cr, uid, ids, context=context):
39 if obj.zip:
40 res[obj.id] = obj.zip[0:2]
41 else:
42 res[obj.id] = ''
43 return res
44
45 def _bikecoop_activity_dates_check(self, cr, uid, ids, context=None):
46 # TODO: check condition and return boolean accordingly
47 currentDateTime = datetime.datetime.now()
48 date = currentDateTime.date()
49 year = date.strftime("%Y")
50
51 min_date = 1950
52 max_date = int(year)
53
54 for partner in self.browse(cr, uid, ids, context=context):
55 if partner.bikecoop_activity_start == 0 and partner.bikecoop_activity_stop == 0:
56 return True
57 if partner.bikecoop_activity_start < 0 or partner.bikecoop_activity_stop < 0:
58 return False
59 if partner.bikecoop_activity_start == 0 and partner.bikecoop_activity_stop > 0:
60 return False
61 if partner.bikecoop_activity_stop > max_date or partner.bikecoop_activity_stop < min_date:
62 if partner.bikecoop_activity_stop != 0:
63 return False
64 if partner.bikecoop_activity_start > max_date or partner.bikecoop_activity_start < min_date:
65 if partner.bikecoop_activity_start != 0:
66 return False
67 if partner.bikecoop_activity_stop < partner.bikecoop_activity_start:
68 if partner.bikecoop_activity_stop != 0:
69 return False
70 return True
71
72 def _rna_check(self, cr, uid, ids, context=None):
73 """Check if RNA code is well formatted"""
74 for partner in self.browse(cr, uid, ids, context=context):
75 if partner.rna:
76 if len(partner.rna) != 10:
77 return False
78 if partner.rna[0] != 'W':
79 return False
80 try:
81 int(partner.rna[1:10])
82 except ValueError:
83 return False
84 return True
85 else:
86 return True
87
88
89 def _siret_check(self, cr, uid, ids, context=None):
90 """Check if SIRET code is well formatted"""
91 for partner in self.browse(cr, uid, ids, context=context):
92 if partner.siret:
93 if len(partner.siret) != 14:
94 return False
95 try:
96 int(partner.siret)
97 except ValueError:
98 return False
99 return True
100 else:
101 return True
102
103
104 _columns = {
105 'usual_contact': fields.boolean(
106 'Usual contact',
107 help="""This contact is a usual contact for L\'Heureux Cyclage
108 employees. This field can be used to discriminated contacts for
109 differents usages."""
110 ),
111 'kit_sent': fields.boolean('Welcome kit sent'),
112 'county_code': fields.function(
113 _get_county,
114 method=True,
115 string='County code',
116 type='char',
117 size=2,
118 store={
119 'res.partner': (
120 lambda self, cr, uid, ids, c={}: ids,
121 ['zip'], 10)},
122 ),
123 'email2': fields.char(
124 'Secondary email',
125 size=240,
126 help="""One more email. Warning: there are no email notification
127 with this email. Only the main email can receive notifications."""),
128 'bikecoop_activity_start': fields.integer('Bikecoop activity start year'),
129 'bikecoop_activity_stop': fields.integer('Bikecoop activity end year'),
130 'rna': fields.char('RNA code', size=10),
131 'siret': fields.char('SIRET', size=14),
132 }
133
134 _defaults = {
135 'kit_sent': lambda *a: False,
136 }
137
138 _constraints = [
139 (_bikecoop_activity_dates_check, 'Error: Bikecoop activity dates are inconsistent!', ['Bikecoop activity dates']),
140 (_rna_check, 'Error: RNA code must begin with a "W" followed by exactly 9 digits.', ['RNA']),
141 (_siret_check, 'Error: SIRET must take exactly 14 digits.', ['SIRET']),
142 ]
143
144
145 class product_template(orm.Model):
146 _inherit = 'product.template'
147
148 _columns = {
149 'standard_price': fields.float(
150 'Cost',
151 digits_compute=dp.get_precision('Product Price'),
152 help="""Cost price of the product used for standard stock valuation
153 in accounting and used as a base price on purchase orders.",
154 groups="base.group_user,lhc.group_volunteer"""
155 ),
156 }
157
158
159 class res_users(orm.Model):
160 _inherit = 'res.users'
161
162 def onchange_partner_id(self, cr, uid, ids, partner_id, login):
163 """Define user email address from partner email address"""
164 v = {}
165 partners = self.pool.get('res.partner')
166 partner = partners.browse(cr, uid, partner_id)
167 if partner.email:
168 v['email'] = partner.email
169 else:
170 v = {}
171 return {'value': v}
172
173
174 class event_event(orm.Model):
175 _inherit = 'event.event'
176
177 _columns = {
178 'duration': fields.float(
179 'Duration',
180 digits_compute=dp.get_precision('Product Unit of Measure'),
181 help='Duration in hours'
182 ),
183 }
184
185
186 class event_registration(orm.Model):
187 _inherit = 'event.registration'
188
189 _columns = {
190 'gender': fields.selection([
191 ('female', 'Female'),
192 ('male', 'Male'),
193 ('other', 'Other')],
194 'Gender'),
195 'position': fields.selection([
196 ('employee', 'Employee'),
197 ('individual', 'Invividual'),
198 ('volunteer', 'Volunteer')],
199 'Position'),
200 'funding_main': fields.selection([
201 ('individual', 'Individual'),
202 ('opco', 'OPCO'),
203 ('company', 'Company'),
204 ('pole_emploi', 'Pole Emploi')],
205 'Main funding', help='Main funding origin'),
206 'fundings_others': fields.char('Others fundings origins', size=128),
207 'sale_order_ids': fields.many2many(
208 'sale.order',
209 'event_registration_sale_order_rel',
210 'event_registration_id',
211 'sale_order_id',
212 'Related sale order(s)'
213 ),
214 'invoice_ids': fields.many2many(
215 'account.invoice',
216 'event_registration_invoice_id_rel',
217 'event_registration_id',
218 'invoice_id',
219 'Related invoice(s)'
220 ),
221 }
222
223 def onchange_contact_id(self, cr, uid, ids, contact, partner, context=None):
224 """Contcat phone with mobile phone if exist. If mobile exist and not
225 phone, add mobile number"""
226 vals = super(event_registration, self).onchange_contact_id(cr, uid, ids, contact, partner, context)
227 addr_obj = self.pool.get('res.partner')
228 contact_id = addr_obj.browse(cr, uid, contact, context=context)
229 phone = vals['value']['phone']
230 mobile = contact_id.mobile
231 if mobile:
232 if phone:
233 vals['value']['phone'] = '%s - %s' % (phone, mobile)
234 else:
235 vals['value']['phone'] = mobile
236 return vals
237
238
239 class hr_expense_line(orm.Model):
240 _inherit = 'hr.expense.line'
241
242 def onchange_product_id(self, cr, uid, ids, product_id, context=None):
243 """Override to manage default name and ref value based on product
244 properties"""
245
246 vals = super(hr_expense_line, self).onchange_product_id(cr, uid, ids, product_id, context)
247 res = vals['value']
248 if product_id:
249 product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
250 res['ref'] = ''
251 if product.description:
252 description = product.description.split('|')
253 if len(description) == 1:
254 res['name'] = description[0]
255 elif len(description) >= 2:
256 res['name'] = description[0]
257 res['ref'] = description[1]
258 return {'value': res}
259
260 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: