-
-
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_inventory_preparation_filter: Migration to 16.0
- Loading branch information
1 parent
e070f93
commit 993ed6c
Showing
4 changed files
with
56 additions
and
97 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
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 |
---|---|---|
|
@@ -3,81 +3,44 @@ | |
# Copyright 2020 Sergio Teruel <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import _, api, fields, models | ||
from odoo import fields, models | ||
from odoo.tools.safe_eval import safe_eval | ||
|
||
|
||
class StockInventory(models.Model): | ||
_inherit = "stock.inventory" | ||
|
||
@api.model | ||
def _selection_filter(self): | ||
"""Get the list of filter allowed according to the options checked | ||
in 'Settings / Warehouse'.""" | ||
res_filter = [ | ||
("products", _("All products")), | ||
("categories", _("Selected Categories")), | ||
("domain", _("Filtered Products")), | ||
] | ||
if self.user_has_groups("stock.group_production_lot"): | ||
res_filter.insert(-1, ("lots", _("Selected Lots"))) | ||
return res_filter | ||
|
||
filter = fields.Selection( | ||
string="Inventory of", | ||
selection="_selection_filter", | ||
required=True, | ||
default="products", | ||
help="If you do an entire inventory, you can choose 'All Products' and " | ||
"it will prefill the inventory with the current stock. If you " | ||
"only do some products (e.g. Cycle Counting) you can choose " | ||
"'Manual Selection of Products' and the system won't propose " | ||
"anything. You can also let the system propose for a single " | ||
"product / lot /... ", | ||
) | ||
categ_ids = fields.Many2many( | ||
comodel_name="product.category", | ||
relation="rel_inventories_categories", | ||
column1="inventory_id", | ||
column2="category_id", | ||
string="Categories", | ||
) | ||
lot_ids = fields.Many2many( | ||
comodel_name="stock.production.lot", | ||
relation="rel_inventories_lots", | ||
column1="inventory_id", | ||
column2="lot_id", | ||
string="Lots", | ||
product_selection = fields.Selection( | ||
selection_add=[("domain", "Filtered Products")], | ||
ondelete={"domain": "set default"}, | ||
) | ||
|
||
product_domain = fields.Char("Domain", default=[("name", "ilike", "")]) | ||
|
||
def _action_start(self): | ||
def action_state_to_in_progress(self): | ||
for inventory in self: | ||
if inventory.state != "draft": | ||
continue | ||
if inventory.filter: | ||
if inventory.product_selection: | ||
products = inventory._prepare_inventory_filter() | ||
if products: | ||
inventory.product_ids = [(6, 0, products.ids)] | ||
return super()._action_start() | ||
return super().action_state_to_in_progress() | ||
|
||
def _prepare_inventory_filter(self): | ||
# This method is designed to be inherited by other modules | ||
# such as the OCA module stock_inventory_preparation_filter_pos | ||
self.ensure_one() | ||
Product = self.env["product.product"] | ||
products = Product | ||
if self.filter == "categories": | ||
products = Product.search([("categ_id", "in", self.categ_ids.ids)]) | ||
elif self.filter == "lots": | ||
products = self.lot_ids.product_id | ||
elif self.filter == "domain": | ||
if self.product_selection == "domain": | ||
domain = safe_eval(self.product_domain) | ||
products = Product.search(domain) | ||
return products | ||
|
||
def _get_inventory_lines_values(self): | ||
vals = super()._get_inventory_lines_values() | ||
if self.filter == "lots": | ||
vals = list(filter(lambda x: x["prod_lot_id"] in self.lot_ids.ids, vals)) | ||
return vals | ||
def _get_quants(self, locations): | ||
if self.product_selection == "domain": | ||
domain = safe_eval(self.product_domain) | ||
products = self.env["product.product"].search(domain) | ||
return self.env["stock.quant"].search([("product_id", "in", products.ids)]) | ||
return super()._get_quants(locations) |
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
32 changes: 6 additions & 26 deletions
32
stock_inventory_preparation_filter/views/stock_inventory_view.xml
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,47 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<odoo> | ||
<record model="ir.ui.view" id="view_inventory_form"> | ||
<record model="ir.ui.view" id="view_inventory_group_form"> | ||
<field name="model">stock.inventory</field> | ||
<field name="inherit_id" ref="stock.view_inventory_form" /> | ||
<field name="inherit_id" ref="stock_inventory.view_inventory_group_form" /> | ||
<field name="arch" type="xml"> | ||
<field name="product_ids" position="attributes"> | ||
<attribute | ||
name="attrs" | ||
>{'invisible': [('filter', '!=', 'products')]}</attribute> | ||
>{'invisible': [('product_selection', '!=', 'all')]}</attribute> | ||
</field> | ||
<xpath expr="//field[@name='location_ids']/../.." position="before"> | ||
<xpath expr="//field[@name='owner_id']/../.." position="after"> | ||
<group name="preparation_filter"> | ||
<group name="preparation_filter_left"> | ||
<field | ||
name="filter" | ||
string="Inventory of" | ||
widget='radio' | ||
attrs="{'readonly': [('state', '!=', 'draft')]}" | ||
/> | ||
</group> | ||
<group name="preparation_filter_right"> | ||
<field | ||
name="product_domain" | ||
nolabel="1" | ||
widget="domain" | ||
attrs="{'invisible': [('filter', '!=', 'domain')]}" | ||
attrs="{'invisible': [('product_selection', '!=', 'domain')]}" | ||
options="{'model': 'product.product'}" | ||
/> | ||
</group> | ||
</group> | ||
</xpath> | ||
<field name="product_ids" position="after"> | ||
<field | ||
name="categ_ids" | ||
widget="many2many_tags" | ||
attrs="{'invisible':[('filter','!=','categories')], 'required': [('filter','=','categories')]}" | ||
/> | ||
<field | ||
name="lot_ids" | ||
widget="many2many_tags" | ||
attrs="{'invisible':[('filter','!=','lots')], 'required': [('filter','=','lots')]}" | ||
/> | ||
</field> | ||
|
||
</field> | ||
</record> | ||
</odoo> |