Skip to content

Commit

Permalink
[IMP] New settings Use Batch Transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
angelinaanaki committed Apr 5, 2024
1 parent d0a585a commit 1c7779d
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 25 deletions.
1 change: 1 addition & 0 deletions stock_quant_package_fast_move/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ class ResCompany(models.Model):
string="Package Move Operation",
domain="[('show_entire_packs', '=', True)]",
)
use_batch_transfers = fields.Boolean()
3 changes: 3 additions & 0 deletions stock_quant_package_fast_move/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
113 changes: 88 additions & 25 deletions stock_quant_package_fast_move/models/stock_quant_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
19 changes: 19 additions & 0 deletions stock_quant_package_fast_move/views/res_config_settings_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
<field name="package_move_picking_type_id" />
</div>
</div>
<br />
<div
attrs="{'invisible': [('module_stock_picking_batch','=',False)]}"
>
<div class="o_setting_left_pane">
<field name="use_batch_transfers" />
</div>
<div class="o_setting_right_pane">
<label for="use_batch_transfers" />
<span
class="fa fa-lg fa-building-o"
title="Values set here are company-specific."
groups="base.group_multi_company"
/>
<div class="text-muted">
When moving packages, the "Use Batch Transfers" setting automatically creates a batch with separate pickings for each package.
</div>
</div>
</div>
</div>
</xpath>
</field>
Expand Down

0 comments on commit 1c7779d

Please sign in to comment.