Skip to Content
Menu
This question has been flagged
1 Reply
4798 Views


this methode to get the product price from the PO , and it works well if the PO have only one record , else i get this error

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

this is the methode

@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

any help will be appreciated


Avatar
Discard
Best Answer

Hi,

I will explain you reason for getting the singleton error in Odoo, so that you can go through the code and update it accordingly, suppose if you have a variable/object named products having value as product.product(2,3) , and if you give a code like products.list_price , you will get the singleton error as you have multiple records there, so to solve this you have to use for loop like below,

for pro in products:
x = pro.list_price


The same will not throw error, if there is only single record in products.

Thanks

Avatar
Discard
Author

Thanks for your help, I know that the loop will fix the error but I don't know how to use it here

Hope you have created the code, so you will have better understanding of the code, so just add some print statements inside the code and see in which variable/objects you are receiving the multiple records. Once you find it, you can iterate it right ? If not tell which is the variable causing the issue

Related Posts Replies Views Activity
0
Dec 24
43
2
Oct 24
217
1
Aug 24
577
2
Jul 24
267
1
Jul 24
289