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.

91 lines
3.4 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 StockPicking(models.Model):
  6. _inherit = 'stock.picking'
  7. # Field Section
  8. final_pos_order_id = fields.Many2one(
  9. string='Final PoS Order', comodel_name='pos.order', readonly=True,
  10. help="This picking has been canceled, because it has been replaced by"
  11. " this PoS Order")
  12. # Custom Section - Picking List Part
  13. @api.model
  14. def _prepare_filter_for_pos(self, pos_session_id):
  15. picking_type_obj = self.env['stock.picking.type']
  16. picking_types = picking_type_obj.search(
  17. [('available_in_pos', '=', True)])
  18. return [
  19. ('picking_type_id', 'in', picking_types.ids),
  20. ('state', 'in', ['confirmed', 'partially_available', 'assigned']),
  21. ]
  22. @api.model
  23. def _prepare_filter_query_for_pos(self, pos_session_id, query):
  24. return [
  25. '|', '|',
  26. ('name', 'ilike', query),
  27. ('origin', 'ilike', query),
  28. ('partner_id', 'ilike', query),
  29. ]
  30. @api.model
  31. def _prepare_fields_for_pos_list(self):
  32. return ['name', 'partner_id', 'scheduled_date', 'origin']
  33. @api.model
  34. def search_pickings_for_pos(self, query, pos_session_id):
  35. session_obj = self.env['pos.session']
  36. config = session_obj.browse(pos_session_id).config_id
  37. condition = self._prepare_filter_for_pos(pos_session_id) +\
  38. self._prepare_filter_query_for_pos(pos_session_id, query)
  39. fields = self._prepare_fields_for_pos_list()
  40. return self.search_read(
  41. condition, fields, limit=config.iface_load_picking_max_qty)
  42. # Custom Section - Load Picking Part
  43. @api.model
  44. def _prepare_line_data_from_stock_move(self, move):
  45. picking_line_data = {
  46. 'name': move.name,
  47. 'product_id': move.product_id.id,
  48. 'quantity': move.product_uom_qty,
  49. }
  50. sale_order_line = move.sale_line_id
  51. if sale_order_line:
  52. # Get price and discount of the order if available
  53. picking_line_data['price_unit'] = sale_order_line.price_unit
  54. picking_line_data['discount'] = sale_order_line.discount
  55. return picking_line_data
  56. @api.model
  57. def load_picking_for_pos(self, picking_id):
  58. picking = self.browse(picking_id)
  59. picking_lines = []
  60. for move in picking.move_lines.filtered(lambda x: x.state != 'cancel'):
  61. picking_lines.append(self._prepare_line_data_from_stock_move(move))
  62. return {
  63. 'id': picking.id,
  64. 'name': picking.name,
  65. 'partner_id': picking.partner_id.id,
  66. 'line_ids': picking_lines,
  67. }
  68. @api.multi
  69. def update_from_origin_picking(self, origin_picking):
  70. if origin_picking.group_id:
  71. self.filtered(lambda p: not p.group_id).write({
  72. 'group_id': origin_picking.group_id.id})
  73. @api.multi
  74. def action_confirm(self):
  75. """ Assign to same procurement group as the origin picking """
  76. if self.env.context.get('origin_picking_id'):
  77. self.update_from_origin_picking(
  78. self.browse(self.env.context['origin_picking_id']))
  79. return super().action_confirm()