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.

65 lines
2.3 KiB

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