diff --git a/addons/mrp/mrp.py b/addons/mrp/mrp.py index fcaac08b70d..e9e099b6df5 100644 --- a/addons/mrp/mrp.py +++ b/addons/mrp/mrp.py @@ -375,6 +375,13 @@ class mrp_bom(osv.osv): default.update(name=_("%s (copy)") % (bom_data['name']), bom_id=False) return super(mrp_bom, self).copy_data(cr, uid, id, default, context=context) + def unlink(self, cr, uid, ids, context=None): + if self.pool['mrp.production'].search(cr, uid, [ + ('bom_id', 'in', ids), ('state', 'not in', ['done', 'cancel']) + ], context=context): + raise osv.except_osv(_('Warning!'), _('You can not delete a Bill of Material with running manufacturing orders.\nPlease close or cancel it first.')) + return super(mrp_bom, self).unlink(cr, uid, ids, context=context) + def rounding(f, r): # TODO for trunk: log deprecation warning diff --git a/addons/project_timesheet/project_timesheet.py b/addons/project_timesheet/project_timesheet.py index 31a396369e2..3c3b7febfb3 100644 --- a/addons/project_timesheet/project_timesheet.py +++ b/addons/project_timesheet/project_timesheet.py @@ -103,49 +103,57 @@ class project_work(osv.osv): res['product_uom_id'] = emp.product_id.uom_id.id return res - def create(self, cr, uid, vals, *args, **kwargs): - timesheet_obj = self.pool.get('hr.analytic.timesheet') - task_obj = self.pool.get('project.task') - uom_obj = self.pool.get('product.uom') + def _create_analytic_entries(self, cr, uid, vals, context): + """Create the hr analytic timesheet from project task work""" + timesheet_obj = self.pool['hr.analytic.timesheet'] + task_obj = self.pool['project.task'] vals_line = {} + timeline_id = False + acc_id = False + + task_obj = task_obj.browse(cr, uid, vals['task_id'], context=context) + result = self.get_user_related_details(cr, uid, vals.get('user_id', uid)) + vals_line['name'] = '%s: %s' % (tools.ustr(task_obj.name), tools.ustr(vals['name'] or '/')) + vals_line['user_id'] = vals['user_id'] + vals_line['product_id'] = result['product_id'] + if vals.get('date'): + vals_line['date' ] = vals['date'][:10] + + # Calculate quantity based on employee's product's uom + vals_line['unit_amount'] = vals['hours'] + + default_uom = self.pool['res.users'].browse(cr, uid, uid, context=context).company_id.project_time_mode_id.id + if result['product_uom_id'] != default_uom: + vals_line['unit_amount'] = self.pool['product.uom']._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id']) + acc_id = task_obj.project_id and task_obj.project_id.analytic_account_id.id or acc_id + if acc_id: + vals_line['account_id'] = acc_id + res = timesheet_obj.on_change_account_id(cr, uid, False, acc_id) + if res.get('value'): + vals_line.update(res['value']) + vals_line['general_account_id'] = result['general_account_id'] + vals_line['journal_id'] = result['journal_id'] + vals_line['amount'] = 0.0 + vals_line['product_uom_id'] = result['product_uom_id'] + amount = vals_line['unit_amount'] + prod_id = vals_line['product_id'] + unit = False + timeline_id = timesheet_obj.create(cr, uid, vals=vals_line, context=context) + + # Compute based on pricetype + amount_unit = timesheet_obj.on_change_unit_amount(cr, uid, timeline_id, + prod_id, amount, False, unit, vals_line['journal_id'], context=context) + if amount_unit and 'amount' in amount_unit.get('value',{}): + updv = { 'amount': amount_unit['value']['amount'] } + timesheet_obj.write(cr, uid, [timeline_id], updv, context=context) + + return timeline_id + + def create(self, cr, uid, vals, *args, **kwargs): context = kwargs.get('context', {}) if not context.get('no_analytic_entry',False): - task_obj = task_obj.browse(cr, uid, vals['task_id']) - result = self.get_user_related_details(cr, uid, vals.get('user_id', uid)) - vals_line['name'] = '%s: %s' % (tools.ustr(task_obj.name), tools.ustr(vals['name'] or '/')) - vals_line['user_id'] = vals['user_id'] - vals_line['product_id'] = result['product_id'] - vals_line['date'] = vals['date'][:10] - - # Calculate quantity based on employee's product's uom - vals_line['unit_amount'] = vals['hours'] - - default_uom = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id - if result['product_uom_id'] != default_uom: - vals_line['unit_amount'] = uom_obj._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id']) - acc_id = task_obj.project_id and task_obj.project_id.analytic_account_id.id or False - if acc_id: - vals_line['account_id'] = acc_id - res = timesheet_obj.on_change_account_id(cr, uid, False, acc_id) - if res.get('value'): - vals_line.update(res['value']) - vals_line['general_account_id'] = result['general_account_id'] - vals_line['journal_id'] = result['journal_id'] - vals_line['amount'] = 0.0 - vals_line['product_uom_id'] = result['product_uom_id'] - amount = vals_line['unit_amount'] - prod_id = vals_line['product_id'] - unit = False - timeline_id = timesheet_obj.create(cr, uid, vals=vals_line, context=context) - - # Compute based on pricetype - amount_unit = timesheet_obj.on_change_unit_amount(cr, uid, timeline_id, - prod_id, amount, False, unit, vals_line['journal_id'], context=context) - if amount_unit and 'amount' in amount_unit.get('value',{}): - updv = { 'amount': amount_unit['value']['amount'] } - timesheet_obj.write(cr, uid, [timeline_id], updv, context=context) - vals['hr_analytic_timesheet_id'] = timeline_id + vals['hr_analytic_timesheet_id'] = self._create_analytic_entries(cr, uid, vals, context=context) return super(project_work,self).create(cr, uid, vals, *args, **kwargs) def write(self, cr, uid, ids, vals, context=None): @@ -233,6 +241,10 @@ class task(osv.osv): def write(self, cr, uid, ids, vals, context=None): if context is None: context = {} + task_work_obj = self.pool['project.task.work'] + acc_id = False + missing_analytic_entries = {} + if vals.get('project_id',False) or vals.get('name',False): vals_line = {} hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet') @@ -244,6 +256,16 @@ class task(osv.osv): if len(task_obj.work_ids): for task_work in task_obj.work_ids: if not task_work.hr_analytic_timesheet_id: + if acc_id : + # missing timesheet activities to generate + missing_analytic_entries[task_work.id] = { + 'name' : task_work.name, + 'user_id' : task_work.user_id.id, + 'date' : task_work.date and task_work.date[:10] or False, + 'account_id': acc_id, + 'hours' : task_work.hours, + 'task_id' : task_obj.id + } continue line_id = task_work.hr_analytic_timesheet_id.id if vals.get('project_id',False): @@ -251,7 +273,14 @@ class task(osv.osv): if vals.get('name',False): vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/') hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {}) - return super(task,self).write(cr, uid, ids, vals, context) + + res = super(task,self).write(cr, uid, ids, vals, context) + + for task_work_id, analytic_entry in missing_analytic_entries.items(): + timeline_id = task_work_obj._create_analytic_entries(cr, uid, analytic_entry, context=context) + task_work_obj.write(cr, uid, task_work_id, {'hr_analytic_timesheet_id' : timeline_id}, context=context) + + return res class res_partner(osv.osv): diff --git a/addons/stock/wizard/stock_partial_move.py b/addons/stock/wizard/stock_partial_move.py index e1956e202c9..baba6e7792b 100644 --- a/addons/stock/wizard/stock_partial_move.py +++ b/addons/stock/wizard/stock_partial_move.py @@ -53,7 +53,7 @@ class stock_partial_move(osv.osv_memory): return res if 'move_ids' in fields: move_ids = self.pool.get('stock.move').browse(cr, uid, move_ids, context=context) - moves = [self._partial_move_for(cr, uid, m) for m in move_ids if m.state not in ('done','cancel')] + moves = [self._partial_move_for(cr, uid, m, context=context) for m in move_ids if m.state not in ('done','cancel')] res.update(move_ids=moves) if 'date' in fields: res.update(date=time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)) diff --git a/addons/stock/wizard/stock_partial_picking.py b/addons/stock/wizard/stock_partial_picking.py index 5419d2dda2a..b67c126e775 100644 --- a/addons/stock/wizard/stock_partial_picking.py +++ b/addons/stock/wizard/stock_partial_picking.py @@ -130,7 +130,7 @@ class stock_partial_picking(osv.osv_memory): res.update(picking_id=picking_id) if 'move_ids' in fields: picking = self.pool.get('stock.picking').browse(cr, uid, picking_id, context=context) - moves = [self._partial_move_for(cr, uid, m) for m in picking.move_lines if m.state not in ('done','cancel')] + moves = [self._partial_move_for(cr, uid, m, context=context) for m in picking.move_lines if m.state not in ('done','cancel')] res.update(move_ids=moves) if 'date' in fields: res.update(date=time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)) @@ -154,7 +154,7 @@ class stock_partial_picking(osv.osv_memory): return {'cost': move.product_id.standard_price, 'currency': product_currency_id or picking_currency_id or False} - def _partial_move_for(self, cr, uid, move): + def _partial_move_for(self, cr, uid, move, context=None): partial_move = { 'product_id' : move.product_id.id, 'quantity' : move.product_qty if move.state == 'assigned' or move.picking_id.type == 'in' else 0,