Skip to Content
Menu
This question has been flagged
3 Replies
474 Views

I want to add a custom field in sale order line and want to show its value in purchase order line. It cannot get value from sale order line because when I apply debugger on 

def _prepare_procurement_values(self, group_id=False):
     res = super()._prepare_procurement_values(group_id) 

it will go only there then break but not execute  

def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, po):
    res = super()._prepare_purchase_order_line(product_id, product_qty, product_uom, company_id, values, po)

that function.

What's the solution anyone suggest me?

Avatar
Discard

Next time please use punctuation in your sentences and format your code so your question is actually readable.

Best Answer

Hii,

please try this 
models/stock_rule.py

from odoo import models


class StockRule(models.Model):

    _inherit = 'stock.rule'


    def _prepare_purchase_order_line(

        self, product_id, product_qty, product_uom, company_id, values, po

    ):

        res = super()._prepare_purchase_order_line(

            product_id, product_qty, product_uom, company_id, values, po

        )

        # Pass custom field to PO line

        if values.get('x_custom_ref'):

            res['x_custom_ref'] = values['x_custom_ref']

        return res


i hope it is usefull

Avatar
Discard
Best Answer

Hi,


You're trying to pass a custom field from the Sales Order Line to the corresponding Purchase Order Line, but during the procurement process, your custom value isn’t flowing through—even though you extended _prepare_procurement_values() and _prepare_purchase_order_line().


Try with the following.

1- Add field to sale.order.line

x_reference_code = fields.Char("Reference Code")


2-Extend _prepare_procurement_values() in sale.order.line

def _prepare_procurement_values(self, group_id=False):

    res = super()._prepare_procurement_values(group_id)

    res.update({

        'x_reference_code': self.x_reference_code,

    })

    return res


3- Extend _prepare_purchase_order_line() in purchase.order


def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, po):

    res = super()._prepare_purchase_order_line(product_id, product_qty, product_uom, company_id, values, po)

   

    if 'x_reference_code' in values:

        res.update({

            'x_reference_code': values['x_reference_code'],

        })

    return res


Also, make sure purchase.order.line has this custom field.

x_reference_code = fields.Char("Reference Code")


Follow the three-step override above, and ensure the correct routes (Make To Order, Dropship, etc.) are active on your sale order line.


Hope it helps



Avatar
Discard
Best Answer

See for example https://github.com/odoo/odoo/blob/18.0/addons/sale/models/sale_order_line.py#L1445 and https://github.com/odoo/odoo/blob/18.0/addons/sale_project/models/sale_order_line.py#L474, i.e.

def _prepare_procurement_values(self, group_id=False):
    """ Prepare specific key for moves or other components that will be created from a stock rule
        coming from a sale order line. This method could be override in order to add other custom key that could
        be used in move/po creation.
        """
        return {}


_prepare_procurement_values() must have a return value (dict). Same goes for _prepare_purchase_order_line().

Avatar
Discard
Related Posts Replies Views Activity
2
Aug 25
1503
1
Jul 25
586
1
Feb 25
1419
2
Jun 23
3238
1
Aug 19
3153