简体   繁体   中英

get product price from order line in odoo?

I've installed a custom module from Odoo store which automatically creates a credit note for returned products but this module gets the price from the product form which may not include the price on orders.

I need to change the value of price on each line to be calculated from the order line even if it is on sale or purchased.

This is the create method:

@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
            pricelist_id = pick.partner_id.property_product_pricelist
            price = pricelist_id.get_product_price(line.product_id, 1, pick.partner_id) if pricelist_id else line.product_id.lst_price


            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

What should I do ? I tried to set the

 price = line.env['purchase.order'].search([('name','=',line.group_id)]).order_line.price_unit

but it makes the price = 0

Any help will be appreciated.

i make it but it get only one record , if there is more than one record i get error

 raise ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: purchase.order.line(11362, 11363)

this the new method

@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
            order_id = line.env['purchase.order'].search([('name', '=', line.origin)]).order_line
            price = 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

You must need to pass 0,0 while creating value of One2Many field.

Replace below code with your code. It may resolve your problem.

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
        line_vals = []
        for line in pick.move_lines:
            name = line.product_id.partner_ref
            pricelist_id = pick.partner_id.property_product_pricelist
            price = pricelist_id.get_product_price(line.product_id, 1, pick.partner_id) if pricelist_id else line.product_id.lst_price
            inv_id = inv_obj.search([('origin', '=', pick.name)], limit=1)
            if not inv_id:
                inv_id = inv_obj.create(inv_lines)
            line_vals.append({
                        '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,
                        'invoice_id': inv_id.id
                        })
        inv_id.invoice_line_ids.create(line_vals)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM