Skip to main content
 首页 » 编程设计

python之Odoo 以编程方式在 python 中向采购订单添加税款

2025年01月19日9powertoolsteam

我正在从外部来源(在线 xml 请求)创建采购订单。 当我遍历每个订单时,我创建了一个采购订单,然后我遍历了产品并为每个产品创建了一个订单行。

除增加税收外,所有这些都有效。我对如何加税感到困惑。我是否应该像这样立即将值添加到采购订单中 ->

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

如果我像这样创建我的采购订单,amount_tax 和 amount_total 将被忽略,我只是从订单行中获取不含税的总计。

这是错误的方式吗?我读过有关在采购订单上调用 onchange 的信息,但我不确定它是如何工作的,因为我看不到这将如何增加税收

这张图片显示订单行没有税

此图为无税订单

简而言之,如何在从 Python 后端创建的采购订单中添加税款(例如 21%)。

感谢能够为我指出正确方向的人,过去 3 天一直在努力寻找这个...

请您参考如下方法:

需要调用odoo默认的on-change方法。

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

第 1 步:您需要在不添加任何订单行的情况下创建采购订单

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

第 2 步:使用以下方法创建所有采购订单行。

    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) 

在第 2 步中,我们创建了采购订单行并调用了 onchange_product_id 方法。它将根据采购订单财务状况和产品默认税自动计算税金。

这可能对你有帮助。