Skip to content

Commit

Permalink
[15.0][IMP] stock_inventory: add product selection and fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidJForgeFlow committed Dec 22, 2022
1 parent 9ad7b96 commit d440ff7
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 45 deletions.
2 changes: 2 additions & 0 deletions stock_inventory/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "Stock Inventory Adjustment",
"version": "15.0.1.0.0",
"license": "LGPL-3",
"maintainer": ["DavidJForgeFlow"],
"development_status": "Beta",
"category": "Inventory/Inventory",
"summary": "Allows to do an easier follow up of the Inventory Adjustments",
"author": "ForgeFlow, Odoo Community Association (OCA)",
Expand Down
150 changes: 120 additions & 30 deletions stock_inventory/models/stock_inventory.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.osv import expression


class InventoryAdjustmentsGroup(models.Model):
_name = "stock.inventory"
_description = "Inventory Adjustment Group"
_order = "date desc, id desc"

name = fields.Char(
required=True, default="Inventory ", string="Inventory Reference"
)
name = fields.Char(required=True, default="Inventory", string="Inventory Reference")

date = fields.Datetime(default=lambda self: fields.Datetime.now())

Expand All @@ -18,12 +17,22 @@ class InventoryAdjustmentsGroup(models.Model):
default="draft",
)

owner_id = fields.Many2one(
"res.partner", "Owner", help="This is the owner of the inventory adjustment"
)

location_ids = fields.Many2many(
"stock.location", string="Location", domain="[('usage', '=', 'internal')]"
"stock.location", string="Locations", domain="[('usage', '=', 'internal')]"
)

product_selection = fields.Selection(
[("all", "All Products"), ("manual", "Manual Selection")],
[
("all", "All Products"),
("manual", "Manual Selection"),
("category", "Product Category"),
("one", "One Product"),
("lot", "Lot/Serial Number"),
],
default="all",
required=True,
)
Expand All @@ -32,6 +41,13 @@ class InventoryAdjustmentsGroup(models.Model):

stock_quant_ids = fields.Many2many("stock.quant", string="Inventory Adjustment")

category_id = fields.Many2one("product.category", string="Product Category")

lot_ids = fields.Many2many(
"stock.production.lot",
string="Lot/Serial Numbers",
)

stock_move_ids = fields.One2many(
"stock.move.line",
"inventory_adjustment_id",
Expand Down Expand Up @@ -67,44 +83,99 @@ def _compute_count_stock_moves(self):
sm_ids = self.mapped("stock_move_ids").ids
self.count_stock_moves = len(sm_ids)

def _get_quants(self, locations):
domain = []
base_domain = self._get_base_domain(locations)
if self.product_selection == "all":
domain = self._get_domain_all_quants(base_domain)
elif self.product_selection == "manual":
domain = self._get_domain_manual_quants(base_domain)
elif self.product_selection == "one":
domain = self._get_domain_one_quant(base_domain)
elif self.product_selection == "lot":
domain = self._get_domain_lot_quants(base_domain)
elif self.product_selection == "category":
domain = self._get_domain_category_quants(base_domain)
return self.env["stock.quant"].search(domain)

def _get_base_domain(self, locations):
return [
"|",
("location_id", "in", locations.mapped("id")),
("location_id", "in", locations.child_ids.ids),
]

def _get_domain_all_quants(self, base_domain):
return base_domain

def _get_domain_manual_quants(self, base_domain):
return expression.AND(
[base_domain, [("product_id", "in", self.product_ids.ids)]]
)

def _get_domain_one_quant(self, base_domain):
return expression.AND(
[
base_domain,
[
("product_id", "in", self.product_ids.ids),
],
]
)

def _get_domain_lot_quants(self, base_domain):
return expression.AND(
[
base_domain,
[
("product_id", "in", self.product_ids.ids),
("lot_id", "in", self.lot_ids.ids),
],
]
)

def _get_domain_category_quants(self, base_domain):
return expression.AND(
[
base_domain,
[
"|",
("product_id.categ_id", "=", self.category_id.id),
("product_id.categ_id", "in", self.category_id.child_id.ids),
],
]
)

def action_state_to_in_progress(self):
active_rec = self.env["stock.inventory"].search([("state", "=", "in_progress")])
active_rec = self.env["stock.inventory"].search(
[
("state", "=", "in_progress"),
"|",
("location_ids", "in", self.location_ids.mapped("id")),
("location_ids", "in", self.location_ids.child_ids.ids),
],
limit=1,
)
if active_rec:
raise ValidationError(
_("There's already an Adjustment in Process: %s") % active_rec.name
_(
"There's already an Adjustment in Process using one requested Location: %s"
)
% active_rec.name
)
self.state = "in_progress"
if self.product_selection == "all":
for location in self._origin.location_ids:
self.stock_quant_ids = self.env["stock.quant"].search(
[
"|",
("location_id", "=", location.id),
("location_id", "in", location.child_ids.ids),
]
)
else:
for location in self._origin.location_ids:
self.stock_quant_ids = self.env["stock.quant"].search(
[
("product_id", "in", self.product_ids.ids),
"|",
("location_id", "=", location.id),
("location_id", "in", location.child_ids.ids),
]
)
self.stock_quant_ids = self._get_quants(self.location_ids)
self.stock_quant_ids.update({"to_do": True})
return

def action_state_to_done(self):
self.state = "done"
for quant in self.stock_quant_ids:
quant.to_do = True
self.stock_quant_ids.update({"to_do": True})
return

def action_state_to_draft(self):
self.state = "draft"
for quant in self.stock_quant_ids:
quant.to_do = True
self.stock_quant_ids.update({"to_do": True})
self.stock_quant_ids = None
return

Expand All @@ -124,3 +195,22 @@ def action_view_stock_moves(self):
result["domain"] = [("id", "in", sm_ids)]
result["context"] = []
return result

@api.constrains("product_selection", "product_ids")
def _check_one_product_in_product_selection(self):
for rec in self:
if len(rec.product_ids) > 1:
if rec.product_selection == "one":
raise ValidationError(
_(
"When 'Product Selection: One Product' is selected"
" you are only able to add one product."
)
)
elif rec.product_selection == "lot":
raise ValidationError(
_(
"When 'Product Selection: Lot Serial Number' is selected"
" you are only able to add one product."
)
)
12 changes: 9 additions & 3 deletions stock_inventory/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ class StockQuant(models.Model):
def _apply_inventory(self):
res = super()._apply_inventory()
record_moves = self.env["stock.move.line"]
adjustment = self.env["stock.inventory"].search([("state", "=", "in_progress")])
for rec in self:
adjustment = (
self.env["stock.inventory"]
.search([("state", "=", "in_progress")])
.filtered(
lambda x: rec.location_id in x.location_ids
or rec.location_id in x.location_ids.child_ids
)
)
moves = record_moves.search(
[
("product_id", "=", rec.product_id.id),
Expand All @@ -22,8 +29,7 @@ def _apply_inventory(self):
]
)
move = moves[len(moves) - 1]
adjustment.stock_move_ids += move
adjustment.stock_move_ids |= move
move.inventory_adjustment_id = adjustment
rec.to_do = False

return res
2 changes: 1 addition & 1 deletion stock_inventory/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* `ForgeFlow <https://www.forgeflow.com>`_:

* David Jiménez
* David Jiménez <[email protected]>
5 changes: 4 additions & 1 deletion stock_inventory/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Go to Inventory / Operations / Inventory Adjustments. Here you can see the list of Adjustment Grouped.
If you create a new Group, you can choose 2 types of product selection:
- All Products (all products from theselected locations)
- All Products (all products from theselected locations).
- Manual Selection (choose manually each product in location).
- One Product (choose only one product in locations).
- Lot Serial Number (choose one product, any lots and locations).
- Product Category (choose one product category [childs also taken into account]).
When you start the adjustment (only one at a time) clicking on adjustments gets you to the view where adjustments are made.
From the group view, if you click on Stock Moves you can see the movements done (includes the 0 qty moves).
Loading

0 comments on commit d440ff7

Please sign in to comment.