[PYTHON] +budget customization
[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-2022 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
30
31 class res_partner(orm.Model):
32 _inherit = 'res.partner'
33
34 def _get_county(self, cr, uid, ids, name, args, context=None):
35 """docstring for _county_get_fnc"""
36 res = {}
37 for obj in self.browse(cr, uid, ids, context=context):
38 if obj.zip:
39 res[obj.id] = obj.zip[0:2]
40 else:
41 res[obj.id] = ''
42 return res
43
44 _columns = {
45 'usual_contact': fields.boolean(
46 'Usual contact',
47 help="""This contact is a usual contact for L\'Heureux Cyclage
48 employees. This field can be used to discriminated contacts for
49 differents usages."""
50 ),
51 'kit_sent': fields.boolean('Welcome kit sent'),
52 'county_code': fields.function(
53 _get_county,
54 method=True,
55 string='County code',
56 type='char',
57 size=2,
58 store={
59 'res.partner': (
60 lambda self, cr, uid, ids, c={}: ids,
61 ['zip'], 10)},
62 ),
63 }
64
65 _defaults = {
66 'kit_sent': lambda *a: False,
67 }
68
69
70 class product_template(orm.Model):
71 _inherit = 'product.template'
72
73 _columns = {
74 'standard_price': fields.float(
75 'Cost',
76 digits_compute=dp.get_precision('Product Price'),
77 help="""Cost price of the product used for standard stock valuation
78 in accounting and used as a base price on purchase orders.",
79 groups="base.group_user,lhc.group_volunteer"""
80 ),
81 }
82
83
84 class res_users(orm.Model):
85 _inherit = 'res.users'
86
87 def onchange_partner_id(self, cr, uid, ids, partner_id, login):
88 """Define user email address from partner email address"""
89 v = {}
90 partners = self.pool.get('res.partner')
91 partner = partners.browse(cr, uid, partner_id)
92 if partner.email:
93 v['email'] = partner.email
94 else:
95 v = {}
96 return {'value': v}
97
98
99 class event_event(orm.Model):
100 _inherit = 'event.event'
101
102 _columns = {
103 'duration': fields.float(
104 'Duration',
105 digits_compute=dp.get_precision('Product Unit of Measure'),
106 help='Duration in hours'
107 ),
108 }
109
110
111 class event_registration(orm.Model):
112 _inherit = 'event.registration'
113
114 _columns = {
115 'gender': fields.selection([
116 ('female', 'Female'),
117 ('male', 'Male'),
118 ('other', 'Other')],
119 'Gender'),
120 'position': fields.selection([
121 ('employee', 'Employee'),
122 ('individual', 'Invividual'),
123 ('volunteer', 'Volunteer')],
124 'Position'),
125 'funding_main': fields.selection([
126 ('individual', 'Individual'),
127 ('opco', 'OPCO'),
128 ('company', 'Company'),
129 ('pole_emploi', 'Pole Emploi')],
130 'Main funding', help='Main funding origin'),
131 'fundings_others': fields.char('Others fundings origins', size=128),
132 'sale_order_ids': fields.many2many(
133 'sale.order',
134 'event_registration_sale_order_rel',
135 'event_registration_id',
136 'sale_order_id',
137 'Related sale order(s)'
138 ),
139 'invoice_ids': fields.many2many(
140 'account.invoice',
141 'event_registration_invoice_id_rel',
142 'event_registration_id',
143 'invoice_id',
144 'Related invoice(s)'
145 ),
146 }
147
148 def onchange_contact_id(self, cr, uid, ids, contact, partner, context=None):
149 """Contcat phone with mobile phone if exist. If mobile exist and not
150 phone, add mobile number"""
151 vals = super(event_registration, self).onchange_contact_id(cr, uid, ids, contact, partner, context)
152 addr_obj = self.pool.get('res.partner')
153 contact_id = addr_obj.browse(cr, uid, contact, context=context)
154 phone = vals['value']['phone']
155 mobile = contact_id.mobile
156 if mobile:
157 if phone:
158 vals['value']['phone'] = '%s - %s' % (phone, mobile)
159 else:
160 vals['value']['phone'] = mobile
161 return vals
162
163
164 class crossovered_budget_lines(orm.Model):
165 _inherit = 'crossovered.budget.lines'
166
167 def _prac_amt(self, cr, uid, ids, context=None):
168 """Calculate pratical amount even if there is no analytic account"""
169 res = {}
170 result = 0.0
171 if context is None:
172 context = {}
173 account_obj = self.pool.get('account.account')
174
175 for line in self.browse(cr, uid, ids, context=context):
176 acc_ids = [x.id for x in line.general_budget_id.account_ids]
177 if not acc_ids:
178 raise osv.except_osv(_('Error!'),_("The Budget '%s' has no accounts!") % ustr(line.general_budget_id.name))
179 acc_ids = account_obj._get_children_and_consol(cr, uid, acc_ids, context=context)
180 date_to = line.date_to
181 date_from = line.date_from
182
183 if line.analytic_account_id.id:
184 res = super(crossovered_budget_lines, self)._prac_amt(cr, uid, ids, context)
185 else:
186 cr.execute("SELECT SUM(credit) - SUM(debit) FROM account_move_line WHERE (date "
187 "between to_date(%s,'yyyy-mm-dd') AND to_date(%s,'yyyy-mm-dd')) AND "
188 "account_id=ANY(%s)", (date_from, date_to,acc_ids,))
189 result = cr.fetchone()[0]
190 if result is None:
191 result = 0.00
192 res[line.id] = result
193
194 return res
195
196
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: