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.

130 lines
4.5 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. ]
  34. @api.model
  35. def _prepare_fields_for_pos_list(self):
  36. return [
  37. 'name', 'pos_reference', 'partner_id', 'date_order',
  38. 'amount_total', 'amount_paid', 'amount_return', 'session_id',
  39. 'amount_tax', 'statement_ids', 'lines', 'invoice_id',
  40. 'returned_order_id', 'fiscal_position_id'
  41. ]
  42. @api.model
  43. def search_done_orders_for_pos(self, query, pos_session_id):
  44. session_obj = self.env['pos.session']
  45. config = session_obj.browse(pos_session_id).config_id
  46. condition = self._prepare_filter_for_pos(pos_session_id)
  47. if not query:
  48. # Search only this POS orders
  49. condition += [('config_id', '=', config.id)]
  50. else:
  51. # Search globally by criteria
  52. condition += self._prepare_filter_query_for_pos(pos_session_id,
  53. query)
  54. field_names = self._prepare_fields_for_pos_list()
  55. return self.search_read(
  56. condition, field_names, limit=config.iface_load_done_order_max_qty)
  57. @api.multi
  58. def _prepare_done_order_for_pos(self):
  59. self.ensure_one()
  60. order_lines = []
  61. payment_lines = []
  62. for order_line in self.lines:
  63. order_line = self._prepare_done_order_line_for_pos(order_line)
  64. order_lines.append(order_line)
  65. for payment_line in self.statement_ids:
  66. payment_line = self._prepare_done_order_payment_for_pos(
  67. payment_line)
  68. payment_lines.append(payment_line)
  69. return {
  70. 'id': self.id,
  71. 'date_order': self.date_order,
  72. 'pos_reference': self.pos_reference,
  73. 'name': self.name,
  74. 'partner_id': self.partner_id.id,
  75. 'fiscal_position': self.fiscal_position_id.id,
  76. 'line_ids': order_lines,
  77. 'statement_ids': payment_lines,
  78. 'origin_invoice_id': bool(self.invoice_id),
  79. 'returned_order_id': (self.returned_order_id and
  80. self.returned_order_id.pos_reference or
  81. False),
  82. }
  83. @api.multi
  84. def _prepare_done_order_line_for_pos(self, order_line):
  85. self.ensure_one()
  86. return {
  87. 'product_id': order_line.product_id.id,
  88. 'qty': order_line.qty,
  89. 'price_unit': order_line.price_unit,
  90. 'discount': order_line.discount,
  91. }
  92. @api.multi
  93. def _prepare_done_order_payment_for_pos(self, payment_line):
  94. self.ensure_one()
  95. return {
  96. 'statement_id': payment_line.statement_id.id,
  97. 'amount': payment_line.amount,
  98. }
  99. @api.multi
  100. def load_done_order_for_pos(self):
  101. self.ensure_one()
  102. return self._prepare_done_order_for_pos()
  103. @api.model
  104. def _process_order(self, pos_order):
  105. if (not pos_order.get('return') or
  106. not pos_order.get('returned_order_id')):
  107. return super()._process_order(pos_order)
  108. order = super(PosOrder, self)._process_order(pos_order)
  109. returned_order_id = pos_order.get('returned_order_id')
  110. if isinstance(returned_order_id, int):
  111. order.returned_order_id = self.browse(returned_order_id)
  112. # Only if the order is returned from the browser saved orders.
  113. else:
  114. order.returned_order_id = self.search([
  115. ('pos_reference', '=', returned_order_id)])
  116. order.returned_order_id.refund_order_ids |= order
  117. return order