From c88aa6d11169c811500394afa0ef7004252f8e99 Mon Sep 17 00:00:00 2001 From: DavidJForgeFlow Date: Mon, 7 Nov 2022 12:02:05 +0100 Subject: [PATCH] [14.0][IMP] stock_cycle_count: add auto start adjustment option --- stock_cycle_count/__manifest__.py | 1 + stock_cycle_count/models/__init__.py | 2 + stock_cycle_count/models/res_company.py | 21 +++++++ .../models/res_config_settings.py | 27 +++++++++ stock_cycle_count/models/stock_cycle_count.py | 7 ++- stock_cycle_count/models/stock_inventory.py | 8 +++ .../views/res_config_settings_view.xml | 58 +++++++++++++++++++ 7 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 stock_cycle_count/models/res_company.py create mode 100644 stock_cycle_count/models/res_config_settings.py create mode 100644 stock_cycle_count/views/res_config_settings_view.xml diff --git a/stock_cycle_count/__manifest__.py b/stock_cycle_count/__manifest__.py index b50ad120d..028e1b21c 100644 --- a/stock_cycle_count/__manifest__.py +++ b/stock_cycle_count/__manifest__.py @@ -21,6 +21,7 @@ "views/stock_warehouse_view.xml", "views/stock_inventory_view.xml", "views/stock_location_view.xml", + "views/res_config_settings_view.xml", "data/cycle_count_sequence.xml", "data/cycle_count_ir_cron.xml", "reports/stock_location_accuracy_report.xml", diff --git a/stock_cycle_count/models/__init__.py b/stock_cycle_count/models/__init__.py index 32526b233..d479160f3 100644 --- a/stock_cycle_count/models/__init__.py +++ b/stock_cycle_count/models/__init__.py @@ -4,3 +4,5 @@ from . import stock_location from . import stock_inventory from . import stock_warehouse from . import stock_move +from . import res_company +from . import res_config_settings diff --git a/stock_cycle_count/models/res_company.py b/stock_cycle_count/models/res_company.py new file mode 100644 index 000000000..a7203d41b --- /dev/null +++ b/stock_cycle_count/models/res_company.py @@ -0,0 +1,21 @@ +from odoo import fields, models + + +class Company(models.Model): + _inherit = "res.company" + + auto_start_inventory_from_cycle_count = fields.Boolean( + string="Auto Start Inventory Adjustment from Cycle Count", + help="If enabled, confirming a Cycle Count will " + "start the related Inventory Adjustment.", + ) + + inventory_adjustment_counted_quantities = fields.Selection( + selection=[ + ("counted", "Default to stock on hand"), + ("zero", "Default to zero"), + ], + string="Inventory Adjustment Counted quantities from Cycle Count", + help="If enabled, confirming a Cycle Count will start the related " + "Inventory Adjustment.", + ) diff --git a/stock_cycle_count/models/res_config_settings.py b/stock_cycle_count/models/res_config_settings.py new file mode 100644 index 000000000..884f5c245 --- /dev/null +++ b/stock_cycle_count/models/res_config_settings.py @@ -0,0 +1,27 @@ +# Copyright 2019 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + auto_start_inventory_from_cycle_count = fields.Boolean( + related="company_id.auto_start_inventory_from_cycle_count", + string="Auto Start Inventory Adjustment from Cycle Count", + help="If enabled, confirming a Cycle Count will start the " + "related Inventory Adjustment.", + readonly=False, + ) + + inventory_adjustment_counted_quantities = fields.Selection( + related="company_id.inventory_adjustment_counted_quantities", + selection=[ + ("counted", "Default to stock on hand"), + ("zero", "Default to zero"), + ], + string="Inventory Adjustment Counted quantities from Cycle Count", + help="If enabled, confirming a Cycle Count will start the related " + "Inventory Adjustment.", + readonly=False, + ) diff --git a/stock_cycle_count/models/stock_cycle_count.py b/stock_cycle_count/models/stock_cycle_count.py index 76ea41f5a..99f646d93 100644 --- a/stock_cycle_count/models/stock_cycle_count.py +++ b/stock_cycle_count/models/stock_cycle_count.py @@ -94,7 +94,12 @@ class StockCycleCount(models.Model): raise UserError(_("You can only confirm cycle counts in state 'Planned'.")) for rec in self: data = rec._prepare_inventory_adjustment() - self.env["stock.inventory"].create(data) + inv = self.env["stock.inventory"].create(data) + if self.company_id.auto_start_inventory_from_cycle_count: + inv.prefill_counted_quantity = ( + self.company_id.inventory_adjustment_counted_quantities + ) + inv.action_start() self.write({"state": "open"}) return True diff --git a/stock_cycle_count/models/stock_inventory.py b/stock_cycle_count/models/stock_inventory.py index b65a6136e..cfe9c9328 100644 --- a/stock_cycle_count/models/stock_inventory.py +++ b/stock_cycle_count/models/stock_inventory.py @@ -37,6 +37,14 @@ class StockInventory(models.Model): group_operator="avg", ) + def _get_default_counted_quantitites(self): + company_id = self.env.context.get("default_company_id", self.env.company) + return company_id.inventory_adjustment_counted_quantities or "counted" + + prefill_counted_quantity = fields.Selection( + default=_get_default_counted_quantitites + ) + def _update_cycle_state(self): for inv in self: if inv.cycle_count_id and inv.state == "done": diff --git a/stock_cycle_count/views/res_config_settings_view.xml b/stock_cycle_count/views/res_config_settings_view.xml new file mode 100644 index 000000000..38ec2ca02 --- /dev/null +++ b/stock_cycle_count/views/res_config_settings_view.xml @@ -0,0 +1,58 @@ + + + res.config.settings.view.form.inherit.cycle.adjustment + res.config.settings + + +
+

Cycle Count

+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+