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.

116 lines
4.2 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
  5. class PosOrder(models.Model):
  6. _inherit = 'pos.order'
  7. @api.model
  8. def _prepare_filter_for_pos(self, pos_session_id):
  9. return [
  10. ('state', 'in', ['paid', 'done', 'invoiced']),
  11. ]
  12. @api.model
  13. def _prepare_filter_query_for_pos(self, pos_session_id, query):
  14. return [
  15. '|',
  16. ('name', 'ilike', query),
  17. ('pos_reference', 'ilike', query),
  18. ]
  19. @api.model
  20. def _prepare_fields_for_pos_list(self):
  21. return [
  22. 'name', 'pos_reference', 'partner_id', 'date_order',
  23. 'amount_total', 'amount_paid', 'amount_return', 'session_id',
  24. 'amount_tax', 'statement_ids', 'lines', 'invoice_id',
  25. 'returned_order_id', 'fiscal_position_id'
  26. ]
  27. @api.model
  28. def search_done_orders_for_pos(self, query, pos_session_id):
  29. session_obj = self.env['pos.session']
  30. config = session_obj.browse(pos_session_id).config_id
  31. condition = self._prepare_filter_for_pos(pos_session_id)
  32. if not query:
  33. # Search only this POS orders
  34. condition += [('config_id', '=', config.id)]
  35. else:
  36. # Search globally by criteria
  37. condition += self._prepare_filter_query_for_pos(pos_session_id,
  38. query)
  39. fields = self._prepare_fields_for_pos_list()
  40. return self.search_read(
  41. condition, fields, limit=config.iface_load_done_order_max_qty)
  42. @api.multi
  43. def _prepare_done_order_for_pos(self):
  44. self.ensure_one()
  45. order_lines = []
  46. payment_lines = []
  47. for order_line in self.lines:
  48. order_line = self._prepare_done_order_line_for_pos(order_line)
  49. order_lines.append(order_line)
  50. for payment_line in self.statement_ids:
  51. payment_line = self._prepare_done_order_payment_for_pos(
  52. payment_line)
  53. payment_lines.append(payment_line)
  54. return {
  55. 'id': self.id,
  56. 'date_order': self.date_order,
  57. 'pos_reference': self.pos_reference,
  58. 'name': self.name,
  59. 'partner_id': self.partner_id.id,
  60. 'fiscal_position': self.fiscal_position_id.id,
  61. 'line_ids': order_lines,
  62. 'statement_ids': payment_lines,
  63. 'origin_invoice_id': bool(self.invoice_id),
  64. 'returned_order_id': (self.returned_order_id and
  65. self.returned_order_id.pos_reference or
  66. False),
  67. }
  68. @api.multi
  69. def _prepare_done_order_line_for_pos(self, order_line):
  70. self.ensure_one()
  71. return {
  72. 'product_id': order_line.product_id.id,
  73. 'qty': order_line.qty,
  74. 'price_unit': order_line.price_unit,
  75. 'discount': order_line.discount,
  76. }
  77. @api.multi
  78. def _prepare_done_order_payment_for_pos(self, payment_line):
  79. self.ensure_one()
  80. return {
  81. 'statement_id': payment_line.statement_id.id,
  82. 'amount': payment_line.amount,
  83. }
  84. @api.multi
  85. def load_done_order_for_pos(self):
  86. self.ensure_one()
  87. return self._prepare_done_order_for_pos()
  88. @api.model
  89. def _process_order(self, pos_order):
  90. if (not pos_order.get('return') or
  91. not pos_order.get('returned_order_id')):
  92. return super()._process_order(pos_order)
  93. order = super(PosOrder,
  94. self.with_context(do_not_check_negative_qty=True)
  95. )._process_order(pos_order)
  96. returned_order_id = pos_order.get('returned_order_id')
  97. if isinstance(returned_order_id, int):
  98. order.returned_order_id = self.browse(returned_order_id)
  99. # Only if the order is returned from the browser saved orders.
  100. else:
  101. order.returned_order_id = self.search([
  102. ('pos_reference', '=', returned_order_id)])
  103. order.returned_order_id.refund_order_ids |= order
  104. return order