From eadd6208a2db3a07b604cab55812d0c337e17cb9 Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Thu, 18 May 2017 19:06:06 -0500 Subject: [PATCH 1/8] [10.0][Stock] Added migration scripts for stock module --- .../10.0.1.1/openupgrade_analysis_work.txt | 16 ++- .../migrations/10.0.1.1/post-migration.py | 113 ++++++++++++++++++ .../migrations/10.0.1.1/pre-migration.py | 32 +++++ odoo/openupgrade/doc/source/modules90-100.rst | 2 +- 4 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 addons/stock/migrations/10.0.1.1/post-migration.py create mode 100644 addons/stock/migrations/10.0.1.1/pre-migration.py diff --git a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt index f9eabfe94213..11dc0a4da985 100644 --- a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt +++ b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt @@ -3,11 +3,14 @@ stock / barcode.rule / type (False) : NEW se # NOTHING TO DO stock / procurement.rule / picking_type_id (many2one) : now required -# TODO: pre-migration. Default to some value, guessing by looking at source +# TODO: post-migration. Default to some value, guessing by looking at source # and destination locations, and warehouse. +# Done! + stock / procurement.rule / route_sequence (float) : type is now 'integer' ('float') # TODO: pre-migration: copy column # TODO: post-migration: move values from old column +# Done! stock / product.product / stock_move_ids (one2many) : NEW relation: stock.move stock / product.product / stock_quant_ids (one2many) : NEW relation: stock.quant @@ -15,7 +18,7 @@ stock / product.product / stock_quant_ids (one2many) : NEW re stock / product.template / location_id (many2one) : not a function anymore stock / product.template / warehouse_id (many2one) : not a function anymore -# TODO: ??? IN V9 were defined as fields.dummy +# NOTHING TO DO as in v9 were dummy fields (existed only for backwards compatibility) stock / product.template / type (False) : NEW selection_keys: ['consu', 'product', 'service'], mode: modify # NOTHING TO DO @@ -31,15 +34,20 @@ stock / stock.inventory.line / state (char) : type i stock / stock.location.path / auto (selection) : selection_keys is now '['manual', 'transparent']' ('['auto', 'manual', 'transparent']') # TODO: pre-migration. copy column # TODO: post-migration. Move 'auto' to some other value +# Done! stock / stock.location.path / route_sequence (float) : type is now 'integer' ('float') # TODO: pre-migration. copy column # TODO: post-migration. copy values to new field converting to integer +# Done! stock / stock.move / ordered_qty (float) : NEW # TODO: post-migration. Default to product_uom_qty +# Done! + stock / stock.pack.operation / ordered_qty (float) : NEW -# TODO: post-migration. Default to product_uom_qty +# TODO: post-migration. Default to product_qty +# Done! stock / stock.pack.operation / processed_boolean (boolean) : was renamed to is_done [nothing to do] # NOTHING TO DO @@ -70,6 +78,8 @@ stock / stock.scrap / scrap_location_id (many2one) : NEW re stock / stock.scrap / scrap_qty (float) : NEW required: required, req_default: function stock / stock.scrap / state (selection) : NEW selection_keys: ['done', 'draft'] # TODO: Create one record for each move that had as destionation a scrap +# TODO ANDRES - Maybe need to create a workflow for the "state" +# TODO ANDRES - Y que pasa con la secuencia NEW ir.sequence: stock.sequence_stock_scrap # location stock / stock.warehouse / active (boolean) : NEW diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py new file mode 100644 index 000000000000..9379a56d5cca --- /dev/null +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +# © 2017 Trescloud +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + +@openupgrade.migrate(use_env=True) +def migrate(env, version): + update_picking_type_id(env) + update_ordered_qty(env) + populate_stock_scrap(env) + +def update_picking_type_id(env): + ''' + Updates picking_type_id, defaulting to some value, guessing by looking at source + and destination locations, and warehouse. + :param env: enviroment variable (self) + ''' + + procurement_rules_to_set = env['procurement.rule'].search([]) + + for procurement_rule in procurement_rules_to_set: + if not procurement_rule.picking_type_id: #TODO ANDRES ponerle en el search por eficiencia + + picking_type_id = False + env.cr.execute( + """ + SELECT id from stock_picking_type + WHERE warehouse_id = %s AND default_location_dest_id = %s AND default_location_src_id = %s""" % ( + procurement_rule.warehouse_id, + procurement_rule.location_id, + procurement_rule.location_src_id, + ) + ) + picking_type_ids = cr.fetchone() + if picking_type_ids: + picking_type_id = picking_type_ids[0] + + if not picking_type_id: #fallback values when everything else fails + if procurement_rule.action == 'buy': + #location_src_id is not considered as is not mandatory in this case + xml_record = env.ref("stock.picking_type_in") + if xml_record: + picking_type_id = xml_record.id + elif procurement_rule.action == 'move': + if procurement_rule.location_id and procurement_rule.location_id.usage == 'customer': + #special case when is a delivarable to a customer location + xml_record = env.ref("stock.picking_type_out") + if xml_record: + picking_type_id = xml_record.id + else: + xml_record = env.ref("stock.picking_type_internal") + if xml_record: + picking_type_id = xml_record.id + elif procurement_rule.action == 'manufacture': + xml_record = env.ref("mrp.picking_type_manufacturing") + if xml_record: + picking_type_id = xml_record.id + + procurement_rule.write({'picking_type_id': picking_type_id}) + +def update_ordered_qty(env): + ''' + Set as default the value of field ordered_qty from the value of field product_uom_qty + :param env: enviroment variable (self) + ''' + #stock / stock.move / ordered_qty (float) : NEW + # TODO: post-migration. Default to product_uom_qty + env.cr.execute( + """ + UPDATE stock_move SET ordered_qty = product_uom_qty + """) + + #stock / stock.pack.operation / ordered_qty (float) : NEW + # TODO: post-migration. Default to product_uom_qty + env.cr.execute( + """ + UPDATE stock_pack_operation SET ordered_qty = product_qty + """) + +def populate_stock_scrap(env): + ''' + Fills up new object "stock.scrap" based on moves with destination scrap + :param env: enviroment variable (self) + ''' + #cr = env.cr + #scrap_locations = env['stock.location'].search([('scrap_location','=',True)]) + + env.cr.execute( + """ + SELECT id from stock_location + WHERE scrap_location is True + """ + ) + scrap_location_ids = env.cr.fetchone() + + #do not call stock_scrap.create as it will create a duplicated stock move, use SQL instead + env.cr.execute( + ''' + INSERT INTO stock_scrap (date_expected,location_id,lot_id,move_id,name,origin,owner_id,picking_id,product_id,product_uom_id,scrap_location_id,scrap_qty,state) + SELECT date_expected,location_id,restrict_lot_id,id,name,origin,restrict_partner_id,picking_id,product_id,product_uom,location_dest_id,product_uom_qty,state + FROM stock_move + WHERE location_id IN %s AND product_uom_qty < 0.0 AND state = 'done' + OR location_dest_id IN %s AND product_uom_qty >= 0.0 AND state = 'done' + ''',(scrap_location_ids,scrap_location_ids)) + #field package_id not set as no value is defined + + + + + + + + diff --git a/addons/stock/migrations/10.0.1.1/pre-migration.py b/addons/stock/migrations/10.0.1.1/pre-migration.py new file mode 100644 index 000000000000..ccdb3a6fa78c --- /dev/null +++ b/addons/stock/migrations/10.0.1.1/pre-migration.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# © 2017 Trescloud +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + +@openupgrade.migrate(use_env=True) +def migrate(env, version): + + #field route_sequence + openupgrade.copy_columns( + env.cr, { + 'procurement_rule': [ + ('route_sequence', None, None), + ], + }) + openupgrade.float_to_integer(env.cr, 'procurement_rule', 'route_sequence') + + #table auto + openupgrade.copy_columns( + env.cr, { + 'stock_location_path': [ + ('auto', None, None), + ('route_sequence', None, None), + ], + }) + env.cr.execute( + """ + UPDATE stock_location_path SET auto = 'manual' WHERE auto = 'auto'; + """) + openupgrade.float_to_integer(env.cr, 'stock_location_path', 'route_sequence') + + diff --git a/odoo/openupgrade/doc/source/modules90-100.rst b/odoo/openupgrade/doc/source/modules90-100.rst index 2cd2a3ba8d0c..f13f80de888b 100644 --- a/odoo/openupgrade/doc/source/modules90-100.rst +++ b/odoo/openupgrade/doc/source/modules90-100.rst @@ -453,7 +453,7 @@ missing in the new release are marked with |del|. +-----------------------------------+-----------------------------------+ |sales_team | Done | +-----------------------------------+-----------------------------------+ -|stock | | +|stock | Done | +-----------------------------------+-----------------------------------+ |stock_account | Nothing to do | +-----------------------------------+-----------------------------------+ From d4191fc57bc3bb0c9e62188b4c75efed6f1f69a3 Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Thu, 18 May 2017 20:28:38 -0500 Subject: [PATCH 2/8] [10.0][stock] Removed messy comments, empty lines --- .../10.0.1.1/openupgrade_analysis_work.txt | 2 -- .../stock/migrations/10.0.1.1/post-migration.py | 17 ++++------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt index 11dc0a4da985..01a1b5361daf 100644 --- a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt +++ b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt @@ -78,8 +78,6 @@ stock / stock.scrap / scrap_location_id (many2one) : NEW re stock / stock.scrap / scrap_qty (float) : NEW required: required, req_default: function stock / stock.scrap / state (selection) : NEW selection_keys: ['done', 'draft'] # TODO: Create one record for each move that had as destionation a scrap -# TODO ANDRES - Maybe need to create a workflow for the "state" -# TODO ANDRES - Y que pasa con la secuencia NEW ir.sequence: stock.sequence_stock_scrap # location stock / stock.warehouse / active (boolean) : NEW diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index 9379a56d5cca..c67c6ec1c3df 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -19,7 +19,7 @@ def update_picking_type_id(env): procurement_rules_to_set = env['procurement.rule'].search([]) for procurement_rule in procurement_rules_to_set: - if not procurement_rule.picking_type_id: #TODO ANDRES ponerle en el search por eficiencia + if not procurement_rule.picking_type_id: picking_type_id = False env.cr.execute( @@ -82,9 +82,7 @@ def populate_stock_scrap(env): Fills up new object "stock.scrap" based on moves with destination scrap :param env: enviroment variable (self) ''' - #cr = env.cr - #scrap_locations = env['stock.location'].search([('scrap_location','=',True)]) - + env.cr.execute( """ SELECT id from stock_location @@ -93,7 +91,8 @@ def populate_stock_scrap(env): ) scrap_location_ids = env.cr.fetchone() - #do not call stock_scrap.create as it will create a duplicated stock move, use SQL instead + #do not call stock_scrap.create as it will create a duplicated stock move, use SQL instead + #field package_id not set as no value is defined env.cr.execute( ''' INSERT INTO stock_scrap (date_expected,location_id,lot_id,move_id,name,origin,owner_id,picking_id,product_id,product_uom_id,scrap_location_id,scrap_qty,state) @@ -102,12 +101,4 @@ def populate_stock_scrap(env): WHERE location_id IN %s AND product_uom_qty < 0.0 AND state = 'done' OR location_dest_id IN %s AND product_uom_qty >= 0.0 AND state = 'done' ''',(scrap_location_ids,scrap_location_ids)) - #field package_id not set as no value is defined - - - - - - - From 341f8b3d0897c855d042288351ca366fd3bec901 Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Thu, 25 May 2017 19:18:33 -0500 Subject: [PATCH 3/8] [10.0][Stock] Improved structure as suggested by contributors --- .../10.0.1.1/openupgrade_analysis_work.txt | 24 ++---- .../migrations/10.0.1.1/post-migration.py | 86 ++++++++----------- .../migrations/10.0.1.1/pre-migration.py | 34 +++----- 3 files changed, 59 insertions(+), 85 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt index 01a1b5361daf..7d76f691939f 100644 --- a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt +++ b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt @@ -3,14 +3,11 @@ stock / barcode.rule / type (False) : NEW se # NOTHING TO DO stock / procurement.rule / picking_type_id (many2one) : now required -# TODO: post-migration. Default to some value, guessing by looking at source +# Done: post-migration. Default to some value, guessing by looking at source # and destination locations, and warehouse. -# Done! stock / procurement.rule / route_sequence (float) : type is now 'integer' ('float') -# TODO: pre-migration: copy column -# TODO: post-migration: move values from old column -# Done! +# Done: pre-migration: copy column and then convert float to integer stock / product.product / stock_move_ids (one2many) : NEW relation: stock.move stock / product.product / stock_quant_ids (one2many) : NEW relation: stock.quant @@ -32,22 +29,16 @@ stock / stock.inventory.line / state (char) : type i # NOTHING TO DO stock / stock.location.path / auto (selection) : selection_keys is now '['manual', 'transparent']' ('['auto', 'manual', 'transparent']') -# TODO: pre-migration. copy column -# TODO: post-migration. Move 'auto' to some other value -# Done! +# Done: pre-migration. copy column and then replace value "auto" with value "manual" stock / stock.location.path / route_sequence (float) : type is now 'integer' ('float') -# TODO: pre-migration. copy column -# TODO: post-migration. copy values to new field converting to integer -# Done! +# Done!: pre-migration: copy column and then convert float to integer stock / stock.move / ordered_qty (float) : NEW -# TODO: post-migration. Default to product_uom_qty -# Done! +# Done: post-migration. Default to product_uom_qty stock / stock.pack.operation / ordered_qty (float) : NEW -# TODO: post-migration. Default to product_qty -# Done! +# Done: post-migration. Default to product_qty stock / stock.pack.operation / processed_boolean (boolean) : was renamed to is_done [nothing to do] # NOTHING TO DO @@ -77,8 +68,7 @@ stock / stock.scrap / product_uom_id (many2one) : NEW re stock / stock.scrap / scrap_location_id (many2one) : NEW relation: stock.location stock / stock.scrap / scrap_qty (float) : NEW required: required, req_default: function stock / stock.scrap / state (selection) : NEW selection_keys: ['done', 'draft'] -# TODO: Create one record for each move that had as destionation a scrap -# location +# Done: pre-migration: Create one record for each move that had as destionation a scrap location stock / stock.warehouse / active (boolean) : NEW stock / stock.warehouse / resupply_from_wh (boolean) : DEL diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index c67c6ec1c3df..b7a0e7ca4c41 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -1,31 +1,29 @@ # -*- coding: utf-8 -*- -# © 2017 Trescloud +# Copyright 2017 Trescloud # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade -@openupgrade.migrate(use_env=True) -def migrate(env, version): - update_picking_type_id(env) - update_ordered_qty(env) - populate_stock_scrap(env) - def update_picking_type_id(env): - ''' - Updates picking_type_id, defaulting to some value, guessing by looking at source + """Updates picking_type_id, defaulting to some value, guessing by looking at source and destination locations, and warehouse. :param env: enviroment variable (self) - ''' - + """ + #load xml data to be used for filling in missing info + xml_stock_picking_type_internal = env.ref("stock.picking_type_internal") + xml_stock_picking_type_out = env.ref("stock.picking_type_out") + xml_stock_picking_type_in = env.ref("stock.picking_type_in") + xml_stock_picking_type_manufacturing = env.ref("mrp.picking_type_manufacturing") + #verify for each procument rule to set procurement_rules_to_set = env['procurement.rule'].search([]) - for procurement_rule in procurement_rules_to_set: if not procurement_rule.picking_type_id: - picking_type_id = False env.cr.execute( - """ + ''' SELECT id from stock_picking_type - WHERE warehouse_id = %s AND default_location_dest_id = %s AND default_location_src_id = %s""" % ( + WHERE warehouse_id = %s AND + default_location_dest_id = %s AND + default_location_src_id = %s''' % ( procurement_rule.warehouse_id, procurement_rule.location_id, procurement_rule.location_src_id, @@ -34,63 +32,50 @@ def update_picking_type_id(env): picking_type_ids = cr.fetchone() if picking_type_ids: picking_type_id = picking_type_ids[0] - if not picking_type_id: #fallback values when everything else fails if procurement_rule.action == 'buy': #location_src_id is not considered as is not mandatory in this case - xml_record = env.ref("stock.picking_type_in") - if xml_record: - picking_type_id = xml_record.id + if xml_stock_picking_type_in: + picking_type_id = xml_stock_picking_type_in.id elif procurement_rule.action == 'move': if procurement_rule.location_id and procurement_rule.location_id.usage == 'customer': #special case when is a delivarable to a customer location - xml_record = env.ref("stock.picking_type_out") - if xml_record: - picking_type_id = xml_record.id + if xml_stock_picking_type_out: + picking_type_id = xml_stock_picking_type_out.id else: - xml_record = env.ref("stock.picking_type_internal") - if xml_record: - picking_type_id = xml_record.id + if xml_stock_picking_type_internal: + picking_type_id = xml_stock_picking_type_internal.id elif procurement_rule.action == 'manufacture': - xml_record = env.ref("mrp.picking_type_manufacturing") - if xml_record: - picking_type_id = xml_record.id - + if xml_stock_picking_type_manufacturing: + picking_type_id = xml_stock_picking_type_manufacturing.id procurement_rule.write({'picking_type_id': picking_type_id}) def update_ordered_qty(env): - ''' - Set as default the value of field ordered_qty from the value of field product_uom_qty + """ Set the value of new field ordered_qty as: + - for stock moves the value of field product_uom_qty + - for stock_pack_operation the value of product_qty :param env: enviroment variable (self) - ''' - #stock / stock.move / ordered_qty (float) : NEW - # TODO: post-migration. Default to product_uom_qty + """ env.cr.execute( - """ + ''' UPDATE stock_move SET ordered_qty = product_uom_qty - """) - - #stock / stock.pack.operation / ordered_qty (float) : NEW - # TODO: post-migration. Default to product_uom_qty + ''') env.cr.execute( - """ + ''' UPDATE stock_pack_operation SET ordered_qty = product_qty - """) + ''') def populate_stock_scrap(env): - ''' - Fills up new object "stock.scrap" based on moves with destination scrap + """ Fills up new object "stock.scrap" based on moves with destination scrap :param env: enviroment variable (self) - ''' - + """ env.cr.execute( - """ + ''' SELECT id from stock_location WHERE scrap_location is True - """ + ''' ) scrap_location_ids = env.cr.fetchone() - #do not call stock_scrap.create as it will create a duplicated stock move, use SQL instead #field package_id not set as no value is defined env.cr.execute( @@ -102,3 +87,8 @@ def populate_stock_scrap(env): OR location_dest_id IN %s AND product_uom_qty >= 0.0 AND state = 'done' ''',(scrap_location_ids,scrap_location_ids)) +@openupgrade.migrate(use_env=True) +def migrate(env, version): + update_picking_type_id(env) + update_ordered_qty(env) + populate_stock_scrap(env) \ No newline at end of file diff --git a/addons/stock/migrations/10.0.1.1/pre-migration.py b/addons/stock/migrations/10.0.1.1/pre-migration.py index ccdb3a6fa78c..adf1d9aee636 100644 --- a/addons/stock/migrations/10.0.1.1/pre-migration.py +++ b/addons/stock/migrations/10.0.1.1/pre-migration.py @@ -1,32 +1,26 @@ # -*- coding: utf-8 -*- -# © 2017 Trescloud +# Copyright 2017 Trescloud # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade +column_copies = { + 'procurement_rule': [ + ('route_sequence', None, None), + ], + 'stock_location_path': [ + ('auto', None, None), + ('route_sequence', None, None), + ], + } + @openupgrade.migrate(use_env=True) -def migrate(env, version): - - #field route_sequence - openupgrade.copy_columns( - env.cr, { - 'procurement_rule': [ - ('route_sequence', None, None), - ], - }) +def migrate(env, version): + openupgrade.copy_columns(env.cr, column_copies) + openupgrade.float_to_integer(env.cr, 'stock_location_path', 'route_sequence') openupgrade.float_to_integer(env.cr, 'procurement_rule', 'route_sequence') - - #table auto - openupgrade.copy_columns( - env.cr, { - 'stock_location_path': [ - ('auto', None, None), - ('route_sequence', None, None), - ], - }) env.cr.execute( """ UPDATE stock_location_path SET auto = 'manual' WHERE auto = 'auto'; """) - openupgrade.float_to_integer(env.cr, 'stock_location_path', 'route_sequence') From 519e7ac964c0a02b3b0a0e6d2abdc6818032852c Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Thu, 25 May 2017 19:26:23 -0500 Subject: [PATCH 4/8] [10.0][Stock] Removed unnecesary white spaces --- addons/stock/migrations/10.0.1.1/pre-migration.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/pre-migration.py b/addons/stock/migrations/10.0.1.1/pre-migration.py index adf1d9aee636..590f5c35084d 100644 --- a/addons/stock/migrations/10.0.1.1/pre-migration.py +++ b/addons/stock/migrations/10.0.1.1/pre-migration.py @@ -21,6 +21,4 @@ def migrate(env, version): env.cr.execute( """ UPDATE stock_location_path SET auto = 'manual' WHERE auto = 'auto'; - """) - - + """) \ No newline at end of file From b186ce2c738ec64ce0b8b5b35ae7e08e6455bb97 Mon Sep 17 00:00:00 2001 From: mreficent Date: Fri, 26 May 2017 14:03:17 +0200 Subject: [PATCH 5/8] [10.0][Stock] Some adjustments as per review comments --- .../10.0.1.1/openupgrade_analysis_work.txt | 6 +- .../migrations/10.0.1.1/post-migration.py | 138 +++++++++++------- .../migrations/10.0.1.1/pre-migration.py | 20 ++- 3 files changed, 97 insertions(+), 67 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt index 7d76f691939f..bc45c4150a8f 100644 --- a/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt +++ b/addons/stock/migrations/10.0.1.1/openupgrade_analysis_work.txt @@ -29,10 +29,10 @@ stock / stock.inventory.line / state (char) : type i # NOTHING TO DO stock / stock.location.path / auto (selection) : selection_keys is now '['manual', 'transparent']' ('['auto', 'manual', 'transparent']') -# Done: pre-migration. copy column and then replace value "auto" with value "manual" +# Done: post-migration. move values 'auto' to 'manual' stock / stock.location.path / route_sequence (float) : type is now 'integer' ('float') -# Done!: pre-migration: copy column and then convert float to integer +# Done: pre-migration: copy column and then convert float to integer stock / stock.move / ordered_qty (float) : NEW # Done: post-migration. Default to product_uom_qty @@ -68,7 +68,7 @@ stock / stock.scrap / product_uom_id (many2one) : NEW re stock / stock.scrap / scrap_location_id (many2one) : NEW relation: stock.location stock / stock.scrap / scrap_qty (float) : NEW required: required, req_default: function stock / stock.scrap / state (selection) : NEW selection_keys: ['done', 'draft'] -# Done: pre-migration: Create one record for each move that had as destionation a scrap location +# Done: post-migration: Create one record for each move linked to a scrap location stock / stock.warehouse / active (boolean) : NEW stock / stock.warehouse / resupply_from_wh (boolean) : DEL diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index b7a0e7ca4c41..fb64fbb644b7 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -3,92 +3,124 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade + +def map_location_auto(cr): + openupgrade.map_values( + cr, + openupgrade.get_legacy_name('auto'), 'auto', + [('auto', 'manual')], + table='stock_location_path', write='sql') + + def update_picking_type_id(env): - """Updates picking_type_id, defaulting to some value, guessing by looking at source - and destination locations, and warehouse. - :param env: enviroment variable (self) + """Updates picking_type_id, defaulting to some value, guessing by looking + at source and destination locations, and warehouse. + :param env: environment variable (self) """ - #load xml data to be used for filling in missing info - xml_stock_picking_type_internal = env.ref("stock.picking_type_internal") + # load xml data to be used for filling in missing info + xml_stock_picking_type_int = env.ref("stock.picking_type_internal") xml_stock_picking_type_out = env.ref("stock.picking_type_out") xml_stock_picking_type_in = env.ref("stock.picking_type_in") - xml_stock_picking_type_manufacturing = env.ref("mrp.picking_type_manufacturing") - #verify for each procument rule to set + + # verify for each procurement rule to set procurement_rules_to_set = env['procurement.rule'].search([]) for procurement_rule in procurement_rules_to_set: if not procurement_rule.picking_type_id: picking_type_id = False env.cr.execute( - ''' + """ SELECT id from stock_picking_type WHERE warehouse_id = %s AND - default_location_dest_id = %s AND - default_location_src_id = %s''' % ( - procurement_rule.warehouse_id, + default_location_dest_id = %s AND + default_location_src_id = %s""" % ( + procurement_rule.warehouse_id, procurement_rule.location_id, procurement_rule.location_src_id, ) ) - picking_type_ids = cr.fetchone() + picking_type_ids = env.cr.fetchone() if picking_type_ids: picking_type_id = picking_type_ids[0] - if not picking_type_id: #fallback values when everything else fails + + # fallback values when everything else fails + if not picking_type_id: if procurement_rule.action == 'buy': - #location_src_id is not considered as is not mandatory in this case + # location_src_id is not considered + # (as is not mandatory in this case) if xml_stock_picking_type_in: picking_type_id = xml_stock_picking_type_in.id elif procurement_rule.action == 'move': - if procurement_rule.location_id and procurement_rule.location_id.usage == 'customer': - #special case when is a delivarable to a customer location + if procurement_rule.location_id and procurement_rule.\ + location_id.usage == 'customer': + # delivery case to a customer location if xml_stock_picking_type_out: picking_type_id = xml_stock_picking_type_out.id else: - if xml_stock_picking_type_internal: - picking_type_id = xml_stock_picking_type_internal.id - elif procurement_rule.action == 'manufacture': - if xml_stock_picking_type_manufacturing: - picking_type_id = xml_stock_picking_type_manufacturing.id + if xml_stock_picking_type_int: + picking_type_id = xml_stock_picking_type_int.id + procurement_rule.write({'picking_type_id': picking_type_id}) -def update_ordered_qty(env): + +def update_ordered_qty(cr): """ Set the value of new field ordered_qty as: - for stock moves the value of field product_uom_qty - for stock_pack_operation the value of product_qty - :param env: enviroment variable (self) + :param cr: cursor variable (self.env) """ - env.cr.execute( - ''' - UPDATE stock_move SET ordered_qty = product_uom_qty - ''') - env.cr.execute( - ''' - UPDATE stock_pack_operation SET ordered_qty = product_qty - ''') - -def populate_stock_scrap(env): - """ Fills up new object "stock.scrap" based on moves with destination scrap - :param env: enviroment variable (self) + + cr.execute( + "UPDATE stock_move SET ordered_qty = product_uom_qty") + cr.execute( + "UPDATE stock_pack_operation SET ordered_qty = product_qty") + + +def populate_stock_scrap(cr): """ - env.cr.execute( - ''' + Fills up new object "stock.scrap" based on moves linked to scrap location + :param cr: cursor variable (self.env) + """ + + cr.execute( + """ SELECT id from stock_location WHERE scrap_location is True - ''' - ) - scrap_location_ids = env.cr.fetchone() - #do not call stock_scrap.create as it will create a duplicated stock move, use SQL instead - #field package_id not set as no value is defined - env.cr.execute( - ''' - INSERT INTO stock_scrap (date_expected,location_id,lot_id,move_id,name,origin,owner_id,picking_id,product_id,product_uom_id,scrap_location_id,scrap_qty,state) - SELECT date_expected,location_id,restrict_lot_id,id,name,origin,restrict_partner_id,picking_id,product_id,product_uom,location_dest_id,product_uom_qty,state - FROM stock_move - WHERE location_id IN %s AND product_uom_qty < 0.0 AND state = 'done' - OR location_dest_id IN %s AND product_uom_qty >= 0.0 AND state = 'done' - ''',(scrap_location_ids,scrap_location_ids)) - + """ + ) + scrap_location_ids = cr.fetchone() + + cr.execute( + """ + INSERT INTO stock_scrap (date_expected,location_id,lot_id,move_id, + name,origin,owner_id,package_id,picking_id,product_id, + product_uom_id,scrap_location_id,scrap_qty,state) + WITH Q1 as (SELECT DISTINCT sq.package_id,sr.move_id + FROM stock_quant_move_rel sr + INNER JOIN stock_quant as sq ON sq.id = sr.quant_id), + Q2 as (SELECT COUNT(Q1.package_id) as n,Q1.move_id + FROM Q1 + GROUP BY Q1.move_id), + Q3 as (SELECT DISTINCT Q2.move_id,sq.package_id + FROM Q2 + LEFT JOIN stock_quant_move_rel sr ON sr.move_id = Q2.move_id + LEFT JOIN stock_quant as sq ON sq.id = sr.quant_id + WHERE Q2.n = 1) + SELECT date_expected,location_id,restrict_lot_id,id,name,origin, + restrict_partner_id,Q3.package_id,picking_id,product_id, + product_uom,location_dest_id,product_uom_qty,state + FROM stock_move sm + LEFT JOIN Q3 ON sm.id = Q3.move_id + WHERE (location_id IN %s AND product_uom_qty < 0.0 AND + state = 'done') + OR (location_dest_id IN %s AND product_uom_qty >= 0.0 AND + state = 'done') + """, (scrap_location_ids, scrap_location_ids)) + + @openupgrade.migrate(use_env=True) def migrate(env, version): + cr = env.cr + map_location_auto(cr) update_picking_type_id(env) - update_ordered_qty(env) - populate_stock_scrap(env) \ No newline at end of file + update_ordered_qty(cr) + populate_stock_scrap(cr) diff --git a/addons/stock/migrations/10.0.1.1/pre-migration.py b/addons/stock/migrations/10.0.1.1/pre-migration.py index 590f5c35084d..59291a8b67eb 100644 --- a/addons/stock/migrations/10.0.1.1/pre-migration.py +++ b/addons/stock/migrations/10.0.1.1/pre-migration.py @@ -6,19 +6,17 @@ column_copies = { 'procurement_rule': [ ('route_sequence', None, None), - ], + ], 'stock_location_path': [ ('auto', None, None), ('route_sequence', None, None), - ], - } + ], +} + @openupgrade.migrate(use_env=True) -def migrate(env, version): - openupgrade.copy_columns(env.cr, column_copies) - openupgrade.float_to_integer(env.cr, 'stock_location_path', 'route_sequence') - openupgrade.float_to_integer(env.cr, 'procurement_rule', 'route_sequence') - env.cr.execute( - """ - UPDATE stock_location_path SET auto = 'manual' WHERE auto = 'auto'; - """) \ No newline at end of file +def migrate(env, version): + cr = env.cr + openupgrade.copy_columns(cr, column_copies) + openupgrade.float_to_integer(cr, 'procurement_rule', 'route_sequence') + openupgrade.float_to_integer(cr, 'stock_location_path', 'route_sequence') From d3deaa468da27e34eb06f4993901f155c7c068b9 Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Fri, 26 May 2017 14:15:31 -0500 Subject: [PATCH 6/8] [10.0][Stock] Minor fixes - Added reate_date to stock_scrap, - Removed positive qtys filter as positive and negative are supported - Removed scrapped source location as domains avoids this in v10 - Added explanation about using SQL instead of ORM - Included case for MRP for ease of coding --- .../migrations/10.0.1.1/post-migration.py | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index fb64fbb644b7..f7d28599926e 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -18,9 +18,10 @@ def update_picking_type_id(env): :param env: environment variable (self) """ # load xml data to be used for filling in missing info - xml_stock_picking_type_int = env.ref("stock.picking_type_internal") - xml_stock_picking_type_out = env.ref("stock.picking_type_out") - xml_stock_picking_type_in = env.ref("stock.picking_type_in") + xml_stock_picking_type_int = env.ref("stock.picking_type_internal",False) + xml_stock_picking_type_out = env.ref("stock.picking_type_out",False) + xml_stock_picking_type_in = env.ref("stock.picking_type_in",False) + xml_stock_picking_type_manufacturing = env.ref("mrp.picking_type_manufacturing",False) # verify for each procurement rule to set procurement_rules_to_set = env['procurement.rule'].search([]) @@ -58,6 +59,12 @@ def update_picking_type_id(env): else: if xml_stock_picking_type_int: picking_type_id = xml_stock_picking_type_int.id + + # special case for mrp module put here for not repeating the logic. + # If mrp not installed won't break + elif procurement_rule.action == 'manufacture': + if xml_stock_picking_type_manufacturing: + picking_type_id = xml_stock_picking_type_manufacturing.id procurement_rule.write({'picking_type_id': picking_type_id}) @@ -88,10 +95,11 @@ def populate_stock_scrap(cr): """ ) scrap_location_ids = cr.fetchone() - + + # Use SQL instead of ORM, otherwise stock_scrap.create() will create a duplicated stock move cr.execute( """ - INSERT INTO stock_scrap (date_expected,location_id,lot_id,move_id, + INSERT INTO stock_scrap (date_expected,create_date,location_id,lot_id,move_id, name,origin,owner_id,package_id,picking_id,product_id, product_uom_id,scrap_location_id,scrap_qty,state) WITH Q1 as (SELECT DISTINCT sq.package_id,sr.move_id @@ -105,16 +113,13 @@ def populate_stock_scrap(cr): LEFT JOIN stock_quant_move_rel sr ON sr.move_id = Q2.move_id LEFT JOIN stock_quant as sq ON sq.id = sr.quant_id WHERE Q2.n = 1) - SELECT date_expected,location_id,restrict_lot_id,id,name,origin, + SELECT date_expected,create_date,location_id,restrict_lot_id,id,name,origin, restrict_partner_id,Q3.package_id,picking_id,product_id, product_uom,location_dest_id,product_uom_qty,state FROM stock_move sm LEFT JOIN Q3 ON sm.id = Q3.move_id - WHERE (location_id IN %s AND product_uom_qty < 0.0 AND - state = 'done') - OR (location_dest_id IN %s AND product_uom_qty >= 0.0 AND - state = 'done') - """, (scrap_location_ids, scrap_location_ids)) + WHERE location_dest_id IN %s AND state = 'done' + """, (scrap_location_ids,)) @openupgrade.migrate(use_env=True) From cc65573525df09124d8afae699eefcf58d6fde87 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Wed, 31 May 2017 11:49:03 +0200 Subject: [PATCH 7/8] [FIX] PEP8 --- .../migrations/10.0.1.1/post-migration.py | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index f7d28599926e..db5eea709759 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -14,15 +14,16 @@ def map_location_auto(cr): def update_picking_type_id(env): """Updates picking_type_id, defaulting to some value, guessing by looking - at source and destination locations, and warehouse. + at source and destination locations, and warehouse. :param env: environment variable (self) """ # load xml data to be used for filling in missing info - xml_stock_picking_type_int = env.ref("stock.picking_type_internal",False) - xml_stock_picking_type_out = env.ref("stock.picking_type_out",False) - xml_stock_picking_type_in = env.ref("stock.picking_type_in",False) - xml_stock_picking_type_manufacturing = env.ref("mrp.picking_type_manufacturing",False) - + xml_stock_picking_type_int = env.ref("stock.picking_type_internal") + xml_stock_picking_type_out = env.ref("stock.picking_type_out") + xml_stock_picking_type_in = env.ref("stock.picking_type_in") + xml_stock_picking_type_manufacturing = env.ref( + "mrp.picking_type_manufacturing" + ) # verify for each procurement rule to set procurement_rules_to_set = env['procurement.rule'].search([]) for procurement_rule in procurement_rules_to_set: @@ -30,9 +31,9 @@ def update_picking_type_id(env): picking_type_id = False env.cr.execute( """ - SELECT id from stock_picking_type - WHERE warehouse_id = %s AND - default_location_dest_id = %s AND + SELECT id from stock_picking_type + WHERE warehouse_id = %s AND + default_location_dest_id = %s AND default_location_src_id = %s""" % ( procurement_rule.warehouse_id, procurement_rule.location_id, @@ -59,8 +60,8 @@ def update_picking_type_id(env): else: if xml_stock_picking_type_int: picking_type_id = xml_stock_picking_type_int.id - - # special case for mrp module put here for not repeating the logic. + + # special case for mrp module put here for not repeating the logic. # If mrp not installed won't break elif procurement_rule.action == 'manufacture': if xml_stock_picking_type_manufacturing: @@ -72,7 +73,7 @@ def update_picking_type_id(env): def update_ordered_qty(cr): """ Set the value of new field ordered_qty as: - for stock moves the value of field product_uom_qty - - for stock_pack_operation the value of product_qty + - for stock_pack_operation the value of product_qty :param cr: cursor variable (self.env) """ @@ -87,19 +88,19 @@ def populate_stock_scrap(cr): Fills up new object "stock.scrap" based on moves linked to scrap location :param cr: cursor variable (self.env) """ - cr.execute( """ - SELECT id from stock_location + SELECT id from stock_location WHERE scrap_location is True """ ) scrap_location_ids = cr.fetchone() - - # Use SQL instead of ORM, otherwise stock_scrap.create() will create a duplicated stock move + # Use SQL instead of ORM, otherwise stock_scrap.create() will create a + # duplicated stock move cr.execute( """ - INSERT INTO stock_scrap (date_expected,create_date,location_id,lot_id,move_id, + INSERT INTO stock_scrap + (date_expected,create_date,location_id,lot_id,move_id, name,origin,owner_id,package_id,picking_id,product_id, product_uom_id,scrap_location_id,scrap_qty,state) WITH Q1 as (SELECT DISTINCT sq.package_id,sr.move_id @@ -113,9 +114,9 @@ def populate_stock_scrap(cr): LEFT JOIN stock_quant_move_rel sr ON sr.move_id = Q2.move_id LEFT JOIN stock_quant as sq ON sq.id = sr.quant_id WHERE Q2.n = 1) - SELECT date_expected,create_date,location_id,restrict_lot_id,id,name,origin, - restrict_partner_id,Q3.package_id,picking_id,product_id, - product_uom,location_dest_id,product_uom_qty,state + SELECT date_expected,create_date,location_id,restrict_lot_id,id, + name,origin,restrict_partner_id,Q3.package_id,picking_id, + product_id,product_uom,location_dest_id,product_uom_qty,state FROM stock_move sm LEFT JOIN Q3 ON sm.id = Q3.move_id WHERE location_dest_id IN %s AND state = 'done' From 113f9e1c130be9031b3a8f9431b30cc7959fcee4 Mon Sep 17 00:00:00 2001 From: Andres Calle Date: Wed, 31 May 2017 14:34:42 -0500 Subject: [PATCH 8/8] [10.0][Stock] Fixed migration of rule picking type for mrp --- addons/stock/migrations/10.0.1.1/post-migration.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/stock/migrations/10.0.1.1/post-migration.py b/addons/stock/migrations/10.0.1.1/post-migration.py index db5eea709759..85b3d85b5339 100644 --- a/addons/stock/migrations/10.0.1.1/post-migration.py +++ b/addons/stock/migrations/10.0.1.1/post-migration.py @@ -18,11 +18,11 @@ def update_picking_type_id(env): :param env: environment variable (self) """ # load xml data to be used for filling in missing info - xml_stock_picking_type_int = env.ref("stock.picking_type_internal") - xml_stock_picking_type_out = env.ref("stock.picking_type_out") - xml_stock_picking_type_in = env.ref("stock.picking_type_in") + xml_stock_picking_type_int = env.ref("stock.picking_type_internal", False) + xml_stock_picking_type_out = env.ref("stock.picking_type_out", False) + xml_stock_picking_type_in = env.ref("stock.picking_type_in", False) xml_stock_picking_type_manufacturing = env.ref( - "mrp.picking_type_manufacturing" + "mrp.picking_type_manufacturing", False ) # verify for each procurement rule to set procurement_rules_to_set = env['procurement.rule'].search([])