I don't have a module but here is the code we use to do this:
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011- Solnet Solutions (<http://www.solnetsolutions.co.nz>).
# Copyright (C) 2010 OpenERP S.A.
http://www.openerp.com# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from datetime import datetime
from dateutil.relativedelta import relativedelta
from osv import osv
class PurchaseQuoteMergeProcurement(osv.osv):
""" Used to over-ride the procurement order make purchase order to add to an existing PO if possible. """
_inherit = "procurement.order"
def make_po(self, cr, uid, ids, context=None):
""" Make purchase order from procurement
This is a copy-paste over-ride of the method from purchasing.
The key difference is this will look for an existing PO and if found,
add the line to it.
@return: New created Purchase Orders procurement wise
"""
res = {}
if context is None:
context = {}
company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
partner_obj = self.pool.get('res.partner')
uom_obj = self.pool.get('product.uom')
pricelist_obj = self.pool.get('product.pricelist')
prod_obj = self.pool.get('product.product')
acc_pos_obj = self.pool.get('account.fiscal.position')
po_obj = self.pool.get('purchase.order')
po_line_obj = self.pool.get('purchase.order.line')
for procurement in self.browse(cr, uid, ids, context=context):
res_id = procurement.move_id.id
partner = procurement.product_id.seller_id # Taken Main Supplier of Product of Procurement.
seller_qty = procurement.product_id.seller_qty
seller_delay = int(procurement.product_id.seller_delay)
partner_id = partner.id
address_id = partner_obj.address_get(cr, uid, [partner_id], ['delivery'])['delivery']
pricelist_id = partner.property_product_pricelist_purchase.id
uom_id = procurement.product_id.uom_po_id.id
qty = uom_obj._compute_qty(cr, uid, procurement.product_uom.id, procurement.product_qty, uom_id)
if seller_qty:
qty = max(qty,seller_qty)
price = pricelist_obj.price_get(cr, uid, [pricelist_id], procurement.product_id.id, qty, partner_id, {'uom': uom_id})[pricelist_id]
newdate = datetime.strptime(procurement.date_planned, '%Y-%m-%d %H:%M:%S')
newdate = (newdate - relativedelta(days=company.po_lead)) - relativedelta(days=seller_delay)
#Passing partner_id to context for purchase order line integrity of Line name
context.update({'lang': partner.lang, 'partner_id': partner_id})
product = prod_obj.browse(cr, uid, procurement.product_id.id, context=context)
line = {
'name': product.partner_ref,
'product_qty': qty,
'product_id': procurement.product_id.id,
'product_uom': uom_id,
'price_unit': price,
'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
'move_dest_id': res_id,
'notes': product.description_purchase,
}
taxes_ids = procurement.product_id.product_tmpl_id.supplier_taxes_id
taxes = acc_pos_obj.map_tax(cr, uid, partner.property_account_position, taxes_ids)
line.update({
'taxes_id': [(6,0,taxes)]
})
# Code from Graeme Gellatley
# Search for an existing matching PO and add the line to it if found, otherwise create a new PO.
# Changed matching PO search.
user_class = self.pool.get('res.users')
user_company_id = user_class.browse(cr, uid, uid, context = context).company_id.id
po_exists = po_obj.search(cr, uid, [('company_id','=', user_company_id),
('partner_id', '=', partner_id),
('state', '=', 'draft')])
if po_exists:
purchase_id = po_exists[0]
else:
purchase_id = po_obj.create(cr, uid, {
'origin': procurement.origin,
'partner_id': partner_id,
'partner_address_id': address_id,
'location_id': procurement.location_id.id,
'pricelist_id': pricelist_id,
'company_id': procurement.company_id.id,
'fiscal_position': partner.property_account_position and partner.property_account_position.id or False
})
line.update({'order_id': purchase_id})
purchase_line_id = po_line_obj.create(cr, uid, line)
res[procurement.id] = purchase_id
self.write(cr, uid, [procurement.id], {'state': 'running', 'purchase_id': purchase_id, 'purchase_line_id': purchase_line_id})
return res
PurchaseQuoteMergeProcurement()