Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
2431 Vistas

How do I stop a reverse transfer from being done for more than the delivered quantity on the Sales Order? At the moment my team can change the quantity on the Reverse Transfer to more than was delivered, this then leave the sales order with a negative delivered quantity.

I have a simple Automated action that stops delivering more product than is ordered. This is run on the Stock Move model, matching all records, and executes the python code.

if record.product_uom_qty

  raise UserError("You can't transfer more than the Initial Demand!")


I can't work out how to write this for a Reverse Transfer (Return Picking model) where the quantity gets compared to the delivered quantity on the sales order.

Any help would be appreciated.

Avatar
Descartar

We are in the same page mate, can you share the server action for avoid delivering more product than is ordered?

Thanks.

Mejor respuesta

To stop a reverse transfer in Odoo 16 from being done for more than the delivered quantity, you can implement an automated action or override the relevant Odoo method to enforce this restriction.

Here’s how you can implement this:

1. Understanding the Requirement

  • Reverse Transfer: This process is initiated from the original delivery picking to return goods.
  • Delivered Quantity: You need to ensure that the return quantity does not exceed the delivered quantity.

2. Automated Action Approach

You can create an Automated Action that validates the return picking quantity before confirmation.

Steps to Create Automated Action

  1. Activate Developer Mode:
    • Go to Settings > Activate Developer Mode.
  2. Navigate to Automated Actions:
    • Go to Settings > Technical > Automated Actions.
  3. Create a New Automated Action:
    • Model: stock.move
    • Trigger: On Creation & Update
    • Condition: Add a domain to target only reverse transfers:
      [('picking_type_id.code', '=', 'incoming'), ('origin_returned_move_id', '!=', False)]
      
    • Python Code:
      for record in records:
          if record.origin_returned_move_id:
              delivered_qty = record.origin_returned_move_id.quantity_done
              if record.quantity_done > delivered_qty:
                  raise UserError(
                      "You cannot return more than the delivered quantity! Delivered: %s, Trying to Return: %s" %
                      (delivered_qty, record.quantity_done)
                  )
      

3. Server-Side Method Override (Recommended)

If your team is familiar with Odoo development, overriding the button_validate method on the stock.picking model ensures this logic is enforced at the server level.

Steps to Implement

  1. Create a Custom Module: If you don’t have one already, create a custom module.
  2. Extend the stock.picking Model: Override the button_validate method to add validation for reverse transfers.
    Code Example:
    from odoo import models, api
    from odoo.exceptions import UserError
    
    class StockPicking(models.Model):
        _inherit = 'stock.picking'
    
        @api.model
        def button_validate(self):
            for move in self.move_ids_without_package:
                if move.origin_returned_move_id:
                    delivered_qty = move.origin_returned_move_id.quantity_done
                    if move.quantity_done > delivered_qty:
                        raise UserError(
                            "You cannot return more than the delivered quantity! Delivered: %s, Trying to Return: %s" %
                            (delivered_qty, move.quantity_done)
                        )
            return super(StockPicking, self).button_validate()
    
  3. Restart the Server and Upgrade the Module:
    • Restart the Odoo server.
    • Upgrade the module.
    ./odoo-bin -u your_custom_module_name
    

4. Testing

  • Create a sales order and confirm delivery.
  • Initiate a reverse transfer.
  • Try to input a return quantity greater than the delivered quantity.
  • The system should block the operation and raise an appropriate error message.

5. Additional Considerations

  • Multi-Line Validation: If reverse transfers have multiple lines, ensure the logic applies to all lines.
  • Negative Quantities: Check if your setup allows negative stock, as this can also affect the validation.
  • Customizable Messaging: You can improve user feedback by including the product name or other relevant details in the error message.

6. Advantages of the Method

  • Automated Action: Quick to implement with minimal technical involvement.
  • Server-Side Method: More robust and ensures the rule is enforced even if users bypass the UI (e.g., through API).

By implementing one of these approaches, you can effectively prevent your team from returning more items than were delivered, maintaining accurate stock and sales order records. Let me know if you need further clarification or help!

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
4
jul 19
4194
2
ago 25
2410
1
jul 25
910
1
ago 25
1151
0
may 25
1345