From 03550646e046800d0477b3cd17e28be3b68759d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Fri, 26 Apr 2019 12:11:20 +0200 Subject: [PATCH] [ADD] b_purchase: Supervisor for sale order This adds a field `supervisor_id` that aims to replace the `create_uid` field. To prevent regressions, the old `create_uid` field is set to be a computed field that has the same value as `supervisor_id`. This allow to ensure that other part of the program that refer to `create_uid` as the responsible for a purchase order will get the value of `supervisor_id`. This new version comes with a migration script that fill the new `supervisor_id` with the value of `create_uid` if `supervisor_id` is not set yet. This trick is done because the `create_uid` field is a magic field that belongs to the ORM. This field cannot be modified in a form. --- beesdoo_purchase/__openerp__.py | 16 +++----- .../migrations/9.0.1.1.0/post-migrate.py | 12 ++++++ beesdoo_purchase/models/__init__.py | 4 +- beesdoo_purchase/models/purchase.py | 41 +++++++++++++++++++ beesdoo_purchase/views/purchase_order.xml | 24 ++++++++++- 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 beesdoo_purchase/migrations/9.0.1.1.0/post-migrate.py create mode 100644 beesdoo_purchase/models/purchase.py diff --git a/beesdoo_purchase/__openerp__.py b/beesdoo_purchase/__openerp__.py index c37e007..50908ce 100644 --- a/beesdoo_purchase/__openerp__.py +++ b/beesdoo_purchase/__openerp__.py @@ -9,24 +9,18 @@ Long description of module's purpose """, - 'author': "Beescoop - Cellule IT", + 'author': "Beescoop - Cellule IT, " + "Coop IT Easy SCRLfs", 'website': "https://github.com/beescoop/Obeesdoo", - # Categories can be used to filter modules in modules listing - # Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml - # for the full list 'category': 'Purchase', - 'version': '0.1', + 'version': '9.0.1.1.0', - # any module necessary for this one to work correctly - 'depends': ['base','purchase','beesdoo_product'], + 'depends': ['base', 'purchase', 'beesdoo_product'], - # always loaded 'data': [ 'views/purchase_order.xml', 'security/ir.model.access.csv', 'report/report_purchaseorder.xml', ], - # only loaded in demonstration mode - 'demo': [], -} \ No newline at end of file +} diff --git a/beesdoo_purchase/migrations/9.0.1.1.0/post-migrate.py b/beesdoo_purchase/migrations/9.0.1.1.0/post-migrate.py new file mode 100644 index 0000000..707e4a8 --- /dev/null +++ b/beesdoo_purchase/migrations/9.0.1.1.0/post-migrate.py @@ -0,0 +1,12 @@ +# coding: utf-8 + + +def migrate(cr, version): + """Set the new supervisor_id field if this one is empty.""" + cr.execute( + """ + UPDATE purchase_order + SET supervisor_id = create_uid + WHERE supervisor_id IS NULL + """ + ) diff --git a/beesdoo_purchase/models/__init__.py b/beesdoo_purchase/models/__init__.py index 667b7e3..cacb10c 100644 --- a/beesdoo_purchase/models/__init__.py +++ b/beesdoo_purchase/models/__init__.py @@ -1,3 +1 @@ -# -*- coding: utf-8 -*- - -import purchase_order \ No newline at end of file +from . import purchase, purchase_order diff --git a/beesdoo_purchase/models/purchase.py b/beesdoo_purchase/models/purchase.py new file mode 100644 index 0000000..0832d37 --- /dev/null +++ b/beesdoo_purchase/models/purchase.py @@ -0,0 +1,41 @@ +# coding: utf-8 + +from openerp import api, fields, models + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + # create_uid must mirror the supervisor_id value. + # create_uid is a magic field that belongs to the ORM that is not + # editable via a form. + create_uid = fields.Many2one( + comodel_name='res.users', + compute='_compute_create_uid', + ) + supervisor_id = fields.Many2one( + comodel_name='res.users', + string='Responsible', + required=True, + default=lambda self: self.env.user, + ) + + @api.depends('supervisor_id') + def _compute_create_uid(self): + for rec in self: + if rec.supervisor_id: + rec.create_uid = rec.supervisor_id + + @api.multi + def write(self, vals): + if 'supervisor_id' in vals: + new_supervisor = vals['supervisor_id'] + for rec in self: + rec.message_unsubscribe_users( + user_ids=rec.supervisor_id.ids, + ) + rec.message_subscribe_users( + user_ids=[new_supervisor], + subtype_ids=[], + ) + return super(PurchaseOrder, self).write(vals) diff --git a/beesdoo_purchase/views/purchase_order.xml b/beesdoo_purchase/views/purchase_order.xml index d84ab2e..f649d78 100644 --- a/beesdoo_purchase/views/purchase_order.xml +++ b/beesdoo_purchase/views/purchase_order.xml @@ -6,12 +6,34 @@ - + [('main_seller_id','=', parent.partner_id), ('purchase_ok', '=', True)] + + + beesdoo.purchase.order.tree.view + purchase.order + + + + + + + + + + beesdoo.purchase.order.search.view + purchase.order + + + + + + +