mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
And improve the wording * Add an option to automatically generate rules When a product variant is created with a (or several) rule template(s), it automatically generates (on the fly) the corresponding rule(s). When a rule template is modified, it automatically updates the existing rules of the linked products (it uses the mechanism in place, disable the old and create a fresh one). The latter update is done by a cron because it might take a long time to update all the products reordering rules. * Add documentation * Copy orderpoint views to orderpoint templates Unfortunately we cannot rely on the possibility to copy a view with "inherit_id" + "mode=primary" in Odoo 9.0 in this use case (precisely with a model that is a "copy by prototype"). The explanation: We "copy by prototype" the model "stock.warehouse.orderpoint" to a new "stock.warehouse.orderpoint.template" model (with both _inherit and different _name). Before this commit, we were reusing the stock.warehouse.orderpoint's views, just making the changes needed for the templates. Thing is: when a third (unrelated) addon adds a column on the model, the ORM doesn't add the column in the stock.warehouse.orderpoint.template model. So the templates' views complains about this unexisting field. Therefore, copy-pasting the view ensure that changes on 'stock.warehouse.orderpoint' does not have any side effect on the templates. From Odoo 10.0, the "copy by prototype" reports the changes made on the "prototype" model to the "copy" so we should be able to revert to the "inherit_id" + "mode=primary" views.
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# © 2012-2016 Camptocamp SA
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
from openerp import api, fields, models
|
|
|
|
|
|
class OrderpointTemplate(models.Model):
|
|
""" Template for orderpoints
|
|
|
|
Here we use same model as stock.warehouse.orderpoint but set product_id
|
|
as non mandatory as we cannot remove it. This field will be ignored.
|
|
|
|
This has the advantage of ensuring that the order point
|
|
and the order point template have the same fields.
|
|
|
|
_table is redefined to separate templates from orderpoints
|
|
"""
|
|
_name = 'stock.warehouse.orderpoint.template'
|
|
_description = 'Reordering Rule Templates'
|
|
|
|
_inherit = 'stock.warehouse.orderpoint'
|
|
_table = 'stock_warehouse_orderpoint_template'
|
|
|
|
name = fields.Char(copy=True)
|
|
group_id = fields.Many2one(copy=True)
|
|
|
|
product_id = fields.Many2one(required=False)
|
|
product_uom = fields.Many2one(required=False)
|
|
|
|
auto_generate = fields.Boolean(
|
|
string='Create Rules Automatically',
|
|
help="When checked, the 'Reordering Rule Templates Generator' "
|
|
"scheduled action will automatically update the rules of a "
|
|
"selection of products."
|
|
)
|
|
auto_product_ids = fields.Many2many(
|
|
comodel_name='product.product',
|
|
string='Products',
|
|
help="A reordering rule will be automatically created by the "
|
|
"scheduled action for every product in this list."
|
|
)
|
|
auto_last_generation = fields.Datetime(string='Last Automatic Generation')
|
|
|
|
def _disable_old_instances(self, product_ids):
|
|
""" Clean old instance by setting those inactives
|
|
"""
|
|
orderpoints = self.env['stock.warehouse.orderpoint'].search(
|
|
[('product_id', 'in', product_ids)]
|
|
)
|
|
orderpoints.write({'active': False})
|
|
|
|
def _create_instances(self, product_ids):
|
|
""" Create instances of model using template inherited model
|
|
"""
|
|
orderpoint_model = self.env['stock.warehouse.orderpoint']
|
|
for data in self.copy_data():
|
|
for product_id in product_ids:
|
|
data['product_id'] = product_id
|
|
orderpoint_model.create(data)
|
|
|
|
@api.multi
|
|
def create_orderpoints(self, product_ids):
|
|
""" Create orderpoint for *product_ids* based on these templates.
|
|
|
|
:type product_ids: list of int
|
|
"""
|
|
self._disable_old_instances(product_ids)
|
|
self._create_instances(product_ids)
|
|
|
|
@api.multi
|
|
def create_auto_orderpoints(self):
|
|
for template in self:
|
|
if not template.auto_generate:
|
|
continue
|
|
if (not template.auto_last_generation or
|
|
template.write_date > template.auto_last_generation):
|
|
template.auto_last_generation = fields.Datetime.now()
|
|
template.create_orderpoints(template.auto_product_ids.ids)
|
|
|
|
@api.model
|
|
def _cron_create_auto_orderpoints(self):
|
|
self.search([('auto_generate', '=', True)]).create_auto_orderpoints()
|