Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.0] Add Automatic Reordering Rules #258

Merged
merged 4 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion stock_orderpoint_generator/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,40 @@
Order point generator
=====================

Add a wizard to configure order points for multiple products in one go.
Add a wizard to configure reordering rules for multiple products in one go,
and allow to automatically update reordering rules from rule templates.

Configuration
=============

Reordering rule templates can be configured in "Inventory > Configuration >
Products > Reordering Rule Templates".

The frequency of the cron that updates the Reordering Rules can be configured
in "Settings > Technical > Actions > Scheduled Actions". The name of the
scheduled action is "Reordering Rule Templates Generator".

Usage
=====

By activating the "Create Rules Automatically" on a reordering rule template,
you are able to select a list of products. Any change on the template will then
be replicated on the products Reordering Rules. The change is not immediate as
it is processed by a scheduled action.

On a product, you can also choose one or more Reordering Rule Templates. Any
template added or removed on the product is immediately reflected on its
Reordering Rules.

Lastly, you can promptly create Reordering Rules for a product or a product
template using the "Reordering Rules Generator". Note that it will replace all
the existing rules for the product. You will usually not want to use this
feature on products that have Automatic Reordering Rules Templates.


.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/153/9.0


Bug Tracker
Expand All @@ -22,9 +55,11 @@ Credits

Contributors
------------

* Yannick Vaucher <[email protected]>
* Matthieu Dietrich <[email protected]>
* Cyril Gaudin <[email protected]>
* Guewen Baconnier <[email protected]>

Maintainer
----------
Expand Down
5 changes: 4 additions & 1 deletion stock_orderpoint_generator/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
{
'name': 'Order point generator',
'summary': 'Mass configuration of stock order points',
'version': '9.0.1.0.0',
'version': '9.0.1.1.0',
'author': "Camptocamp, Odoo Community Association (OCA)",
'category': 'Warehouse',
'license': 'AGPL-3',
'website': "http://www.camptocamp.com",
'depends': ['stock'],
'data': [
'views/orderpoint_template_views.xml',
'views/product_views.xml',
"wizard/orderpoint_generator_view.xml",
"data/ir_cron.xml",
"security/ir.model.access.csv",
],
'installable': True,
Expand Down
16 changes: 16 additions & 0 deletions stock_orderpoint_generator/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">

<record id="ir_cron_auto_orderpoint_template" model="ir.cron">
<field name="name">Reordering Rule Templates Generator</field>
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="'stock.warehouse.orderpoint.template'" name="model"/>
<field eval="'_cron_create_auto_orderpoints'" name="function"/>
<field eval="'()'" name="args"/>
<field name="active" eval="True" />
</record>

</odoo>
1 change: 1 addition & 0 deletions stock_orderpoint_generator/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-

from . import orderpoint_template
from . import product
29 changes: 29 additions & 0 deletions stock_orderpoint_generator/models/orderpoint_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class OrderpointTemplate(models.Model):
_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'
Expand All @@ -28,6 +29,20 @@ class OrderpointTemplate(models.Model):
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
"""
Expand All @@ -53,3 +68,17 @@ def create_orderpoints(self, product_ids):
"""
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()
33 changes: 33 additions & 0 deletions stock_orderpoint_generator/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from openerp import api, fields, models


class ProductProduct(models.Model):
_inherit = 'product.product'

auto_orderpoint_template_ids = fields.Many2many(
comodel_name='stock.warehouse.orderpoint.template',
string="Automatic Reordering Rules",
domain=[('auto_generate', '=', True)],
help="When one or several automatic reordering rule is selected, "
"a Scheduled Action will automatically generate or update "
"the reordering rules of the product."
)

@api.model
def create(self, vals):
record = super(ProductProduct, self).create(vals)
if vals.get('auto_orderpoint_template_ids'):
record.auto_orderpoint_template_ids.create_orderpoints(record.ids)
return record

@api.multi
def write(self, vals):
result = super(ProductProduct, self).write(vals)
if vals.get('auto_orderpoint_template_ids'):
orderpoint_templates = self.mapped('auto_orderpoint_template_ids')
orderpoint_templates.create_orderpoints(self.ids)
return result
84 changes: 84 additions & 0 deletions stock_orderpoint_generator/views/orderpoint_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="view_warehouse_orderpoint_template_tree" model="ir.ui.view">
<field name="name">stock.warehouse.orderpoint.template.tree</field>
<field name="model">stock.warehouse.orderpoint.template</field>
<field name="mode">primary</field>
<field name="inherit_id" ref="stock.view_warehouse_orderpoint_tree"/>
<field name="arch" type="xml">
<xpath expr="//tree" position="attributes">
<attribute name="name">Reordering Rule Templates</attribute>
</xpath>
<field name="product_max_qty" position="after">
<field name="auto_generate"/>
</field>
<field name="product_id" position="replace"></field>
<field name="product_uom" position="replace"></field>
</field>
</record>

<record model="ir.ui.view" id="view_warehouse_orderpoint_template_search">
<field name="name">stock.warehouse.orderpoint.template.search</field>
<field name="model">stock.warehouse.orderpoint.template</field>
<field name="mode">primary</field>
<field name="inherit_id" ref="stock.warehouse_orderpoint_search"/>
<field name="arch" type="xml">
<xpath expr="//search" position="attributes">
<attribute name="name">Reordering Rule Templates</attribute>
</xpath>
<field name="product_id" position="replace"></field>
</field>
</record>

<record id="view_warehouse_orderpoint_template_form" model="ir.ui.view">
<field name="name">stock.warehouse.orderpoint.template.form</field>
<field name="model">stock.warehouse.orderpoint.template</field>
<field name="mode">primary</field>
<field name="inherit_id" ref="stock.view_warehouse_orderpoint_form"/>
<field name="arch" type="xml">
<xpath expr="//form" position="attributes">
<attribute name="name">Reordering Rule Templates</attribute>
</xpath>
<field name="product_id" position="replace"></field>
<field name="active" position="after">
<field name="auto_generate"/>
</field>

<sheet position="before">
<header>
<button name="create_auto_orderpoints" type="object" string="Generate Automatic Rules"
help="Reordering rules will be created for the selected products. This is equivalent to the Scheduled Action action."
attrs="{'invisible': [('auto_generate', '=', False)]}"/>
</header>
</sheet>
<xpath expr="//sheet/group[2]" position="inside">
<group string="Automatic Rules" name="auto_rules" attrs="{'invisible': [('auto_generate', '=', False)]}">
<field name="auto_product_ids"/>
</group>
</xpath>
</field>
</record>

<record id="action_orderpoint_template" model="ir.actions.act_window">
<field name="name">Reordering Rule Templates</field>
<field name="res_model">stock.warehouse.orderpoint.template</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="view_warehouse_orderpoint_template_tree"/>
<field name="search_view_id" ref="view_warehouse_orderpoint_template_search" />
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a reordering rule template.
</p>
</field>
</record>

<menuitem
id="menu_orderpoint_template" name="Reordering Rule Templates"
parent="stock.menu_product_in_config_stock"
action="action_orderpoint_template"
/>

</odoo>
14 changes: 14 additions & 0 deletions stock_orderpoint_generator/views/product_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="product_normal_form_view" model="ir.ui.view">
<field name="name">product.product.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<field name="seller_ids" position="before">
<separator string="Automatic Reordering Rules"/>
<field name="auto_orderpoint_template_ids" context="{'default_auto_generate': True}"/>
</field>
</field>
</record>
</odoo>
2 changes: 1 addition & 1 deletion stock_orderpoint_generator/wizard/orderpoint_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OrderpointGenerator(models.TransientModel):
orderpoint_template_id = fields.Many2many(
'stock.warehouse.orderpoint.template',
rel='order_point_generator_rel',
string='Stock rule template'
string='Reordering Rule Templates'
)

@api.multi
Expand Down
11 changes: 5 additions & 6 deletions stock_orderpoint_generator/wizard/orderpoint_generator_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
<field name="name">stock.warehouse.orderpoint.generator</field>
<field name="model">stock.warehouse.orderpoint.generator</field>
<field name="arch" type="xml">
<form string="Product warehouse config">
<form string="Reordering Rules Generator">
<label string="This wizard will apply the following orderpoint to selected product(s)"/>
<group string="Templates" colspan="4">
<field name="orderpoint_template_id" colspan="4"/>
</group>
<footer>
<button name="action_configure" string="Apply" type="object" class="oe_highlight"
icon="gtk-execute"/>
<button string="Cancel" class="oe_link" special="cancel"/>
<button name="action_configure" string="Apply" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>

<act_window name="Product warehouse config"
<act_window name="Reordering Rules Generator"
res_model="stock.warehouse.orderpoint.generator"
src_model="product.product"
view_mode="form"
target="new"
key2="client_action_multi"
id="act_create_product_conf"/>

<act_window name="Product warehouse config"
<act_window name="Reordering Rules Generator"
res_model="stock.warehouse.orderpoint.generator"
src_model="product.template"
view_mode="form"
Expand Down