Odoo Help

Welcome!

This community 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.

0

How to auto generate purchase order from manufacturing in odoo8?

By
bhanukiran
on 7/13/16, 2:10 AM 274 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'

    @api.multi
    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:
                    continue

                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,
                                                date_planned=line.date_planned)['value']

                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
                purchase_line_obj.create(line_vals)

        return True

0
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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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