Hello,
I'm using Odoo 13 Community Edition.
In my case I have very much workorders that needed to be started at the same time (Like 50 records), so to make it easier for me I tried to make an action like the production_order plan action to plan multiple production orders.
So I copied the exisisting action and tried to use it for multiple records with some changes, but when i use it, it throws no error but it only start one workorder, so whats wrong or is there a better solution?
from datetime import datetime, timedelta
from odoo import api, fields, models, _, SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.tools import float_compare, float_round
class MrpWorkorder(models.Model):
_inherit = ['mrp.workorder']
def button_start_mycase(self):
orders_to_plan = self.filtered(lambda order: order.state == 'ready')
for order in orders_to_plan:
# Need a loss in case of the real time exceeding the expected
timeline = self.env['mrp.workcenter.productivity']
if order.duration < order.duration_expected:
loss_id = self.env['mrp.workcenter.productivity.loss'].search([('loss_type','=','productive')], limit=1)
if not len(loss_id):
raise UserError(_("You need to define at least one productivity loss in the category 'Productivity'. Create one from the Manufacturing app, menu: Configuration / Productivity Losses."))
else:
loss_id = self.env['mrp.workcenter.productivity.loss'].search([('loss_type','=','performance')], limit=1)
if not len(loss_id):
raise UserError(_("You need to define at least one productivity loss in the category 'Performance'. Create one from the Manufacturing app, menu: Configuration / Productivity Losses."))
if order.production_id.state != 'progress':
order.production_id.write({
'date_start': datetime.now(),
})
timeline.create({
'workorder_id': order.id,
'workcenter_id': order.workcenter_id.id,
'description': _('Time Tracking: ')+self.env.user.name,
'loss_id': loss_id[0].id,
'date_start': datetime.now(),
'user_id': self.env.user.id, # FIXME sle: can be inconsistent with company_id
'company_id': order.company_id.id,
})
if order.state == 'progress':
return True
else:
start_date = datetime.now()
vals = {
'state': 'progress',
'date_start': start_date,
'date_planned_start': start_date,
}
if order.date_planned_finished < start_date:
vals['date_planned_finished'] = start_date
return order.write(vals)
<record id="workorder_server_action_start_mycase" model="ir.actions.server">
<field name="name">Starte Arbeitsaufträge</field>
<field name="model_id" ref="mrp.model_mrp_workorder"/>
<field name="binding_model_id" ref="mrp.model_mrp_workorder"/>
<field name="binding_view_types">list</field>
<field name="groups_id" eval="[(4, ref('mrp.group_mrp_manager'))]"/>
<field name="state">code</field>
<field name="code">records.button_start_mycase()</field>
</record>