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.

131 lines
4.6 KiB

  1. # Copyright 2018 GRAP - Sylvain LE GAL
  2. # Copyright 2018 Tecnativa S.L. - David Vidal
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models, fields
  5. class PosOrder(models.Model):
  6. _inherit = 'pos.order'
  7. returned_order_id = fields.Many2one(
  8. comodel_name='pos.order',
  9. string='Returned Order',
  10. readonly=True,
  11. )
  12. refund_order_ids = fields.One2many(
  13. comodel_name='pos.order',
  14. inverse_name='returned_order_id',
  15. string='Refund Orders',
  16. readonly=True,
  17. )
  18. refund_order_qty = fields.Integer(
  19. compute='_compute_refund_order_qty',
  20. string='Refund Orders Quantity',
  21. )
  22. @api.model
  23. def _prepare_filter_for_pos(self, pos_session_id):
  24. return [
  25. ('state', 'in', ['paid', 'done', 'invoiced']),
  26. ]
  27. @api.model
  28. def _prepare_filter_query_for_pos(self, pos_session_id, query):
  29. return [
  30. '|', '|',
  31. ('name', 'ilike', query),
  32. ('pos_reference', 'ilike', query),
  33. ('partner_id.display_name', 'ilike', query),
  34. ]
  35. @api.model
  36. def _prepare_fields_for_pos_list(self):
  37. return [
  38. 'name', 'pos_reference', 'partner_id', 'date_order',
  39. 'amount_total', 'amount_paid', 'amount_return', 'session_id',
  40. 'amount_tax', 'statement_ids', 'lines', 'invoice_id',
  41. 'returned_order_id', 'fiscal_position_id'
  42. ]
  43. @api.model
  44. def search_done_orders_for_pos(self, query, pos_session_id):
  45. session_obj = self.env['pos.session']
  46. config = session_obj.browse(pos_session_id).config_id
  47. condition = self._prepare_filter_for_pos(pos_session_id)
  48. if not query:
  49. # Search only this POS orders
  50. condition += [('config_id', '=', config.id)]
  51. else:
  52. # Search globally by criteria
  53. condition += self._prepare_filter_query_for_pos(
  54. pos_session_id, query)
  55. field_names = self._prepare_fields_for_pos_list()
  56. return self.search_read(
  57. condition, field_names, limit=config.iface_load_done_order_max_qty)
  58. @api.multi
  59. def _prepare_done_order_for_pos(self):
  60. self.ensure_one()
  61. order_lines = []
  62. payment_lines = []
  63. for order_line in self.lines:
  64. order_line = self._prepare_done_order_line_for_pos(order_line)
  65. order_lines.append(order_line)
  66. for payment_line in self.statement_ids:
  67. payment_line = self._prepare_done_order_payment_for_pos(
  68. payment_line)
  69. payment_lines.append(payment_line)
  70. return {
  71. 'id': self.id,
  72. 'date_order': self.date_order,
  73. 'pos_reference': self.pos_reference,
  74. 'name': self.name,
  75. 'partner_id': self.partner_id.id,
  76. 'fiscal_position': self.fiscal_position_id.id,
  77. 'line_ids': order_lines,
  78. 'statement_ids': payment_lines,
  79. 'origin_invoice_id': bool(self.invoice_id),
  80. 'returned_order_id': (self.returned_order_id and
  81. self.returned_order_id.pos_reference or
  82. False),
  83. }
  84. @api.multi
  85. def _prepare_done_order_line_for_pos(self, order_line):
  86. self.ensure_one()
  87. return {
  88. 'product_id': order_line.product_id.id,
  89. 'qty': order_line.qty,
  90. 'price_unit': order_line.price_unit,
  91. 'discount': order_line.discount,
  92. }
  93. @api.multi
  94. def _prepare_done_order_payment_for_pos(self, payment_line):
  95. self.ensure_one()
  96. return {
  97. 'journal_id': payment_line.journal_id.id,
  98. 'amount': payment_line.amount,
  99. }
  100. @api.multi
  101. def load_done_order_for_pos(self):
  102. self.ensure_one()
  103. return self._prepare_done_order_for_pos()
  104. @api.model
  105. def _process_order(self, pos_order):
  106. if (not pos_order.get('return') or
  107. not pos_order.get('returned_order_id')):
  108. return super()._process_order(pos_order)
  109. order = super(PosOrder, self)._process_order(pos_order)
  110. returned_order_id = pos_order.get('returned_order_id')
  111. if isinstance(returned_order_id, int):
  112. order.returned_order_id = self.browse(returned_order_id)
  113. # Only if the order is returned from the browser saved orders.
  114. else:
  115. order.returned_order_id = self.search([
  116. ('pos_reference', '=', returned_order_id)])
  117. order.returned_order_id.refund_order_ids |= order
  118. return order