跳至内容
菜单
此问题已终结

How do i store a temporary (NewId) in sale.order.line table? Here is my scenario:

Before converting a quotation to sales order, i need to insert a default product id manually in sales order line upon creation of a quotation. However, it is returning an error: "Missing required field". I checked it and it was "order_id" field from sale.order.line. 


Sample code:

# Create SO line - from sales.order (this function will be called when there are changes in default product line)

@api.onchange('default_product_line')

def .....(self):

for line in record.default_product_line:

      params = {

            'order_id': self.id, # causing error because quotation is not saved yet, still in draft

            'name': line.name,

            'product_uom_qty': line.qty,

            'price_unit': line.price_unit,

            'product_id': line.product_id.id

      }

      self.env['sale.order.line'].create(params)




How can i store sale order line temporarily if a quotation state is still draft?

形象
丢弃

Please provide more details, I don't see clear exactly what you are doing that have the missing order_id field, how and where? post some code...

编写者

Updated the details

I'm sure that I will get errors if I try to run the code you post, why did you put it like that? what it's the value of the record variable?

最佳答案
  1. Add a One2many field in the sale.order model to store the temporary sale order lines.

  2. Modify the onchange method to add the temporary sale order lines to the One2many field.

  3. When converting the quotation to a sales order, copy the temporary sale order lines from the One2many field to the actual sale order lines.

Here's the updated code:

  1. Add a One2many field in the sale.order model:

class SaleOrder(models.Model):

_inherit = 'sale.order'


temp_order_lines = fields.One2many('sale.order.line', 'order_id_temp', string='Temporary Order Lines')


Modify the onchange method to add the temporary sale order lines to the temp_order_lines field:

@api.onchange('default_product_line')

def add_temp_order_lines(self):

temp_lines = []

for line in self.default_product_line:

temp_lines.append((0, 0, {

'name': line.name,

'product_uom_qty': line.qty,

'price_unit': line.price_unit,

'product_id': line.product_id.id

}))

self.temp_order_lines = temp_lines


When converting the quotation to a sales order, copy the temporary sale order lines to the actual sale order lines:

def convert_to_sales_order(self):

# Create the sales order

sale_order_vals = {

# Add other required fields for the sales order

}

sale_order = self.env['sale.order'].create(sale_order_vals)


# Copy temporary sale order lines to actual sale order lines

for temp_line in self.temp_order_lines:

sale_order_line_vals = {

'order_id': sale_order.id,

'name': temp_line.name,

'product_uom_qty': temp_line.product_uom_qty,

'price_unit': temp_line.price_unit,

'product_id': temp_line.product_id.id,

# Add other required fields for the sales order line

}

self.env['sale.order.line'].create(sale_order_line_vals)


# Optionally, you can remove the temporary sale order lines from the quotation

self.temp_order_lines = [(5, 0, 0)]


return sale_order


形象
丢弃
最佳答案

Here is how you could do it

      self.write({'order_line': [(0,0, {

            'name': line.name,

            'product_uom_qty': line.qty,

            'price_unit': line.price_unit,

            'product_id': line.product_id.id

      })]})

The idea it's that you don't need to create anything in the database, since it's an onchange you need to return the change of the data. In this case you are adding a new line to the sale order that will be showing in the browser and saved into the database once you save the sale order form

形象
丢弃
相关帖文 回复 查看 活动
2
1月 24
5304
0
6月 23
1916
1
12月 22
3192
0
4月 22
2422
1
4月 19
6126