1
votes

I am using Odoo 10. I have a custom field call linear_units in Sales Order. I have make to order ticked and it creates an automatic Purchase order. I would like to include the field linear_units from Sales order to the purchase order. With below code I can select the Sales order but I cant figure out how to add a field.

class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    sale_order_id = fields.Many2one(
        'sale.order',
        "Sale Order",
        help="Reference to Sale Order")

The above code works for selecting a sales order in purchase order. I have a float field in Sales order called linear_units. I need this field to copy to purchase order. I tried below but does not work

class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'


    linear_units2 = fields.Float("Linear Units")

    @api.onchange('product_id','linear_units')
    def _onchange_product_qty(self):
        if self.product_id:
            self.linear_units2 = self.sale.order.linear_units
2
What it's your issue?, or your code posted here is incomplete or you have missed the ending ) in the field definitionAxel Mendoza
Is 'linear_units' compute field ?Keval Mehta
The above code works for selecting a sales order in purchase order. I have a float field in Sales order called linear_units. I need this field to copy to purchase orderuser2379186

2 Answers

0
votes

you can add a related field in the purchase order for linear_units like below

class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    sale_order_id = fields.Many2one('sale.order', "Sale Order", help="Reference to Sale Order")
    linear_units = fields.Float(related='sale_order_id.linear_units')

It will fetch the related linear_units value from the selected sale_order_id

hope this helps!

0
votes

What is the purpose of this field. Is it supposed to be on each order line or is it supposed to be on the sale order as a whole. With the setup you have, you two options: First

sale_order_lines = fields.One2many('sale.order.line', 'Sale Order Lines')

Then from there you can reference your order number and your linear units.

sale_order_id = fields.Many2one('sale.order', related='sale_order_lines.order_id', string='Sale Order')
linear_units2 = fields.Float(related='sale_order_lines.linear_units', string='Linear Units')

and Second:

sale_order_id = fields.Many2one('sale.order', string='Sale Order')
linear_units = fields.Float(related='sale_order_id.sale_order_lines.linear_units', string='Linear units')

Though I'm not entirely certain that the second option will work. If this is the same value on all order lines then I would suggest putting linear_units on sale.order, then if you need it on the order lines you can put a related field on the order lines and then your fields will look like below

class SaleOrder(model.Models):
    _inherit='sale.order'

    linear_units = fields.Float(string='Linear Units')


class SaleOrderLines(model.Models):
    _inherit='sale.order.lines'

    linear_units = fields.Float(related='order_id.linear_units', string='Linear Units', readonly=True)

class PurchaseOrder(models.Models):
    _inherit='purchase.order'

    sale_order_id = fields.Many2one('sale.order', string='Sale Order')
    linear_units = fields.Float(related='sale_order_id.linear_units', string='Linear Units', readonly=True)

(I suggest putting the read only on your related fields because if they are changed on your inherited view it will change it for that sale order and all of its relations.)