Skip to content

Commit 936bb80

Browse files
committed
[IMP]stock_inventory_non_stocked: Applied changes from code review, code optimization and some fixes.
1 parent 5f73a03 commit 936bb80

File tree

5 files changed

+81
-66
lines changed

5 files changed

+81
-66
lines changed

stock_inventory_non_stocked/README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ This module adds a option to a create a 0 quantity quant for products without an
3535
.. contents::
3636
:local:
3737

38+
Use Cases / Context
39+
===================
40+
41+
In older versions of `stock_inventory`, the lines in the `stock.inventory` model were not part of the `stock.quant` model.
42+
In those versions, a line was created for every product found by the inventory adjustment group, regardless of whether
43+
the product was in stock or not.
44+
45+
Currently, the lines of the model use Odoo's base `stock.quant` model. This means that if you select any configuration
46+
that includes a product without a `stock.quant` record, no line is created for that product. This behavior disrupts the
47+
intended functionality of the `stock_inventory` module, which is supposed to group all lines into a single record. If the
48+
lines do not exist, they are not displayed, making the adjustment process meaningless.
49+
3850
Usage
3951
=====
4052

stock_inventory_non_stocked/models/product_template.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,13 @@ class ProductProduct(models.Model):
1010
has_quants = fields.Boolean(
1111
name="Has Quants",
1212
compute="_compute_has_quants",
13+
precompute=True,
1314
store=True,
1415
)
1516

16-
@api.depends("qty_available", "incoming_qty", "outgoing_qty")
17-
def _compute_has_quants(self):
18-
for record in self:
19-
record.has_quants = (
20-
self.env["stock.quant"].search_count([("product_id", "=", record.id)])
21-
> 0
22-
)
23-
2417
lot_ids = fields.One2many("stock.lot", "product_id", string="Lots", copy=False)
2518

26-
27-
class StockQuant(models.Model):
28-
_inherit = "stock.quant"
29-
30-
@api.model_create_multi
31-
def create(self, vals_list):
32-
res = super(StockQuant, self).create(vals_list)
33-
res.product_id._compute_has_quants()
34-
return res
19+
@api.depends("qty_available", "incoming_qty", "outgoing_qty", "stock_quant_ids")
20+
def _compute_has_quants(self):
21+
for record in self:
22+
record.has_quants = record.stock_quant_ids and True or False

stock_inventory_non_stocked/models/stock_inventory.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,17 @@ class StockInventory(models.Model):
1818
def _get_quants(self, locations):
1919
res = super(StockInventory, self)._get_quants(locations)
2020
if self.create_non_stocked:
21-
if self.product_selection == "all":
22-
product_ids = self.env["product.product"].search(
23-
[
24-
("has_quants", "=", False),
25-
("active", "=", True),
26-
("type", "=", "product"),
27-
]
28-
)
29-
new_quants = self.create_zero_quants(product_ids)
30-
res |= new_quants
31-
elif self.product_selection in ["manual", "one"]:
32-
product_ids = self.product_ids.filtered(lambda x: not x.has_quants)
33-
new_quants = self.create_zero_quants(product_ids)
34-
res |= new_quants
21+
self._check_responsible_of_inventory()
22+
# If the option selected is all, this domain will be applied
23+
domain = [
24+
("has_quants", "=", False),
25+
("active", "=", True),
26+
("type", "=", "product"),
27+
]
28+
if self.product_selection in ["manual", "one"]:
29+
domain.append(("id", "in", self.product_ids.ids))
3530
elif self.product_selection == "category":
36-
product_ids = self.env["product.product"].search(
37-
[
38-
("has_quants", "=", False),
39-
("active", "=", True),
40-
("type", "=", "product"),
41-
("categ_id", "=", self.category_id.id),
42-
]
43-
)
44-
new_quants = self.create_zero_quants(product_ids)
45-
res |= new_quants
31+
domain.append(("categ_id", "=", self.category_id.id))
4632
elif self.product_selection == "domain":
4733
domain = self.product_domain
4834
domain = (
@@ -52,38 +38,48 @@ def _get_quants(self, locations):
5238
+ domain[-1:]
5339
)
5440
domain = ast.literal_eval(domain)
55-
product_ids = self.env["product.product"].search(domain)
56-
new_quants = self.create_zero_quants(product_ids)
57-
res |= new_quants
41+
new_quants = self.create_zero_quants(domain)
42+
res |= new_quants
5843
return res
5944

60-
def create_zero_quants(self, products):
61-
new_quants = self.env["stock.quant"]
45+
def create_zero_quants(self, domain):
46+
products = self.env["product.product"].search_read(
47+
domain, ["id", "tracking", "lot_ids"]
48+
)
49+
values = []
6250
for product in products:
6351
for location in self.location_ids:
6452
# If the product is tracked by lot, we create a quant for each lot
6553
# If the product is not tracked, we create a single quant
6654
# If the product is tracked but has no lots, we don't create any quant
67-
if product.tracking == "lot" and product.lot_ids:
55+
if product["tracking"] in ["lot", "serial"] and product.lot_ids:
6856
for lot in product.lot_ids:
69-
new_quants |= self.env["stock.quant"].create(
57+
values.append(
7058
{
71-
"product_id": product.id,
59+
"product_id": product["id"],
7260
"location_id": location.id,
7361
"quantity": 0.0,
7462
"lot_id": lot.id,
75-
"user_id": self.env.uid,
7663
"last_count_date": fields.Datetime.now(),
7764
}
7865
)
79-
elif product.tracking == "none":
80-
new_quants |= self.env["stock.quant"].create(
66+
elif product["tracking"] == "none":
67+
values.append(
8168
{
82-
"product_id": product.id,
69+
"product_id": product["id"],
8370
"location_id": location.id,
8471
"quantity": 0.0,
85-
"user_id": self.env.uid,
8672
"last_count_date": fields.Datetime.now(),
8773
}
8874
)
75+
new_quants = self.env["stock.quant"].create(values)
8976
return new_quants
77+
78+
# This method is designed to assing a responsible
79+
# user to the inventory if it is not assigned
80+
# this is necessary to avoid _unlink_zero_quants()
81+
# method to remove quants created by this module
82+
def _check_responsible_of_inventory(self):
83+
for record in self:
84+
if not record.responsible_id:
85+
record.responsible_id = self.env.user
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In older versions of `stock_inventory`, the lines in the `stock.inventory` model were not part of the `stock.quant` model.
2+
In those versions, a line was created for every product found by the inventory adjustment group, regardless of whether
3+
the product was in stock or not.
4+
5+
Currently, the lines of the model use Odoo's base `stock.quant` model. This means that if you select any configuration
6+
that includes a product without a `stock.quant` record, no line is created for that product. This behavior disrupts the
7+
intended functionality of the `stock_inventory` module, which is supposed to group all lines into a single record. If the
8+
lines do not exist, they are not displayed, making the adjustment process meaningless.

stock_inventory_non_stocked/static/description/index.html

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -373,49 +373,60 @@ <h1 class="title">Stock Inventory Non Stocked</h1>
373373
<p><strong>Table of contents</strong></p>
374374
<div class="contents local topic" id="contents">
375375
<ul class="simple">
376-
<li><a class="reference internal" href="#usage" id="toc-entry-1">Usage</a></li>
377-
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-2">Bug Tracker</a></li>
378-
<li><a class="reference internal" href="#credits" id="toc-entry-3">Credits</a><ul>
379-
<li><a class="reference internal" href="#authors" id="toc-entry-4">Authors</a></li>
380-
<li><a class="reference internal" href="#contributors" id="toc-entry-5">Contributors</a></li>
381-
<li><a class="reference internal" href="#maintainers" id="toc-entry-6">Maintainers</a></li>
376+
<li><a class="reference internal" href="#use-cases-context" id="toc-entry-1">Use Cases / Context</a></li>
377+
<li><a class="reference internal" href="#usage" id="toc-entry-2">Usage</a></li>
378+
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-3">Bug Tracker</a></li>
379+
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
380+
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
381+
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
382+
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
382383
</ul>
383384
</li>
384385
</ul>
385386
</div>
387+
<div class="section" id="use-cases-context">
388+
<h1><a class="toc-backref" href="#toc-entry-1">Use Cases / Context</a></h1>
389+
<p>In older versions of <cite>stock_inventory</cite>, the lines in the <cite>stock.inventory</cite> model were not part of the <cite>stock.quant</cite> model.
390+
In those versions, a line was created for every product found by the inventory adjustment group, regardless of whether
391+
the product was in stock or not.</p>
392+
<p>Currently, the lines of the model use Odoo’s base <cite>stock.quant</cite> model. This means that if you select any configuration
393+
that includes a product without a <cite>stock.quant</cite> record, no line is created for that product. This behavior disrupts the
394+
intended functionality of the <cite>stock_inventory</cite> module, which is supposed to group all lines into a single record. If the
395+
lines do not exist, they are not displayed, making the adjustment process meaningless.</p>
396+
</div>
386397
<div class="section" id="usage">
387-
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
398+
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
388399
<ul class="simple">
389400
<li>In the inventory adjustments, create a new adjustment group.</li>
390401
<li>Enable the option of create non stocked products.</li>
391402
<li>Start the inventory adjustment, it will create new quants for the non stocked products.</li>
392403
</ul>
393404
</div>
394405
<div class="section" id="bug-tracker">
395-
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
406+
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
396407
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/stock-logistics-warehouse/issues">GitHub Issues</a>.
397408
In case of trouble, please check there if your issue has already been reported.
398409
If you spotted it first, help us to smash it by providing a detailed and welcomed
399410
<a class="reference external" href="https://github.com/OCA/stock-logistics-warehouse/issues/new?body=module:%20stock_inventory_non_stocked%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
400411
<p>Do not contact contributors directly about support or help with technical issues.</p>
401412
</div>
402413
<div class="section" id="credits">
403-
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
414+
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
404415
<div class="section" id="authors">
405-
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
416+
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
406417
<ul class="simple">
407418
<li>Ivan Perez</li>
408419
<li>Coninpe</li>
409420
</ul>
410421
</div>
411422
<div class="section" id="contributors">
412-
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
423+
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
413424
<ul class="simple">
414425
<li>Ivan Perez &lt;<a class="reference external" href="mailto:iperez&#64;coninpe.es">iperez&#64;coninpe.es</a>&gt;</li>
415426
</ul>
416427
</div>
417428
<div class="section" id="maintainers">
418-
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
429+
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
419430
<p>This module is maintained by the OCA.</p>
420431
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
421432
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose

0 commit comments

Comments
 (0)