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 autofill sale.order.line from linked purchase.order(.line)?

By
Christian Spitzl
on 12/10/14, 3:38 AM 1,552 views

Hi, I've created two modules.
The first one "Sale Quotation 2 Purchase Quotation" links a Sale Order with a Purchase Order and autofills the PO Lines with the values of the linked SO Lines.
So far, so good.
The second one "Purchase Order 2 Sale Order" gives me a lot of trouble. 
It should do nearly the same like the first one, only in the other direction. It should autofill the SO Lines with the values of the linked PO Lines.
The problem is the "product_id_change" method. It checks whether a customer is defined before you can chose a product.
How can I solve it and get it work?

Many thanks for an adivce.

 


FIRST:
Sale Quotation 2 Purchase Quotation

 

<code></p>

class purchase_order(osv.osv):

    _inherit = 'purchase.order'
    _name = 'purchase.order'
    
    STATE_SELECTION = [
        ('draft', 'Draft PO'),
        ('sent', 'RFQ Sent'),
        ('received', 'RFQ Received'),
        ('confirmed', 'Waiting Approval'),
        ('approved', 'Purchase Order'),
        ('except_picking', 'Shipping Exception'),
        ('except_invoice', 'Invoice Exception'),
        ('done', 'Done'),
        ('cancel', 'Cancelled')
    ]
    
    _columns = {
       'state': fields.selection(STATE_SELECTION, 'Status', readonly=True, select=True),
       'purchase_quotation_id': fields.many2one('sale.order', 'Sale Quotation', domain=[('state','in',('draft','sent'))]),
    }
    _defaults = {
        'state': 'draft',
        'purchase_quotation_id': lambda self, cr, uid, context: context.get('purchase_quotation_id', False),
    }

    def onchange_purchase_order(self, cr, uid, id, purchase_quotation_id, partner_id, pricelist_id):
        domain = []
        value = []
        if purchase_quotation_id:
            sale = self.pool.get('sale.order').browse(cr,uid,purchase_quotation_id)
            for line in sale.order_line:
                vals = self.pool.get('purchase.order.line').onchange_product_id(cr, uid, id, pricelist_id, line.product_id.id, line.product_uom_qty, line.product_uom.id, partner_id)
                vals['value'].update({
                    'product_id': line.product_id.id, 
                    'state':'draft', 
                    'move_ids':[], 
                    'invoiced':0,
                    'invoice_lines':[],
                })
            value.append(vals['value'])
            domain.append(vals['domain'])
        return {'value': {'order_line': value}, 'domain': {'order_line': domain}}    

 

class sale_order(osv.osv):
    _inherit = "sale.order"
    _name = "sale.order"

    _columns = {
        'sale_quotation': fields.one2many('purchase.order', 'purchase_quotation_id', 'Purchase Quotation'),
    }
    
sale_order()

</code></p>

 

SECOND:

Purchase Quotation 2 Sale Quotation

<code><p>

class sale_order(osv.osv):

    _inherit = 'sale.order'
    _name = 'sale.order'
    
    STATE_SELECTION = [
        ('draft', 'Draft Quotation'),
        ('sent', 'Quotation Sent'),
        ('cancel', 'Cancelled'),
        ('waiting_date', 'Waiting Schedule'),
        ('progress', 'Sale Order'),
        ('manual', 'Sale to Invoice'),
        ('invoice_except', 'Invoice Exception'),
        ('done', 'Done'),
    ]
    
    _columns = {
       'state': fields.selection(STATE_SELECTION, 'Status', readonly=True, select=True),
       'sale_quotation_id': fields.many2one('purchase.order', 'Purchase Quotation', domain=[('state','in',('draft','sent'))]),
    }
    _defaults = {
        'state': 'draft',
        'sale_quotation_id': lambda self, cr, uid, context: context.get('sale_quotation_id', False),
    }

    def onchange_sale_order(self, cr, uid, id, sale_quotation_id, partner_id, pricelist_id):
        domain = []
        value = []
        if sale_quotation_id:
            purchase = self.pool.get('purchase.order').browse(cr,uid,sale_quotation_id)
            for line in purchase.order_line:
>>>??            vals = self.pool.get('sale.order.line').product_id_change(cr, uid, id, pricelist_id, line.product_id.id, line.product_qty, line.product_uom.id, partner_id)
                vals['value'].update({
                    'product_id': line.product_id.id, 
                    'state':'draft', 
                    'move_ids':[], 
                    'invoiced':0,
                    'invoice_lines':[],
                })
            value.append(vals['value'])
            domain.append(vals['domain'])
        return {'value': {'order_line': value}, 'domain': {'order_line': domain}}    

 

class purchase_order(osv.osv):
    _inherit = "purchase.order"
    _name = "purchase.order"

    _columns = {
        'purchase_quotation': fields.one2many('sale.order', 'sale_quotation_id', 'Sale Quotation'),
    }
    
purchase_order()

</code></p>

0

Ludo - Neobis

--Ludo - Neobis--
2387
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--

Developer

Ludo - Neobis
On 12/10/14, 4:37 AM

Do not be offended by my remarks, but I was wondering why you went through all this effort instead of using Odoo regular procurement system for the previous work (from sale to purchase)? Is it because you did not want to confirm the sale before creating the purchase?

Other than that, I think its much simpler that the sketched situation. Create a wizard that takes the current sale order (usually in context under "active id") and use it to read the order line for line (assuming you already know where the purchase order goes), sending the product_id and qty to the on_change method for purchase order line. If configured correctly. You should first create a purchase order, but since that can practically be empty, you should have no trouble creating an order line for the PO based on its id.

Alternatively, you can check the system of procurements (procurement.order in Odoo) and write a method on that. Procurements already have links to both sales as well as purchase orders. Writing a simple method that converts it to purchase orders should do the trick. Then all you have to do is make sure the SO generates a procurement at a point you see fit.

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: 12/10/14, 3:38 AM
Seen: 1552 times
Last updated: 3/16/15, 8:10 AM