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.

198 lines
7.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2018 Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # Copyright 2018 David Vidal <david.vidal@tecnativa.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import ValidationError
  7. class PosOrder(models.Model):
  8. _inherit = 'pos.order'
  9. returned_order_id = fields.Many2one(
  10. comodel_name='pos.order',
  11. string='Returned Order',
  12. readonly=True,
  13. )
  14. refund_order_ids = fields.One2many(
  15. comodel_name='pos.order',
  16. inverse_name='returned_order_id',
  17. string='Refund Orders',
  18. readonly=True,
  19. )
  20. refund_order_qty = fields.Integer(
  21. compute='_compute_refund_order_qty',
  22. string='Refund Orders Quantity',
  23. )
  24. def _compute_refund_order_qty(self):
  25. order_data = self.env['pos.order'].read_group(
  26. [('returned_order_id', 'in', self.ids)],
  27. ['returned_order_id'], ['returned_order_id']
  28. )
  29. mapped_data = dict(
  30. [(order['returned_order_id'][0], order['returned_order_id_count'])
  31. for order in order_data])
  32. for order in self:
  33. order.refund_order_qty = mapped_data.get(order.id, 0)
  34. def _blank_refund(self, res):
  35. self.ensure_one()
  36. new_order = self.browse(res['res_id'])
  37. new_order.returned_order_id = self
  38. # Remove created lines and recreate and link Lines
  39. new_order.lines.unlink()
  40. return new_order
  41. def _prepare_invoice(self):
  42. res = super(PosOrder, self)._prepare_invoice()
  43. if not self.returned_order_id:
  44. return res
  45. res.update({
  46. 'origin': self.name,
  47. 'type': 'out_refund',
  48. 'refund_invoice_id': self.returned_order_id.invoice_id.id,
  49. })
  50. return res
  51. def _action_create_invoice_line(self, line=False, invoice_id=False):
  52. line = super(PosOrder, self
  53. )._action_create_invoice_line(line, invoice_id)
  54. if not self.returned_order_id:
  55. return line
  56. # Goes to refund invoice thus it should be positive
  57. line.quantity = -line.quantity
  58. return line
  59. def _action_pos_order_invoice(self):
  60. """Wrap common process"""
  61. self.action_pos_order_invoice()
  62. self.invoice_id.sudo().action_invoice_open()
  63. self.account_move = self.invoice_id.move_id
  64. def refund(self):
  65. # Call super to use original refund algorithm (session management, ...)
  66. ctx = dict(self.env.context, do_not_check_negative_qty=True)
  67. res = super(PosOrder, self.with_context(ctx)).refund()
  68. new_order = self._blank_refund(res)
  69. for line in self.lines:
  70. qty = - line.max_returnable_qty([])
  71. if qty != 0:
  72. copy_line = line.copy()
  73. copy_line.write({
  74. 'order_id': new_order.id,
  75. 'returned_line_id': line.id,
  76. 'qty': qty,
  77. })
  78. return res
  79. def partial_refund(self, partial_return_wizard):
  80. ctx = dict(self.env.context, partial_refund=True)
  81. res = self.with_context(ctx).refund()
  82. new_order = self._blank_refund(res)
  83. for wizard_line in partial_return_wizard.line_ids:
  84. qty = -wizard_line.qty
  85. if qty != 0:
  86. copy_line = wizard_line.pos_order_line_id.copy()
  87. copy_line.write({
  88. 'order_id': new_order.id,
  89. 'returned_line_id': wizard_line.pos_order_line_id.id,
  90. 'qty': qty,
  91. })
  92. return res
  93. def action_pos_order_paid(self):
  94. if self.returned_order_id and self.returned_order_id.invoice_id:
  95. self._action_pos_order_invoice()
  96. return super(PosOrder, self).action_pos_order_paid()
  97. def _create_picking_return(self):
  98. self.ensure_one()
  99. picking = self.returned_order_id.picking_id
  100. ctx = dict(self.env.context,
  101. active_ids=picking.ids, active_id=picking.id)
  102. wizard = self.env['stock.return.picking'].with_context(ctx).create({})
  103. # Discard not returned lines
  104. wizard.product_return_moves.filtered(
  105. lambda x: x.product_id not in self.mapped(
  106. 'lines.product_id')).unlink()
  107. to_return = {}
  108. for product in self.lines.mapped('product_id'):
  109. to_return[product] = -sum(
  110. self.lines.filtered(
  111. lambda x: x.product_id == product).mapped('qty'))
  112. for move in wizard.product_return_moves:
  113. if to_return[move.product_id] < move.quantity:
  114. move.quantity = to_return[move.product_id]
  115. to_return[move.product_id] -= move.quantity
  116. return wizard
  117. def create_picking(self):
  118. """Odoo bases return picking if the quantities are negative, but it's
  119. not linked to the original one"""
  120. res = super(PosOrder, self.filtered(lambda x: not x.returned_order_id)
  121. ).create_picking()
  122. for order in self.filtered('returned_order_id'):
  123. wizard = order._create_picking_return()
  124. res = wizard.create_returns()
  125. order.write({'picking_id': res['res_id']})
  126. return res
  127. class PosOrderLine(models.Model):
  128. _inherit = 'pos.order.line'
  129. returned_line_id = fields.Many2one(
  130. comodel_name='pos.order.line',
  131. string='Returned Order',
  132. readonly=True,
  133. )
  134. refund_line_ids = fields.One2many(
  135. comodel_name='pos.order.line',
  136. inverse_name='returned_line_id',
  137. string='Refund Lines',
  138. readonly=True,
  139. )
  140. @api.model
  141. def max_returnable_qty(self, ignored_line_ids):
  142. qty = self.qty
  143. for refund_line in self.refund_line_ids:
  144. if refund_line.id not in ignored_line_ids:
  145. qty += refund_line.qty
  146. return qty
  147. @api.constrains('returned_line_id', 'qty')
  148. def _check_return_qty(self):
  149. if self.env.context.get('do_not_check_negative_qty', False):
  150. return True
  151. for line in self:
  152. if line.returned_line_id and -line.qty > line.returned_line_id.qty:
  153. raise ValidationError(_(
  154. "You can not return %d %s of %s because the original "
  155. "Order line only mentions %d %s."
  156. ) % (-line.qty, line.product_id.uom_id.name,
  157. line.product_id.name, line.returned_line_id.qty,
  158. line.product_id.uom_id.name))
  159. if (line.returned_line_id and
  160. -line.qty >
  161. line.returned_line_id.max_returnable_qty([line.id])):
  162. raise ValidationError(_(
  163. "You can not return %d %s of %s because some refunds"
  164. " has been yet done.\n Maximum quantity allowed :"
  165. " %d %s."
  166. ) % (-line.qty, line.product_id.uom_id.name,
  167. line.product_id.name,
  168. line.returned_line_id.max_returnable_qty([line.id]),
  169. line.product_id.uom_id.name))
  170. if (not line.returned_line_id and
  171. line.qty < 0 and not
  172. line.product_id.product_tmpl_id.pos_allow_negative_qty):
  173. raise ValidationError(_(
  174. "For legal and traceability reasons, you can not set a"
  175. " negative quantity (%d %s of %s), without using "
  176. "return wizard."
  177. ) % (line.qty, line.product_id.uom_id.name,
  178. line.product_id.name))