如何从OpenERP获取发票中的销售订单字段值

时间:2014-01-22 09:58:56

标签: python openerp

我在销售订单中创建了两个自定义字段,但这些字段未显示在发票表单中。我还在发票表单视图和列中添加并更新服务器,但此值未显示在发票中。 如何在发票表单中显示这些值。这是我的视图文件:

<record id="view_invoice_line_form" model="ir.ui.view">
        <field name="name">account.invoice.line.form</field>
        <field name="model">account.invoice.line</field>
        <field name="arch" type="xml">
            <form string="Invoice Line" version="7.0">
                <group>
                    <group>
                        <field name="product_id" on_change="product_id_change(product_id, uos_id, quantity, name, parent.type, parent.partner_id, parent.fiscal_position, price_unit, parent.currency_id, context, parent.company_id)"/>
                        <label for="quantity"/>
                        <div>
                            <field name="quantity" class="oe_inline"/>
                            <field name="uos_id" class="oe_inline" groups="product.group_uom"
                              on_change="uos_id_change(product_id, uos_id, quantity, name, parent.type, parent.partner_id, parent.fiscal_position, price_unit, parent.currency_id, context, parent.company_id)"/>
                        </div>
                        <field name="purchase_order"/>
                        <field name="amt_total"/> 
                        <field name="price_unit"/>
                        <field name="discount" groups="sale.group_discount_per_so_line"/>
                    </group>
                    <group>
                        <field domain="[('company_id', '=', parent.company_id), ('journal_id', '=', parent.journal_id), ('type', '&lt;&gt;', 'view')]" name="account_id" on_change="onchange_account_id(product_id, parent.partner_id, parent.type, parent.fiscal_position,account_id)" groups="account.group_account_user"/>
                        <field name="invoice_line_tax_id" context="{'type':parent.type}" domain="[('parent_id','=',False),('company_id', '=', parent.company_id)]" widget="many2many_tags"/>
                        <field domain="[('type','&lt;&gt;','view'), ('company_id', '=', parent.company_id)]" name="account_analytic_id" groups="analytic.group_analytic_accounting"/>
                        <field name="company_id" groups="base.group_multi_company" readonly="1"/>
                    </group>
                </group>
                <label for="name"/>
                <field name="name"/>
            </form>
        </field>
    </record>

在.py文件中添加了两个字段:

'purchase_order':fields.many2one('purchase.order', 'Old Gold', ondelete="cascade"),
'amt_total':fields.related('purchase_order', 'amount_total', type='float', relation='purchase.order', string="Amount", readonly=True),

我创建了函数

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
    cur_obj = self.pool.get('res.currency')
    res = {}

    for order in self.browse(cr, uid, ids, context=context):


        res[order.id] = {
            'amount_untaxed': 0.0,
            'amount_tax': 0.0,
            'amount_total': 0.0,
            'amt_total':0.0
        }
        val = val1 =  0.0
        cur = order.pricelist_id.currency_id
        for line in order.order_line:
            val1 += line.price_subtotal

            val += self._amount_line_tax(cr, uid, line, context=context)
        res[order.id]['amount_tax'] = cur_obj.round(cr, uid, cur, val)
        res[order.id]['amount_untaxed'] = cur_obj.round(cr, uid, cur, val1)

        res[order.id]['amount_untaxed'] -= order.amt_total
        res[order.id]['amount_total'] = res[order.id]['amount_untaxed'] + res[order.id]['amount_tax']
    return res

此处我可以从amt_total中减去total_amount,但在发票中我无法从amt_total中减去total_amount

当我在发票表单中执行相同的操作时,它会给我一个错误:

ProgrammingError: column account_invoice_line.amt_total does not exist
LINE 1: ...ne."partner_id",account_invoice_line."product_id",account_in...

3 个答案:

答案 0 :(得分:2)

正如@archetipo所说,最好的方法是创建一个新的自定义模块。

以您的解决方案为例,试试这个,

  • 创建新模块并创建 account_invoice_line 对象

    class account_invoice_line(osv.Model):
        _inherit = 'account.invoice.line'
        _columns = {
            'purchase_order': fields.char('Purchase Order'),
            'amt_total': fields.float('Amout Total'),
        }
    

您应该根据自己的要求提供字段类型。现在在视图方面,例如在产品描述之后放在上面的字段中。

<record id="view_account_order_extend1_form" model="ir.ui.view">
    <field name="name">account.order.form.price.extend</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="account.invoice_form" />
    <field name="arch" type="xml">
        <xpath expr="//page[@string='Invoice Lines']/field[@name='invoice_line']/tree[@string='Invoice Lines']/field[@name='name']" position="after">
            <field name="purchase_order"/>
            <field name="amt_total"/>
        </xpath>
    </field>
</record>

希望这有助于您理解。

答案 1 :(得分:0)

您好有2种方法可以在视图中添加字段:

1)直接修改原始模块,非常糟糕的方式

2)创建自己的自定义模块,其中包含您继承的模块的新声明,即原始模块,即

class account_invoice_line(orm.Model)   #osv.osv is deprecated
    _inherit='account.invoice.line'
    _column={ your new field here...}   

视图继承建议使用此链接Openerp Book

所以,如果您使用了第一个选项,请删除所有并创建自己的自定义模块,因为在下次更新openerp时您可能会丢失工作

答案 2 :(得分:0)

在sale.order中使用此功能

def _prepare_invoice(self, cr, uid, order, lines, context=None):
    """Prepare the dict of values to create the new invoice for a
       sales order. This method may be overridden to implement custom
       invoice generation (making sure to call super() to establish
       a clean extension chain).

       :param browse_record order: sale.order record to invoice
       :param list(int) line: list of invoice line IDs that must be
                              attached to the invoice
       :return: dict of value to create() the invoice
    """
    if context is None:
        context = {}
    journal_ids = self.pool.get('account.journal').search(cr, uid,
        [('type', '=', 'sale'), ('company_id', '=', order.company_id.id)],
        limit=1)
    if not journal_ids:
        raise osv.except_osv(_('Error!'),
            _('Please define sales journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
    invoice_vals = {
        'name': order.client_order_ref or '',
        'origin': order.name,
        'type': 'out_invoice',
        'reference': order.client_order_ref or order.name,
        'account_id': order.partner_id.property_account_receivable.id,
        'partner_id': order.partner_invoice_id.id,
        'journal_id': journal_ids[0],
        'invoice_line': [(6, 0, lines)],
        'currency_id': order.pricelist_id.currency_id.id,
        'comment': order.note,

        'amt_total':order.amt_total,
        'qty_related':order.qty_related,

        'payment_term': order.payment_term and order.payment_term.id or False,
        'fiscal_position': order.fiscal_position.id or order.partner_id.property_account_position.id,
        'date_invoice': context.get('date_invoice', False),
        'company_id': order.company_id.id,
        'user_id': order.user_id and order.user_id.id or False,


    }

    # Care for deprecated _inv_get() hook - FIXME: to be removed after 6.1
    invoice_vals.update(self._inv_get(cr, uid, order, context=context))
    return invoice_vals

并以发票形式创建字段