Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
2602 Widoki

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?

Awatar
Odrzuć

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...

Autor

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?

Najlepsza odpowiedź
  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


Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
sty 24
5360
0
cze 23
1968
1
gru 22
3269
0
kwi 22
2487
1
kwi 19
6152