Skip to Content
Menu
This question has been flagged
341 Zobrazenia

I have created a wizard to scan the products. I have added O2M field with comodel 'wizard.line'.

Now when I type barcode in the wizard's barcode field, It should find the product of that barcode and add it to the lines or update the quantity if line of that product is available.

def on_barcode_scanned(self):
​barcode = self.barcode
​product = self.env["product.product"].search(
[("barcode", "=", barcode)], limit=1
)

if not product:
return {
"warning": {
"title": "Invalid Barcode",
"type": "notification",
"message": f"Product not found with barcode: {barcode}",
}
}

sequence = 0
product_line = self.line_ids.filtered(
lambda ol: ol.product_id.barcode == barcode
)
if product_line:
product_line.scanned_qty += 1
for line in self.line_ids:
line.sequence += 1

product_line.sequence = sequence
else:
vals = {
"product_id": product.id,
"scanned_qty": 1,
"sequence": 0,
}
for line in self.line_ids:
line.sequence += 1

new_line_ids = self.line_ids.create(vals)
self.line_ids += new_line_ids

This is the onchange method. It is adding the line and updating the existing line properly and setting the sequence to 0.

But the tree view is not showing in proper sequence. This is the XML of the wizard's line_ids(O2M) field in the wizard's form view.

<field name="line_ids" widget="one2many">
<tree editable="bottom" default_order="sequence" decoration-info="sequence == 0">
<field name="sequence" widget="handle" force_save="1"/>
<field name="id" column_invisible="True"/>
<field name="product_id" force_save="1"/>
<field name="on_hand_qty"/>
<field name="scanned_qty" force_save="1"/>
<field name="difference_qty"
decoration-muted="difference_qty == 0"
decoration-danger="difference_qty &lt; 0"
decoration-success="difference_qty &gt; 0"/>
</tree>
</field>


Also _order is  set to 'sequence' in 'wizard.line' model.


I tried with sorting lines in python and assign them back to line_ids. Still doesn't work.

Please if anyone have the solution for this.

Thank You.

Avatar
Zrušiť