Skip to Content
Menu
This question has been flagged
5 Replies
4504 Views

Hello, I'm using Odoo 10 in english. I'm a novice.

In purchase order page I add a dropdown with a reference to Sale Order.

In PurchaseOrder inherited model, I wrote following code:

@api.onchange('sale_order_id')    
def onchange_sale_order_lines(self):
    model_purchase_order_line = self.env['purchase.order.line']

    for so_line in self.sale_order_id.order_line:            
        vals = {}            
        vals['order_id'] = self._origin.id            
        vals['product_id'] = so_line.product_id.id            
        vals['product_qty'] = so_line.product_uom_qty            
        vals['product_uom'] = so_line.product_uom.id            
        vals['price_unit'] = so_line.price_unit            
        vals['price_total'] = so_line.price_total
        vals['price_tax'] = so_line.price_tax            
        vals['name'] = so_line.name            
        vals['date_planned'] = date.today()            
        model_purchase_order_line.create(vals)

but when sale order dropdown changes, I have the following error (Odoo administrator user set to english language):​

Traceback (most recent call last):
  File "/home/odooadmin/OCB/odoo/http.py", line 642, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/odooadmin/OCB/odoo/http.py", line 684, in dispatch
    result = self._call_function(**self.params)
  File "/home/odooadmin/OCB/odoo/http.py", line 334, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/odooadmin/OCB/odoo/service/model.py", line 101, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/odooadmin/OCB/odoo/http.py", line 327, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/odooadmin/OCB/odoo/http.py", line 942, in __call__
    return self.method(*args, **kw)
  File "/home/odooadmin/OCB/odoo/http.py", line 507, in response_wrap
    response = f(*args, **kw)
  File "/home/odooadmin/OCB/addons/web/controllers/main.py", line 895, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/odooadmin/OCB/addons/web/controllers/main.py", line 887, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/odooadmin/OCB/odoo/api.py", line 689, in call_kw
    return call_kw_multi(method, model, args, kwargs)
  File "/home/odooadmin/OCB/odoo/api.py", line 678, in call_kw_multi
    recs = self.with_context(context or {}).browse(ids)
  File "/home/odooadmin/OCB/odoo/models.py", line 4835, in browse
    return self._browse(ids, self.env, prefetch)
  File "/home/odooadmin/OCB/odoo/models.py", line 4822, in _browse
    prefetch[cls._name].update(ids)
TypeError: unhashable type: 'list'


unexpectedly, when I switch to italian language, I have a different error:

(translated from italian)

The operation cannot probably be concluded for the following reasons:
- elimination: other recordings refer to the registration that we want to delete
- creation / modification: a mandatory field is not filled in correctly

[object with reference: order_id - order.id]


I don't understand which is the problem, can you help me?

thanks in advance


Tony

Avatar
Discard

Do you want to create sale order from purchase order?

Best Answer

Hi, this is an example:


def create_po_orders(self):
self.ensure_one()
po_obj = self.env['purchase.order']
vendors = self.mapped('wizard_line_ids.partner_id')

for vendor in vendors:
# creating the order
fiscal_position = vendor.property_account_position_id
po_order = po_obj.create({
'partner_id': vendor.id,
'fiscal_position_id': fiscal_position.id,
'date_planned': fields.Datetime.to_string(datetime.today()),
})

# creating order lines

lines = self.env['sale.order'].filtered(lambda l: l.partner_id == vendor)
line_ids = []
for line in lines:
price = line.product_id.standard_price
taxes = line.product_id.taxes_id.filtered(
lambda r: r.company_id == company)
if fiscal_position and taxes:
tax_ids = fiscal_position.map_tax(taxes).ids
else:
tax_ids = taxes.ids

line_ids.append((0, 0, {
'name': line.product_id.name,
'product_id': line.product_id.id,
'product_qty': line.qty_purchase,
'price_unit': price,
'product_uom': line.product_id.uom_po_id,
'date_planned': fields.Datetime.to_string(datetime.today()),
'taxes_id': [(6, 0, tax_ids)],

}))
po_order.update({'order_line': line_ids})
Avatar
Discard
Author

Thanks to @GiusyG:

my code:

@api.onchange('sale_order_id')

def onchange_sale_order_lines(self):

# import pdb; pdb.set_trace()

id_order = False

if self._origin.id:

id_order = self._origin.id

if not id_order and self.id:

id_order = self.id

vals = []

for so_line in self.sale_order_id.order_line:

vals.append((0,0, {

'order_id': id_order,

'name': so_line.name,

'product_id': so_line.product_id.id,

'product_qty': so_line.product_uom_qty,

'price_unit': so_line.price_unit,

'product_uom': so_line.product_uom.id,

'date_planned': fields.Datetime.to_string(datetime.today()),

}))

self.update({'order_line' : vals})

Best Answer


I think you can fill the field by inheriting the function 

_prepare_purchase_order_line
this is the default function for creating purchase order from sale order


Avatar
Discard
Author Best Answer

No, I want to create purchase order lines from a related sale order lines.

Avatar
Discard
Author

Sorry, I was wrong to write the title

Related Posts Replies Views Activity
2
Nov 24
25064
2
May 24
5512
3
Mar 24
4961
0
Mar 24
261
3
Feb 24
11415