Openerp 如何在odoo中将一个字段的值复制到另一个字段?
我已经在product.template模块中创建了一个自定义字段折扣,现在我想复制它并将其分配给sales模块中的折扣字段。我想在发票中键入折扣的名称、价格和折扣值后自动进行折扣。我正在通过发票中的“创建和编辑”选项创建产品。如果我再次为同一产品生成发票,一旦我选择了该产品,它的折扣将自动应用。我尝试使用从互联网上获得的代码之一it自动折扣,但产品说明和价格值消失了。 折扣是销售模块中的一个字段,x_折扣是产品模块中的一个字段,我想将x_折扣值复制到折扣,因为它们在不同的模块中,我发现这很困难Openerp 如何在odoo中将一个字段的值复制到另一个字段?,openerp,odoo,openerp-7,Openerp,Odoo,Openerp 7,我已经在product.template模块中创建了一个自定义字段折扣,现在我想复制它并将其分配给sales模块中的折扣字段。我想在发票中键入折扣的名称、价格和折扣值后自动进行折扣。我正在通过发票中的“创建和编辑”选项创建产品。如果我再次为同一产品生成发票,一旦我选择了该产品,它的折扣将自动应用。我尝试使用从互联网上获得的代码之一it自动折扣,但产品说明和价格值消失了。 折扣是销售模块中的一个字段,x_折扣是产品模块中的一个字段,我想将x_折扣值复制到折扣,因为它们在不同的模块中,我发现这很困难
dis = self.pool.get('product.template').browse(cr, uid,product,context=context).x_discount
return {'value': {'discount':dis}}
对此,您必须使用onchange:- “onchange”机制为客户端界面提供了一种方法,可以在用户在字段中填写值时更新表单,而无需将任何内容保存到数据库中 此处已对产品标识进行了更改,因此:- 要实现这一点,您必须覆盖sale.order.line中的product\u id\u change() 为此,您必须继承sale.order.line对象,并通过添加此行来重新定义产品id\u change()(在定义产品对象的行之后) 有关更多帮助:-如果您正在使用v7,请将此代码复制到module.py文件中:-
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
class sale_order_line(osv.osv):
_inherit = 'sale.order.line'
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
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.'))
warning = {}
product_uom_obj = self.pool.get('product.uom')
partner_obj = self.pool.get('res.partner')
product_obj = self.pool.get('product.product')
context = {'lang': lang, 'partner_id': partner_id}
if partner_id:
lang = partner_obj.browse(cr, uid, partner_id).lang
context_partner = {'lang': lang, 'partner_id': partner_id}
if not product:
return {'value': {'th_weight': 0,
'product_uos_qty': qty}, 'domain': {'product_uom': [],
'product_uos': []}}
if not date_order:
date_order = time.strftime(DEFAULT_SERVER_DATE_FORMAT)
result = {}
warning_msgs = ''
product_obj = product_obj.browse(cr, uid, product, context=context_partner) # product_obj definition
result['discount'] = product_obj.x_discount # modification
uom2 = False
if uom:
uom2 = product_uom_obj.browse(cr, uid, uom)
if product_obj.uom_id.category_id.id != uom2.category_id.id:
uom = False
if uos:
if product_obj.uos_id:
uos2 = product_uom_obj.browse(cr, uid, uos)
if product_obj.uos_id.category_id.id != uos2.category_id.id:
uos = False
else:
uos = False
fpos = fiscal_position and self.pool.get('account.fiscal.position').browse(cr, uid, fiscal_position) or False
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)
if not flag:
result['name'] = self.pool.get('product.product').name_get(cr, uid, [product_obj.id], context=context_partner)[0][1]
if product_obj.description_sale:
result['name'] += '\n'+product_obj.description_sale
domain = {}
if (not uom) and (not uos):
result['product_uom'] = product_obj.uom_id.id
if product_obj.uos_id:
result['product_uos'] = product_obj.uos_id.id
result['product_uos_qty'] = qty * product_obj.uos_coeff
uos_category_id = product_obj.uos_id.category_id.id
else:
result['product_uos'] = False
result['product_uos_qty'] = qty
uos_category_id = False
result['th_weight'] = qty * product_obj.weight
domain = {'product_uom':
[('category_id', '=', product_obj.uom_id.category_id.id)],
'product_uos':
[('category_id', '=', uos_category_id)]}
elif uos and not uom: # only happens if uom is False
result['product_uom'] = product_obj.uom_id and product_obj.uom_id.id
result['product_uom_qty'] = qty_uos / product_obj.uos_coeff
result['th_weight'] = result['product_uom_qty'] * product_obj.weight
elif uom: # whether uos is set or not
default_uom = product_obj.uom_id and product_obj.uom_id.id
q = product_uom_obj._compute_qty(cr, uid, uom, qty, default_uom)
if product_obj.uos_id:
result['product_uos'] = product_obj.uos_id.id
result['product_uos_qty'] = qty * product_obj.uos_coeff
else:
result['product_uos'] = False
result['product_uos_qty'] = qty
result['th_weight'] = q * product_obj.weight # Round the quantity up
if not uom2:
uom2 = product_obj.uom_id
# get unit price
if not pricelist:
warn_msg = _('You have to select a pricelist or a customer in the sales form !\n'
'Please set one before choosing a product.')
warning_msgs += _("No Pricelist ! : ") + warn_msg +"\n\n"
else:
price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist],
product, qty or 1.0, partner_id, {
'uom': uom or result.get('product_uom'),
'date': date_order,
})[pricelist]
if price is False:
warn_msg = _("Cannot find a pricelist line matching this product and quantity.\n"
"You have to change either the product, the quantity or the pricelist.")
warning_msgs += _("No valid pricelist line found ! :") + warn_msg +"\n\n"
else:
result.update({'price_unit': price})
if warning_msgs:
warning = {
'title': _('Configuration Error!'),
'message' : warning_msgs
}
return {'value': result, 'domain': domain, 'warning': warning}
详细步骤:-
我们正在为此创建自己的自定义模块(最佳推荐方式):-
使用以下文件创建名为“product_custom”的文件夹:-
__init__.py
__openerp__.py
product_custom.py
product_custom_view.xml
初始化文件内容:-
import product_custom
{
'name': "Product Customization",
'version': '1.0',
'depends': ['base','product','sale'],
'author': "Baiju KS",
'category': 'Customization',
'description': """
Module used to add discount to product.
""",
# data files always loaded at installation
'data': [
'product_custom_view.xml',
],
'installable': True,
'auto_install': False, }
openerp文件内容:-
import product_custom
{
'name': "Product Customization",
'version': '1.0',
'depends': ['base','product','sale'],
'author': "Baiju KS",
'category': 'Customization',
'description': """
Module used to add discount to product.
""",
# data files always loaded at installation
'data': [
'product_custom_view.xml',
],
'installable': True,
'auto_install': False, }
产品自定义文件:
from openerp.osv import fields, osv
from openerp import SUPERUSER_ID
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
import time
class product_template(osv.osv):
_inherit = "product.template"
_columns = {
'x_discount': fields.float('Discount(%)'),
}
class sale_order_line(osv.osv):
_inherit = 'sale.order.line'
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
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.'))
warning = False
product_uom_obj = self.pool.get('product.uom')
partner_obj = self.pool.get('res.partner')
product_obj = self.pool.get('product.product')
partner = partner_obj.browse(cr, uid, partner_id)
lang = partner.lang
context_partner = context.copy()
context_partner.update({'lang': lang, 'partner_id': partner_id})
if not product:
return {'value': {'th_weight': 0,
'product_uos_qty': qty}, 'domain': {'product_uom': [],
'product_uos': []}}
if not date_order:
date_order = time.strftime(DEFAULT_SERVER_DATE_FORMAT)
result = {}
warning_msgs = ''
product_obj = product_obj.browse(cr, uid, product, context=context_partner)
result['discount'] = product_obj.x_discount # modification
uom2 = False
if uom:
uom2 = product_uom_obj.browse(cr, uid, uom)
if product_obj.uom_id.category_id.id != uom2.category_id.id:
uom = False
if uos:
if product_obj.uos_id:
uos2 = product_uom_obj.browse(cr, uid, uos)
if product_obj.uos_id.category_id.id != uos2.category_id.id:
uos = False
else:
uos = False
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
# The superuser is used by website_sale in order to create a sale order. We need to make
# sure we only select the taxes related to the company of the partner. This should only
# apply if the partner is linked to a company.
if uid == SUPERUSER_ID and context.get('company_id'):
taxes = product_obj.taxes_id.filtered(lambda r: r.company_id.id == context['company_id'])
else:
taxes = product_obj.taxes_id
result['tax_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, taxes)
if not flag:
result['name'] = self.pool.get('product.product').name_get(cr, uid, [product_obj.id], context=context_partner)[0][1]
if product_obj.description_sale:
result['name'] += '\n'+product_obj.description_sale
domain = {}
if (not uom) and (not uos):
result['product_uom'] = product_obj.uom_id.id
if product_obj.uos_id:
result['product_uos'] = product_obj.uos_id.id
result['product_uos_qty'] = qty * product_obj.uos_coeff
uos_category_id = product_obj.uos_id.category_id.id
else:
result['product_uos'] = False
result['product_uos_qty'] = qty
uos_category_id = False
result['th_weight'] = qty * product_obj.weight
domain = {'product_uom':
[('category_id', '=', product_obj.uom_id.category_id.id)],
'product_uos':
[('category_id', '=', uos_category_id)]}
elif uos and not uom: # only happens if uom is False
result['product_uom'] = product_obj.uom_id and product_obj.uom_id.id
result['product_uom_qty'] = qty_uos / product_obj.uos_coeff
result['th_weight'] = result['product_uom_qty'] * product_obj.weight
elif uom: # whether uos is set or not
default_uom = product_obj.uom_id and product_obj.uom_id.id
q = product_uom_obj._compute_qty(cr, uid, uom, qty, default_uom)
if product_obj.uos_id:
result['product_uos'] = product_obj.uos_id.id
result['product_uos_qty'] = qty * product_obj.uos_coeff
else:
result['product_uos'] = False
result['product_uos_qty'] = qty
result['th_weight'] = q * product_obj.weight # Round the quantity up
if not uom2:
uom2 = product_obj.uom_id
# get unit price
if not pricelist:
warn_msg = _('You have to select a pricelist or a customer in the sales form !\n'
'Please set one before choosing a product.')
warning_msgs += _("No Pricelist ! : ") + warn_msg +"\n\n"
else:
ctx = dict(
context,
uom=uom or result.get('product_uom'),
date=date_order,
)
price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist],
product, qty or 1.0, partner_id, ctx)[pricelist]
if price is False:
warn_msg = _("Cannot find a pricelist line matching this product and quantity.\n"
"You have to change either the product, the quantity or the pricelist.")
warning_msgs += _("No valid pricelist line found ! :") + warn_msg +"\n\n"
else:
if update_tax:
price = self.pool['account.tax']._fix_tax_included_price(cr, uid, price, taxes, result['tax_id'])
result.update({'price_unit': price})
if context.get('uom_qty_change', False):
values = {'price_unit': price}
if result.get('product_uos_qty'):
values['product_uos_qty'] = result['product_uos_qty']
return {'value': values, 'domain': {}, 'warning': False}
if warning_msgs:
warning = {
'title': _('Configuration Error!'),
'message' : warning_msgs
}
return {'value': result, 'domain': domain, 'warning': warning}
产品\自定义\视图文件:-
<openerp>
<data>
<record id="product_template_form_view_dis_inherit" model="ir.ui.view">
<field name="name">product.template.common.form.dis.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="/form/sheet/notebook/page[1]/group[1]/group[1]/field[3]" position="after">
<field name="x_discount" />
</xpath>
</field>
</record>
</data>
product.template.common.form.dis.inherit
产品模板
然后将此文件复制到addons文件夹并重新启动服务器,
之后,更新模块列表并安装自定义模块
希望这有帮助。源代码已经有sale\u order\u line类。我应该继承该类吗?如果您正在编辑源代码本身,无需继承sale.order.line,只需将上面显示的行添加到产品\u id\u change()。如果您要通过自己的自定义模块添加自定义,您可以通过继承继续执行上述代码。请包括您执行的步骤,请提及您的自定义方法:是在您的模块中进行继承还是编辑源代码?当你像这样更新代码时,你有没有遇到任何错误?我就是这样尝试的,1)结果={}product\u obj=product\u obj.browse(cr,uid,product,context=context\u partner)结果['discount']=product_obj.x_折扣2)结果={}product_obj=product_obj.browse(cr、uid、product、context=context\u partner.x_折扣结果。更新({'discount':product_obj})对不起,你对我问题的回答似乎无关紧要。你试过我上面提供的脚本了吗?我自己试过,效果很好