python 2.7 - How to autofill an field value for other model in Odoo? -
this re-work on mro module available link - https://www.odoo.com/apps/modules/8.0/mro/
following sequence of workflow
1) maintenance request created , and after confirming proceeded maintenance order
2) when maintenance order processed , reaches state 'done', maintenance request document origin of order reach state 'done'.
there 2 classes
mro.request
class mro_request(osv.osv): """ maintenance requests """ _name = 'mro.request' _description = 'maintenance request' _inherit = ['mail.thread', 'ir.needaction_mixin'] state_selection = [ ('draft', 'draft'), ('claim', 'claim'), ('run', 'execution'), ('done', 'done'), ('reject', 'rejected'), ('cancel', 'canceled') ] _track = { 'state': { 'mro.mt_request_sent': lambda self, cr, uid, obj, ctx=none: obj['state'] == 'claim', 'mro.mt_request_confirmed': lambda self, cr, uid, obj, ctx=none: obj['state'] == 'run', 'mro.mt_request_rejected': lambda self, cr, uid, obj, ctx=none: obj['state'] == 'reject', }, } _columns = { 'name': fields.char('reference', size=64), 'state': fields.selection(state_selection, 'status', readonly=true, help="when maintenance request created status set 'draft'.\n\ if request sent status set 'claim'.\n\ if request confirmed status set 'execution'.\n\ if request rejected status set 'rejected'.\n\ when maintenance over, status set 'done'."), 'asset_id': fields.many2one('asset.asset', 'asset', required=true, readonly=true, states={'draft': [('readonly', false)]}), 'cause': fields.char('cause', size=64, translate=true, required=true, readonly=true, states={'draft': [('readonly', false)]}), 'description': fields.text('description', readonly=true, states={'draft': [('readonly', false)]}), 'reject_reason': fields.text('reject reason', readonly=true), 'requested_date': fields.datetime('requested date', required=true, select=1, readonly=true, states={'draft': [('readonly', false)]}, help="date requested customer maintenance."), 'execution_date': fields.datetime('execution date', required=true, select=1, readonly=true, states={'draft':[('readonly',false)],'claim':[('readonly',false)]}), 'breakdown': fields.boolean('breakdown', readonly=true, states={'draft': [('readonly', false)]}), 'create_uid': fields.many2one('res.users', 'responsible'), } _defaults = { 'state': 'draft', 'requested_date': lambda *a: time.strftime('%y-%m-%d %h:%m:%s'), 'execution_date': lambda *a: time.strftime('%y-%m-%d %h:%m:%s'), 'breakdown': false, }
and there mro.order class
class mro_order(osv.osv): """ maintenance orders """ _name = 'mro.order' _description = 'maintenance order' _inherit = ['mail.thread', 'ir.needaction_mixin'] state_selection = [ ('draft', 'draft'), ('released', 'waiting parts'), ('ready', 'ready maintenance'), ('done', 'done'), ('cancel', 'canceled') ] maintenance_type_selection = [ ('bm', 'breakdown'), ('cm', 'corrective') ] _track = { 'state': { 'mro.mt_order_confirmed': lambda self, cr, uid, obj, ctx=none: obj['state'] == 'ready', }, } _columns = { 'name': fields.char('reference', size=64), 'origin': fields.char('source document', size=64, readonly=true, states={'draft': [('readonly', false)]}, help="reference of document generated maintenance order."), 'state': fields.selection(state_selection, 'status', readonly=true, help="when maintenance order created status set 'draft'.\n\ if order confirmed status set 'waiting parts'.\n\ if stock available status set 'ready maintenance'.\n\ when maintenance over, status set 'done'."), 'maintenance_type': fields.selection(maintenance_type_selection, 'maintenance type', required=true, readonly=true, states={'draft': [('readonly', false)]}), 'task_id': fields.many2one('mro.task', 'task', readonly=true, states={'draft': [('readonly', false)]}), 'description': fields.char('description', size=64, translate=true, required=true, readonly=true, states={'draft': [('readonly', false)]}), 'asset_id': fields.many2one('asset.asset', 'asset', required=true, readonly=true, states={'draft': [('readonly', false)]}), 'date_planned': fields.datetime('planned date', required=true, select=1, readonly=true, states={'draft':[('readonly',false)]}), 'date_scheduled': fields.datetime('scheduled date', required=true, select=1, readonly=true, states={'draft':[('readonly',false)],'released':[('readonly',false)],'ready':[('readonly',false)]}), 'date_execution': fields.datetime('execution date', required=true, select=1, readonly=true, states={'draft':[('readonly',false)],'released':[('readonly',false)],'ready':[('readonly',false)]}), 'parts_lines': fields.one2many('mro.order.parts.line', 'maintenance_id', 'planned parts', readonly=true, states={'draft':[('readonly',false)]}), 'parts_ready_lines': fields.function(_get_available_parts, relation="stock.move", method=true, type="one2many", multi='parts'), 'parts_move_lines': fields.function(_get_available_parts, relation="stock.move", method=true, type="one2many", multi='parts'), 'parts_moved_lines': fields.function(_get_available_parts, relation="stock.move", method=true, type="one2many", multi='parts'), 'tools_description': fields.text('tools description',translate=true), 'labor_description': fields.text('labor description',translate=true), 'operations_description': fields.text('operations description',translate=true), 'documentation_description': fields.text('documentation description',translate=true), 'problem_description': fields.text('problem description'), 'company_id': fields.many2one('res.company','company',required=true, readonly=true, states={'draft':[('readonly',false)]}), 'procurement_group_id': fields.many2one('procurement.group', 'procurement group', copy=false), } _defaults = { 'state': lambda *a: 'draft', 'maintenance_type': lambda *a: 'bm', 'date_planned': lambda *a: time.strftime('%y-%m-%d %h:%m:%s'), 'date_scheduled': lambda *a: time.strftime('%y-%m-%d %h:%m:%s'), 'date_execution': lambda *a: time.strftime('%y-%m-%d %h:%m:%s'), 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'mro.order', context=c), }
and there button "done" complete flow of maintenance order
def action_done(self, cr, uid, ids, context=none): order in self.browse(cr, uid, ids, context=context): self.pool.get('stock.move').action_done(cr, uid, [x.id x in order.parts_move_lines]) self.write(cr, uid, ids, {'state': 'done', 'date_execution': time.strftime('%y-%m-%d %h:%m:%s')}) return true
i wanted such when "done" button clicked, value of "date_execution" field value of mro.order updated "execution date" field of mro.request
for scenario, trying following code:
def action_done(self, cr, uid, ids, context=none): sobj = self.pool.get('mro.request') sobj1 = self.browse(cr, uid, ids) order in self.browse(cr, uid, ids, context=context): ref_date = sobj1.date_execution self.pool.get('stock.move').action_done(cr, uid, [x.id x in order.parts_move_lines]) sobj2 = sobj.browse(cr, uid, ids, [('name','=','sobj1.origin')]) sobj2.write(cr, uid, ids, {'execution_date':'ref_date'}) self.write(cr, uid, ids, {'state': 'done', 'date_execution': time.strftime('%y-%m-%d %h:%m:%s')}) return true
well, till not able succeed. suggestions helpful. !!!
Comments
Post a Comment