Update module purchase_suggest: much better perfs and several small enhancements
This commit is contained in:
@@ -43,22 +43,24 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
default=lambda self: self.env.ref('stock.stock_location_stock'))
|
default=lambda self: self.env.ref('stock.stock_location_stock'))
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _prepare_suggest_line(self, orderpoint):
|
def _prepare_suggest_line(self, product_id, qty_dict):
|
||||||
porderline_id = False
|
porderline_id = False
|
||||||
if orderpoint.product_id.seller_id:
|
if qty_dict['product'].seller_id:
|
||||||
porderlines = self.env['purchase.order.line'].search([
|
porderlines = self.env['purchase.order.line'].search([
|
||||||
('state', 'not in', ('draft', 'cancel')),
|
('state', 'not in', ('draft', 'cancel')),
|
||||||
('product_id', '=', orderpoint.product_id.id)],
|
('product_id', '=', product_id)],
|
||||||
order='id desc', limit=1)
|
order='id desc', limit=1)
|
||||||
# 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 = {
|
||||||
'product_id': orderpoint.product_id.id,
|
'product_id': product_id,
|
||||||
'seller_id': orderpoint.product_id.seller_id.id or False,
|
'seller_id': qty_dict['product'].seller_id.id or False,
|
||||||
'qty_available': orderpoint.product_id.qty_available,
|
'qty_available': qty_dict['qty_available'],
|
||||||
'incoming_qty': orderpoint.product_id.incoming_qty,
|
'incoming_qty': qty_dict['incoming_qty'],
|
||||||
'outgoing_qty': orderpoint.product_id.outgoing_qty,
|
'outgoing_qty': qty_dict['outgoing_qty'],
|
||||||
'orderpoint_id': orderpoint.id,
|
'draft_po_qty': qty_dict['draft_po_qty'],
|
||||||
|
'orderpoint_id': qty_dict['orderpoint'].id,
|
||||||
|
'min_qty': qty_dict['min_qty'],
|
||||||
'last_po_line_id': porderline_id,
|
'last_po_line_id': porderline_id,
|
||||||
}
|
}
|
||||||
return sline
|
return sline
|
||||||
@@ -69,7 +71,7 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
pso = self.env['purchase.suggest']
|
pso = self.env['purchase.suggest']
|
||||||
polo = self.env['purchase.order.line']
|
polo = self.env['purchase.order.line']
|
||||||
swoo = self.env['stock.warehouse.orderpoint']
|
swoo = self.env['stock.warehouse.orderpoint']
|
||||||
poo = self.env['procurement.order']
|
ppo = self.env['product.product']
|
||||||
op_domain = [
|
op_domain = [
|
||||||
('suggest', '=', True),
|
('suggest', '=', True),
|
||||||
('company_id', '=', self.env.user.company_id.id),
|
('company_id', '=', self.env.user.company_id.id),
|
||||||
@@ -83,36 +85,65 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
if self.seller_id:
|
if self.seller_id:
|
||||||
product_domain.append(
|
product_domain.append(
|
||||||
('seller_id', '=', self.seller_id.id))
|
('seller_id', '=', self.seller_id.id))
|
||||||
products = self.env['product.product'].search(product_domain)
|
products_subset = ppo.search(product_domain)
|
||||||
op_domain.append(('product_id', 'in', products.ids))
|
op_domain.append(('product_id', 'in', products_subset.ids))
|
||||||
ops = swoo.search(op_domain)
|
ops = swoo.search(op_domain)
|
||||||
p_suggest_lines = []
|
p_suggest_lines = []
|
||||||
lines = {} # key = product_id ; value = {'min_qty', ...}
|
products = {}
|
||||||
|
# key = product_id
|
||||||
|
# value = {'virtual_qty': 1.0, 'draft_po_qty': 4.0, 'min_qty': 6.0}
|
||||||
|
# TODO : handle the uom
|
||||||
|
logger.info('Starting to compute the purchase suggestions')
|
||||||
for op in ops:
|
for op in ops:
|
||||||
# TODO : take into account the draft PO lines
|
if op.product_id.id not in products:
|
||||||
virtual_qty = poo._product_virtual_get(op)
|
products[op.product_id.id] = {
|
||||||
draft_po_qty = 0
|
'min_qty': op.product_min_qty,
|
||||||
cur_qty = virtual_qty + draft_po_qty
|
'draft_po_qty': 0.0,
|
||||||
logger.debug(
|
'orderpoint': op,
|
||||||
'Product: %s Virtual qty = %s Draft PO qty = %s '
|
'product': op.product_id
|
||||||
'Min. qty = %s',
|
}
|
||||||
op.product_id.name, virtual_qty, draft_po_qty,
|
else:
|
||||||
op.product_min_qty)
|
|
||||||
if float_compare(
|
|
||||||
cur_qty, op.product_min_qty,
|
|
||||||
precision_rounding=op.product_uom.rounding) < 0:
|
|
||||||
if op.product_id.id in lines:
|
|
||||||
raise Warning(
|
raise Warning(
|
||||||
_("There are 2 orderpoints (%s and %s) for the same "
|
_("There are 2 orderpoints (%s and %s) for the same "
|
||||||
"product on stock location %s or its "
|
"product on stock location %s or its "
|
||||||
"children.") % (
|
"children.") % (
|
||||||
lines[op.product_id.id]['orderpoint'].name,
|
products[op.product_id.id]['orderpoint'].name,
|
||||||
op.name,
|
op.name,
|
||||||
self.location_id.complete_name))
|
self.location_id.complete_name))
|
||||||
p_suggest_lines.append(self._prepare_suggest_line(op))
|
logger.info('Min qty computed on %d products', len(products))
|
||||||
|
polines = polo.search([
|
||||||
|
('state', '=', 'draft'), ('product_id', 'in', products.keys())])
|
||||||
|
for line in polines:
|
||||||
|
products[line.product_id.id]['draft_po_qty'] += line.product_qty
|
||||||
|
logger.info('Draft PO qty computed on %d products', len(products))
|
||||||
|
virtual_qties = self.pool['product.product']._product_available(
|
||||||
|
self._cr, self._uid, products.keys(),
|
||||||
|
context={'location': self.location_id.id})
|
||||||
|
logger.info('Stock levels qty computed on %d products', len(products))
|
||||||
|
for product_id, qty_dict in products.iteritems():
|
||||||
|
qty_dict['virtual_available'] =\
|
||||||
|
virtual_qties[product_id]['virtual_available']
|
||||||
|
qty_dict['incoming_qty'] =\
|
||||||
|
virtual_qties[product_id]['incoming_qty']
|
||||||
|
qty_dict['outgoing_qty'] =\
|
||||||
|
virtual_qties[product_id]['outgoing_qty']
|
||||||
|
qty_dict['qty_available'] =\
|
||||||
|
virtual_qties[product_id]['qty_available']
|
||||||
logger.debug(
|
logger.debug(
|
||||||
'Create a procurement suggestion for %s',
|
'Product ID: %d Virtual qty = %s Draft PO qty = %s '
|
||||||
op.product_id.name)
|
'Min. qty = %s',
|
||||||
|
product_id, qty_dict['virtual_available'],
|
||||||
|
qty_dict['draft_po_qty'], qty_dict['min_qty'])
|
||||||
|
if float_compare(
|
||||||
|
qty_dict['virtual_available'] + qty_dict['draft_po_qty'],
|
||||||
|
qty_dict['min_qty'],
|
||||||
|
precision_rounding=op.product_uom.rounding) < 0:
|
||||||
|
vals = self._prepare_suggest_line(product_id, qty_dict)
|
||||||
|
if vals:
|
||||||
|
p_suggest_lines.append(vals)
|
||||||
|
logger.debug(
|
||||||
|
'Created a procurement suggestion for product ID %d',
|
||||||
|
product_id)
|
||||||
p_suggest_lines_sorted = sorted(
|
p_suggest_lines_sorted = sorted(
|
||||||
p_suggest_lines, key=lambda to_sort: to_sort['seller_id'])
|
p_suggest_lines, key=lambda to_sort: to_sort['seller_id'])
|
||||||
if p_suggest_lines_sorted:
|
if p_suggest_lines_sorted:
|
||||||
@@ -159,16 +190,16 @@ class PurchaseSuggest(models.TransientModel):
|
|||||||
readonly=True)
|
readonly=True)
|
||||||
last_po_date = fields.Datetime(
|
last_po_date = fields.Datetime(
|
||||||
related='last_po_line_id.order_id.date_order',
|
related='last_po_line_id.order_id.date_order',
|
||||||
string='Date of the last PO', readonly=True)
|
string='Date of the Last Order', readonly=True)
|
||||||
last_po_qty = fields.Float(
|
last_po_qty = fields.Float(
|
||||||
related='last_po_line_id.product_qty', readonly=True,
|
related='last_po_line_id.product_qty', readonly=True,
|
||||||
|
digits=dp.get_precision('Product Unit of Measure'),
|
||||||
string='Quantity of the Last Order')
|
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)
|
||||||
min_qty = fields.Float(
|
min_qty = fields.Float(
|
||||||
string="Min Quantity", readonly=True,
|
string="Min Quantity", readonly=True,
|
||||||
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(
|
qty_to_order = fields.Float(
|
||||||
string='Quantity to Order',
|
string='Quantity to Order',
|
||||||
@@ -217,6 +248,10 @@ class PurchaseSuggestPoCreate(models.TransientModel):
|
|||||||
for line in self.env['purchase.suggest'].browse(psuggest_ids):
|
for line in self.env['purchase.suggest'].browse(psuggest_ids):
|
||||||
if not line.qty_to_order:
|
if not line.qty_to_order:
|
||||||
continue
|
continue
|
||||||
|
if not line.product_id.seller_id:
|
||||||
|
raise Warning(_(
|
||||||
|
"No supplier configured for product '%s'.")
|
||||||
|
% line.product_id.name)
|
||||||
if line.seller_id in po_to_create:
|
if line.seller_id in po_to_create:
|
||||||
po_to_create[line.seller_id].append(
|
po_to_create[line.seller_id].append(
|
||||||
(line.product_id, line.qty_to_order))
|
(line.product_id, line.qty_to_order))
|
||||||
|
|||||||
@@ -36,8 +36,8 @@
|
|||||||
|
|
||||||
<menuitem id="purchase_suggest_generate_menu"
|
<menuitem id="purchase_suggest_generate_menu"
|
||||||
action="purchase_suggest_generate_action"
|
action="purchase_suggest_generate_action"
|
||||||
parent="stock.menu_stock_sched"
|
parent="purchase.menu_procurement_management"
|
||||||
sequence="100"/>
|
sequence="14"/>
|
||||||
|
|
||||||
<record id="purchase_suggest_tree" model="ir.ui.view">
|
<record id="purchase_suggest_tree" model="ir.ui.view">
|
||||||
<field name="name">purchase_suggest.tree</field>
|
<field name="name">purchase_suggest.tree</field>
|
||||||
@@ -89,7 +89,8 @@
|
|||||||
This wizard will create Purchase Orders.
|
This wizard will create Purchase Orders.
|
||||||
</p>
|
</p>
|
||||||
<footer>
|
<footer>
|
||||||
<button type="object" name="create_po" string="Create POs" class="oe_highlight"/>
|
<button type="object" name="create_po"
|
||||||
|
string="Create Purchase Orders" class="oe_highlight"/>
|
||||||
<button special="cancel" string="Cancel" class="oe_link"/>
|
<button special="cancel" string="Cancel" class="oe_link"/>
|
||||||
</footer>
|
</footer>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user