Skip to Content
Menu
This question has been flagged
2 Replies
565 Views

Hi guy's,


Could you tell me how to auto merge orders with the same product please ?

I would like make it with an automated action on studio.


Thank you :)

Avatar
Discard
Best Answer

Hello  Benoit,

I understand that you want to merge sales orders based on similar products. Below is an approach using Odoo Studio's Automated Actions. If this solution fits your requirements, let me know; otherwise, we can adjust the logic as needed.

Steps to Implement:

1. Create an Automated Action

  • Navigate to Odoo Studio → Automated Actions
  • Click Create to add a new action

2. Configure the Action

  • Model: Sales Order (sale.order)
  • Trigger: On Creation & Update
  • Conditions:
    • State = Draft (state = 'draft')
    • (Optional) Filter by partner_id to ensure it's a customer order
  • Action to Execute: Python Code

3. Python Code for Order Merging

# Get current sales order
so = record

# Proceed only if the order is in draft state
if so.state == 'draft':
    customer = so.partner_id
   
    # Fetch other draft sales orders for the same customer
    existing_orders = env['sale.order'].search([
        ('partner_id', '=', customer.id),
        ('state', '=', 'draft'),
        ('id', '!=', so.id)  # Exclude the current order
    ])
   
    if existing_orders:
        # Select the first existing draft order for merging
        target_so = existing_orders[0]

        for line in so.order_line:
            product = line.product_id
            qty = line.product_uom_qty

            # Check if the product exists in the target SO
            existing_line = target_so.order_line.filtered(lambda l: l.product_id == product)
           
            if existing_line:
                # Update quantity in the existing order line
                existing_line.product_uom_qty += qty
            else:
                # Move the line to the existing order
                line.write({'order_id': target_so.id})

        # Delete the original SO if it's empty after merging
        if not so.order_line:
            so.unlink()

Use Cases Covered:

✅ If a new Draft Sales Order (SO) is created for a customer and contains a product already present in another Draft SO, the product is merged into the existing order.

✅ If the original SO is empty after merging, it is automatically deleted.

Let me know if this approach works for you! 😊


Avatar
Discard

Can something like this be done on contacts too?

yes, sure you replace sale order to contacts in place of model and then update logic according to the case

Alec, If this helps, please upvote the answer!

Author

I've test it and I cant' save the automated rule due to this message :

forbidden opcode(s) in "# Available variables:\n# - env: environment on which the action is triggered\n# - model: model of the record on which the action is triggered; is a void recordset\n# - record: record on which the action is triggered; may be void\n# - records: recordset of all records on which the action is triggered in multi-mode; may be void\n# - time, datetime, dateutil, timezone: useful Python libraries\n# - float_compare: utility function to compare floats based on specific precision\n# - b64encode, b64decode: functions to encode/decode binary data\n# - log: log(message, level='info'): logging function to record debug information in ir.logging table\n# - _logger: _logger.info(message): logger to emit messages in server logs\n# - UserError: exception class for raising user-facing warning messages\n# - Command: x2many commands namespace\n# To return an action, assign: action = {...}\n\n# Get current sales order\nso = record\n\n# Proceed only if the order is in draft state\nif so.state == 'draft':\n customer = so.partner_id\n \n # Fetch other draft sales orders for the same customer\n existing_orders = env['sale.order'].search([\n ('partner_id', '=', customer.id),\n ('state', '=', 'draft'),\n ('id', '!=', so.id) # Exclude the current order\n ])\n \n if existing_orders:\n # Select the first existing draft order for merging\n target_so = existing_orders[0]\n\n for line in so.order_line:\n product = line.product_id\n qty = line.product_uom_qty\n\n # Check if the product exists in the target SO\n existing_line = target_so.order_line.filtered(lambda l: l.product_id == product)\n \n if existing_line:\n # Update quantity in the existing order line\n existing_line.product_uom_qty += qty\n else:\n # Move the line to the existing order\n line.write({'order_id': target_so.id})\n\n # Delete the original SO if it's empty after merging\n if not so.order_line:\n so.unlink()": STORE_ATTR

Author Best Answer

Thanks Sujata.


To Begin I've test to put in on the model mrp.production.


Here is the result, but it doesn't work :


# Get current manufacturing order

mo = record


# Proceed only if in confirmed state

if mo.state == 'confirmed':

   

    # Fetch other confirmed manufacturing orders excluding the current one

    existing_orders = env['mrp.production'].sudo().search([

        ('state', '=', 'confirmed'),

        ('id', '!=', target_mo.id})


        # Invalidate cache uniquement avant la suppression

        if not mo.sudo().move_finished_ids:

            mo.invalidate_cache()

            mo.sudo().unlink()


Avatar
Discard