From ae74ebb5ce74b9f62fe1f5222b82983780db2d47 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 16 Jun 2016 11:24:13 +0200 Subject: [PATCH] [FIX] purchase: `related_usage` must be readonly The `related_usage` field of `purchase.order` is a related field to the location `usage` field, which is defined in order to display/hide some other field in the PO according to the location usage This is purely a technical field, which is set only to change the form view according to the location usage. This is not expected to change the location usage through this field, this field must therefore be set to readonly. A change on the location usage could happen because of the `onchange_picking_type_id`: If the user changed the picking type, and then the location, the usage returned by the `onchange_picking_type_id` was applied on the location, and this must not have happened. In addition, since `related_usage` is a related to the location usage, it should be changed as well when the location is changed. (in the new api, this is no longer required, the related field is updated automatically). This revision therefore adds an onchange on the location as well, to handle this. opw-676428 --- addons/purchase/purchase.py | 9 ++++++++- addons/purchase/purchase_view.xml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 5d453686f97..253e8e52fc1 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -316,7 +316,7 @@ class purchase_order(osv.osv): 'picking_type_id': fields.many2one('stock.picking.type', 'Deliver To', help="This will determine picking type of incoming shipment", required=True, states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)], 'done': [('readonly', True)]}), 'related_location_id': fields.related('picking_type_id', 'default_location_dest_id', type='many2one', relation='stock.location', string="Related location", store=True), - 'related_usage': fields.related('location_id', 'usage', type='char'), + 'related_usage': fields.related('location_id', 'usage', type='char', readonly=True), 'shipment_count': fields.function(_count_all, type='integer', string='Incoming Shipments', multi=True), 'invoice_count': fields.function(_count_all, type='integer', string='Invoices', multi=True) } @@ -421,6 +421,13 @@ class purchase_order(osv.osv): value.update({'related_location_id': picktype.default_location_dest_id.id}) return {'value': value} + def onchange_location_id(self, cr, uid, ids, location_id, context=None): + value = {'related_usage': False} + if location_id: + value['related_usage'] = self.pool['stock.location'].browse(cr, uid, location_id, context=context).usage + return {'value': value} + + def onchange_partner_id(self, cr, uid, ids, partner_id, context=None): partner = self.pool.get('res.partner') if not partner_id: diff --git a/addons/purchase/purchase_view.xml b/addons/purchase/purchase_view.xml index 2b22a23890a..cf55d63ae7c 100644 --- a/addons/purchase/purchase_view.xml +++ b/addons/purchase/purchase_view.xml @@ -274,7 +274,7 @@ - +