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.6 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. line.copy(
  66. {
  67. 'order_id': new_order.id,
  68. 'returned_line_id': line.id,
  69. 'qty': qty,
  70. }
  71. )
  72. return res
  73. def partial_refund(self, partial_return_wizard):
  74. ctx = dict(self.env.context, partial_refund=True)
  75. res = self.with_context(ctx).refund()
  76. new_order = self._blank_refund(res)
  77. for wizard_line in partial_return_wizard.line_ids:
  78. qty = -wizard_line.qty
  79. if qty != 0:
  80. copy_line = wizard_line.pos_order_line_id.copy(
  81. {
  82. 'order_id': new_order.id,
  83. 'returned_line_id': wizard_line.pos_order_line_id.id,
  84. 'qty': qty,
  85. }
  86. )
  87. copy_line._onchange_amount_line_all()
  88. new_order._onchange_amount_all()
  89. return res
  90. def action_pos_order_paid(self):
  91. res = super(PosOrder, self).action_pos_order_paid()
  92. if self.returned_order_id and self.returned_order_id.invoice_id:
  93. self._action_pos_order_invoice()
  94. return res
  95. def _create_picking_return(self):
  96. self.ensure_one()
  97. picking = self.returned_order_id.picking_id
  98. ctx = dict(self.env.context,
  99. active_ids=picking.ids, active_id=picking.id)
  100. wizard = self.env['stock.return.picking'].with_context(ctx).create({})
  101. # Discard not returned lines
  102. wizard.product_return_moves.filtered(
  103. lambda x: x.product_id not in self.mapped(
  104. 'lines.product_id')).unlink()
  105. to_return = {}
  106. for product in self.lines.mapped('product_id'):
  107. to_return[product] = -sum(
  108. self.lines.filtered(
  109. lambda x: x.product_id == product).mapped('qty'))
  110. for move in wizard.product_return_moves:
  111. if to_return[move.product_id] < move.quantity:
  112. move.quantity = to_return[move.product_id]
  113. to_return[move.product_id] -= move.quantity
  114. return wizard
  115. def create_picking(self):
  116. """Odoo bases return picking if the quantities are negative, but it's
  117. not linked to the original one"""
  118. orders = self.filtered(
  119. lambda x: not x.returned_order_id
  120. or not x.returned_order_id.picking_id)
  121. res = super(PosOrder, orders).create_picking()
  122. for order in self - orders:
  123. wizard = order._create_picking_return()
  124. res = wizard.create_returns()
  125. order.write({'picking_id': res['res_id']})
  126. order._force_picking_done(order.picking_id)
  127. return res
  128. class PosOrderLine(models.Model):
  129. _inherit = 'pos.order.line'
  130. returned_line_id = fields.Many2one(
  131. comodel_name='pos.order.line',
  132. string='Returned Order',
  133. readonly=True,
  134. )
  135. refund_line_ids = fields.One2many(
  136. comodel_name='pos.order.line',
  137. inverse_name='returned_line_id',
  138. string='Refund Lines',
  139. readonly=True,
  140. )
  141. @api.model
  142. def max_returnable_qty(self, ignored_line_ids):
  143. qty = self.qty
  144. for refund_line in self.refund_line_ids:
  145. if refund_line.id not in ignored_line_ids:
  146. qty += refund_line.qty
  147. return qty
  148. @api.constrains('returned_line_id', 'qty')
  149. def _check_return_qty(self):
  150. if self.env.context.get('do_not_check_negative_qty', False):
  151. return True
  152. for line in self:
  153. if line.returned_line_id and -line.qty > line.returned_line_id.qty:
  154. raise ValidationError(_(
  155. "You can not return %d %s of %s because the original "
  156. "Order line only mentions %d %s."
  157. ) % (-line.qty, line.product_id.uom_id.name,
  158. line.product_id.name, line.returned_line_id.qty,
  159. line.product_id.uom_id.name))
  160. if (line.returned_line_id and
  161. -line.qty >
  162. line.returned_line_id.max_returnable_qty([line.id])):
  163. raise ValidationError(_(
  164. "You can not return %d %s of %s because some refunds"
  165. " have already been done.\n Maximum quantity allowed :"
  166. " %d %s."
  167. ) % (-line.qty, line.product_id.uom_id.name,
  168. line.product_id.name,
  169. line.returned_line_id.max_returnable_qty([line.id]),
  170. line.product_id.uom_id.name))
  171. if (not line.returned_line_id and
  172. line.qty < 0 and not
  173. line.product_id.product_tmpl_id.pos_allow_negative_qty):
  174. raise ValidationError(_(
  175. "For legal and traceability reasons, you can not set a"
  176. " negative quantity (%d %s of %s), without using "
  177. "return wizard."
  178. ) % (line.qty, line.product_id.uom_id.name,
  179. line.product_id.name))