Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] write several products without forcing the sequence on non-serialized products #1835

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions product_lot_sequence/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,46 @@ def _inverse_lot_seq_number_next(self):

def write(self, vals):
seq_policy = self.env["stock.lot"]._get_sequence_policy()
if seq_policy == "product":
for template in self:
if seq_policy == "global":
return super().write(vals)
else:
serial_templates = (
self.filtered(lambda pdt: pdt.tracking in ["lot", "serial"])
if (
not vals.get("tracking", False)
or vals.get("tracking", False) == "none"
)
else self
)
super(ProductTemplate, self - serial_templates).write(vals)
# templates with sequence
serial_tmpl_seq = serial_templates.filtered("lot_sequence_id")
serial_tmpl_wo_seq = serial_templates - serial_tmpl_seq
new_sequence = self.env["ir.sequence"]
if len(serial_tmpl_wo_seq) and not vals.get("lot_sequence_id", False):
new_sequence = serial_tmpl_wo_seq[0].sudo()._create_lot_sequence(vals)
elif vals.get("lot_sequence_id", False):
new_sequence = self.env["ir.sequence"].browse(vals["lot_sequence_id"])
# template with sequence and update it
for template in serial_tmpl_seq:
tracking = vals.get("tracking", False) or template.tracking
if tracking in ["lot", "serial"]:
if (
not vals.get("lot_sequence_id", False)
and not template.lot_sequence_id
):
vals["lot_sequence_id"] = (
template.sudo()._create_lot_sequence(vals).id
)
elif vals.get("lot_sequence_id", False):
if vals.get("lot_sequence_id", False):
lot_sequence_id = self.env["ir.sequence"].browse(
vals["lot_sequence_id"]
)
vals["lot_sequence_prefix"] = lot_sequence_id.prefix
vals["lot_sequence_padding"] = lot_sequence_id.padding
return super().write(vals)
super(ProductTemplate, serial_tmpl_seq).write(vals)
# template without sequence, set new sequence, prefix and padding
for template in serial_tmpl_wo_seq:
tracking = vals.get("tracking", False) or template.tracking
if tracking in ["lot", "serial"] or vals.get("lot_sequence_id", False):
if new_sequence:
vals["lot_sequence_id"] = new_sequence.id
vals["lot_sequence_prefix"] = new_sequence.prefix
vals["lot_sequence_padding"] = new_sequence.padding
return super(ProductTemplate, serial_tmpl_wo_seq).write(vals)

@api.model_create_multi
def create(self, vals_list):
Expand Down
142 changes: 142 additions & 0 deletions product_lot_sequence/tests/test_product_lot_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,145 @@ def test_open_detailed_operations(self):
self.assertEqual(
new_next_sequence_number, seq.get_next_char(seq.number_next_actual)
)

def test_write_multiple_products(self):
self.env["ir.config_parameter"].set_param(
"product_lot_sequence.policy", "global"
)
product_template_model = self.env["product.template"]
pdt_serial = product_template_model.create(
{
"name": "Test Product Serial 1",
"type": "product",
"tracking": "serial",
}
)
pdt_simple = product_template_model.create(
{
"name": "Test Product 2",
"type": "product",
"tracking": "none",
}
)
pdt_service = product_template_model.create(
{
"name": "Test service 3",
"type": "service",
"tracking": "none",
}
)
pdt_ids = pdt_serial + pdt_simple + pdt_service
pdt_ids.write(
{
"description_picking": "test note",
}
)
self.assertFalse(pdt_serial.lot_sequence_id)
self.assertFalse(pdt_simple.lot_sequence_id)
self.assertFalse(pdt_service.lot_sequence_id)

self.env["ir.config_parameter"].set_param(
"product_lot_sequence.policy", "product"
)
pdt_ids.write(
{
"description_picking": "note for picking",
}
)
self.assertTrue(pdt_serial.lot_sequence_id)
self.assertFalse(pdt_simple.lot_sequence_id)
self.assertFalse(pdt_service.lot_sequence_id)
self.assertTrue(
all(
[
"note for picking" == desc
for desc in pdt_ids.mapped("description_picking")
]
)
)

def test_write_multiple_serial_products(self):
self.env["ir.config_parameter"].set_param(
"product_lot_sequence.policy", "global"
)
product_template_model = self.env["product.template"]
sequence = product_template_model.sudo()._create_lot_sequence(
{
"name": "Test Sequence",
}
)
pdt_serial = product_template_model.create(
{
"name": "Test Product Serial 1",
"type": "product",
"tracking": "serial",
}
)
pdt_serial_lot = product_template_model.create(
{
"name": "Test Product Serial 2",
"type": "product",
"tracking": "serial",
"lot_sequence_id": sequence.id,
}
)
pdt_ids = pdt_serial + pdt_serial_lot
pdt_ids.write(
{
"description_picking": "test note",
}
)
self.assertFalse(pdt_serial.lot_sequence_id)
self.assertTrue(pdt_serial_lot.lot_sequence_id)

self.env["ir.config_parameter"].set_param(
"product_lot_sequence.policy", "product"
)
pdt_ids.write(
{
"description_picking": "note for picking",
}
)
self.assertTrue(pdt_serial.lot_sequence_id)
self.assertNotEqual(pdt_serial.lot_sequence_id, sequence)
self.assertEqual(pdt_serial_lot.lot_sequence_id, sequence)

def test_write_tracking(self):
product_template_model = self.env["product.template"]
pdt_simple = product_template_model.create(
{
"name": "Test Product 2",
"type": "product",
}
)
self.assertFalse(pdt_simple.lot_sequence_id)
pdt_simple.write(
{
"tracking": "lot",
}
)
self.assertTrue(pdt_simple.lot_sequence_id)
self.assertEqual(pdt_simple.name, pdt_simple.lot_sequence_id.name)

def test_write_sequence(self):
product_template_model = self.env["product.template"]
pdt_simple = product_template_model.create(
{
"name": "Test Product 2",
"type": "product",
}
)
self.assertFalse(pdt_simple.lot_sequence_id)
sequence = pdt_simple.sudo()._create_lot_sequence(
{
"name": "Test Sequence",
}
)
pdt_simple.write(
{
"lot_sequence_id": sequence.id,
}
)
self.assertTrue(pdt_simple.lot_sequence_id)
self.assertTrue(pdt_simple.lot_sequence_padding)
self.assertNotEqual(pdt_simple.name, pdt_simple.lot_sequence_id.name)
Loading