[IMP] purchase: decrease qty log an activity on receipt picking

When the user decrease the quantity log an activity on directly linked
pickings in order to help the user to react on its receipt.
This commit is contained in:
Arnold Moyaux
2018-02-01 15:26:59 +01:00
committed by Simon Lejeune
parent 3169832a7d
commit cf99ccf84d
2 changed files with 78 additions and 0 deletions
+50
View File
@@ -199,6 +199,21 @@ class PurchaseOrder(models.Model):
vals['name'] = self.env['ir.sequence'].next_by_code('purchase.order') or '/'
return super(PurchaseOrder, self).create(vals)
def write(self, vals):
if vals.get('order_line') and self.state == 'purchase':
for order in self:
pre_order_line_qty = {order_line: order_line.product_qty for order_line in order.mapped('order_line')}
res = super(PurchaseOrder, self).write(vals)
if vals.get('order_line') and self.state == 'purchase':
for order in self:
to_log = {}
for order_line in order.order_line:
if pre_order_line_qty.get(order_line, False) and float_compare(pre_order_line_qty[order_line], order_line.product_qty, precision_rounding=order_line.product_uom.rounding) > 0:
to_log[order_line] = (order_line.product_qty, pre_order_line_qty[order_line])
if to_log:
order._log_decrease_ordered_quantity(to_log)
return res
@api.multi
def unlink(self):
for order in self:
@@ -471,6 +486,41 @@ class PurchaseOrder(models.Model):
except AccessError: # no write access rights -> just ignore
break
def _log_decrease_ordered_quantity(self, purchase_order_lines_quantities):
def _keys_in_sorted(move):
""" sort by picking and the responsible for the product the
move.
"""
return (move.picking_id.id, move.product_id.responsible_id.id)
def _keys_in_groupby(move):
""" group by picking and the responsible for the product the
move.
"""
return (move.picking_id, move.product_id.responsible_id)
def _render_note_exception_quantity_po(order_exceptions):
order_line_ids = self.env['purchase.order.line'].browse([order_line.id for order in order_exceptions.values() for order_line in order[0]])
purchase_order_ids = order_line_ids.mapped('order_id')
move_ids = self.env['stock.move'].concat(*rendering_context.keys())
impacted_pickings = move_ids.mapped('picking_id')._get_impacted_pickings(move_ids) - move_ids.mapped('picking_id')
values = {
'purchase_order_ids': purchase_order_ids,
'order_exceptions': order_exceptions.values(),
'impacted_pickings': impacted_pickings,
}
return self.env.ref('purchase.exception_on_po').render(values=values)
documents = self.env['stock.picking']._log_activity_get_documents(purchase_order_lines_quantities, 'move_ids', 'DOWN', _keys_in_sorted, _keys_in_groupby)
filtered_documents = {}
for (parent, responsible), rendering_context in documents.items():
if parent._name == 'stock.picking':
if parent.state == 'cancel':
continue
filtered_documents[(parent, responsible)] = rendering_context
self.env['stock.picking']._log_activity(_render_note_exception_quantity_po, filtered_documents)
@api.multi
def action_view_picking(self):
'''
@@ -14,4 +14,32 @@
</ul>
</div>
</template>
<template id="exception_on_po">
<div class="alert alert-warning">
Exception(s) occurred on the purchase order(s):
<t t-foreach="purchase_order_ids" t-as="purchase_order">
<a href="#" data-oe-model="purchase.order" t-att-data-oe-id="purchase_order.id"><t t-esc="purchase_order.name"/></a>.
</t>
Manual actions may be needed.
<div class="mt16">
<p>Exception(s):</p>
<ul t-foreach="order_exceptions" t-as="exception">
<li>
<t t-set="order_line" t-value="exception[0]"/>
<t t-set="new_qty" t-value="exception[1][0]"/>
<t t-set="old_qty" t-value="exception[1][1]"/>
<t t-esc="new_qty"/> <t t-esc="order_line.product_uom.name"/> of <t t-esc="order_line.product_id.name"/>
ordered instead of <t t-esc="old_qty"/> <t t-esc="order_line.product_uom.name"/>
</li>
</ul>
</div>
<div class="mt16" t-if="impacted_pickings">
<p>Next transfer(s) impacted:</p>
<ul t-foreach="impacted_pickings" t-as="picking">
<li><a href="#" data-oe-model="stock.picking" t-att-data-oe-id="picking.id"><t t-esc="picking.name"/></a></li>
</ul>
</div>
</div>
</template>
</odoo>