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.

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