diff --git a/stock_quant_package_fast_move/models/res_company.py b/stock_quant_package_fast_move/models/res_company.py
index 029e08ad5920..77824dd54f62 100644
--- a/stock_quant_package_fast_move/models/res_company.py
+++ b/stock_quant_package_fast_move/models/res_company.py
@@ -12,3 +12,4 @@ class ResCompany(models.Model):
string="Package Move Operation",
domain="[('show_entire_packs', '=', True)]",
)
+ use_batch_transfers = fields.Boolean()
diff --git a/stock_quant_package_fast_move/models/res_config_settings.py b/stock_quant_package_fast_move/models/res_config_settings.py
index 4ad3129027a6..26c66700811c 100644
--- a/stock_quant_package_fast_move/models/res_config_settings.py
+++ b/stock_quant_package_fast_move/models/res_config_settings.py
@@ -10,3 +10,6 @@ class ResConfigSettings(models.TransientModel):
package_move_picking_type_id = fields.Many2one(
related="company_id.package_move_picking_type_id", readonly=False
)
+ use_batch_transfers = fields.Boolean(
+ related="company_id.use_batch_transfers", readonly=False
+ )
diff --git a/stock_quant_package_fast_move/models/stock_quant_package.py b/stock_quant_package_fast_move/models/stock_quant_package.py
index a00ebc3f84f5..7a2c25ac664a 100644
--- a/stock_quant_package_fast_move/models/stock_quant_package.py
+++ b/stock_quant_package_fast_move/models/stock_quant_package.py
@@ -21,6 +21,61 @@ def action_show_package_fast_move_wizard(self):
"context": self.env.context,
}
+ def create_batch_transfer(
+ self, location, destination_package, validate, picking_type_id
+ ):
+ """
+ Creates a new Batch Transfer and a separate picking for each package,
+ adds them to the Batch Transfer, and confirms the transfer. If the "validate"
+ option is enabled, the batch is also validated.
+
+ Parameters:
+ - location (recordset of stock.location): The destination location.
+ - destination_package (recordset of stock.quant.package, optional):
+ Optional destination package. If provided, it must belong to the specified location.
+ - validate (boolean, optional):
+ If set to True, the created picking will be validated.
+ Otherwise it will remain in the "Ready" state. Defaults to False.
+ - picking_type_id (recordset of stock.picking.type):
+ The picking type to be used for the transfer, as configured in settings.
+ """
+ # Create a batch transfer
+ batch_vals = {
+ "picking_type_id": picking_type_id.id,
+ }
+ batch_transfer = self.env["stock.picking.batch"].create(batch_vals)
+
+ for package in self:
+ # Create a picking
+ picking_vals = {
+ "location_id": package.location_id.id,
+ "location_dest_id": location.id,
+ "picking_type_id": picking_type_id.id,
+ }
+ picking = self.env["stock.picking"].create(picking_vals)
+
+ # Create a package_level record
+ package_level_vals = {
+ "package_id": package.id,
+ "package_dest_id": destination_package.id
+ if destination_package
+ else False,
+ "picking_id": picking.id,
+ "company_id": self.env.company.id,
+ "location_id": package.location_id.id,
+ "location_dest_id": location.id,
+ }
+ package_level = self.env["stock.package_level"].create(package_level_vals)
+ package_level.write({"is_done": True})
+ picking.write({"batch_id": batch_transfer.id})
+
+ # Confirm the Batch Transfer
+ batch_transfer.action_confirm()
+
+ if validate:
+ # Validate the Batch Transfer
+ batch_transfer.action_done()
+
def _move_to_location(self, location, destination_package=None, validate=False):
"""
Move packages to a specified location.
@@ -61,34 +116,42 @@ def _move_to_location(self, location, destination_package=None, validate=False):
active_company = self.env.company
picking_type_id = active_company.package_move_picking_type_id
+ use_batch_transfers = active_company.use_batch_transfers
- # Create a picking
- picking_vals = {
- "location_id": self[0].location_id.id,
- "location_dest_id": location.id,
- "picking_type_id": picking_type_id.id,
- }
- picking = self.env["stock.picking"].create(picking_vals)
-
- for package in self:
- # Create a package_level record for each package
- package_level_vals = {
- "package_id": package.id,
- "package_dest_id": destination_package.id
- if destination_package
- else False,
- "picking_id": picking.id,
- "company_id": active_company.id,
- "location_id": package.location_id.id,
+ if use_batch_transfers:
+ self.create_batch_transfer(
+ location, destination_package, validate, picking_type_id
+ )
+ else:
+ # Create a picking
+ picking_vals = {
+ "location_id": self[0].location_id.id,
"location_dest_id": location.id,
+ "picking_type_id": picking_type_id.id,
}
- package_level = self.env["stock.package_level"].create(package_level_vals)
- package_level.write({"is_done": True})
-
- picking.action_confirm()
- if validate:
- # Validate the picking
- picking.button_validate()
+ picking = self.env["stock.picking"].create(picking_vals)
+
+ for package in self:
+ # Create a package_level record for each package
+ package_level_vals = {
+ "package_id": package.id,
+ "package_dest_id": destination_package.id
+ if destination_package
+ else False,
+ "picking_id": picking.id,
+ "company_id": active_company.id,
+ "location_id": package.location_id.id,
+ "location_dest_id": location.id,
+ }
+ package_level = self.env["stock.package_level"].create(
+ package_level_vals
+ )
+ package_level.write({"is_done": True})
+
+ picking.action_confirm()
+ if validate:
+ # Validate the picking
+ picking.button_validate()
return True
diff --git a/stock_quant_package_fast_move/tests/test_stock_quant_package_fast_move.py b/stock_quant_package_fast_move/tests/test_stock_quant_package_fast_move.py
index 2f11c5e48767..07676c939a49 100644
--- a/stock_quant_package_fast_move/tests/test_stock_quant_package_fast_move.py
+++ b/stock_quant_package_fast_move/tests/test_stock_quant_package_fast_move.py
@@ -684,3 +684,46 @@ def test_stock_quant_package_fast_move_no_picking_validation(self):
"assigned",
"Wrong picking state.",
)
+
+ def test_stock_quant_package_fast_move_use_batch_transfers(self):
+ # Test use batch transfers
+ self.company.write({"use_batch_transfers": True})
+ with Form(
+ self.env["stock.quant.package.fast.move.wizard"]
+ .with_company(self.company.id)
+ .with_context(active_ids=[self.package2.id, self.package3.id])
+ ) as f1:
+ f1.location_dest_id = self.int_type.default_location_dest_id
+ f1.validate = True
+
+ wizard = f1.save()
+ wizard.action_move()
+
+ # Check the location_id of the moved packages
+ self.assertEqual(
+ self.package2.location_id.id,
+ self.int_type.default_location_dest_id.id,
+ "Wrong location on the package.",
+ )
+ self.assertEqual(
+ self.package3.location_id.id,
+ self.int_type.default_location_dest_id.id,
+ "Wrong location on the package.",
+ )
+
+ # Check if the batch transfer is created and validated
+ picking_ids = (
+ self.env["stock.move.line"]
+ .search([("result_package_id", "in", [self.package2.id, self.package3.id])])
+ .picking_id
+ )
+ batch_id = picking_ids.batch_id
+ self.assertTrue(
+ batch_id,
+ "The Batch Transfer is not created.",
+ )
+ self.assertEqual(
+ batch_id.state,
+ "done",
+ "The state of the Batch Transfer is wrong.",
+ )
diff --git a/stock_quant_package_fast_move/views/res_config_settings_view.xml b/stock_quant_package_fast_move/views/res_config_settings_view.xml
index 7147f2a47139..dee959fec65a 100644
--- a/stock_quant_package_fast_move/views/res_config_settings_view.xml
+++ b/stock_quant_package_fast_move/views/res_config_settings_view.xml
@@ -29,6 +29,25 @@
+