I created this method that adds a new situation to the situation list. The situation has a many-to-one relation with project.task. This method seems a little longer than it should be and I don't know how to optimize it:
@api.multi
def create_situation(self):
active_id = self.env.context.get('active_ids', []) or []
proj_obj = self.env['project.task'].browse(active_id)
situation_orders = self.env['project.task.situation'].browse([])
task_lines = self.env['task.situation.line'].browse([])
situation_ids = self.env['project.task.situation'].search([('task_id', '=', proj_obj.id)], order='id desc', limit=1)
size_list_situation_ids = len(situation_ids)
last_id = situation_ids and max(situation_ids)
s_sequence = "Situation N° " + str(proj_obj.sequence)
for record in proj_obj:
task_lines = [(0, 0, {'ref': line.ref,
'product_id': line.product_id.id,
'quantity': line.quantity,
'um_id': line.um_id,
'price_unit': line.price_unit,
'price_subtotal': line.price_subtotal,
'progress': line.progress,
'cumulative_amount_ht': line.cumulative_amount_ht,
'previous_amount_ht':line1.cumulative_amount_ht if size_list_situation_ids != 0 else 0,
'amount_net_ht': line.cumulative_amount_ht if size_list_situation_ids == 0 else (line.cumulative_amount_ht - line1.cumulative_amount_ht)}) for line, line1 in izip_longest(record.task_line_ids, last_id.task_line)]
situation_order = {'partner_id': proj_obj.project_id.partner_id.id,
'task_id':proj_obj.id,
'task_line':task_lines,
'user_id': proj_obj.user_id.id,
'name':s_sequence,
'tyoe': 'situation' if self._context.get('type', 'situation') else 'final_count',
'project_id':proj_obj.project_id.id,
'due_date': self.date_id.id}
situation_orders+=situation_orders.with_context(self._context).create(situation_order)
proj_obj.write({'sequence':proj_obj.sequence+1})