You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import models, api, fields
  6. class PosOrder(models.Model):
  7. _inherit = 'pos.order'
  8. # Field Section
  9. origin_picking_id = fields.Many2one(
  10. string='Origin Picking', comodel_name='stock.picking',
  11. readonly=True)
  12. # Overloadable Section
  13. @api.multi
  14. def _handle_orders_with_original_picking(self):
  15. """By default, the module cancel original stock picking and sale
  16. order. Overload / Overwrite this function if you want another
  17. behaviour"""
  18. sale_order_obj = self.env['sale.order']
  19. for order in self:
  20. # Cancel Picking
  21. order.origin_picking_id.action_cancel()
  22. order.origin_picking_id.write({'final_pos_order_id': order.id})
  23. # Ignore Delivery exception of the Sale Order
  24. sale_orders = sale_order_obj.search([
  25. ('procurement_group_id', '=',
  26. order.origin_picking_id.group_id.id)])
  27. sale_orders.signal_workflow('ship_corrected')
  28. sale_orders.write({'final_pos_order_id': order.id})
  29. # Overload Section
  30. @api.model
  31. def create_from_ui(self, orders):
  32. """Cancel the original picking, when the pos order is done"""
  33. res = super(PosOrder, self).create_from_ui(orders)
  34. orders_with_original_picking = self.search([
  35. ('id', 'in', res), ('origin_picking_id', '!=', False),
  36. ('state', '!=', 'draft')])
  37. orders_with_original_picking._handle_orders_with_original_picking()
  38. return res
  39. @api.model
  40. def _order_fields(self, ui_order):
  41. res = super(PosOrder, self)._order_fields(ui_order)
  42. if 'origin_picking_id' in ui_order:
  43. res['origin_picking_id'] = ui_order['origin_picking_id']
  44. return res
  45. @api.multi
  46. def create_picking(self):
  47. """ Call super() for each order separately with the origin picking id
  48. in the context. The new picking will be updated accordingly in the
  49. picking's action_confirm() """
  50. for order in self:
  51. if order.picking_id:
  52. continue
  53. if order.origin_picking_id:
  54. order = order.with_context(
  55. origin_picking_id=order.origin_picking_id.id)
  56. super(PosOrder, order).create_picking()
  57. return True