Better handling of companies

Fix issue where the location_id was not taken into account on the PO
Now merge with existing draft PO if exists
This commit is contained in:
Alexis de Lattre
2015-05-28 23:15:11 +02:00
parent 681be2ec9e
commit 979c33a457
3 changed files with 105 additions and 38 deletions

View File

@@ -35,6 +35,8 @@ This module is an ALTERNATIVE to the module *procurement_suggest* ; it is simila
The advantage is that you are not impacted by the faulty procurements (for example : a procurement generates a PO ; the PO is confirmed ; the related picking is cancelled and deleted -> the procurements will always stay in running without related stock moves !) The advantage is that you are not impacted by the faulty procurements (for example : a procurement generates a PO ; the PO is confirmed ; the related picking is cancelled and deleted -> the procurements will always stay in running without related stock moves !)
Warning: this module doesn't handle the case where a product uses multiple units of measures for the moment.
This module has been written by Alexis de Lattre from Akretion <alexis.delattre@akretion.com>. This module has been written by Alexis de Lattre from Akretion <alexis.delattre@akretion.com>.
""", """,
'author': 'Akretion', 'author': 'Akretion',

View File

@@ -53,6 +53,7 @@ class PurchaseSuggestionGenerate(models.TransientModel):
# I cannot filter on 'date_order' because it is not a stored field # I cannot filter on 'date_order' because it is not a stored field
porderline_id = porderlines and porderlines[0].id or False porderline_id = porderlines and porderlines[0].id or False
sline = { sline = {
'company_id': qty_dict['orderpoint'].company_id.id,
'product_id': product_id, 'product_id': product_id,
'seller_id': qty_dict['product'].seller_id.id or False, 'seller_id': qty_dict['product'].seller_id.id or False,
'qty_available': qty_dict['qty_available'], 'qty_available': qty_dict['qty_available'],
@@ -168,6 +169,8 @@ class PurchaseSuggest(models.TransientModel):
_description = 'Purchase Suggestions' _description = 'Purchase Suggestions'
_rec_name = 'product_id' _rec_name = 'product_id'
company_id = fields.Many2one(
'res.company', string='Company', required=True)
product_id = fields.Many2one( product_id = fields.Many2one(
'product.product', string='Product', required=True, readonly=True) 'product.product', string='Product', required=True, readonly=True)
seller_id = fields.Many2one( seller_id = fields.Many2one(
@@ -210,68 +213,127 @@ class PurchaseSuggestPoCreate(models.TransientModel):
_name = 'purchase.suggest.po.create' _name = 'purchase.suggest.po.create'
_description = 'PurchaseSuggestPoCreate' _description = 'PurchaseSuggestPoCreate'
@api.model def _prepare_purchase_order(self, partner, company, location):
def _prepare_purchase_order_vals(self, partner, po_lines): poo = self.env['purchase.order']
polo = self.pool['purchase.order.line'] spto = self.env['stock.picking.type']
ponull = self.env['purchase.order'].browse(False) po_vals = {'partner_id': partner.id, 'company_id': company.id}
po_vals = {'partner_id': partner.id} ponull = poo.browse(False)
partner_change_dict = ponull.onchange_partner_id(partner.id) partner_change_dict = ponull.onchange_partner_id(partner.id)
po_vals.update(partner_change_dict['value']) po_vals.update(partner_change_dict['value'])
picking_type_id = self.env['purchase.order']._get_picking_in() pick_type_dom = [
picking_type_dict = ponull.onchange_picking_type_id(picking_type_id) ('code', '=', 'incoming'),
po_vals.update(picking_type_dict['value']) ('warehouse_id.company_id', '=', company.id)]
order_lines = []
for product, qty_to_order in po_lines: pick_types = spto.search(
product_change_res = polo.onchange_product_id( pick_type_dom + [('default_location_dest_id', '=', location.id)])
self._cr, self._uid, [], if not pick_types:
partner.property_product_pricelist_purchase.id, pick_types = spto.search(pick_type_domain)
product.id, qty_to_order, False, partner.id, if not pick_types:
fiscal_position_id=partner.property_account_position.id, raise Warning(_(
context=self.env.context) "Make sure you have at least an incoming picking "
product_change_vals = product_change_res['value'] "type defined"))
taxes_id_vals = [] po_vals['picking_type_id'] = pick_types[0].id
if product_change_vals.get('taxes_id'): pick_type_dict = ponull.onchange_picking_type_id(pick_types.id)
for tax_id in product_change_vals['taxes_id']: po_vals.update(pick_type_dict['value'])
taxes_id_vals.append((4, tax_id)) # I do that at the very end because onchange_picking_type_id()
product_change_vals['taxes_id'] = taxes_id_vals # returns a default location_id
order_lines.append( po_vals['location_id'] = location.id
[0, 0, dict(product_change_vals, product_id=product.id)])
po_vals['order_line'] = order_lines
return po_vals return po_vals
def _prepare_purchase_order_line(self, partner, product, qty_to_order):
polo = self.env['purchase.order.line']
polnull = polo.browse(False)
product_change_res = polnull.onchange_product_id(
partner.property_product_pricelist_purchase.id,
product.id, qty_to_order, False, partner.id,
fiscal_position_id=partner.property_account_position.id)
product_change_vals = product_change_res['value']
taxes_id_vals = []
if product_change_vals.get('taxes_id'):
for tax_id in product_change_vals['taxes_id']:
taxes_id_vals.append((4, tax_id))
product_change_vals['taxes_id'] = taxes_id_vals
vals = dict(product_change_vals, product_id=product.id)
return vals
def _create_update_purchase_order(
self, partner, company, po_lines, location):
polo = self.env['purchase.order.line']
poo = self.env['purchase.order']
existing_pos = poo.search([
('partner_id', '=', partner.id),
('company_id', '=', company.id),
('state', '=', 'draft'),
('location_id', '=', location.id),
])
if existing_pos:
# update the first existing PO
existing_po = existing_pos[0]
for product, qty_to_order in po_lines:
existing_poline = polo.search([
('product_id', '=', product.id),
('order_id', '=', existing_po.id),
])
if existing_poline:
existing_poline[0].product_qty += qty_to_order
else:
pol_vals = self._prepare_purchase_order_line(
partner, product, qty_to_order)
pol_vals['order_id'] = existing_po.id
polo.create(pol_vals)
existing_po.message_post(
_('Purchase order updated from purchase suggestions.'))
return existing_po
else:
# create new PO
po_vals = self._prepare_purchase_order(partner, company, location)
order_lines = []
for product, qty_to_order in po_lines:
pol_vals = self._prepare_purchase_order_line(
partner, product, qty_to_order)
order_lines.append((0, 0, pol_vals))
po_vals['order_line'] = order_lines
new_po = poo.create(po_vals)
return new_po
@api.multi @api.multi
def create_po(self): def create_po(self):
self.ensure_one() self.ensure_one()
# group by supplier # group by supplier
po_to_create = {} # key = seller_id, value = [(product, qty)] po_to_create = {}
# key = (seller, company)
# value = [(product1, qty1), (product2, qty2)]
psuggest_ids = self.env.context.get('active_ids') psuggest_ids = self.env.context.get('active_ids')
location = False
for line in self.env['purchase.suggest'].browse(psuggest_ids): for line in self.env['purchase.suggest'].browse(psuggest_ids):
if not location:
location = line.orderpoint_id.location_id
if not line.qty_to_order: if not line.qty_to_order:
continue continue
if not line.product_id.seller_id: if not line.product_id.seller_id:
raise Warning(_( raise Warning(_(
"No supplier configured for product '%s'.") "No supplier configured for product '%s'.")
% line.product_id.name) % line.product_id.name)
if line.seller_id in po_to_create: if (line.seller_id, line.company_id) in po_to_create:
po_to_create[line.seller_id].append( po_to_create[(line.seller_id, line.company_id)].append(
(line.product_id, line.qty_to_order)) (line.product_id, line.qty_to_order))
else: else:
po_to_create[line.seller_id] = [ po_to_create[(line.seller_id, line.company_id)] = [
(line.product_id, line.qty_to_order)] (line.product_id, line.qty_to_order)]
new_po_ids = [] if not po_to_create:
for seller, po_lines in po_to_create.iteritems(): raise Warning(_('No purchase orders created or updated'))
po_vals = self._prepare_purchase_order_vals( po_ids = []
seller, po_lines) for (seller, company), po_lines in po_to_create.iteritems():
new_po = self.env['purchase.order'].create(po_vals) assert location, 'No stock location'
new_po_ids.append(new_po.id) po = self._create_update_purchase_order(
seller, company, po_lines, location)
po_ids.append(po.id)
if not new_po_ids:
raise Warning(_('No purchase orders created'))
action = self.env['ir.actions.act_window'].for_xml_id( action = self.env['ir.actions.act_window'].for_xml_id(
'purchase', 'purchase_rfq') 'purchase', 'purchase_rfq')
action.update({ action.update({
'nodestroy': False, 'nodestroy': False,
'target': 'current', 'target': 'current',
'domain': [('id', 'in', new_po_ids)], 'domain': [('id', 'in', po_ids)],
}) })
return action return action

View File

@@ -96,6 +96,9 @@
<form string="Create Purchase Orders"> <form string="Create Purchase Orders">
<p class="oe_grey"> <p class="oe_grey">
Click on the red button below to create the purchase orders. Click on the red button below to create the purchase orders.
If a draft purchase order already exists for one of the suppliers
for the same stock location, it will be updated with the new
products and quantities to order.
</p> </p>
<footer> <footer>
<button type="object" name="create_po" <button type="object" name="create_po"