Split procurement_suggest in 2 modules : procurement_suggest + procurement_suggest_purchase

This commit is contained in:
Alexis de Lattre
2015-05-27 11:02:07 +02:00
parent 604dab1bb9
commit de76a64f7e
8 changed files with 199 additions and 91 deletions

View File

@@ -33,14 +33,11 @@ Procurement Suggest
TODO TODO
Roadmap : split the module in 2 and move the purchase-specific part in a module
that has a dependancy on this module and purchase. This module will then not have a depandancy on purchase any more.
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',
'website': 'http://www.akretion.com', 'website': 'http://www.akretion.com',
'depends': ['stock', 'purchase'], 'depends': ['stock'],
'data': [ 'data': [
'stock_view.xml', 'stock_view.xml',
'wizard/procurement_suggest_view.xml', 'wizard/procurement_suggest_view.xml',

View File

@@ -40,14 +40,6 @@ class ProcurementSuggestionGenerate(models.TransientModel):
@api.model @api.model
def _prepare_suggest_line(self, orderpoint): def _prepare_suggest_line(self, orderpoint):
porderline_id = False
if orderpoint.product_id.seller_id:
porderlines = self.env['purchase.order.line'].search([
('state', 'not in', ('draft', 'cancel')),
('product_id', '=', orderpoint.product_id.id)],
order='id desc', limit=1)
# I cannot filter on 'date_order' because it is not a stored field
porderline_id = porderlines and porderlines[0].id or False
sline = { sline = {
'product_id': orderpoint.product_id.id, 'product_id': orderpoint.product_id.id,
'seller_id': orderpoint.product_id.seller_id.id or False, 'seller_id': orderpoint.product_id.seller_id.id or False,
@@ -55,7 +47,6 @@ class ProcurementSuggestionGenerate(models.TransientModel):
'incoming_qty': orderpoint.product_id.incoming_qty, 'incoming_qty': orderpoint.product_id.incoming_qty,
'outgoing_qty': orderpoint.product_id.outgoing_qty, 'outgoing_qty': orderpoint.product_id.outgoing_qty,
'orderpoint_id': orderpoint.id, 'orderpoint_id': orderpoint.id,
'last_po_line_id': porderline_id,
} }
return sline return sline
@@ -82,6 +73,7 @@ class ProcurementSuggestionGenerate(models.TransientModel):
p_suggest_lines = [] p_suggest_lines = []
lines = {} # key = product_id ; value = {'min_qty', ...} lines = {} # key = product_id ; value = {'min_qty', ...}
for op in ops: for op in ops:
# TODO : take into account the running procurements ?
if op.product_id.virtual_available < op.product_min_qty: if op.product_id.virtual_available < op.product_min_qty:
if op.product_id.id in lines: if op.product_id.id in lines:
raise Warning( raise Warning(
@@ -130,89 +122,55 @@ class ProcurementSuggest(models.TransientModel):
outgoing_qty = fields.Float( outgoing_qty = fields.Float(
string='Outgoing Quantity', readonly=True, string='Outgoing Quantity', readonly=True,
digits=dp.get_precision('Product Unit of Measure')) digits=dp.get_precision('Product Unit of Measure'))
last_po_line_id = fields.Many2one(
'purchase.order.line', string='Last Purchase Order Line',
readonly=True)
last_po_date = fields.Datetime(
related='last_po_line_id.order_id.date_order',
string='Date of the last PO', readonly=True)
last_po_qty = fields.Float(
related='last_po_line_id.product_qty', readonly=True,
string='Quantity of the Last Order')
orderpoint_id = fields.Many2one( orderpoint_id = fields.Many2one(
'stock.warehouse.orderpoint', string='Re-ordering Rule', 'stock.warehouse.orderpoint', string='Re-ordering Rule',
readonly=True) readonly=True)
uom_id = fields.Many2one(
'product.uom', related='product_id.uom_id', readonly=True)
# on orderpoids, uom_id is a related field of product_id.uom_id
# so I do the same here
min_qty = fields.Float( min_qty = fields.Float(
string="Min Quantity", readonly=True, string="Min Quantity", readonly=True,
related='orderpoint_id.product_min_qty', related='orderpoint_id.product_min_qty',
digits=dp.get_precision('Product Unit of Measure')) digits=dp.get_precision('Product Unit of Measure'))
qty_to_order = fields.Float( procurement_qty = fields.Float(
string='Quantity to Order', string='Procurement Quantity',
digits=dp.get_precision('Product Unit of Measure')) digits=dp.get_precision('Product Unit of Measure'))
class ProcurementSuggestPoCreate(models.TransientModel): class ProcurementCreateFromSuggest(models.TransientModel):
_name = 'procurement.suggest.po.create' _name = 'procurement.create.from.suggest'
_description = 'ProcurementSuggestPoCreate' _description = 'Create Procurements from Procurement Suggestions'
@api.model @api.model
def _prepare_purchase_order_vals(self, partner, po_lines): def _prepare_procurement_order(self, proc_suggest):
polo = self.pool['purchase.order.line'] proc_vals = {
ponull = self.env['purchase.order'].browse(False) 'name': u'INT: ' + unicode(self.env.user.login),
po_vals = {'partner_id': partner.id} 'product_id': proc_suggest.product_id.id,
partner_change_dict = ponull.onchange_partner_id(partner.id) 'product_qty': proc_suggest.procurement_qty,
po_vals.update(partner_change_dict['value']) 'product_uom': proc_suggest.uom_id.id,
picking_type_id = self.env['purchase.order']._get_picking_in() 'location_id': proc_suggest.orderpoint_id.location_id.id,
picking_type_dict = ponull.onchange_picking_type_id(picking_type_id) 'company_id': proc_suggest.orderpoint_id.company_id.id,
po_vals.update(picking_type_dict['value']) 'origin': _('Procurement Suggest'),
order_lines = [] }
for product, qty_to_order in po_lines: return proc_vals
product_change_res = polo.onchange_product_id(
self._cr, self._uid, [],
partner.property_product_pricelist_purchase.id,
product.id, qty_to_order, False, partner.id,
fiscal_position_id=partner.property_account_position.id,
context=self.env.context)
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
order_lines.append(
[0, 0, dict(product_change_vals, product_id=product.id)])
po_vals['order_line'] = order_lines
return po_vals
@api.multi @api.multi
def create_po(self): def create_proc(self):
self.ensure_one() self.ensure_one()
# group by supplier
po_to_create = {} # key = seller_id, value = [(product, qty)]
psuggest_ids = self.env.context.get('active_ids') psuggest_ids = self.env.context.get('active_ids')
poo = self.env['procurement.order']
new_procs = poo.browse(False)
for line in self.env['procurement.suggest'].browse(psuggest_ids): for line in self.env['procurement.suggest'].browse(psuggest_ids):
if not line.qty_to_order: if line.procurement_qty:
continue vals = self._prepare_procurement_order(line)
if line.seller_id in po_to_create: new_procs += poo.create(vals)
po_to_create[line.seller_id].append( if new_procs:
(line.product_id, line.qty_to_order)) new_procs.signal_workflow('button_confirm')
else: new_procs.run()
po_to_create[line.seller_id] = [ else:
(line.product_id, line.qty_to_order)] raise Warning(_('All requested quantities are null.'))
new_po_ids = []
for seller, po_lines in po_to_create.iteritems():
po_vals = self._prepare_purchase_order_vals(
seller, po_lines)
new_po = self.env['purchase.order'].create(po_vals)
new_po_ids.append(new_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') 'procurement', 'procurement_action')
action.update({ action['domain'] = [('id', 'in', new_procs.ids)]
'nodestroy': False,
'target': 'current',
'domain': [('id', 'in', new_po_ids)],
})
return action return action

View File

@@ -50,9 +50,8 @@
<field name="incoming_qty"/> <field name="incoming_qty"/>
<field name="outgoing_qty"/> <field name="outgoing_qty"/>
<field name="min_qty"/> <field name="min_qty"/>
<field name="last_po_date" widget="date"/> <field name="uom_id"/>
<field name="last_po_qty"/> <field name="procurement_qty"/>
<field name="qty_to_order"/>
</tree> </tree>
</field> </field>
</record> </record>
@@ -77,29 +76,30 @@
<field name="res_model">procurement.suggest</field> <field name="res_model">procurement.suggest</field>
<field name="view_mode">tree</field> <field name="view_mode">tree</field>
<field name="target">new</field> <field name="target">new</field>
<field name="limit">500</field>
</record> </record>
<record id="procurement_suggest_po_create_form" model="ir.ui.view"> <record id="procurement_create_from_suggest_form" model="ir.ui.view">
<field name="name">procurement_suggest_po_create.form</field> <field name="name">procurement_create_from_suggest.form</field>
<field name="model">procurement.suggest.po.create</field> <field name="model">procurement.create.from.suggest</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Create Purchase Orders"> <form string="Create Purchase Orders">
<p class="oe_grey"> <p class="oe_grey">
This wizard will create Purchase Orders. This wizard will create procurements and confirm them, so that it creates purchase orders or manufacturing orders.
</p> </p>
<footer> <footer>
<button type="object" name="create_po" string="Create POs" class="oe_highlight"/> <button type="object" name="create_proc" string="Create Procurements" class="oe_highlight"/>
<button special="cancel" string="Cancel" class="oe_link"/> <button special="cancel" string="Cancel" class="oe_link"/>
</footer> </footer>
</form> </form>
</field> </field>
</record> </record>
<act_window id="procurement_suggest_po_create_action" <act_window id="procurement_create_from_suggest_action"
multi="True" multi="True"
key2="client_action_multi" key2="client_action_multi"
name="Create Purchase Orders" name="Create Procurements"
res_model="procurement.suggest.po.create" res_model="procurement.create.from.suggest"
src_model="procurement.suggest" src_model="procurement.suggest"
view_mode="form" view_mode="form"
target="new" /> target="new" />

View File

@@ -0,0 +1,3 @@
# -*- encoding: utf-8 -*-
from . import wizard

View File

@@ -0,0 +1,45 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Procurement Suggest Purchase module for Odoo
# Copyright (C) 2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': 'Procurement Suggest Purchase',
'version': '0.1',
'category': 'Procurements',
'license': 'AGPL-3',
'summary': 'Usability enhancements to procurement suggest for purchase',
'description': """
Procurement Suggest Purchase
============================
TODO
This module has been written by Alexis de Lattre from Akretion <alexis.delattre@akretion.com>.
""",
'author': 'Akretion',
'website': 'http://www.akretion.com',
'depends': ['purchase', 'procurement_suggest'],
'data': [
'wizard/procurement_suggest_view.xml',
],
'installable': True,
}

View File

@@ -0,0 +1,3 @@
# -*- encoding: utf-8 -*-
from . import procurement_suggest

View File

@@ -0,0 +1,75 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Procurement Suggest Purchase module for Odoo
# Copyright (C) 2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields, api
class ProcurementSuggestionGenerate(models.TransientModel):
_inherit = 'procurement.suggest.generate'
@api.model
def _prepare_suggest_line(self, orderpoint):
sline = super(ProcurementSuggestionGenerate, self).\
_prepare_suggest_line(orderpoint)
porderline_id = False
if orderpoint.product_id.seller_id:
porderlines = self.env['purchase.order.line'].search([
('state', 'not in', ('draft', 'cancel')),
('product_id', '=', orderpoint.product_id.id)],
order='id desc', limit=1)
# I cannot filter on 'date_order' because it is not a stored field
porderline_id = porderlines and porderlines[0].id or False
sline['last_po_line_id'] = porderline_id
return sline
class ProcurementSuggest(models.TransientModel):
_inherit = 'procurement.suggest'
last_po_line_id = fields.Many2one(
'purchase.order.line', string='Last Purchase Order Line',
readonly=True)
last_po_date = fields.Datetime(
related='last_po_line_id.order_id.date_order',
string='Date of the last PO', readonly=True)
last_po_qty = fields.Float(
related='last_po_line_id.product_qty', readonly=True,
string='Quantity of the Last Order')
class ProcurementCreateFromSuggest(models.TransientModel):
_inherit = 'procurement.create.from.suggest'
@api.multi
def create_proc(self):
action = super(ProcurementCreateFromSuggest, self).create_proc()
poo = self.env['procurement.order']
new_procs = poo.browse(action['domain'][0][2])
po_ids = []
for proc in new_procs:
if proc.purchase_id and proc.purchase_id.id not in po_ids:
po_ids.append(proc.purchase_id.id)
if po_ids:
action = self.env['ir.actions.act_window'].for_xml_id(
'purchase', 'purchase_rfq')
action['domain'] = [('id', 'in', po_ids)]
return action

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 Akretion (http://www.akretion.com/)
@author Alexis de Lattre <alexis.delattre@akretion.com>
The licence is in the file __openerp__.py
-->
<openerp>
<data>
<record id="procurement_suggest_tree" model="ir.ui.view">
<field name="name">procurement_suggest.tree</field>
<field name="model">procurement.suggest</field>
<field name="inherit_id" ref="procurement_suggest.procurement_suggest_tree"/>
<field name="arch" type="xml">
<field name="min_qty" position="after">
<field name="last_po_date" widget="date"/>
<field name="last_po_qty"/>
</field>
</field>
</record>
</data>
</openerp>