[FIX] project_issue, resource: fixed computation of hours / days to open (assign...
[odoo/odoo.git] / addons / project_issue / project_issue.py
1  #-*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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
24 from openerp import api
25 from openerp import SUPERUSER_ID
26 from openerp import tools
27 from openerp.osv import fields, osv, orm
28 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
29 from openerp.tools import html2plaintext
30 from openerp.tools.translate import _
31
32
33 class project_issue_version(osv.Model):
34     _name = "project.issue.version"
35     _order = "name desc"
36     _columns = {
37         'name': fields.char('Version Number', required=True),
38         'active': fields.boolean('Active', required=False),
39     }
40     _defaults = {
41         'active': 1,
42     }
43
44 class project_issue(osv.Model):
45     _name = "project.issue"
46     _description = "Project Issue"
47     _order = "priority, create_date desc"
48     _inherit = ['mail.thread', 'ir.needaction_mixin']
49
50     _mail_post_access = 'read'
51     _track = {
52         'stage_id': {
53             # this is only an heuristics; depending on your particular stage configuration it may not match all 'new' stages
54             'project_issue.mt_issue_new': lambda self, cr, uid, obj, ctx=None: obj.stage_id and obj.stage_id.sequence <= 1,
55             'project_issue.mt_issue_stage': lambda self, cr, uid, obj, ctx=None: obj.stage_id and obj.stage_id.sequence > 1,
56         },
57         'user_id': {
58             'project_issue.mt_issue_assigned': lambda self, cr, uid, obj, ctx=None: obj.user_id and obj.user_id.id,
59         },
60         'kanban_state': {
61             'project_issue.mt_issue_blocked': lambda self, cr, uid, obj, ctx=None: obj.kanban_state == 'blocked',
62             'project_issue.mt_issue_ready': lambda self, cr, uid, obj, ctx=None: obj.kanban_state == 'done',
63         },
64     }
65
66     def _get_default_partner(self, cr, uid, context=None):
67         project_id = self._get_default_project_id(cr, uid, context)
68         if project_id:
69             project = self.pool.get('project.project').browse(cr, uid, project_id, context=context)
70             if project and project.partner_id:
71                 return project.partner_id.id
72         return False
73
74     def _get_default_project_id(self, cr, uid, context=None):
75         """ Gives default project by checking if present in the context """
76         return self._resolve_project_id_from_context(cr, uid, context=context)
77
78     def _get_default_stage_id(self, cr, uid, context=None):
79         """ Gives default stage_id """
80         project_id = self._get_default_project_id(cr, uid, context=context)
81         return self.stage_find(cr, uid, [], project_id, [('fold', '=', False)], context=context)
82
83     def _resolve_project_id_from_context(self, cr, uid, context=None):
84         """ Returns ID of project based on the value of 'default_project_id'
85             context key, or None if it cannot be resolved to a single
86             project.
87         """
88         if context is None:
89             context = {}
90         if type(context.get('default_project_id')) in (int, long):
91             return context.get('default_project_id')
92         if isinstance(context.get('default_project_id'), basestring):
93             project_name = context['default_project_id']
94             project_ids = self.pool.get('project.project').name_search(cr, uid, name=project_name, context=context)
95             if len(project_ids) == 1:
96                 return int(project_ids[0][0])
97         return None
98
99     def _read_group_stage_ids(self, cr, uid, ids, domain, read_group_order=None, access_rights_uid=None, context=None):
100         access_rights_uid = access_rights_uid or uid
101         stage_obj = self.pool.get('project.task.type')
102         order = stage_obj._order
103         # lame hack to allow reverting search, should just work in the trivial case
104         if read_group_order == 'stage_id desc':
105             order = "%s desc" % order
106         # retrieve section_id from the context and write the domain
107         # - ('id', 'in', 'ids'): add columns that should be present
108         # - OR ('case_default', '=', True), ('fold', '=', False): add default columns that are not folded
109         # - OR ('project_ids', 'in', project_id), ('fold', '=', False) if project_id: add project columns that are not folded
110         search_domain = []
111         project_id = self._resolve_project_id_from_context(cr, uid, context=context)
112         if project_id:
113             search_domain += ['|', ('project_ids', '=', project_id)]
114         search_domain += [('id', 'in', ids)]
115         # perform search
116         stage_ids = stage_obj._search(cr, uid, search_domain, order=order, access_rights_uid=access_rights_uid, context=context)
117         result = stage_obj.name_get(cr, access_rights_uid, stage_ids, context=context)
118         # restore order of the search
119         result.sort(lambda x,y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0])))
120
121         fold = {}
122         for stage in stage_obj.browse(cr, access_rights_uid, stage_ids, context=context):
123             fold[stage.id] = stage.fold or False
124         return result, fold
125
126     def _compute_day(self, cr, uid, ids, fields, args, context=None):
127         """
128         @param cr: the current row, from the database cursor,
129         @param uid: the current user’s ID for security checks,
130         @param ids: List of Openday’s IDs
131         @return: difference between current date and log date
132         @param context: A standard dictionary for contextual values
133         """
134         Calendar = self.pool['resource.calendar']
135
136         res = dict.fromkeys(ids, dict())
137         for issue in self.browse(cr, uid, ids, context=context):
138             values = {
139                 'day_open': 0.0, 'day_close': 0.0,
140                 'working_hours_open': 0.0, 'working_hours_close': 0.0,
141                 'days_since_creation': 0.0, 'inactivity_days': 0.0,
142             }
143             # if the working hours on the project are not defined, use default ones (8 -> 12 and 13 -> 17 * 5), represented by None
144             calendar_id = None
145             if issue.project_id and issue.project_id.resource_calendar_id:
146                 calendar_id = issue.project_id.resource_calendar_id.id
147
148             dt_create_date = datetime.strptime(issue.create_date, DEFAULT_SERVER_DATETIME_FORMAT)
149
150             if issue.date_open:
151                 dt_date_open = datetime.strptime(issue.date_open, DEFAULT_SERVER_DATETIME_FORMAT)
152                 values['day_open'] = (dt_date_open - dt_create_date).total_seconds() / (24.0 * 3600)
153                 values['working_hours_open'] = Calendar._interval_hours_get(
154                     cr, uid, calendar_id, dt_create_date, dt_date_open,
155                     timezone_from_uid=issue.user_id.id or uid,
156                     exclude_leaves=False, context=context)
157
158             if issue.date_closed:
159                 dt_date_closed = datetime.strptime(issue.date_closed, DEFAULT_SERVER_DATETIME_FORMAT)
160                 values['day_close'] = (dt_date_closed - dt_create_date).total_seconds() / (24.0 * 3600)
161                 values['working_hours_close'] = Calendar._interval_hours_get(
162                     cr, uid, calendar_id, dt_create_date, dt_date_closed,
163                     timezone_from_uid=issue.user_id.id or uid,
164                     exclude_leaves=False, context=context)
165
166             days_since_creation = datetime.today() - dt_create_date
167             values['days_since_creation'] = days_since_creation.days
168             if issue.date_action_last:
169                 inactive_days = datetime.today() - datetime.strptime(issue.date_action_last, DEFAULT_SERVER_DATETIME_FORMAT)
170             elif issue.date_last_stage_update:
171                 inactive_days = datetime.today() - datetime.strptime(issue.date_last_stage_update, DEFAULT_SERVER_DATETIME_FORMAT)
172             else:
173                 inactive_days = datetime.today() - datetime.strptime(issue.create_date, DEFAULT_SERVER_DATETIME_FORMAT)
174             values['inactivity_days'] = inactive_days.days
175
176             # filter only required values
177             for field in fields:
178                 res[issue.id][field] = values[field]
179
180         return res
181
182     def _hours_get(self, cr, uid, ids, field_names, args, context=None):
183         task_pool = self.pool.get('project.task')
184         res = {}
185         for issue in self.browse(cr, uid, ids, context=context):
186             progress = 0.0
187             if issue.task_id:
188                 progress = task_pool._hours_get(cr, uid, [issue.task_id.id], field_names, args, context=context)[issue.task_id.id]['progress']
189             res[issue.id] = {'progress' : progress}
190         return res
191
192     def on_change_project(self, cr, uid, ids, project_id, context=None):
193         if project_id:
194             project = self.pool.get('project.project').browse(cr, uid, project_id, context=context)
195             if project and project.partner_id:
196                 return {'value': {'partner_id': project.partner_id.id}}
197         return {}
198
199     def _get_issue_task(self, cr, uid, ids, context=None):
200         issues = []
201         issue_pool = self.pool.get('project.issue')
202         for task in self.pool.get('project.task').browse(cr, uid, ids, context=context):
203             issues += issue_pool.search(cr, uid, [('task_id','=',task.id)])
204         return issues
205
206     def _get_issue_work(self, cr, uid, ids, context=None):
207         issues = []
208         issue_pool = self.pool.get('project.issue')
209         for work in self.pool.get('project.task.work').browse(cr, uid, ids, context=context):
210             if work.task_id:
211                 issues += issue_pool.search(cr, uid, [('task_id','=',work.task_id.id)])
212         return issues
213
214     _columns = {
215         'id': fields.integer('ID', readonly=True),
216         'name': fields.char('Issue', required=True),
217         'active': fields.boolean('Active', required=False),
218         'create_date': fields.datetime('Creation Date', readonly=True, select=True),
219         'write_date': fields.datetime('Update Date', readonly=True),
220         'days_since_creation': fields.function(_compute_day, string='Days since creation date', \
221                                                multi='compute_day', type="integer", help="Difference in days between creation date and current date"),
222         'date_deadline': fields.date('Deadline'),
223         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
224                         select=True, help='Sales team to which Case belongs to.\
225                              Define Responsible user and Email account for mail gateway.'),
226         'partner_id': fields.many2one('res.partner', 'Contact', select=1),
227         'company_id': fields.many2one('res.company', 'Company'),
228         'description': fields.text('Private Note'),
229         'kanban_state': fields.selection([('normal', 'Normal'),('blocked', 'Blocked'),('done', 'Ready for next stage')], 'Kanban State',
230                                          track_visibility='onchange',
231                                          help="A Issue's kanban state indicates special situations affecting it:\n"
232                                               " * Normal is the default situation\n"
233                                               " * Blocked indicates something is preventing the progress of this issue\n"
234                                               " * Ready for next stage indicates the issue is ready to be pulled to the next stage",
235                                          required=False),
236         'email_from': fields.char('Email', size=128, help="These people will receive email.", select=1),
237         'email_cc': fields.char('Watchers Emails', size=256, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
238         'date_open': fields.datetime('Assigned', readonly=True, select=True),
239         # Project Issue fields
240         'date_closed': fields.datetime('Closed', readonly=True, select=True),
241         'date': fields.datetime('Date'),
242         'date_last_stage_update': fields.datetime('Last Stage Update', select=True),
243         'channel': fields.char('Channel', help="Communication channel."),
244         'categ_ids': fields.many2many('project.category', string='Tags'),
245         'priority': fields.selection([('0','Low'), ('1','Normal'), ('2','High')], 'Priority', select=True),
246         'version_id': fields.many2one('project.issue.version', 'Version'),
247         'stage_id': fields.many2one ('project.task.type', 'Stage',
248                         track_visibility='onchange', select=True,
249                         domain="[('project_ids', '=', project_id)]", copy=False),
250         'project_id': fields.many2one('project.project', 'Project', track_visibility='onchange', select=True),
251         'duration': fields.float('Duration'),
252         'task_id': fields.many2one('project.task', 'Task', domain="[('project_id','=',project_id)]"),
253         'day_open': fields.function(_compute_day, string='Days to Assign',
254                                     multi='compute_day', type="float",
255                                     store={'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['date_open'], 10)}),
256         'day_close': fields.function(_compute_day, string='Days to Close',
257                                      multi='compute_day', type="float",
258                                      store={'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['date_closed'], 10)}),
259         'user_id': fields.many2one('res.users', 'Assigned to', required=False, select=1, track_visibility='onchange'),
260         'working_hours_open': fields.function(_compute_day, string='Working Hours to assign the Issue',
261                                               multi='compute_day', type="float",
262                                               store={'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['date_open'], 10)}),
263         'working_hours_close': fields.function(_compute_day, string='Working Hours to close the Issue',
264                                                multi='compute_day', type="float",
265                                                store={'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['date_closed'], 10)}),
266         'inactivity_days': fields.function(_compute_day, string='Days since last action',
267                                            multi='compute_day', type="integer", help="Difference in days between last action and current date"),
268         'color': fields.integer('Color Index'),
269         'user_email': fields.related('user_id', 'email', type='char', string='User Email', readonly=True),
270         'date_action_last': fields.datetime('Last Action', readonly=1),
271         'date_action_next': fields.datetime('Next Action', readonly=1),
272         'progress': fields.function(_hours_get, string='Progress (%)', multi='hours', group_operator="avg", help="Computed as: Time Spent / Total Time.",
273             store = {
274                 'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['task_id'], 10),
275                 'project.task': (_get_issue_task, ['progress'], 10),
276                 'project.task.work': (_get_issue_work, ['hours'], 10),
277             }),
278     }
279
280     _defaults = {
281         'active': 1,
282         'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c),
283         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
284         'priority': '1',
285         'kanban_state': 'normal',
286         'date_last_stage_update': fields.datetime.now,
287         'user_id': lambda obj, cr, uid, context: uid,
288     }
289
290     _group_by_full = {
291         'stage_id': _read_group_stage_ids
292     }
293
294     def copy(self, cr, uid, id, default=None, context=None):
295         issue = self.read(cr, uid, [id], ['name'], context=context)[0]
296         if not default:
297             default = {}
298         default = default.copy()
299         default.update(name=_('%s (copy)') % (issue['name']))
300         return super(project_issue, self).copy(cr, uid, id, default=default, context=context)
301
302     def create(self, cr, uid, vals, context=None):
303         context = dict(context or {})
304         if vals.get('project_id') and not context.get('default_project_id'):
305             context['default_project_id'] = vals.get('project_id')
306         if vals.get('user_id'):
307             vals['date_open'] = fields.datetime.now()
308         if 'stage_id' in vals:
309             vals.update(self.onchange_stage_id(cr, uid, None, vals.get('stage_id'), context=context)['value'])
310
311         # context: no_log, because subtype already handle this
312         create_context = dict(context, mail_create_nolog=True)
313         return super(project_issue, self).create(cr, uid, vals, context=create_context)
314
315     def write(self, cr, uid, ids, vals, context=None):
316         # stage change: update date_last_stage_update
317         if 'stage_id' in vals:
318             vals.update(self.onchange_stage_id(cr, uid, ids, vals.get('stage_id'), context=context)['value'])
319             vals['date_last_stage_update'] = fields.datetime.now()
320             if 'kanban_state' not in vals:
321                 vals['kanban_state'] = 'normal'
322         # user_id change: update date_start
323         if vals.get('user_id'):
324             vals['date_open'] = fields.datetime.now()
325
326         return super(project_issue, self).write(cr, uid, ids, vals, context)
327
328     def onchange_task_id(self, cr, uid, ids, task_id, context=None):
329         if not task_id:
330             return {'value': {}}
331         task = self.pool.get('project.task').browse(cr, uid, task_id, context=context)
332         return {'value': {'user_id': task.user_id.id, }}
333
334     def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
335         """ This function returns value of partner email address based on partner
336             :param part: Partner's id
337         """
338         result = {}
339         if partner_id:
340             partner = self.pool['res.partner'].browse(cr, uid, partner_id, context)
341             result['email_from'] = partner.email
342         return {'value': result}
343
344     def get_empty_list_help(self, cr, uid, help, context=None):
345         context = dict(context or {})
346         context['empty_list_help_model'] = 'project.project'
347         context['empty_list_help_id'] = context.get('default_project_id')
348         context['empty_list_help_document_name'] = _("issues")
349         return super(project_issue, self).get_empty_list_help(cr, uid, help, context=context)
350
351     # -------------------------------------------------------
352     # Stage management
353     # -------------------------------------------------------
354
355     def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
356         if not stage_id:
357             return {'value': {}}
358         stage = self.pool['project.task.type'].browse(cr, uid, stage_id, context=context)
359         if stage.fold:
360             return {'value': {'date_closed': fields.datetime.now()}}
361         return {'value': {'date_closed': False}}
362
363     def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None):
364         """ Override of the base.stage method
365             Parameter of the stage search taken from the issue:
366             - type: stage type must be the same or 'both'
367             - section_id: if set, stages must belong to this section or
368               be a default case
369         """
370         if isinstance(cases, (int, long)):
371             cases = self.browse(cr, uid, cases, context=context)
372         # collect all section_ids
373         section_ids = []
374         if section_id:
375             section_ids.append(section_id)
376         for task in cases:
377             if task.project_id:
378                 section_ids.append(task.project_id.id)
379         # OR all section_ids and OR with case_default
380         search_domain = []
381         if section_ids:
382             search_domain += [('|')] * (len(section_ids)-1)
383             for section_id in section_ids:
384                 search_domain.append(('project_ids', '=', section_id))
385         search_domain += list(domain)
386         # perform search, return the first found
387         stage_ids = self.pool.get('project.task.type').search(cr, uid, search_domain, order=order, context=context)
388         if stage_ids:
389             return stage_ids[0]
390         return False
391
392     def case_escalate(self, cr, uid, ids, context=None):        # FIXME rename this method to issue_escalate
393         for issue in self.browse(cr, uid, ids, context=context):
394             data = {}
395             esc_proj = issue.project_id.project_escalation_id
396             if not esc_proj:
397                 raise osv.except_osv(_('Warning!'), _('You cannot escalate this issue.\nThe relevant Project has not configured the Escalation Project!'))
398
399             data['project_id'] = esc_proj.id
400             if esc_proj.user_id:
401                 data['user_id'] = esc_proj.user_id.id
402             issue.write(data)
403
404             if issue.task_id:
405                 issue.task_id.write({'project_id': esc_proj.id, 'user_id': False})
406         return True
407
408     # -------------------------------------------------------
409     # Mail gateway
410     # -------------------------------------------------------
411
412     def message_get_reply_to(self, cr, uid, ids, context=None):
413         """ Override to get the reply_to of the parent project. """
414         issues = self.browse(cr, SUPERUSER_ID, ids, context=context)
415         project_ids = set([issue.project_id.id for issue in issues if issue.project_id])
416         aliases = self.pool['project.project'].message_get_reply_to(cr, uid, list(project_ids), context=context)
417         return dict((issue.id, aliases.get(issue.project_id and issue.project_id.id or 0, False)) for issue in issues)
418
419     def message_get_suggested_recipients(self, cr, uid, ids, context=None):
420         recipients = super(project_issue, self).message_get_suggested_recipients(cr, uid, ids, context=context)
421         try:
422             for issue in self.browse(cr, uid, ids, context=context):
423                 if issue.partner_id:
424                     self._message_add_suggested_recipient(cr, uid, recipients, issue, partner=issue.partner_id, reason=_('Customer'))
425                 elif issue.email_from:
426                     self._message_add_suggested_recipient(cr, uid, recipients, issue, email=issue.email_from, reason=_('Customer Email'))
427         except (osv.except_osv, orm.except_orm):  # no read access rights -> just ignore suggested recipients because this imply modifying followers
428             pass
429         return recipients
430
431     def message_new(self, cr, uid, msg, custom_values=None, context=None):
432         """ Overrides mail_thread message_new that is called by the mailgateway
433             through message_process.
434             This override updates the document according to the email.
435         """
436         if custom_values is None:
437             custom_values = {}
438         context = dict(context or {}, state_to='draft')
439         defaults = {
440             'name':  msg.get('subject') or _("No Subject"),
441             'email_from': msg.get('from'),
442             'email_cc': msg.get('cc'),
443             'partner_id': msg.get('author_id', False),
444             'user_id': False,
445         }
446         defaults.update(custom_values)
447         res_id = super(project_issue, self).message_new(cr, uid, msg, custom_values=defaults, context=context)
448         return res_id
449
450     @api.cr_uid_ids_context
451     def message_post(self, cr, uid, thread_id, body='', subject=None, type='notification', subtype=None, parent_id=False, attachments=None, context=None, content_subtype='html', **kwargs):
452         """ Overrides mail_thread message_post so that we can set the date of last action field when
453             a new message is posted on the issue.
454         """
455         if context is None:
456             context = {}
457         res = super(project_issue, self).message_post(cr, uid, thread_id, body=body, subject=subject, type=type, subtype=subtype, parent_id=parent_id, attachments=attachments, context=context, content_subtype=content_subtype, **kwargs)
458         if thread_id and subtype:
459             self.write(cr, SUPERUSER_ID, thread_id, {'date_action_last': fields.datetime.now()}, context=context)
460         return res
461
462
463 class project(osv.Model):
464     _inherit = "project.project"
465
466     def _get_alias_models(self, cr, uid, context=None):
467         return [('project.task', "Tasks"), ("project.issue", "Issues")]
468
469     def _issue_count(self, cr, uid, ids, field_name, arg, context=None):
470         Issue = self.pool['project.issue']
471         return {
472             project_id: Issue.search_count(cr,uid, [('project_id', '=', project_id), ('stage_id.fold', '=', False)], context=context)
473             for project_id in ids
474         }
475     _columns = {
476         'project_escalation_id': fields.many2one('project.project', 'Project Escalation',
477             help='If any issue is escalated from the current Project, it will be listed under the project selected here.',
478             states={'close': [('readonly', True)], 'cancelled': [('readonly', True)]}),
479         'issue_count': fields.function(_issue_count, type='integer', string="Issues",),
480         'issue_ids': fields.one2many('project.issue', 'project_id',
481                                      domain=[('stage_id.fold', '=', False)])
482     }
483
484     def _check_escalation(self, cr, uid, ids, context=None):
485         project_obj = self.browse(cr, uid, ids[0], context=context)
486         if project_obj.project_escalation_id:
487             if project_obj.project_escalation_id.id == project_obj.id:
488                 return False
489         return True
490
491     _constraints = [
492         (_check_escalation, 'Error! You cannot assign escalation to the same project!', ['project_escalation_id'])
493     ]
494
495
496 class account_analytic_account(osv.Model):
497     _inherit = 'account.analytic.account'
498     _description = 'Analytic Account'
499
500     _columns = {
501         'use_issues': fields.boolean('Issues', help="Check this field if this project manages issues"),
502     }
503
504     def on_change_template(self, cr, uid, ids, template_id, date_start=False, context=None):
505         res = super(account_analytic_account, self).on_change_template(cr, uid, ids, template_id, date_start=date_start, context=context)
506         if template_id and 'value' in res:
507             template = self.browse(cr, uid, template_id, context=context)
508             res['value']['use_issues'] = template.use_issues
509         return res
510
511     def _trigger_project_creation(self, cr, uid, vals, context=None):
512         if context is None:
513             context = {}
514         res = super(account_analytic_account, self)._trigger_project_creation(cr, uid, vals, context=context)
515         return res or (vals.get('use_issues') and not 'project_creation_in_progress' in context)
516
517
518 class project_project(osv.Model):
519     _inherit = 'project.project'
520
521     _defaults = {
522         'use_issues': True
523     }
524
525     def _check_create_write_values(self, cr, uid, vals, context=None):
526         """ Perform some check on values given to create or write. """
527         # Handle use_tasks / use_issues: if only one is checked, alias should take the same model
528         if vals.get('use_tasks') and not vals.get('use_issues'):
529             vals['alias_model'] = 'project.task'
530         elif vals.get('use_issues') and not vals.get('use_tasks'):
531             vals['alias_model'] = 'project.issue'
532
533     def on_change_use_tasks_or_issues(self, cr, uid, ids, use_tasks, use_issues, context=None):
534         values = {}
535         if use_tasks and not use_issues:
536             values['alias_model'] = 'project.task'
537         elif not use_tasks and use_issues:
538             values['alias_model'] = 'project.issues'
539         return {'value': values}
540
541     def create(self, cr, uid, vals, context=None):
542         self._check_create_write_values(cr, uid, vals, context=context)
543         return super(project_project, self).create(cr, uid, vals, context=context)
544
545     def write(self, cr, uid, ids, vals, context=None):
546         self._check_create_write_values(cr, uid, vals, context=context)
547         return super(project_project, self).write(cr, uid, ids, vals, context=context)
548
549 class res_partner(osv.osv):
550     def _issue_count(self, cr, uid, ids, field_name, arg, context=None):
551         Issue = self.pool['project.issue']
552         return {
553             partner_id: Issue.search_count(cr,uid, [('partner_id', '=', partner_id)])
554             for partner_id in ids
555         }
556     
557     """ Inherits partner and adds Issue information in the partner form """
558     _inherit = 'res.partner'
559     _columns = {
560         'issue_count': fields.function(_issue_count, string='# Issues', type='integer'),
561     }
562 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: