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.

194 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. orders = self.filtered(
  115. lambda x: not x.returned_order_id
  116. or not x.returned_order_id.picking_id)
  117. res = super(PosOrder, orders).create_picking()
  118. for order in self - orders:
  119. wizard = order._create_picking_return()
  120. res = wizard.create_returns()
  121. order.write({'picking_id': res['res_id']})
  122. order._force_picking_done(order.picking_id)
  123. return res
  124. class PosOrderLine(models.Model):
  125. _inherit = 'pos.order.line'
  126. returned_line_id = fields.Many2one(
  127. comodel_name='pos.order.line',
  128. string='Returned Order',
  129. readonly=True,
  130. )
  131. refund_line_ids = fields.One2many(
  132. comodel_name='pos.order.line',
  133. inverse_name='returned_line_id',
  134. string='Refund Lines',
  135. readonly=True,
  136. )
  137. @api.model
  138. def max_returnable_qty(self, ignored_line_ids):
  139. qty = self.qty
  140. for refund_line in self.refund_line_ids:
  141. if refund_line.id not in ignored_line_ids:
  142. qty += refund_line.qty
  143. return qty
  144. @api.constrains('returned_line_id', 'qty')
  145. def _check_return_qty(self):
  146. if self.env.context.get('do_not_check_negative_qty', False):
  147. return True
  148. for line in self:
  149. if line.returned_line_id and -line.qty > line.returned_line_id.qty:
  150. raise ValidationError(_(
  151. "You can not return %d %s of %s because the original "
  152. "Order line only mentions %d %s."
  153. ) % (-line.qty, line.product_id.uom_id.name,
  154. line.product_id.name, line.returned_line_id.qty,
  155. line.product_id.uom_id.name))
  156. if (line.returned_line_id and
  157. -line.qty >
  158. line.returned_line_id.max_returnable_qty([line.id])):
  159. raise ValidationError(_(
  160. "You can not return %d %s of %s because some refunds"
  161. " have already been done.\n Maximum quantity allowed :"
  162. " %d %s."
  163. ) % (-line.qty, line.product_id.uom_id.name,
  164. line.product_id.name,
  165. line.returned_line_id.max_returnable_qty([line.id]),
  166. line.product_id.uom_id.name))
  167. if (not line.returned_line_id and
  168. line.qty < 0 and not
  169. line.product_id.product_tmpl_id.pos_allow_negative_qty):
  170. raise ValidationError(_(
  171. "For legal and traceability reasons, you can not set a"
  172. " negative quantity (%d %s of %s), without using "
  173. "return wizard."
  174. ) % (line.qty, line.product_id.uom_id.name,
  175. line.product_id.name))