Odoo Help


How to auto generate purchase order from manufacturing in odoo8?

on 7/13/16, 2:10 AM 897 views

I want to generate a purchase order invoice from the information obtained from mrp. I want to get products from bill of materials(raw materials),price from product's cost price.Right now I have created a button for generating purchase order,it is only copying mrp invoice no and date created it is not entering the inside for loop and also I am not getting how to retrieve the current mrp id,I have hardcoded the id itself.This method is called when the button is pressed.

from openerp import models,api
class mrp_production(models.Model):
    _inherit = 'mrp.production'

    def generate_purchase_order(self,supplier_id,pricelist_id,warehouse_id):
        purchase_obj = self.env['purchase.order']
        purchase_line_obj = self.env['purchase.order.line']
        warehouse_obj = self.env['stock.warehouse']

        warehouse = warehouse_obj.browse(warehouse_id)
        if not warehouse:
            return False
        if isinstance(warehouse, list):
            warehouse = warehouse[0]

        for order in self.browse(1):#how to remove this hardcoded id?
            vals = {}
            vals = purchase_obj.onchange_partner_id(supplier_id)
            vals['origin'] = order.name
            vals['partner_id'] = supplier_id
            vals['pricelist_id'] = pricelist_id
            vals['warehouse_id'] = warehouse_id
            vals['location_id'] = warehouse.lot_stock_id.id
            vals['date_order'] = order.date_planned
            purchase_id = purchase_obj.create(vals)

            for line in self.bom_id.bom_line_ids:
            #this for loop is not getting executed
                if not line.product_id:

                line_vals = purchase_line_obj.onchange_product_id(self,pricelist_id,line.product_id.id,
                                                line.product_uom_qty, line.product_uom.id, supplier_id,

                line_vals['product_id'] = line.product_id.id
                if not line_vals.get('price_unit', False):
                    line_vals['price_unit'] = line.product_id.list_price
                line_vals['product_uom'] = line.product_uom.id
                line_vals['product_uom_qty'] = 181.13
                line_vals['order_id'] = purchase_id

        return True

Nikunj Antala
On 7/13/16, 3:06 AM

Hello Bhanukiran,

As per new api we can access its fields and methods from directly using self because self it self is browsable record.

Example :

class account_invoice(models.Model):

    _name = "account.invoice"

    def test_function(self):

    for invoice in self:

        print invoice.number # It will print Number of all invoices

Hope this help.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/13/16, 2:10 AM
Seen: 897 times
Last updated: 7/13/16, 3:06 AM