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.

161 lines
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 GRAP - Sylvain LE GAL
  3. # Copyright 2018 Tecnativa S.L. - David Vidal
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, models, fields
  6. class PosOrder(models.Model):
  7. _inherit = 'pos.order'
  8. returned_order_id = fields.Many2one(
  9. comodel_name='pos.order',
  10. string='Returned Order',
  11. readonly=True,
  12. )
  13. returned_order_reference = fields.Char(
  14. related='returned_order_id.pos_reference',
  15. string='Reference of the returned Order')
  16. refund_order_ids = fields.One2many(
  17. comodel_name='pos.order',
  18. inverse_name='returned_order_id',
  19. string='Refund Orders',
  20. readonly=True,
  21. )
  22. refund_order_qty = fields.Integer(
  23. compute='_compute_refund_order_qty',
  24. string='Refund Orders Quantity',
  25. )
  26. @api.multi
  27. @api.depends('refund_order_ids')
  28. def _compute_refund_order_qty(self):
  29. for order in self:
  30. order.refund_order_qty = len(order.refund_order_ids)
  31. @api.multi
  32. def action_view_refund_orders(self):
  33. self.ensure_one()
  34. action = self.env.ref('point_of_sale.action_pos_pos_form').read()[0]
  35. if self.refund_order_qty == 1:
  36. action['views'] = [
  37. (self.env.ref('point_of_sale.view_pos_pos_form').id, 'form')]
  38. action['res_id'] = self.refund_order_ids.ids[0]
  39. else:
  40. action['domain'] = [('id', 'in', self.refund_order_ids.ids)]
  41. return action
  42. @api.multi
  43. def refund(self):
  44. return super(PosOrder, self.with_context(refund=True)).refund()
  45. @api.multi
  46. @api.returns('self', lambda value: value.id)
  47. def copy(self, default=None):
  48. self.ensure_one()
  49. order = super(PosOrder, self).copy(default=default)
  50. if self.env.context.get('refund', False):
  51. order.returned_order_id = self.id
  52. return order
  53. @api.model
  54. def _prepare_filter_for_pos(self, pos_session_id):
  55. return [
  56. ('state', 'in', ['paid', 'done', 'invoiced']),
  57. ]
  58. @api.model
  59. def _prepare_filter_query_for_pos(self, pos_session_id, query):
  60. return [
  61. '|', '|',
  62. ('name', 'ilike', query),
  63. ('pos_reference', 'ilike', query),
  64. ('partner_id.display_name', 'ilike', query),
  65. ]
  66. @api.model
  67. def _prepare_fields_for_pos_list(self):
  68. return [
  69. 'name', 'pos_reference', 'partner_id', 'date_order',
  70. 'amount_total',
  71. ]
  72. @api.model
  73. def search_done_orders_for_pos(self, query, pos_session_id):
  74. session_obj = self.env['pos.session']
  75. config = session_obj.browse(pos_session_id).config_id
  76. condition = self._prepare_filter_for_pos(pos_session_id)
  77. if not query:
  78. # Search only this POS orders
  79. condition += [('config_id', '=', config.id)]
  80. else:
  81. # Search globally by criteria
  82. condition += self._prepare_filter_query_for_pos(
  83. pos_session_id, query)
  84. field_names = self._prepare_fields_for_pos_list()
  85. return self.search_read(
  86. condition, field_names, limit=config.iface_load_done_order_max_qty)
  87. @api.multi
  88. def _prepare_done_order_for_pos(self):
  89. self.ensure_one()
  90. order_lines = []
  91. payment_lines = []
  92. for order_line in self.lines:
  93. order_line = self._prepare_done_order_line_for_pos(order_line)
  94. order_lines.append(order_line)
  95. for payment_line in self.statement_ids:
  96. payment_line = self._prepare_done_order_payment_for_pos(
  97. payment_line)
  98. payment_lines.append(payment_line)
  99. res = {
  100. 'id': self.id,
  101. 'date_order': self.date_order,
  102. 'pos_reference': self.pos_reference,
  103. 'name': self.name,
  104. 'partner_id': self.partner_id.id,
  105. 'fiscal_position': self.fiscal_position_id.id,
  106. 'line_ids': order_lines,
  107. 'statement_ids': payment_lines,
  108. 'to_invoice': bool(self.invoice_id),
  109. 'returned_order_id': self.returned_order_id.id,
  110. 'returned_order_reference': self.returned_order_reference,
  111. }
  112. return res
  113. @api.multi
  114. def _prepare_done_order_line_for_pos(self, order_line):
  115. self.ensure_one()
  116. return {
  117. 'product_id': order_line.product_id.id,
  118. 'qty': order_line.qty,
  119. 'price_unit': order_line.price_unit,
  120. 'discount': order_line.discount,
  121. }
  122. @api.multi
  123. def _prepare_done_order_payment_for_pos(self, payment_line):
  124. self.ensure_one()
  125. return {
  126. 'journal_id': payment_line.journal_id.id,
  127. 'amount': payment_line.amount,
  128. }
  129. @api.multi
  130. def load_done_order_for_pos(self):
  131. self.ensure_one()
  132. return self._prepare_done_order_for_pos()
  133. @api.model
  134. def _order_fields(self, ui_order):
  135. res = super(PosOrder, self)._order_fields(ui_order)
  136. res.update({
  137. 'returned_order_id': ui_order.get('returned_order_id', False),
  138. })
  139. return res