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. # 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. returned_order_reference = fields.Char(
  13. related='returned_order_id.pos_reference',
  14. string='Reference of the returned Order')
  15. refund_order_ids = fields.One2many(
  16. comodel_name='pos.order',
  17. inverse_name='returned_order_id',
  18. string='Refund Orders',
  19. readonly=True,
  20. )
  21. refund_order_qty = fields.Integer(
  22. compute='_compute_refund_order_qty',
  23. string='Refund Orders Quantity',
  24. )
  25. @api.multi
  26. @api.depends('refund_order_ids')
  27. def _compute_refund_order_qty(self):
  28. for order in self:
  29. order.refund_order_qty = len(order.refund_order_ids)
  30. @api.multi
  31. def action_view_refund_orders(self):
  32. self.ensure_one()
  33. action = self.env.ref('point_of_sale.action_pos_pos_form').read()[0]
  34. if self.refund_order_qty == 1:
  35. action['views'] = [
  36. (self.env.ref('point_of_sale.view_pos_pos_form').id, 'form')]
  37. action['res_id'] = self.refund_order_ids.ids[0]
  38. else:
  39. action['domain'] = [('id', 'in', self.refund_order_ids.ids)]
  40. return action
  41. @api.multi
  42. def refund(self):
  43. return super(PosOrder, self.with_context(refund=True)).refund()
  44. @api.multi
  45. @api.returns('self', lambda value: value.id)
  46. def copy(self, default=None):
  47. self.ensure_one()
  48. order = super().copy(default=default)
  49. if self.env.context.get('refund', False):
  50. order.returned_order_id = self.id
  51. return order
  52. @api.model
  53. def _prepare_filter_for_pos(self, pos_session_id):
  54. return [
  55. ('state', 'in', ['paid', 'done', 'invoiced']),
  56. ]
  57. @api.model
  58. def _prepare_filter_query_for_pos(self, pos_session_id, query):
  59. return [
  60. '|', '|',
  61. ('name', 'ilike', query),
  62. ('pos_reference', 'ilike', query),
  63. ('partner_id.display_name', 'ilike', query),
  64. ]
  65. @api.model
  66. def _prepare_fields_for_pos_list(self):
  67. return [
  68. 'name', 'pos_reference', 'partner_id', 'date_order',
  69. 'amount_total',
  70. ]
  71. @api.model
  72. def search_done_orders_for_pos(self, query, pos_session_id):
  73. session_obj = self.env['pos.session']
  74. config = session_obj.browse(pos_session_id).config_id
  75. condition = self._prepare_filter_for_pos(pos_session_id)
  76. if not query:
  77. # Search only this POS orders
  78. condition += [('config_id', '=', config.id)]
  79. else:
  80. # Search globally by criteria
  81. condition += self._prepare_filter_query_for_pos(
  82. pos_session_id, query)
  83. field_names = self._prepare_fields_for_pos_list()
  84. return self.search_read(
  85. condition, field_names, limit=config.iface_load_done_order_max_qty)
  86. @api.multi
  87. def _prepare_done_order_for_pos(self):
  88. self.ensure_one()
  89. order_lines = []
  90. payment_lines = []
  91. for order_line in self.lines:
  92. order_line = self._prepare_done_order_line_for_pos(order_line)
  93. order_lines.append(order_line)
  94. for payment_line in self.statement_ids:
  95. payment_line = self._prepare_done_order_payment_for_pos(
  96. payment_line)
  97. payment_lines.append(payment_line)
  98. res = {
  99. 'id': self.id,
  100. 'date_order': self.date_order,
  101. 'pos_reference': self.pos_reference,
  102. 'name': self.name,
  103. 'partner_id': self.partner_id.id,
  104. 'fiscal_position': self.fiscal_position_id.id,
  105. 'line_ids': order_lines,
  106. 'statement_ids': payment_lines,
  107. 'to_invoice': bool(self.invoice_id),
  108. 'returned_order_id': self.returned_order_id.id,
  109. 'returned_order_reference': self.returned_order_reference,
  110. }
  111. return res
  112. @api.multi
  113. def _prepare_done_order_line_for_pos(self, order_line):
  114. self.ensure_one()
  115. return {
  116. 'product_id': order_line.product_id.id,
  117. 'qty': order_line.qty,
  118. 'price_unit': order_line.price_unit,
  119. 'discount': order_line.discount,
  120. 'pack_lot_names': order_line.pack_lot_ids.mapped('lot_name'),
  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()._order_fields(ui_order)
  136. res.update({
  137. 'returned_order_id': ui_order.get('returned_order_id', False),
  138. })
  139. return res