Odoo Help

Welcome!

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

0

Custom Import method - how to ?

By
Dr Obx
on 2/15/16, 5:57 AM 434 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
name
email
item
price
1
Miriam234
Ted Butcher
tedbut@gmail.co

1



VW Polo
12345.00
1



Alloys 18"
4321.00
2
Fariam554
Frank Pitchka
pitchka@yahoo.jp
Pencil "Bravo"
1.20

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()

else:

#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 '================================================================================'

0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 2/15/16, 8:47 AM

Rob,

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:

try:

val=base64.decodestring(obj.file)

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']

try:

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

try:

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

line_data.update({

'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))

purchase_order[po][2].append(part_obj)

for key, order in purchase_order.iteritems():

po = order[0]

lines = order[1]

po.write({

'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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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