In project.task
I have 2 types of task lines:
class ProjectTask (models.Model):
_inherit = "project.task"
task_line_ids = fields.One2many('project.task.line','task_id',string="Articles")
task_line_rider_ids = fields.One2many('project.task.line','task_rider_id',string="Riders")
class ProjectTaskLine(models.Model):
_name = 'project.task.line'
quantity = fields.Float("Quantity")
price_unit = fields.Float("Unit Price")
description = fields.Text("Description")
product_id = fields.Many2one('product.product', string='Article')
task_id = fields.Many2one('project.task')
um_id = fields.Many2one('product.uom', 'Unit of Measure')
ref = fields.Char('Reference')
progress = fields.Float('Progress %')
state = fields.Selection((('draft','Draft'),('open','In progress'),('done','Done'),('cancel','Cancelled')), related="task_id.state", readonly=True)
price_subtotal = fields.Monetary(compute='_compute_amount', string='Amount H.T', store=True, readonly=True)
currency_id = fields.Many2one('res.currency', string='Currency', store=True)
cumulative_amount_ht = fields.Monetary(compute='_compute_amount_ht',string='Amount Cumulative HT', store=True, readonly=True)
previous_amount_ht = fields.Monetary(compute='_compute_previous_amount_ht', string='Previous Cumulative amount HT', store=True)
amount_net_ht = fields.Monetary(string='Amount Net of the month HT')
task_rider_id = fields.Many2one('project.task')
@api.depends('quantity', 'price_unit')
def _compute_amount(self):
for record in self:
record.update({
'price_subtotal': record.price_unit * record.quantity})
@api.depends('price_subtotal', 'progress')
def _compute_amount_ht(self):
for record in self:
record.update({
'cumulative_amount_ht': ((record.price_subtotal * record.progress) / 100)})
I created a wizard (task.make.situation
) that allows me to have a model containing the current situation of a project.task
(progress field) and I created this method inside of 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([])
situation_line_ids = 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:
situation_line_ids = [(0, 0, {'ref': line.ref,
'product_id': line.product_id.id,
'quantity': line.quantity,
'um_id': line.um_id.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.situation_line)]
situation_order = {'partner_id': proj_obj.project_id.partner_id.id,
'task_id':proj_obj.id,
'situation_line':situation_line_ids,
'user_id': proj_obj.user_id.id,
'name': s_sequence,
'type': '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})
if len(proj_obj.task_line_rider_ids) != 0:
return self.add_riders(proj_obj, last_id, size_list_situation_ids)
@api.multi
def add_riders(self,proj_obj, last_id, size_list_situation_ids):
situation_ids = self. env['project.task.situation'].search([('task_id', '=', proj_obj.id)], order='id desc', limit=1)
just_created = situation_ids and max(situation_ids)
#rider_lines = []
for record in proj_obj:
for line in record.task_line_rider_ids:
rider_lines = { 'product_id': line.product_id.id,
'quantity': line.quantity,
'um_id': line.um_id.id,
'price_unit': line.price_unit,
'price_subtotal': line.price_subtotal,
'progress': line.progress,
'cumulative_amount_ht': line.cumulative_amount_ht}
just_created.update({'situation_line': [0,0, rider_lines]})
The problem is that everytime I call this method it appears this error:
AttributeError: 'NoneType' object has no attribute 'ref'
And when I comment this line: 'ref': line.ref,
the error is pointed on product_id
Is 'situation_line' in 'project.task.situation' a one2many or many2many relation to 'project.task.line'
It's one2many relation with 'task.situation.line' model.