0

这种从采购订单获取产品价格的方法,如果采购订单只有一条记录,则效果很好,否则我会收到此错误。

raise ValueError("Expected singleton: %s" % self)

这是方法

@api.multi
def create_refund_invoice(self):
    inv_obj = self.env['account.invoice']
    for pick in self.filtered(lambda x:x.return_type):
        type = 'in_refund' if pick.return_type == 'purchase' else 'out_refund'
        inv_lines = {'type':type, 'partner_id':pick.partner_id.id, 'invoice_line_ids':[]}
        account = pick.return_type == 'sale' and pick.partner_id.property_account_receivable_id.id or pick.partner_id.property_account_payable_id.id
        inv_lines['account_id'] = account
        inv_lines['origin'] = pick.name
        inv_lines['name'] = pick.origin
        for line in pick.move_lines:
            name = line.product_id.partner_ref
            for rec in self:

                rec.order_id = line.env['purchase.order'].search([('name', '=', line.origin)]).order_line
                rec.price = rec.order_id.price_unit
            inv_lines['invoice_line_ids'] += [(0, None, {
                'product_id':line.product_id.id,
                'name':name,
                'quantity':line.quantity_done,
                'price_unit': rec.price,
                'account_id':line.product_id.product_tmpl_id.get_product_accounts()['income'].id})]
        if inv_lines['invoice_line_ids']:
            inv_id = inv_obj.create(inv_lines)
            pick.invoice_id = inv_id.id

任何帮助将不胜感激。

4

1 回答 1

0

odoo 有必要在获得多条记录时不能直接访问它的字段值。

在您尝试获取purchase_order_line的代码中purchase_order,可能有许多行可以在一个订单中使用。

def create_refund_invoice(self):
    purchase_order_obj = self.env['purchase.order']
    inv_obj = self.env['account.invoice']
    for pick in self.filtered(lambda x:x.return_type):
        type = 'in_refund' if pick.return_type == 'purchase' else 'out_refund'
        inv_lines = {'type':type, 'partner_id':pick.partner_id.id, 'invoice_line_ids':[]}
        account = pick.return_type == 'sale' and pick.partner_id.property_account_receivable_id.id or pick.partner_id.property_account_payable_id.id
        inv_lines['account_id'] = account
        inv_lines['origin'] = pick.name
        inv_lines['name'] = pick.origin
        for line in pick.move_lines:
            name = line.product_id.partner_ref
            for rec in self:
                order_lines = purchase_order_obj.search([('name', '=', line.origin)]).order_line
                for pol in order_lines:
                    price = pol.order_id.price_unit
                    inv_lines['invoice_line_ids'] += [(0, None, {
                                            'product_id':line.product_id.id,
                                            'name':name,
                                            'quantity':line.quantity_done,
                                            'price_unit': price,
                                            'account_id':line.product_id.product_tmpl_id.get_product_accounts()['income'].id})
                                            ]
        if inv_lines['invoice_line_ids']:
            inv_id = inv_obj.create(inv_lines)
            pick.invoice_id = inv_id.id

我已经更新了代码上面的代码测试,并根据您的要求进行了更新。

于 2019-12-19T11:05:30.547 回答