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.

1

[SOLVED] Get and Set default tax by Python command line

By
Selverine
on 3/17/15, 1:18 PM 1,692 views

Hi,

First, thank you for your time.

I would like to get and set the default tax for my product.

The creation is little different than usual. I create automatically two product for each order.

I would like to get and set the tax_id for this two products. But I have not a clue...


My code:

    def create(self, cr, uid, vals, context=None):
context = context or {}
sol = self.pool.get('sale.order.line') 
if vals.get('name','/')=='/':
vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
res = super(sale_order, self).create(cr, uid, vals, context=context)
# res is the id of the created sale order, and we create 2 lines with product_1 and product_2. Here I would like ti add tax_id: 
self.write(cr, uid, [res], {'order_line': [[0, 0, {'product_id': 1,'name': 'Service','price_unit': 50}], [0, 0, {'product_id': 4,'name': 'transport','price_unit': 15}]]})
 return res

3

Cyril Gaspard (GEM)

--Cyril Gaspard (GEM)--
4136
| 5 6 8
Paris, France
--Cyril Gaspard (GEM)--

Forum contributor since 2008

Cyril Gaspard (GEM)
On 3/17/15, 7:05 PM

Hi,

call the onchange product with good order pricelist, fiscal position, date order, partner_id order .... and you will have all good values like if you will do this manually in the view.

UPDATE : code written without testing

1) just do operation in create sale.order :

def create(self, cr, uid, vals, context=None):

    context = context or {}

    sol = self.pool.get('sale.order.line')

    if vals.get('name','/')=='/':

    vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'

    pricelist_id = vals.get('pricelist_id') or False

    fiscal_position_id = vals.get('fiscal_position') or False

    partner_id = vals.get('partner_id') or False

    date_order = vals.get('date_order') or False

    order_line = False

   # In onchange, I put quantity = 1 because not specified, important to calculate correctly pricelist sometimes give different price_unit depending of quantity ... (specify you real quantity by product). You can loop directly on onchange I think ...

    for product_id, qty in [(1, 1), (4, 1)]:

        onchange = sol.product_id_change(cr, uid, [], pricelist_id, product_id, qty=qty, uom=False, qty_uos=qty, uos=False, name='', partner_id=partner_id, lang=False, update_tax=True, date_order=date_order, packaging=False, fiscal_position=fiscal_position_id , flag=False, context=context)

        onchange = onchange.get('value') or False

        if onchange:

            tax_id = onchange.get('tax_id') or []

            if tax_id:

                tax_id = [[6, 0, tax_id]]

            onchange['tax_id'] = tax_id

            onchange['product_id'] = product_id

            onchange['qty'] = qty

            order_line += [(0, 0, onchange)]

    if order_line:

        vals['order_line'] += order_line

    res = super(sale_order, self).create(cr, uid, vals, context=context) 



2) to get tax (see in sol function product_id_change):

context = context or {}

lang = lang or context.get('lang', False)

if not partner_id:

     raise osv.except_osv(_('No Customer Defined!'), _('Before choosing a product,\n select a customer in the sales form.'))

partner_obj = self.pool.get('res.partner')

product_obj = self.pool.get('product.product')

context = {'lang': lang, 'partner_id': partner_id}

partner = partner_obj.browse(cr, uid, partner_id)

lang = partner.lang

context_partner = {'lang': lang, 'partner_id': partner_id}

fpos = False

if not fiscal_position:

     fpos = partner.property_account_position or False

else:

     fpos = self.pool.get('account.fiscal.position').browse(cr, uid, fiscal_position)

if update_tax: #The quantity only have changed

     result['tax_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, product_obj.taxes_id)

Bye

Hi Cyril. Thank you for your help. Is it possible to call the onchange product from create? Because one is on sale.order ( create function) the other one is in sale.order.line (onchange product). So how can I find the parameters ids for onchange product? Or need I to do it in order function? Thank you. Selverine

Selverine
on 3/18/15, 12:12 PM
0
Selverine
On 3/19/15, 10:35 AM

Hi Cyril.

Thank you again a lot for your help.

I try the first solution but even if I don't have any erros, nothing happen.

pricelist_id,partner_id and date_order are correct.
fiscal_position_id is egual to False  (fiscal_position_id = vals.get('fiscal_position') or False)

            onchange = sol.product_id_change(cr, uid, [], pricelist_id, product_id, qty=qty, uom=False, qty_uos=5, uos=False, name='', partner_id=partner_id, lang=False, update_tax=True, date_order=date_order, packaging=False, fiscal_position=fiscal_position_id , flag=False, context=context)

            onchange = onchange.get('value') and onchange['value'].get('result') or False

            if onchange:
                tax_id = onchange.get('tax_id') or []
I have nothing for tax_id so I suppose that onchange didn't works.

Do you have any idea please?

Update3:

The output:


-> res = super(sale_order, self).create(cr, uid, vals, context=context)
(Pdb) n
IndexError: 'list index out of range'
> /opt/OpenERP/openerp/HorizonGroup/openerp/addons/sale/sale.py(519)create()
-> res = super(sale_order, self).create(cr, uid, vals, context=context)
(Pdb) print vals
{'origin': False, 'message_follower_ids': False, 'bat_sent_to_Ed': False, 'categ_ids': [[6, False, []]], 'order_line': [(0, 0, {'product_uos_qty': 1, 'name': u'Service', 'product_uom': 5, 'order_id': 168L, 'price_unit': 75.0, 'price_list': u'Service', 'delay': 7.0, 'product_packaging': False, 'product_id': 1, 'th_weight': 0.0, 'product_uos': False, 'type': u'make_to_order', 'purchase_price': 30.0, 'condition': u' ', 'tax_id': [6, 0, [2]]}), (0, 0, {'product_uos_qty': 1, 'name': u'Transport', 'product_uom': 1, 'price_unit': 1.0, 'price_list': u'Transport', 'delay': 7.0, 'product_packaging': False, 'product_id': 4, 'th_weight': 0.0, 'product_uos': False, 'type': u'make_to_order', 'purchase_price': 0.0, 'condition': u' ', 'tax_id': [6, 0, [2]]})], 'picking_policy': 'direct', 'order_policy': 'manual', 'image': '/9j/4AAQSkZJRgABAQAAAQABAAD/4QcqRXhpZgAASUkqAAgAAAAIABIBCQABAAAAAQAAABoBCQABAAAASAAAABsBCQABAAAASAAAACgBCQABAAAAAgAAADEBAgAOAAAAbgAAADIBAgAUAAAAfAAAABMCCQABAAAAAQAAAGmHBAABAAAAkAAAAN4AAABnVGh1bWIgMi4xNC4zADIwMTU6MDI6MDEgMTk6NDM6NDEABgAAkAcABAAAADAyMjEBkQcABAAAAAECAwAAoAcABAAAADAxMDABoAkAAQAAAAEAAAACoAkAAQAAAGQAAAADoAkAAQAAADMAAAAAAAAABgADAQMAAQAAAAYAAAAaAQkAAQAAAEgAAAAbAQkAAQAAAEgAAAAoAQkAAQAAAAIAAAABAgQAAQAAACwBAAACAgQAAQAAAPUFAAAAAAAA/9j/4AAQSkZ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DV5dxekuZ8PnaBz0Ej4hQPsHtdpc0g9LLMVe0HMM/sx1EcN+TIx87la9X4riFbKZqueSR5B4neoFXDRMPyznHaB6g8k/E6d31ABsUovZCb6hq6dFduVp5anLtBPMbyPgaSeu5UFE1z5LWueu/x4q/8tQiDL9BECTpp2cf7Ba/Jml0sltVh+bqVi4s1t9ayCIi+gKEh3hVbnTKNZTTzVNDE+eleSbMF3Mvyt0VpIpuJ4XFiMXRydVtR/pMU9S6B2k1edpoi0uBBvp0m+4r4WcJG7iRbc4m5K9AYlgmFYiD2uhhkce9ps74jeteq9nmCTEuhfUQE8g4ED4hYWpydWMPwnBw/B9uKtRYvER2wQqbLiSdTr2POOx+K6vby0HVa+q3krVm2ZxH+Fislujo7/NR3bMZtVxirf8AWfVTDlnFG/xcR7poYnTHzeqrHSHNvYXJsCR4LlrbMtbgbE/grOZswPexUDff2YvqpcGzLD2n9/iE8nUNaB6ruzLGJu1x23j3XLsUph5uBVTi17m/DdZSKWmnqHiOGGSR7uAaCSVc1FkPL1OQXU0k5H9R/pZZ+iw+hom6aSkhhH3GAKpTZMqXm8zw0fbrPJKy4zGO40lVrlDIdXNNHU4szs9O0giHvP8AA9P+q0mNDWhrQAALADkuUW2w3C4MOj0IvHWTrKiVFS+odpPRERUkuiIiEIiIhCIiIQiIiEIiIhCIiIQiIiEL/9k=', 'order_line4': [], 'shop_id': 1, 'client_order_ref': False, 'order_line5': [], 'date_order': '2015-04-04', 'partner_id': 27, 'message_ids': False, 'fiscal_position': False, 'user_id': 1, 'sale_BAT3': [], 'payment_term': 4, 'company_id': 8, 'texte_bat': 1, 'pricelist_id': 1, 'project_id': False, 'incoterm': False, 'ecart': 0, 'section_id': False, 'link_purchase': False, 'partner_invoice_id': 27, 'description_BAT': False, 'order_line3': [], 'name': u'SO182', 'partner_shipping_id': 27, 'description_Quotation': False, 'invoice_quantity': 'order', 'devis_accept_by_client': False}
(Pdb) print order_line
[(0, 0, {'product_uos_qty': 1, 'name': u'Service', 'product_uom': 5, 'order_id': 168L, 'price_unit': 75.0, 'price_list': u'Service', 'delay': 7.0, 'product_packaging': False, 'product_id': 1, 'th_weight': 0.0, 'product_uos': False, 'type': u'make_to_order', 'purchase_price': 30.0, 'condition': u' ', 'tax_id': [6, 0, [2]]}), (0, 0, {'product_uos_qty': 1, 'name': u'Transport', 'product_uom': 1, 'price_unit': 1.0, 'price_list': u'Transport', 'delay': 7.0, 'product_packaging': False, 'product_id': 4, 'th_weight': 0.0, 'product_uos': False, 'type': u'make_to_order', 'purchase_price': 0.0, 'condition': u' ', 'tax_id': [6, 0, [2]]})]

Best regards,


Selverine


PS: name_BAT was a text field. I changed my code and now it is working better ;-)

Hi Cyril. I updated my answer. Than you !

Selverine
on 3/20/15, 6:26 AM

hi, sorry for delay, alot of work, I updated my code, you should now enter in the condition if onchange, but in the dict 'value' you returned with pdb, what is the field 'name_BAT': u'ServiceBAT[]1' ? Type of field and value ? I think the value will give an error, stand answer. bye

Cyril Gaspard (GEM)
on 3/20/15, 3:13 PM

Hi Cyril. I am really sorry to answer just now...some problems too :-(. I updated my answer with the new output. Thank you a lot, for your time. It is really nice ! Selverine

Selverine
on 4/4/15, 5:45 AM

Hi, oups, I forgot to set calculated value in vals for field order line, just add after the loop for => if order_line: vals['order_line'] = order_line, I updated my answer with this, should work now (if no tax displayed, save the sale order, and verify if taxes are displayed), bye

Cyril Gaspard (GEM)
on 4/4/15, 8:20 AM

I updated my answer to ;-). Now I have an error: IndexError: 'list index out of range'. Selverine

Selverine
on 4/4/15, 9:51 AM

oups 2, tax_id should be a list of list : tax_id = [[6, 0, tax_id]], code updated.

Cyril Gaspard (GEM)
on 4/4/15, 10:47 AM

You are my god, It works! Thank you a lot! I just changed this: vals['order_line'] = order_line to vals['order_line'] += order_line in order keep the others order_line.

Selverine
on 4/5/15, 2:04 PM

Hi, right for +=, good news now it works, good continuation, bye

Cyril Gaspard (GEM)
on 4/5/15, 2:18 PM

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: 3/17/15, 1:18 PM
Seen: 1692 times
Last updated: 1/19/16, 4:19 AM