Skip to content

Commit

Permalink
[IMP] stock_inventory
Browse files Browse the repository at this point in the history
Add company to adjustments to avoid multi-company errors. Adds information fields to tree view. Also adds 'Assign to' in stock inventory that propagates to quants. Also propagates the date field.
  • Loading branch information
DavidJForgeFlow committed Apr 22, 2024
1 parent fd6f59b commit 22543ec
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
1 change: 1 addition & 0 deletions stock_inventory/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"depends": ["stock"],
"data": [
"security/ir.model.access.csv",
"security/security.xml",
"views/stock_inventory.xml",
"views/stock_quant.xml",
"views/stock_move_line.xml",
Expand Down
83 changes: 75 additions & 8 deletions stock_inventory/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class InventoryAdjustmentsGroup(models.Model):

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

company_id = fields.Many2one(
comodel_name="res.company",
readonly=True,
index=True,
states={"draft": [("readonly", False)]},
default=lambda self: self.env.company,
)

state = fields.Selection(
[("draft", "Draft"), ("in_progress", "In Progress"), ("done", "Done")],
default="draft",
Expand All @@ -22,7 +30,9 @@ class InventoryAdjustmentsGroup(models.Model):
)

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

product_selection = fields.Selection(
Expand All @@ -37,15 +47,22 @@ class InventoryAdjustmentsGroup(models.Model):
required=True,
)

product_ids = fields.Many2many("product.product", string="Products")
product_ids = fields.Many2many(
"product.product", string="Products", domain="[('company_id', '=', company_id)]"
)

stock_quant_ids = fields.Many2many("stock.quant", string="Inventory Adjustment")
stock_quant_ids = fields.Many2many(
"stock.quant",
string="Inventory Adjustment",
domain="[('company_id', '=', company_id)]",
)

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

lot_ids = fields.Many2many(
"stock.production.lot",
string="Lot/Serial Numbers",
domain="[('company_id', '=', company_id)]",
)

stock_move_ids = fields.One2many(
Expand All @@ -71,6 +88,13 @@ class InventoryAdjustmentsGroup(models.Model):
"the locations settled, not the child ones."
)

responsible_id = fields.Many2one(
comodel_name="res.users",
string="Assigned to",
tracking=True,
help="Specific responsible of Inventory Adjustment.",
)

@api.depends("stock_quant_ids")
def _compute_count_stock_quants(self):
self.count_stock_quants = len(self.stock_quant_ids)
Expand Down Expand Up @@ -162,8 +186,8 @@ def action_state_to_in_progress(self):
[
("state", "=", "in_progress"),
"|",
("location_ids", "in", self.location_ids.mapped("id")),
("location_ids", "in", self.location_ids.child_ids.ids),
("location_ids", "in", self.location_ids.ids),
("location_ids", "child_of", self.location_ids.ids),
],
limit=1,
)
Expand All @@ -176,12 +200,24 @@ def action_state_to_in_progress(self):
)
self.state = "in_progress"
self.refresh_stock_quant_ids()
self.stock_quant_ids.update({"to_do": True})
self.stock_quant_ids.update(
{
"to_do": True,
"user_id": self.responsible_id,
"inventory_date": self.date,
}
)
return

def action_state_to_done(self):
self.state = "done"
self.stock_quant_ids.update({"to_do": True})
self.stock_quant_ids.update(
{
"to_do": True,
"user_id": False,
"inventory_date": False,
}
)
return

def action_auto_state_to_done(self):
Expand All @@ -192,7 +228,13 @@ def action_auto_state_to_done(self):

def action_state_to_draft(self):
self.state = "draft"
self.stock_quant_ids.update({"to_do": True})
self.stock_quant_ids.update(
{
"to_do": True,
"user_id": False,
"inventory_date": False,
}
)
self.stock_quant_ids = None
return

Expand All @@ -212,6 +254,31 @@ def action_view_stock_moves(self):
result["context"] = []
return result

@api.constrains("state", "location_ids")
def _check_inventory_in_progress_not_override(self):
inventories = self.search([("state", "=", "in_progress")])
for rec in inventories:
inventory = inventories.filtered(
lambda x: x.id != rec.id
and (
any(i in x.location_ids for i in rec.location_ids)
or (
any(
i in x.location_ids.child_internal_location_ids
for i in rec.location_ids
)
and not x.exclude_sublocation
)
)
)
if len(inventory) > 0:
raise ValidationError(
_(
"Cannot be more than one in progress inventory adjustment "
"affecting the same location at the same time."
)
)

@api.constrains("product_selection", "product_ids")
def _check_one_product_in_product_selection(self):
for rec in self:
Expand Down
5 changes: 4 additions & 1 deletion stock_inventory/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def _apply_inventory(self):
.search([("state", "=", "in_progress")])
.filtered(
lambda x: rec.location_id in x.location_ids
or rec.location_id in x.location_ids.child_ids
or (
rec.location_id in x.location_ids.child_internal_location_ids
and not x.exclude_sublocation
)
)
)
moves = record_moves.search(
Expand Down
11 changes: 11 additions & 0 deletions stock_inventory/security/security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="0">
<record model="ir.rule" id="stock_inventory_comp_rule">
<field name="name">Stock Inventory multi-company</field>
<field name="model_id" ref="model_stock_inventory" />
<field name="global" eval="True" />
<field
name="domain_force"
>['|',('company_id','=',False),('company_id', 'in', company_ids)]</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions stock_inventory/views/stock_inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
</group>
<group>
<field name="date" />
<field name="company_id" />
<field name="responsible_id" />
<field
name="owner_id"
attrs="{'readonly':[('state', '=', 'done')]}"
Expand Down Expand Up @@ -129,6 +131,9 @@
<field name="arch" type="xml">
<tree>
<field name="name" />
<field name="product_selection" optional="hide" />
<field name="location_ids" widget="many2many_tags" optional="hide" />
<field name="responsible_id" optional="hide" />
<field
name="state"
widget="badge"
Expand All @@ -137,6 +142,7 @@
decoration-muted="state == 'draft'"
/>
<field name="date" />
<field name="company_id" optional="hide" />
</tree>
</field>
</record>
Expand Down

0 comments on commit 22543ec

Please sign in to comment.