Search code examples
pythonodoo

Odoo programatically add taxes to purchase order in python


I am creating purchase orders from an external source (online xml request). While I loop over every order I create a purchase order, then I loop over the products and create an orderline for each product.

All of this is working except for adding taxes. I am puzzled on how to add the taxes. Should I add the values immediately to the purchase order like this ->

# Create orderline foreach product (this happens in the loop foreach product)
orderlineList =  {
                'name': itemText,
                'product_id': itemId,
                'product_qty': itemOrdered,
                'product_uom': 1,
                'price_unit': itemPrice,
                'date_planned': orderDatePlanned,
            }

            struct = orderlinetuple + (orderlineList,)
            po_vals.append(struct)

 #This adds all the orderlines into 'order_line'
  orderDict = { 
         'amount_untaxed' : totalNet,
         'amount_tax': totalTax,
         'partner_id': api_partner,
         'amount_total' : totalBrut,
         'order_line': po_vals,
     }
# Then we create the purchase order with the added orderlines in one go
  self.PurchaseOrder = self.env['purchase.order']   
  po_id = self.PurchaseOrder.create(orderDict)

If i create my purchase orders like this the amount_tax and amount_total are ignored I just get the totals from the orderlines without taxes.

Is this the wrong way? I have read about calling onchange on the purchase order but I am unsure how this works since I don't see how that will add taxes

this picture show orderlines has no taxes enter image description here

This picture show the order has no taxes enter image description here

In short, how to add taxes (f.e. 21%) to a purchase order when created from the backend in python.

Kudos to the person who can point me in the right direction, been trying to find this for the last 3 days...


Solution

  • You need to call default on-change method of odoo.

    When you call on-change method then system will automatic set tax based on product default tax and purchase order fiscal position.

    Step 1: You need to create purchase order without adding any order lines

    self.env['purchase.order'].create({'partner_id':'',...})

    Step 2: Create all purchase order line using following method.

        new_record=self.env['purchase.order.line'].new({'order_id':purchase_order.id,
                  'product_id':product_id,
                  'product_uom':uom+id,
                  'name':product_name
                  })
    
        new_record.onchange_product_id()
        order_vals=new_record._convert_to_write({name: new_record[name] for name in new_record._cache}) 
        order_vals.update({'product_qty':product_qty,'price_unit':price_unit})
        self.env['purchase.order.line'].create(order_vals)
    

    In step-2 we have create purchase order line and call onchange_product_id method. It will automatically calculate tax based on purchase order fiscal position and product default tax.

    This may help you.