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.

192 lines
7.4 KiB

  1. # Copyright 2016-2018 Sylvain LE GAL (https://twitter.com/legalsylvain)
  2. # Copyright 2018 David Vidal <david.vidal@tecnativa.com>
  3. # Copyright 2018 Lambda IS DOOEL <https://www.lambda-is.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.invoice_id:
  44. return res
  45. res.update({
  46. 'origin': self.returned_order_id.invoice_id.number,
  47. 'name': _(
  48. 'Return of %s' % self.returned_order_id.invoice_id.number),
  49. 'refund_invoice_id': self.returned_order_id.invoice_id.id,
  50. })
  51. return res
  52. def _action_pos_order_invoice(self):
  53. """Wrap common process"""
  54. self.action_pos_order_invoice()
  55. self.invoice_id.sudo().action_invoice_open()
  56. self.account_move = self.invoice_id.move_id
  57. def refund(self):
  58. # Call super to use original refund algorithm (session management, ...)
  59. ctx = dict(self.env.context, do_not_check_negative_qty=True)
  60. res = super(PosOrder, self.with_context(ctx)).refund()
  61. new_order = self._blank_refund(res)
  62. for line in self.lines:
  63. qty = - line.max_returnable_qty([])
  64. if qty != 0:
  65. copy_line = line.copy()
  66. copy_line.write({
  67. 'order_id': new_order.id,
  68. 'returned_line_id': line.id,
  69. 'qty': qty,
  70. })
  71. return res
  72. def partial_refund(self, partial_return_wizard):
  73. ctx = dict(self.env.context, partial_refund=True)
  74. res = self.with_context(ctx).refund()
  75. new_order = self._blank_refund(res)
  76. for wizard_line in partial_return_wizard.line_ids:
  77. qty = -wizard_line.qty
  78. if qty != 0:
  79. copy_line = wizard_line.pos_order_line_id.copy()
  80. copy_line.write({
  81. 'order_id': new_order.id,
  82. 'returned_line_id': wizard_line.pos_order_line_id.id,
  83. 'qty': qty,
  84. })
  85. return res
  86. def action_pos_order_paid(self):
  87. res = super(PosOrder, self).action_pos_order_paid()
  88. if self.returned_order_id and self.returned_order_id.invoice_id:
  89. self._action_pos_order_invoice()
  90. return res
  91. def _create_picking_return(self):
  92. self.ensure_one()
  93. picking = self.returned_order_id.picking_id
  94. ctx = dict(self.env.context,
  95. active_ids=picking.ids, active_id=picking.id)
  96. wizard = self.env['stock.return.picking'].with_context(ctx).create({})
  97. # Discard not returned lines
  98. wizard.product_return_moves.filtered(
  99. lambda x: x.product_id not in self.mapped(
  100. 'lines.product_id')).unlink()
  101. to_return = {}
  102. for product in self.lines.mapped('product_id'):
  103. to_return[product] = -sum(
  104. self.lines.filtered(
  105. lambda x: x.product_id == product).mapped('qty'))
  106. for move in wizard.product_return_moves:
  107. if to_return[move.product_id] < move.quantity:
  108. move.quantity = to_return[move.product_id]
  109. to_return[move.product_id] -= move.quantity
  110. return wizard
  111. def create_picking(self):
  112. """Odoo bases return picking if the quantities are negative, but it's
  113. not linked to the original one"""
  114. res = super(PosOrder, self.filtered(lambda x: not x.returned_order_id)
  115. ).create_picking()
  116. for order in self.filtered('returned_order_id'):
  117. wizard = order._create_picking_return()
  118. res = wizard.create_returns()
  119. order.write({'picking_id': res['res_id']})
  120. order._force_picking_done(order.picking_id)
  121. return res
  122. class PosOrderLine(models.Model):
  123. _inherit = 'pos.order.line'
  124. returned_line_id = fields.Many2one(
  125. comodel_name='pos.order.line',
  126. string='Returned Order',
  127. readonly=True,
  128. )
  129. refund_line_ids = fields.One2many(
  130. comodel_name='pos.order.line',
  131. inverse_name='returned_line_id',
  132. string='Refund Lines',
  133. readonly=True,
  134. )
  135. @api.model
  136. def max_returnable_qty(self, ignored_line_ids):
  137. qty = self.qty
  138. for refund_line in self.refund_line_ids:
  139. if refund_line.id not in ignored_line_ids:
  140. qty += refund_line.qty
  141. return qty
  142. @api.constrains('returned_line_id', 'qty')
  143. def _check_return_qty(self):
  144. if self.env.context.get('do_not_check_negative_qty', False):
  145. return True
  146. for line in self:
  147. if line.returned_line_id and -line.qty > line.returned_line_id.qty:
  148. raise ValidationError(_(
  149. "You can not return %d %s of %s because the original "
  150. "Order line only mentions %d %s."
  151. ) % (-line.qty, line.product_id.uom_id.name,
  152. line.product_id.name, line.returned_line_id.qty,
  153. line.product_id.uom_id.name))
  154. if (line.returned_line_id and
  155. -line.qty >
  156. line.returned_line_id.max_returnable_qty([line.id])):
  157. raise ValidationError(_(
  158. "You can not return %d %s of %s because some refunds"
  159. " have already been done.\n Maximum quantity allowed :"
  160. " %d %s."
  161. ) % (-line.qty, line.product_id.uom_id.name,
  162. line.product_id.name,
  163. line.returned_line_id.max_returnable_qty([line.id]),
  164. line.product_id.uom_id.name))
  165. if (not line.returned_line_id and
  166. line.qty < 0 and not
  167. line.product_id.product_tmpl_id.pos_allow_negative_qty):
  168. raise ValidationError(_(
  169. "For legal and traceability reasons, you can not set a"
  170. " negative quantity (%d %s of %s), without using "
  171. "return wizard."
  172. ) % (line.qty, line.product_id.uom_id.name,
  173. line.product_id.name))