2010-01-29 07:51:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import wizard
|
|
|
|
import pooler
|
|
|
|
from tools.translate import _
|
|
|
|
import datetime
|
|
|
|
from resource.faces import *
|
2010-02-10 06:14:22 +00:00
|
|
|
from new import classobj
|
2010-03-02 13:27:02 +00:00
|
|
|
import project_resource as proj
|
2010-01-29 07:51:40 +00:00
|
|
|
|
|
|
|
compute_form = """<?xml version="1.0" ?>
|
2010-02-10 06:14:22 +00:00
|
|
|
<form string="Compute Scheduling of Phases">
|
2010-01-29 07:51:40 +00:00
|
|
|
|
2010-02-26 11:18:14 +00:00
|
|
|
<field name="project_id" colspan="4"/>
|
2010-01-29 07:51:40 +00:00
|
|
|
|
|
|
|
</form>"""
|
|
|
|
|
|
|
|
compute_fields = {
|
2010-02-26 11:18:14 +00:00
|
|
|
'project_id': {'string':'Project', 'type':'many2one', 'relation': 'project.project'},
|
2010-01-29 07:51:40 +00:00
|
|
|
|
|
|
|
}
|
2010-02-10 06:14:22 +00:00
|
|
|
|
|
|
|
success_msg = """<?xml version="1.0" ?>
|
|
|
|
<form string="Compute Scheduling of Phases">
|
|
|
|
<label string="Phase Scheduling completed successfully."/>
|
|
|
|
</form>"""
|
|
|
|
|
2010-03-02 13:27:02 +00:00
|
|
|
def phase_schedule(cr, uid, phase, start_date, calendar_id=False):
|
2010-02-26 11:18:14 +00:00
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
phase_pool = pool.get('project.phase')
|
|
|
|
resource_pool = pool.get('resource.resource')
|
|
|
|
uom_pool = pool.get('product.uom')
|
|
|
|
wktime_cal = []
|
|
|
|
resource_cal = False
|
|
|
|
phase_resource = False
|
|
|
|
if phase:
|
2010-03-02 13:27:02 +00:00
|
|
|
resource_id = resource_pool.search(cr, uid, [('user_id','=',phase.responsible_id.id)])
|
2010-02-26 11:18:14 +00:00
|
|
|
if resource_id:
|
2010-03-02 13:27:02 +00:00
|
|
|
resource_obj = resource_pool.browse(cr, uid, resource_id)[0]
|
|
|
|
leaves = proj.leaves_resource(cr, uid, calendar_id or False , resource_id, resource_obj.calendar_id.id)
|
|
|
|
phase_resource = classobj(str(resource_obj.name), (Resource,), {'__doc__' : resource_obj.name, '__name__' : resource_obj.name, 'vacation' : tuple(leaves), 'efficiency' : resource_obj.time_efficiency})
|
|
|
|
default_uom_id = uom_pool.search(cr, uid, [('name','=','Hour')])[0]
|
|
|
|
avg_hours = uom_pool._compute_qty(cr, uid, phase.product_uom.id, phase.duration, default_uom_id)
|
2010-02-26 11:18:14 +00:00
|
|
|
duration = str(avg_hours) + 'H'
|
|
|
|
|
|
|
|
# Creating a new project for each phase
|
|
|
|
def Project():
|
|
|
|
start = start_date
|
|
|
|
minimum_time_unit = 1
|
|
|
|
resource = phase_resource
|
|
|
|
# If project has working calendar else the default one would be considered
|
|
|
|
if calendar_id:
|
2010-03-02 13:27:02 +00:00
|
|
|
working_days = proj.compute_working_calendar(cr, uid, calendar_id)
|
|
|
|
vacation = tuple(proj.leaves_resource(cr, uid, calendar_id))
|
2010-02-26 11:18:14 +00:00
|
|
|
|
|
|
|
def phase():
|
|
|
|
effort = duration
|
|
|
|
|
|
|
|
project = BalancedProject(Project)
|
|
|
|
s_date = project.phase.start.to_datetime()
|
|
|
|
e_date = project.phase.end.to_datetime()
|
|
|
|
# According to constraints on date start and date end on phase recalculation done
|
|
|
|
if phase.constraint_date_start and str(s_date) < phase.constraint_date_start:
|
2010-03-02 13:27:02 +00:00
|
|
|
start_date = datetime.datetime.strptime(phase.constraint_date_start, '%Y-%m-%d %H:%M:%S')
|
2010-02-26 11:18:14 +00:00
|
|
|
else:
|
|
|
|
start_date = s_date
|
|
|
|
if phase.constraint_date_end and str(e_date) > phase.constraint_date_end:
|
2010-03-02 13:27:02 +00:00
|
|
|
end_date= datetime.datetime.strptime(phase.constraint_date_end, '%Y-%m-%d %H:%M:%S')
|
2010-02-26 11:18:14 +00:00
|
|
|
date_start = phase.constraint_date_end[:-3]
|
|
|
|
else:
|
|
|
|
end_date = e_date
|
|
|
|
date_start = end_date
|
2010-02-10 06:14:22 +00:00
|
|
|
|
2010-02-26 11:18:14 +00:00
|
|
|
# Writing the dates back
|
2010-03-02 13:27:02 +00:00
|
|
|
phase_pool.write(cr, uid, [phase.id], {'date_start' :start_date.strftime('%Y-%m-%d %H:%M:%S'), 'date_end' :end_date.strftime('%Y-%m-%d %H:%M:%S')}, context={'scheduler' :True})
|
2010-02-10 06:14:22 +00:00
|
|
|
|
2010-02-26 11:18:14 +00:00
|
|
|
# Recursive calling the next phases till all the phases are scheduled
|
|
|
|
for phase in phase.next_phase_ids:
|
|
|
|
if phase.state in ['draft','open','pending']:
|
2010-03-02 13:27:02 +00:00
|
|
|
phase_schedule(cr, uid, phase, date_start, phase.project_id.resource_calendar_id.id or False)
|
2010-02-26 11:18:14 +00:00
|
|
|
else:
|
|
|
|
continue
|
2010-02-10 06:14:22 +00:00
|
|
|
|
2010-02-26 11:18:14 +00:00
|
|
|
#
|
2010-01-29 07:51:40 +00:00
|
|
|
class wizard_compute_phases(wizard.interface):
|
|
|
|
|
|
|
|
def _compute_date(self, cr, uid, data, context):
|
2010-02-10 06:14:22 +00:00
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
project_pool = pool.get('project.project')
|
|
|
|
phase_pool = pool.get('project.phase')
|
2010-02-26 11:18:14 +00:00
|
|
|
|
|
|
|
# if project mentioned
|
|
|
|
if data['form']['project_id']:
|
2010-03-02 13:27:02 +00:00
|
|
|
project_id = project_pool.browse(cr, uid, data['form']['project_id'])
|
|
|
|
phase_ids = phase_pool.search(cr, uid, [('project_id','=',project_id.id), ('state','in',['draft','open','pending']), ('previous_phase_ids','=',False)])
|
2010-02-26 11:18:14 +00:00
|
|
|
|
|
|
|
# else all the draft,open,pending states phases taken
|
2010-02-10 06:14:22 +00:00
|
|
|
else:
|
2010-03-02 13:27:02 +00:00
|
|
|
phase_ids = phase_pool.search(cr, uid,[('state','in',['draft','open','pending']), ('previous_phase_ids','=',False)])
|
2010-02-26 11:18:14 +00:00
|
|
|
|
|
|
|
phase_ids.sort()
|
2010-03-02 13:27:02 +00:00
|
|
|
phase_objs = phase_pool.browse(cr, uid, phase_ids)
|
2010-02-26 11:18:14 +00:00
|
|
|
for phase in phase_objs:
|
2010-02-26 12:26:17 +00:00
|
|
|
start_date = phase.project_id.date_start
|
|
|
|
if not phase.project_id.date_start:
|
|
|
|
start_date = datetime.datetime.now().strftime("%Y-%m-%d")
|
2010-03-02 13:27:02 +00:00
|
|
|
start_dt = datetime.datetime.strftime((datetime.datetime.strptime(start_date, "%Y-%m-%d")), "%Y-%m-%d %H:%M")
|
2010-02-26 11:18:14 +00:00
|
|
|
calendar_id = phase.project_id.resource_calendar_id.id
|
2010-03-02 13:27:02 +00:00
|
|
|
phase_schedule(cr, uid, phase, start_dt, calendar_id or False)
|
2010-01-29 07:51:40 +00:00
|
|
|
return {}
|
2010-02-26 11:18:14 +00:00
|
|
|
|
2010-01-29 07:51:40 +00:00
|
|
|
states = {
|
|
|
|
'init': {
|
|
|
|
'actions': [],
|
|
|
|
'result': {'type':'form', 'arch':compute_form, 'fields':compute_fields, 'state':[
|
|
|
|
('end', 'Cancel'),
|
2010-02-10 06:14:22 +00:00
|
|
|
('compute', 'Compute')
|
2010-01-29 07:51:40 +00:00
|
|
|
|
|
|
|
]},
|
|
|
|
},
|
2010-02-10 06:14:22 +00:00
|
|
|
'compute': {
|
2010-01-29 07:51:40 +00:00
|
|
|
'actions': [_compute_date],
|
2010-02-10 06:14:22 +00:00
|
|
|
'result': {'type':'form','arch':success_msg,'fields':{}, 'state':[('end', 'Ok')]},
|
2010-01-29 07:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wizard_compute_phases('wizard.compute.phases')
|
|
|
|
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|