Skip to content

Commit

Permalink
[IMP] stock_inventory: Add flag to autocomplete adjustment when fully…
Browse files Browse the repository at this point in the history
… done.
  • Loading branch information
DavidJForgeFlow committed Apr 17, 2024
1 parent 608c718 commit fd6f59b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 7 deletions.
1 change: 1 addition & 0 deletions stock_inventory/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"views/stock_inventory.xml",
"views/stock_quant.xml",
"views/stock_move_line.xml",
"views/res_config_settings_view.xml",
],
"installable": True,
"application": False,
Expand Down
2 changes: 2 additions & 0 deletions stock_inventory/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from . import stock_inventory
from . import stock_quant
from . import stock_move_line
from . import res_company
from . import res_config_settings
15 changes: 15 additions & 0 deletions stock_inventory/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).


from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

stock_inventory_auto_complete = fields.Boolean(
help="If enabled, when all the quants prepared for the adjustment "
"are done, the adjustment is automatically set to done.",
default=False,
)
12 changes: 12 additions & 0 deletions stock_inventory/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

stock_inventory_auto_complete = fields.Boolean(
related="company_id.stock_inventory_auto_complete", readonly=False
)
12 changes: 7 additions & 5 deletions stock_inventory/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ class InventoryAdjustmentsGroup(models.Model):
@api.depends("stock_quant_ids")
def _compute_count_stock_quants(self):
self.count_stock_quants = len(self.stock_quant_ids)
count_todo = len(
self.stock_quant_ids.search(
[("id", "in", self.stock_quant_ids.ids), ("to_do", "=", "True")]
)
)
count_todo = len(self.stock_quant_ids.filtered(lambda sq: sq.to_do))
self.count_stock_quants_string = "{} / {}".format(
count_todo, self.count_stock_quants
)
Expand Down Expand Up @@ -188,6 +184,12 @@ def action_state_to_done(self):
self.stock_quant_ids.update({"to_do": True})
return

def action_auto_state_to_done(self):
self.ensure_one()
if not any(self.stock_quant_ids.filtered(lambda sq: sq.to_do)):
self.action_state_to_done()
return

def action_state_to_draft(self):
self.state = "draft"
self.stock_quant_ids.update({"to_do": True})
Expand Down
2 changes: 2 additions & 0 deletions stock_inventory/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def _apply_inventory(self):
adjustment.stock_move_ids |= move
move.inventory_adjustment_id = adjustment
rec.to_do = False
if self.env.company.stock_inventory_auto_complete:
adjustment.action_auto_state_to_done()
return res

def _get_inventory_fields_write(self):
Expand Down
1 change: 0 additions & 1 deletion stock_inventory/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down
50 changes: 50 additions & 0 deletions stock_inventory/tests/test_stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class TestStockInventory(TransactionCase):
def setUp(self):
super(TestStockInventory, self).setUp()
self.env.company.stock_inventory_auto_complete = False
self.quant_model = self.env["stock.quant"]
self.move_model = self.env["stock.move.line"]
self.inventory_model = self.env["stock.inventory"]
Expand Down Expand Up @@ -359,3 +360,52 @@ def test_06_exclude_sub_locations(self):
self.assertEqual(inventory1.stock_move_ids.lot_id.id, self.lot_1.id)
self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location1.id)
inventory1.action_state_to_done()

def test_07_stock_inventory_auto_complete(self):
self.env.company.stock_inventory_auto_complete = True
with self.assertRaises(ValidationError), self.cr.savepoint():
inventory1 = self.inventory_model.create(
{
"name": "Inventory_Test_5",
"product_selection": "one",
"location_ids": [self.location1.id],
"product_ids": [self.product.id, self.product2.id],
}
)
inventory1 = self.inventory_model.create(
{
"name": "Inventory_Test_5",
"product_selection": "one",
"location_ids": [self.location1.id],
"product_ids": [self.product.id],
}
)
inventory1.action_state_to_in_progress()
inventory1.product_ids = [self.product.id]
self.assertEqual(
inventory1.stock_quant_ids.ids, [self.quant1.id, self.quant3.id]
)
inventory1.action_state_to_draft()
self.assertEqual(inventory1.stock_quant_ids.ids, [])
inventory1.action_state_to_in_progress()
self.assertEqual(inventory1.state, "in_progress")
self.assertEqual(inventory1.count_stock_moves, 0)
self.assertEqual(inventory1.count_stock_quants, 2)
self.assertEqual(inventory1.count_stock_quants_string, "2 / 2")
inventory1.action_view_inventory_adjustment()
self.quant3.inventory_quantity = 74
self.quant3.action_apply_inventory()
inventory1._compute_count_stock_quants()
inventory1.action_view_stock_moves()
self.assertEqual(inventory1.count_stock_moves, 1)
self.assertEqual(inventory1.count_stock_quants, 2)
self.assertEqual(inventory1.count_stock_quants_string, "1 / 2")
self.assertEqual(inventory1.stock_move_ids.qty_done, 26)
self.assertEqual(inventory1.stock_move_ids.product_id.id, self.product.id)
self.assertEqual(inventory1.stock_move_ids.lot_id.id, self.lot_3.id)
self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location3.id)
self.quant1.inventory_quantity = 65
self.quant1.action_apply_inventory()
self.assertEqual(inventory1.count_stock_moves, 2)
self.assertEqual(inventory1.count_stock_quants, 2)
self.assertEqual(inventory1.state, "done")
28 changes: 28 additions & 0 deletions stock_inventory/views/res_config_settings_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2019-2023 ForgeFlow S.L.
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res_config_settings_view_form - stock_inventory</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="stock.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@id='production_lot_info']" position='after'>
<h2>Stock Inventory</h2>
<div class="row mt16 o_settings_container">
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="stock_inventory_auto_complete" />
</div>
<div class="o_setting_right_pane">
<label for="stock_inventory_auto_complete" />
<div class="text-muted">
If enabled, when all the quants prepared for the adjustment are done, the adjustment is automatically set to done.
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>
4 changes: 3 additions & 1 deletion stock_inventory/views/stock_inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,7 @@
sequence="30"
action="action_view_inventory_group_form"
/>
<delete model="ir.ui.menu" id="stock.menu_action_inventory_tree" />
<record id="stock.menu_action_inventory_tree" model="ir.ui.menu">
<field name="active" eval="False" />
</record>
</odoo>

0 comments on commit fd6f59b

Please sign in to comment.