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.

65 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2016-Today: La Louve (<http://www.lalouve.net/>)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import fields, models, api
  6. from openerp.exceptions import ValidationError
  7. from openerp.tools.translate import _
  8. class PosOrderLine(models.Model):
  9. _inherit = 'pos.order.line'
  10. # Column Section
  11. returned_line_id = fields.Many2one(
  12. comodel_name='pos.order.line', string='Returned Order',
  13. readonly=True)
  14. refund_line_ids = fields.One2many(
  15. comodel_name='pos.order.line', inverse_name='returned_line_id',
  16. string='Refund Lines', readonly=True)
  17. # Compute Section
  18. @api.model
  19. def max_returnable_qty(self, ignored_line_ids):
  20. qty = self.qty
  21. for refund_line in self.refund_line_ids:
  22. if refund_line.id not in ignored_line_ids:
  23. qty += refund_line.qty
  24. return qty
  25. # Constraint Section
  26. @api.one
  27. @api.constrains('returned_line_id', 'qty')
  28. def _check_return_qty(self):
  29. if self.env.context.get('do_not_check_negative_qty', False):
  30. return True
  31. if self.returned_line_id:
  32. if - self.qty > self.returned_line_id.qty:
  33. raise ValidationError(_(
  34. "You can not return %d %s of %s because the original"
  35. " Order line only mentions %d %s.") % (
  36. - self.qty, self.product_id.uom_id.name,
  37. self.product_id.name, self.returned_line_id.qty,
  38. self.product_id.uom_id.name))
  39. elif - self.qty >\
  40. self.returned_line_id.max_returnable_qty([self.id]):
  41. raise ValidationError(_(
  42. "You can not return %d %s of %s because some refunds"
  43. " has been yet done.\n Maximum quantity allowed :"
  44. " %d %s.") % (
  45. - self.qty, self.product_id.uom_id.name,
  46. self.product_id.name,
  47. self.returned_line_id.max_returnable_qty([self.id]),
  48. self.product_id.uom_id.name))
  49. else:
  50. if self.qty < 0 and\
  51. not self.product_id.product_tmpl_id.pos_allow_negative_qty:
  52. raise ValidationError(_(
  53. "For legal and traceability reasons, you can not set a"
  54. " negative quantity (%d %s of %s), without using return"
  55. " wizard.") % (
  56. self.qty, self.product_id.uom_id.name,
  57. self.product_id.name))