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.

57 lines
2.0 KiB

  1. # coding: utf-8
  2. # Copyright 2018 - Today Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class PosOrder(models.Model):
  8. _inherit = 'pos.order'
  9. has_picking_delayed = fields.Boolean(
  10. default=False, help="This checkbox is checked if the generation of"
  11. " the picking has been delayed. The picking will be created by cron.")
  12. # Overload Section
  13. @api.model
  14. def create_from_ui(self, orders):
  15. PosSession = self.env['pos.session']
  16. for order_data in orders:
  17. session_id = order_data.get('data').get('pos_session_id')
  18. session = PosSession.browse(session_id)
  19. order_data['data']['has_picking_delayed'] =\
  20. session.config_id.picking_creation_delayed
  21. return super(PosOrder, self.with_context(
  22. create_from_ui=True)).create_from_ui(orders)
  23. def create_picking(self):
  24. if self.env.context.get('create_from_ui', False):
  25. orders = self.filtered(lambda x: not x.has_picking_delayed)
  26. else:
  27. orders = self
  28. res = super(PosOrder, orders).create_picking()
  29. orders.write({'has_picking_delayed': False})
  30. return res
  31. @api.model
  32. def _order_fields(self, ui_order):
  33. res = super(PosOrder, self)._order_fields(ui_order)
  34. res['has_picking_delayed'] = ui_order['has_picking_delayed']
  35. return res
  36. # Custom Section
  37. @api.model
  38. def create_delayed_picking(self):
  39. orders = self.search([
  40. ('state', '!=', 'draft'),
  41. ('has_picking_delayed', '=', True),
  42. ], order='date_order')
  43. for order in orders:
  44. order.sudo(order.user_id.id).with_context(
  45. force_company=order.company_id.id).create_picking()
  46. if orders:
  47. _logger.info("Pickings handled for %d PoS Orders" % (len(orders)))