Odoo Help


Custom Import method - how to ?

Dr Obx
on 2/15/16, 5:57 AM 740 views

How to import csv file with multiple 'order_lines'?

Meantime I was doing some research and actually I found that I do not need to create import method but just add a condition which will check how many products is in the order.

Sales record numberUser id
Ted Butcher


VW Polo

Alloys 18"
Frank Pitchka
Pencil "Bravo"

So we have 2 orders with two different form. Order 1 is multiple and 2 is single.

Based on this how can I using API create a sale_order with multiple order_lines ?

Something like that ..

def order_sort(self):

records = self.env['iprodstep.log'].search([('state','=','draft')])

for record in records:

if record is False:

raise except_orm('Nothing to sort')

record.write({'image_url': ''.join(map(str,(record.item_id,'.jpg')))})

record.write({'item_id': record.item_id})

print ' '

print ' '

print ' '

print '== record ======================================================================'

print ' ',record.id

if record.sales_record_number:

a = 1

customer = self.env['res.partner'].search([('name','ilike',record.buyer_full_name)])

print ' '

print '.. Customer ....................................................................'

if not customer:

#if record.buyer_postcode:

postcode = "%s %s" %(record.buyer_postcode[:-3].strip(), record.buyer_postcode[-3:])

self.env['res.partner'].create({'name': record.buyer_full_name.title(), 'city': record.buyer_city.title(), 'zip': postcode.upper(),'email': record.buyer_email.lower(), 'phone': record.buyer_phone_number})

print record.buyer_full_name.title()

print record.buyer_email.lower()

print record.buyer_address_1.title()

print record.buyer_city.title()

print postcode.upper()


#if record.buyer_postcode:

postcode = "%s %s" %(record.buyer_postcode[:-3].strip(), record.buyer_postcode[-3:])

print record.buyer_full_name.title()

print record.buyer_email.lower()

print record.buyer_address_1.title()

print record.buyer_city.title()

print postcode.upper()

items = self.env['iprodstep.log'].search([('sales_record_number','=',record.sales_record_number)])

print '.. order .......................................................................'

for item in items:

if item.item_id:

print ' ',a,'. ',item.item_id

a = a + 1

partner_id = self.env['res.partner'].search([['name', '=', record.buyer_full_name], ['zip','=', postcode]])

print '.. partner_id ..................................................................'

print ' ',partner_id.id

print '================================================================================'

On 2/15/16, 8:47 AM


please check the code below to import csv file to create Purchase Order, you can manipulate it to create Sale order s per your requirement:



inp = cStringIO.StringIO(val)

reader = csv.DictReader(inp, quotechar='"', delimiter=',')

except Exception as e:

raise UserError(_('Invalid File!.\nPlease try using another file.'))

Now reader will contain the data from csv file in form of dictionary...... now use it as per your requirement

purchase_order = {}

po_pool = self.env['purchase.order']

po_line_pool = self.env['purchase.order.line']

ven_pool = self.env['res.partner']

part_pool = self.env['part.number']

product_pool = self.env['product.product']


for row in reader:

vendor = row['Vendor Id'].strip()

po = row['PO Number'].strip()

if po not in purchase_order:

po_obj = po_pool.search([('name', '=ilike', po)])

if not po_obj:

po_data ={}

#create PO

vendor_obj = ven_pool.search([('supplier_code', '=ilike', vendor)], limit=1, order="id desc")

if not vendor:

raise UserError(_('''Vendor Id '%s' does not exist in the system''')%(vendor))


cuttoff_date = datetime.datetime.strptime(row['PO Date'].strip(), "%d-%b-%y")

except ValueError:

raise ValidationError(_('''Date '%s' is not in correct format''')%(row['PO Date'].strip()))

po_data = {

'name': po,

'partner_id': vendor_obj.id,

'partner_ref': vendor_obj.supplier_code,

'cuttoff_date': cuttoff_date


po_obj = po_pool.create(po_data)

purchase_order[po] = [po_obj, [], []]

line_data = {}

part_number = row['Part Number']

part_obj = part_pool.search([('name', '=ilike', part_number)], limit=1, order="id desc")

if not part_obj:

raise ValidationError(_('''Part '%s' does not exist in the system''')%(part_number))

product_obj = product_pool.search(['|', ('part_number', '=', part_obj.id), ('alt_pt_no_ids', 'in', part_obj.id)], limit=1, order="id desc")

if not product_obj:

raise ValidationError(_('''No product is tagged to part '%s' ''')%(part_number))


'part_no': part_obj.id,

'product': product_obj.id,

'price_unit': row['Unit Price'].strip(),

'product_qty': 1,

'product_uom': product_obj.uom_id.id,


purchase_order[po][1].append((0, 0, line_data))


for key, order in purchase_order.iteritems():

po = order[0]

lines = order[1]


'order_line': lines,

'state': 'done',


for pno in order[2]:

lines = po_line_pool.search([('order_id', '=', po.id), ('part_no', '=', pno.id)])

history = []

for line in lines:

history.append((4, line.id))

pno.write({'po_history_ids': history})

except Exception as e:

raise UserError(_('%s'%str(e)))

return True

I was thinking about it a lot and in conclusion I decided to change it completely. Because my module was working on own table which is good on one hand but on the other hand if I can use sale_order/order_lines instead ..... I think it should be better. Why ? Because I can/will have more than one source of orders (webstore, ebay, orders created by sale department) so module will load and operate on one/global list of orders. However import method will be required :)

Dr Obx
on 2/15/16, 9:58 AM

Thank you Pawan :)

Dr Obx
on 2/15/16, 9:58 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 2/15/16, 5:57 AM
Seen: 740 times
Last updated: 2/15/16, 6:42 PM