Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions coop_inventory_recurrent/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ cooperatives.

It allows users to:

- Create and manage recurring physical inventory adjustments grouped by
product category groups.
- Print inventory reports for one or multiple inventory adjustments at
once.
- Create and manage recurring physical inventory adjustments grouped by
product category groups.
- Print inventory reports for one or multiple inventory adjustments at
once.

**Table of contents**

Expand Down
20 changes: 12 additions & 8 deletions coop_inventory_recurrent/models/stock_inventory_category_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ def _default_location_id(self):

@api.onchange("category_ids")
def onchange_category_ids(self):
lines_by_categ = {line.category_id.id: line for line in self._origin.line_ids}
lines_by_categ.update({line.category_id.id: line for line in self.line_ids})
lines_in_form = {line.category_id.id: line.copies for line in self.line_ids}
lines_in_origin = {
line.category_id.id: line.copies for line in self._origin.line_ids
}
GroupLine = self.env["stock.inventory.category.group.line"]
new_lines = self.env["stock.inventory.category.group.line"]
for categ in self.category_ids:
existing = lines_by_categ.get(categ.id)
new_lines |= (
existing
if existing
else GroupLine.new({"category_id": categ.id, "copies": "2"})
)
cat_id = categ._origin.id
if cat_id in lines_in_form:
copies = lines_in_form[cat_id]
elif cat_id in lines_in_origin:
copies = lines_in_origin[cat_id]
else:
copies = "2"
new_lines |= GroupLine.new({"category_id": categ.id, "copies": copies})
self.line_ids = new_lines
12 changes: 8 additions & 4 deletions coop_inventory_recurrent/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ class StockQuant(models.Model):
compute="_compute_category_group_line_id",
)

@api.depends("product_id.categ_id")
@api.depends("product_id.categ_id", "current_inventory_id")
def _compute_category_group_line_id(self):
GroupLine = self.env["stock.inventory.category.group.line"]
for quant in self:
quant.category_group_line_id = GroupLine.search(
[("category_id", "=", quant.product_id.categ_id.id)], limit=1
)
inv_line = quant.current_inventory_id.category_group_line_id
if inv_line:
quant.category_group_line_id = inv_line
else:
quant.category_group_line_id = GroupLine.search(
[("category_id", "=", quant.product_id.categ_id.id)], limit=1
)

def get_copi_variants(self):
res0 = []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html

from odoo import Command, fields, models
from odoo import Command, _, fields, models
from odoo.exceptions import UserError


class StockInventoryRecurrentWizard(models.TransientModel):
Expand All @@ -15,33 +16,34 @@ class StockInventoryRecurrentWizard(models.TransientModel):

def action_execute(self):
self.ensure_one()
all_category_ids = self.category_group_ids.mapped("line_ids.category_id").ids
existing_in_progress = self.env["stock.inventory"].search(
[("category_id", "in", all_category_ids), ("state", "=", "in_progress")]
)
if existing_in_progress:
names = ", ".join(existing_in_progress.mapped("name"))
raise UserError(
_(
"The following inventories are already in progress: %(names)s. "
"Please validate or cancel them before generating new ones.",
names=names,
)
)
inventories = self.env["stock.inventory"]
new_inventories = self.env["stock.inventory"]
for categ_group in self.category_group_ids:
for line in categ_group.line_ids:
existing = self.env["stock.inventory"].search(
[
("category_id", "=", line.category_id.id),
("state", "=", "in_progress"),
],
limit=1,
inventory = self.env["stock.inventory"].create(
{
"name": line.category_id.name,
"product_selection": "category",
"category_id": line.category_id.id,
"category_group_line_id": line.id,
"location_ids": [Command.set(categ_group.location_id.ids)],
}
)
if existing:
if existing.category_group_line_id != line:
existing.category_group_line_id = line
inventories |= existing
else:
inventory = self.env["stock.inventory"].create(
{
"name": line.category_id.name,
"product_selection": "category",
"category_id": line.category_id.id,
"category_group_line_id": line.id,
"location_ids": [Command.set(categ_group.location_id.ids)],
}
)
new_inventories |= inventory
inventories |= inventory
new_inventories |= inventory
inventories |= inventory
for inventory in new_inventories:
inventory.action_state_to_in_progress()
tree_view_id = self.env.ref("stock_inventory.view_inventory_group_tree").id
Expand Down
Loading