-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] stock_change_qty_reason: Migration to 15.0
- Loading branch information
1 parent
06dfa70
commit 51b9b0e
Showing
18 changed files
with
220 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
stock_change_qty_reason/migrations/15.0.1.0.0/post-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2023 ForgeFlow <http://www.forgeflow.com> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from openupgradelib import openupgrade | ||
|
||
|
||
def fill_stock_quant_reason(env): | ||
openupgrade.logged_query( | ||
env.cr, | ||
""" | ||
UPDATE stock_move_line sml | ||
SET reason = sil.reason | ||
FROM stock_inventory_line sil | ||
JOIN stock_inventory si ON sil.inventory_id = si.id | ||
JOIN stock_location sl ON sil.location_id = sl.id | ||
JOIN stock_move sm ON sm.inventory_id = si.id | ||
WHERE sil.reason IS NOT NULL AND sml.move_id = sm.id | ||
AND sml.location_id = sl.id | ||
AND sl.usage in ('internal', 'transit') | ||
AND si.state = 'done' AND sml.product_id = sil.product_id | ||
AND ((sil.prod_lot_id IS NULL AND sml.lot_id IS NULL) OR ( | ||
sil.prod_lot_id = sml.lot_id)) | ||
AND COALESCE(sml.date, sm.date) = COALESCE( | ||
sil.inventory_date, si.date)""", | ||
) | ||
|
||
|
||
def fill_stock_move_line_preset_reason_id(env): | ||
openupgrade.logged_query( | ||
env.cr, | ||
""" | ||
UPDATE stock_move_line sml | ||
SET preset_reason_id = sm.preset_reason_id | ||
FROM stock_move sm | ||
WHERE sml.move_id = sm.id AND sm.preset_reason_id IS NOT NULL""", | ||
) | ||
|
||
|
||
def fill_stock_move_line_reason(env): | ||
openupgrade.logged_query( | ||
env.cr, | ||
""" | ||
UPDATE stock_move_line sml | ||
SET reason = COALESCE(sml.reason, sqr.name) | ||
FROM stock_quant_reason sqr | ||
WHERE sml.preset_reason_id = sqr.id""", | ||
) | ||
|
||
|
||
@openupgrade.migrate() | ||
def migrate(env, version): | ||
fill_stock_quant_reason(env) | ||
fill_stock_move_line_preset_reason_id(env) | ||
fill_stock_move_line_reason(env) |
25 changes: 25 additions & 0 deletions
25
stock_change_qty_reason/migrations/15.0.1.0.0/pre-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2023 ForgeFlow <http://www.forgeflow.com> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from openupgradelib import openupgrade | ||
|
||
|
||
def fill_required_reason(env): | ||
openupgrade.logged_query( | ||
env.cr, | ||
""" | ||
UPDATE stock_quant_reason sqr | ||
SET name = '???' | ||
WHERE name IS NULL""", | ||
) | ||
|
||
|
||
@openupgrade.migrate() | ||
def migrate(env, version): | ||
openupgrade.rename_tables( | ||
env.cr, [("stock_inventory_line_reason", "stock_quant_reason")] | ||
) | ||
openupgrade.rename_models( | ||
env.cr, [("stock.inventory.line.reason", "stock.quant.reason")] | ||
) | ||
fill_required_reason(env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from . import res_config_settings | ||
from . import stock_inventory | ||
from . import stock_inventory_line | ||
from . import stock_inventory_line_reason | ||
from . import stock_move | ||
from . import stock_move_line | ||
from . import stock_quant | ||
from . import stock_quant_reason |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2019-2023 ForgeFlow S.L. | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). | ||
from odoo import fields, models | ||
|
||
|
||
class StockMoveLine(models.Model): | ||
_inherit = "stock.move.line" | ||
|
||
preset_reason_id = fields.Many2one("stock.quant.reason") | ||
reason = fields.Char(help="Type in a reason for the product quantity change") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2016-2017 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import api, fields, models | ||
|
||
|
||
class StockQuant(models.Model): | ||
"""Class to inherit model stock.quant""" | ||
|
||
_inherit = "stock.quant" | ||
|
||
reason = fields.Char(help="Type in a reason for the product quantity change") | ||
preset_reason_id = fields.Many2one("stock.quant.reason") | ||
|
||
@api.model | ||
def _get_inventory_fields_write(self): | ||
res = super()._get_inventory_fields_write() | ||
res.extend(["reason", "preset_reason_id"]) | ||
return res | ||
|
||
def _get_inventory_move_values(self, qty, location_id, location_dest_id, out=False): | ||
"""Function to super _get_inventory_move_values""" | ||
res = super()._get_inventory_move_values( | ||
qty, location_id, location_dest_id, out | ||
) | ||
context = ( | ||
self.reason if not self.preset_reason_id else self.preset_reason_id.name | ||
) | ||
line = res["move_line_ids"][0][2] | ||
line["reason"] = context | ||
if res.get("origin"): | ||
res["origin"] = " ,".join([res.get("origin"), context]) | ||
else: | ||
res["origin"] = context | ||
if self.preset_reason_id: | ||
line["preset_reason_id"] = self.preset_reason_id.id | ||
self.preset_reason_id = False | ||
if self.reason: | ||
self.reason = False | ||
return res |
11 changes: 5 additions & 6 deletions
11
...son/models/stock_inventory_line_reason.py → ...e_qty_reason/models/stock_quant_reason.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
access_stock_inventory_line_reason_user,stock.inventory.line.reason,model_stock_inventory_line_reason,stock.group_stock_user,1,0,0,0 | ||
access_stock_inventory_line_reason_manager,stock.inventory.line.reason,model_stock_inventory_line_reason,stock.group_stock_manager,1,1,1,1 | ||
access_stock_quant_reason_user,stock.quant.reason,model_stock_quant_reason,stock.group_stock_user,1,0,0,0 | ||
access_stock_quant_reason_manager,stock.quant.reason,model_stock_quant_reason,stock.group_stock_manager,1,1,1,1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.