[FIX] purchase_stock: post init hook for warehouses rules

This commit is contained in:
Arnold Moyaux
2018-07-19 14:43:08 +02:00
committed by Simon Lejeune
parent 827958a890
commit 202b0515c8
3 changed files with 21 additions and 6 deletions
+13
View File
@@ -3,3 +3,16 @@
from . import models
from . import report
from odoo import api, SUPERUSER_ID
def _create_buy_rules(cr, registry):
""" This hook is used to add a default buy_pull_id on every warehouse. It is
necessary if the purchase_stock module is installed after some warehouses
were already created.
"""
env = api.Environment(cr, SUPERUSER_ID, {})
warehouse_ids = env['stock.warehouse'].search([('buy_pull_id', '=', False)])
for warehouse_id in warehouse_ids:
warehouse_id.buy_pull_id = warehouse_id._create_buy_rule()
+1
View File
@@ -26,4 +26,5 @@
],
'installable': True,
'auto_install': True,
'post_init_hook': '_create_buy_rules',
}
+7 -6
View File
@@ -135,9 +135,7 @@ class StockWarehouse(models.Model):
def create_routes(self):
res = super(StockWarehouse, self).create_routes() # super applies ensure_one()
if self.buy_to_resupply:
buy_pull_vals = self._get_buy_pull_rule()
buy_pull = self.env['stock.rule'].create(buy_pull_vals)
res['buy_pull_id'] = buy_pull.id
res['buy_pull_id'] = self._create_buy_rule()
return res
@api.multi
@@ -146,9 +144,7 @@ class StockWarehouse(models.Model):
if vals.get("buy_to_resupply"):
for warehouse in self:
if not warehouse.buy_pull_id:
buy_pull_vals = self._get_buy_pull_rule()
buy_pull = self.env['stock.rule'].create(buy_pull_vals)
vals['buy_pull_id'] = buy_pull.id
vals['buy_pull_id'] = self._create_buy_rule()
else:
for warehouse in self:
if warehouse.buy_pull_id:
@@ -178,6 +174,11 @@ class StockWarehouse(models.Model):
warehouse.buy_pull_id.write({'location_id': warehouse.in_type_id.default_location_dest_id.id})
return res
def _create_buy_rule(self):
buy_pull_vals = self._get_buy_pull_rule()
buy_pull = self.env['stock.rule'].create(buy_pull_vals)
return buy_pull.id
class ReturnPicking(models.TransientModel):
_inherit = "stock.return.picking"