diff options
| author | Yogesh Sakhreliya <ysa@tinyerp.com> | 2011-01-06 16:50:01 +0530 |
|---|---|---|
| committer | Yogesh Sakhreliya <ysa@tinyerp.com> | 2011-01-06 16:50:01 +0530 |
| commit | 8697c0f3b254a27043d9698ebde5f992bb25dbf6 (patch) | |
| tree | 2285087bb437f1be076014293f909982b541e934 /addons/project_long_term/project_long_term.py | |
| parent | 5d8831174421c3965f5427e7dddbb86c04b26193 (diff) | |
| parent | aa4594a43e157c17af815d2b72033e899ba940a7 (diff) | |
Merge with trunk-dev-addons2.
bzr revid: ysa@tinyerp.com-20110106112001-wh6d4y46mpm0bf5h
Diffstat (limited to 'addons/project_long_term/project_long_term.py')
| -rw-r--r-- | addons/project_long_term/project_long_term.py | 180 |
1 files changed, 65 insertions, 115 deletions
diff --git a/addons/project_long_term/project_long_term.py b/addons/project_long_term/project_long_term.py index 2caf36766b0..45d75b16b05 100644 --- a/addons/project_long_term/project_long_term.py +++ b/addons/project_long_term/project_long_term.py @@ -19,7 +19,7 @@ # ############################################################################## -from datetime import datetime +from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta from tools.translate import _ from osv import fields, osv @@ -91,8 +91,8 @@ class project_phase(osv.osv): model_data_obj = self.pool.get('ir.model.data') model_data_id = model_data_obj._get_id(cr, uid, 'product', 'uom_hour') return model_data_obj.read(cr, uid, [model_data_id], ['res_id'])[0]['res_id'] - - def _compute(self, cr, uid, ids, context=None): + + def _compute(self, cr, uid, ids, field_name, arg, context=None): res = {} if not ids: return res @@ -100,15 +100,13 @@ class project_phase(osv.osv): tot = 0.0 for task in phase.task_ids: tot += task.planned_hours - res[phase.id] = { - 'total_hours' : tot - } + res[phase.id] = tot return res _columns = { 'name': fields.char("Name", size=64, required=True), - 'date_start': fields.date('Start Date', help="Starting Date of the phase", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), - 'date_end': fields.date('End Date', help="Ending Date of the phase", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), + 'date_start': fields.date('Start Date', help="It's computed according to the phases order : the start date of the 1st phase is set by you while the other start dates depend on the end date of their previous phases", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), + 'date_end': fields.date('End Date', help=" It's computed by the scheduler according to the start date and the duration.", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), 'constraint_date_start': fields.date('Minimum Start Date', help='force the phase to start after this date', states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), 'constraint_date_end': fields.date('Deadline', help='force the phase to finish before this date', states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}), 'project_id': fields.many2one('project.project', 'Project', required=True), @@ -145,18 +143,9 @@ class project_phase(osv.osv): project_id = project_obj.browse(cr, uid, project, context=context) result['date_start'] = project_id.date_start return {'value': result} - - def onchange_days(self, cr, uid, ids, project, context=None): - result = {} - for id in ids: - project_id = self.browse(cr, uid, id, context=context) - newdate = datetime.strptime(project_id.date_start, '%Y-%m-%d') + relativedelta(days=project_id.duration or 0.0) - result['date_end'] = newdate.strftime('%Y-%m-%d') - return {'value': result} + def _check_date_start(self, cr, uid, phase, date_end, context=None): - if context is None: - context = {} """ Check And Compute date_end of phase if change in date_start < older time. """ @@ -177,8 +166,6 @@ class project_phase(osv.osv): self.write(cr, uid, [phase.id], {'date_start': dt_start, 'date_end': date_end.strftime('%Y-%m-%d')}, context=context) def _check_date_end(self, cr, uid, phase, date_start, context=None): - if context is None: - context = {} """ Check And Compute date_end of phase if change in date_end > older time. """ @@ -198,55 +185,6 @@ class project_phase(osv.osv): dt_end = work_times[-1][1].strftime('%Y-%m-%d') self.write(cr, uid, [phase.id], {'date_start': date_start.strftime('%Y-%m-%d'), 'date_end': dt_end}, context=context) - def write(self, cr, uid, ids, vals, context=None): - resource_calendar_obj = self.pool.get('resource.calendar') - resource_obj = self.pool.get('resource.resource') - uom_obj = self.pool.get('product.uom') - if context is None: - context = {} - res = super(project_phase, self).write(cr, uid, ids, vals, context=context) - if context.get('scheduler',False): - return res - # Consider calendar and efficiency if the phase is performed by a resource - # otherwise consider the project's working calendar - - #TOCHECK : why need this ? - if isinstance(ids, (int, long)): - ids = [ids] - default_uom_id = self._get_default_uom_id(cr, uid) - for phase in self.browse(cr, uid, ids, context=context): - calendar_id = phase.project_id.resource_calendar_id and phase.project_id.resource_calendar_id.id or False - resource_id = resource_obj.search(cr, uid, [('user_id', '=', phase.responsible_id.id)],context=context) - if resource_id: - cal_id = resource_obj.browse(cr, uid, resource_id[0], context=context).calendar_id.id - if cal_id: - calendar_id = cal_id - - avg_hours = uom_obj._compute_qty(cr, uid, phase.product_uom.id, phase.duration, default_uom_id) - # Change the date_start and date_end - # for previous and next phases respectively based on valid condition - if vals.get('date_start', False) and vals['date_start'] < phase.date_start: - dt_start = datetime.strptime(vals['date_start'], '%Y-%m-%d') - work_times = resource_calendar_obj.interval_get(cr, uid, calendar_id, dt_start, avg_hours or 0.0, resource_id and resource_id[0] or False) - if work_times: - vals['date_end'] = work_times[-1][1].strftime('%Y-%m-%d') - for prv_phase in phase.previous_phase_ids: - if prv_phase.id == phase.id: - continue - self._check_date_start(cr, uid, prv_phase, dt_start, context=context) - - if vals.get('date_end', False) and vals['date_end'] > phase.date_end: - dt_end = datetime.strptime(vals['date_end'], '%Y-%m-%d') - work_times = resource_calendar_obj.interval_min_get(cr, uid, calendar_id, dt_end, avg_hours or 0.0, resource_id and resource_id[0] or False) - if work_times: - vals['date_start'] = work_times[0][0].strftime('%Y-%m-%d') - for next_phase in phase.next_phase_ids: - if next_phase.id == phase.id: - continue - self._check_date_end(cr, uid, next_phase, dt_end, context=context) - - return res - def copy(self, cr, uid, id, default=None, context=None): if default is None: default = {} @@ -279,8 +217,6 @@ class project_phase(osv.osv): Return a list of Resource Class objects for the resources allocated to the phase. """ res = {} - if context is None: - context = {} resource_pool = self.pool.get('resource.resource') for phase in self.browse(cr, uid, ids, context=context): user_ids = map(lambda x:x.resource_id.user_id.id, phase.resource_ids) @@ -290,26 +226,31 @@ class project_phase(osv.osv): res[phase.id] = resource_objs return res - def generate_schedule(self, cr, uid, ids, start_date, calendar_id=False, context=None): + def generate_schedule(self, cr, uid, ids, start_date=False, calendar_id=False, context=None): """ Schedule phase with the start date till all the next phases are completed. - @param: start_dsate : start date for the phase + @param: start_date (datetime.datetime) : start date for the phase. It would be either Start date of phase or start date of project or system current date @param: calendar_id : working calendar of the project - """ + """ if context is None: context = {} resource_pool = self.pool.get('resource.resource') + data_pool = self.pool.get('ir.model.data') + resource_allocation_pool = self.pool.get('project.resource.allocation') uom_pool = self.pool.get('product.uom') - if context is None: - context = {} - default_uom_id = self._get_default_uom_id(cr, uid) + data_model, day_uom_id = data_pool.get_object_reference(cr, uid, 'product', 'uom_day') for phase in self.browse(cr, uid, ids, context=context): if not phase.responsible_id: raise osv.except_osv(_('No responsible person assigned !'),_("You must assign a responsible person for phase '%s' !") % (phase.name,)) - - phase_resource_obj = resource_pool.generate_resources(cr, uid, [phase.responsible_id.id], calendar_id, context=context) - avg_hours = uom_pool._compute_qty(cr, uid, phase.product_uom.id, phase.duration, default_uom_id) - duration = str(avg_hours) + 'H' + + if not start_date: + start_date = phase.project_id.date_start or phase.date_start or datetime.now().strftime("%Y-%m-%d") + start_date = datetime.strftime((datetime.strptime(start_date, "%Y-%m-%d")), "%Y-%m-%d") + phase_resource_obj = self.generate_resources(cr, uid, [phase.id], context=context)[phase.id] + avg_days = uom_pool._compute_qty(cr, uid, phase.product_uom.id, phase.duration, day_uom_id) + if not phase_resource_obj: #TOCHECK: why need this ? + avg_days = avg_days - 1 + duration = str(avg_days) + 'd' # Create a new project for each phase def Project(): # If project has working calendar then that @@ -317,14 +258,17 @@ class project_phase(osv.osv): start = start_date minimum_time_unit = 1 resource = phase_resource_obj + working_hours_per_day = 24 + vacation = [] if calendar_id: - working_days = resource_pool.compute_working_calendar(cr, uid, calendar_id, context=context) + working_hours_per_day = 8 #TODO: it should be come from calendars vacation = tuple(resource_pool.compute_vacation(cr, uid, calendar_id)) - + working_days = resource_pool.compute_working_calendar(cr, uid, calendar_id, context=context) def phase(): effort = duration project = Task.BalancedProject(Project) + s_date = project.phase.start.to_datetime() e_date = project.phase.end.to_datetime() # Recalculate date_start and date_end @@ -346,12 +290,17 @@ class project_phase(osv.osv): 'date_start': start_date.strftime('%Y-%m-%d'), 'date_end': end_date.strftime('%Y-%m-%d') }, context=ctx) - + # write dates into Resources Allocation + for resource in phase.resource_ids: + resource_allocation_pool.write(cr, uid, [resource.id], { + 'date_start': start_date.strftime('%Y-%m-%d'), + 'date_end': end_date.strftime('%Y-%m-%d') + }, context=ctx) # Recursive call till all the next phases scheduled - for phase in phase.next_phase_ids: - if phase.state in ['draft', 'open', 'pending']: - id_cal = phase.project_id.resource_calendar_id and phase.project_id.resource_calendar_id.id or False - self.generate_schedule(cr, uid, [phase.id], date_start, id_cal, context=context) + for next_phase in phase.next_phase_ids: + if next_phase.state in ['draft', 'open', 'pending']: + id_cal = next_phase.project_id.resource_calendar_id and next_phase.project_id.resource_calendar_id.id or False + self.generate_schedule(cr, uid, [next_phase.id], date_start+timedelta(days=1), id_cal, context=context) else: continue return True @@ -362,8 +311,6 @@ class project_phase(osv.osv): """ task_pool = self.pool.get('project.task') resource_pool = self.pool.get('resource.resource') - if context is None: - context = {} resources_list = self.generate_resources(cr, uid, ids, context=context) return_msg = {} for phase in self.browse(cr, uid, ids, context=context): @@ -371,10 +318,10 @@ class project_phase(osv.osv): if not start_date and phase.project_id.date_start: start_date = phase.project_id.date_start if not start_date: - start_date = datetime.now().strftime("%Y-%m-%d") + start_date = datetime.now().strftime("%Y-%m-%d") resources = resources_list.get(phase.id, []) calendar_id = phase.project_id.resource_calendar_id.id - task_ids = map(lambda x : x.id, (filter(lambda x : x.state in ['open', 'draft', 'pending'] , phase.task_ids))) + task_ids = map(lambda x : x.id, (filter(lambda x : x.state in ['draft'] , phase.task_ids))) #reassign only task not yet started if task_ids: task_pool.generate_schedule(cr, uid, task_ids, resources, calendar_id, start_date, context=context) @@ -391,14 +338,23 @@ class project_resource_allocation(osv.osv): _name = 'project.resource.allocation' _description = 'Project Resource Allocation' _rec_name = 'resource_id' + + def get_name(self, cr, uid, ids, field_name, arg, context=None): + res = {} + for allocation in self.browse(cr, uid, ids, context=context): + name = allocation.phase_id.name + name += ' (%s%%)' %(allocation.useability) + res[allocation.id] = name + return res _columns = { + 'name': fields.function(get_name, method=True, type='char', size=256), 'resource_id': fields.many2one('resource.resource', 'Resource', required=True), 'phase_id': fields.many2one('project.phase', 'Project Phase', ondelete='cascade', required=True), 'project_id': fields.related('phase_id', 'project_id', type='many2one', relation="project.project", string='Project', store=True), 'user_id': fields.related('resource_id', 'user_id', type='many2one', relation="res.users", string='User'), 'date_start': fields.date('Start Date', help="Starting Date"), 'date_end': fields.date('End Date', help="Ending Date"), - 'useability': fields.float('Usability', help="Usability of this resource for this project phase in percentage (=50%)"), + 'useability': fields.float('Availability', help="Availability of this resource for this project phase in percentage (=50%)"), } _defaults = { 'useability': 100, @@ -418,8 +374,6 @@ class project(osv.osv): """ res = {} resource_pool = self.pool.get('resource.resource') - if context is None: - context = {} for project in self.browse(cr, uid, ids, context=context): user_ids = map(lambda x:x.id, project.members) calendar_id = project.resource_calendar_id and project.resource_calendar_id.id or False @@ -431,8 +385,6 @@ class project(osv.osv): """ Schedule the phases. """ - if context is None: - context = {} if type(ids) in (long, int,): ids = [ids] phase_pool = self.pool.get('project.phase') @@ -441,12 +393,9 @@ class project(osv.osv): ('state', 'in', ['draft', 'open', 'pending']), ('previous_phase_ids', '=', False) ]) - start_date = project.date_start - if not start_date: - start_date = datetime.now().strftime("%Y-%m-%d") - start_dt = datetime.strftime((datetime.strptime(start_date, "%Y-%m-%d")), "%Y-%m-%d %H:%M") calendar_id = project.resource_calendar_id and project.resource_calendar_id.id or False - phase_pool.generate_schedule(cr, uid, phase_ids, start_dt, calendar_id, context=context) + start_date = False + phase_pool.generate_schedule(cr, uid, phase_ids, start_date, calendar_id, context=context) return True def schedule_tasks(self, cr, uid, ids, context=None): @@ -458,21 +407,18 @@ class project(osv.osv): user_pool = self.pool.get('res.users') task_pool = self.pool.get('project.task') resource_pool = self.pool.get('resource.resource') - if context is None: - context = {} - resources_list = self.generate_members(cr, uid, ids, context=context) return_msg = {} for project in self.browse(cr, uid, ids, context=context): start_date = project.date_start if not start_date: - start_date = datetime.now().strftime("%Y-%m-%d") + start_date = datetime.now().strftime("%Y-%m-%d") resources = resources_list.get(project.id, []) calendar_id = project.resource_calendar_id.id task_ids = task_pool.search(cr, uid, [('project_id', '=', project.id), ('state', 'in', ['draft', 'open', 'pending']) ]) - + if task_ids: task_pool.generate_schedule(cr, uid, task_ids, resources, calendar_id, start_date, context=context) @@ -483,7 +429,7 @@ class project(osv.osv): else: return_msg["warning"] = return_msg["warning"] + "\n" + warning_msg - return return_msg + return return_msg project() @@ -494,7 +440,7 @@ class resource_resource(osv.osv): context = {} if context.get('project_id',False): project_pool = self.pool.get('project.project') - project_rec = project_pool.browse(cr, uid, context['project_id']) + project_rec = project_pool.browse(cr, uid, context['project_id'], context=context) user_ids = [user_id.id for user_id in project_rec.members] args.append(('user_id','in',user_ids)) return super(resource_resource, self).search(cr, uid, args, offset, limit, order, context, count) @@ -506,11 +452,12 @@ class project_task(osv.osv): _columns = { 'phase_id': fields.many2one('project.phase', 'Project Phase'), } - + def generate_schedule(self, cr, uid, ids, resources, calendar_id, start_date, context=None): """ Schedule the tasks according to resource available and priority. """ + resource_pool = self.pool.get('resource.resource') if not ids: return False if context is None: @@ -539,11 +486,14 @@ class project_task(osv.osv): try: resource = reduce(operator.or_, resources) except: - raise osv.except_osv(_('Error'), _('Should have Resources Allocation or Project Members!')) + raise osv.except_osv(_('Error'), _('Resources should be allocated to your phases and Members should be assigned to your Project!')) minimum_time_unit = 1 - if calendar_id: # If project has working calendar - working_days = resource_pool.compute_working_calendar(cr, uid, calendar_id, context=context) + working_hours_per_day = 24 + vacation = [] + if calendar_id: + working_hours_per_day = 8 #TODO: it should be come from calendars vacation = tuple(resource_pool.compute_vacation(cr, uid, calendar_id, context=context)) + working_days = resource_pool.compute_working_calendar(cr, uid, calendar_id, context=context) # Dynamic creation of tasks task_number = 0 for openobect_task in self.browse(cr, uid, ids, context=context): @@ -556,10 +506,10 @@ class project_task(osv.osv): if task_resource.__name__ == task_resource: real_resource = task_resource break - + task = create_tasks(task_number, hours, priorty, real_resource) task_number += 1 - + face_projects = Task.BalancedProject(Project) loop_no = 0 |
