Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bbn-bernard
Created November 6, 2014 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbn-bernard/87a0f46af030e3d459e1 to your computer and use it in GitHub Desktop.
Save bbn-bernard/87a0f46af030e3d459e1 to your computer and use it in GitHub Desktop.
purchase_action_cancel
def get_return_history(self, cr, uid, pick_id, context=None):
"""
Get return_history.
@param self: The object pointer.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param pick_id: Picking id
@param context: A standard dictionary
@return: A dictionary which of values.
"""
pick_obj = self.pool.get('stock.picking')
pick = pick_obj.browse(cr, uid, pick_id, context=context)
return_history = {}
for m in pick.move_lines:
if m.state == 'done':
return_history[m.id] = 0
for rec in m.move_history_ids2:
# only take into account 'product return' moves, ignoring any other
# kind of upstream moves, such as internal procurements, etc.
# a valid return move will be the exact opposite of ours:
# (src location, dest location) <=> (dest location, src location))
if rec.state == 'done' and rec.location_dest_id.id == m.location_id.id \
and rec.location_id.id == m.location_dest_id.id:
return_history[m.id] += (rec.product_qty * rec.product_uom.factor)
return return_history
def action_cancel(self, cr, uid, ids, context=None):
wf_service = netsvc.LocalService("workflow")
for purchase in self.browse(cr, uid, ids, context=context):
# a hack to allow cancel this when product returned with state 'done'
error = False
for pick in purchase.picking_ids:
if pick.type == 'out':
continue
valid_lines = 0
return_history = self.get_return_history(cr, uid, pick.id, context)
if return_history:
for m in pick.move_lines:
if (m.state == 'done') and (return_history.get(m.id) is not None) and (m.product_qty * m.product_uom.factor > return_history[m.id]):
valid_lines += 1
if valid_lines:
error = True
else:
if pick.state == 'done':
error = True
if error:
raise osv.except_osv(
_('Unable to cancel this purchase order!'),
_('You must first cancel all receptions related to this purchase order.'))
for pick in purchase.picking_ids:
wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_cancel', cr)
for inv in purchase.invoice_ids:
if inv and inv.state not in ('cancel','draft'):
raise osv.except_osv(
_('Unable to cancel this purchase order!'),
_('You must first cancel all invoices related to this purchase order.'))
if inv:
wf_service.trg_validate(uid, 'account.invoice', inv.id, 'invoice_cancel', cr)
self.write(cr,uid,ids,{'state':'cancel'})
for (id, name) in self.name_get(cr, uid, ids):
wf_service.trg_validate(uid, 'purchase.order', id, 'purchase_cancel', cr)
message = _("Purchase order '%s' is cancelled.") % name
self.log(cr, uid, id, message)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment