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.

82 lines
2.9 KiB

  1. # Copyright (C) 2015 - Today: GRAP (http://www.grap.coop)
  2. # @author: Julien WESTE
  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 odoo import _, api, fields, models
  6. from odoo.tools import float_is_zero
  7. from odoo.exceptions import Warning as UserError
  8. class PosOrder(models.Model):
  9. _inherit = "pos.order"
  10. @api.multi
  11. def change_payment(self, payment_lines):
  12. """
  13. Change payment of a given order.
  14. payment_lines should be a list of data that are
  15. the argument of the Odoo Core function add_payment()
  16. Return a list of order ids, depending on the
  17. payment_change_policy of the related pos_config.
  18. """
  19. self.ensure_one()
  20. order_ids = [self.id]
  21. # Removing zero lines
  22. precision = self.pricelist_id.currency_id.decimal_places
  23. payment_lines = [
  24. x for x in payment_lines if not float_is_zero(
  25. x["amount"], precision_digits=precision)
  26. ]
  27. self._check_payment_change_allowed()
  28. if self.config_id.payment_change_policy == "update":
  29. self.statement_ids.with_context().unlink()
  30. # Create new payment
  31. for line in payment_lines:
  32. self.add_payment(line)
  33. elif self.config_id.payment_change_policy == "refund":
  34. # Refund order and mark it as paid
  35. # with same payment method as the original one
  36. refund_result = self.refund()
  37. refund_order = self.browse(refund_result["res_id"])
  38. for statement in self.statement_ids:
  39. refund_order.add_payment({
  40. "journal": statement.journal_id.id,
  41. "amount": - statement.amount,
  42. "payment_date": fields.Date.context_today(self),
  43. })
  44. refund_order.action_pos_order_paid()
  45. # Resale order and mark it as paid
  46. # with the new payment
  47. resale_order = self.copy()
  48. for line in payment_lines:
  49. resale_order.add_payment(line)
  50. resale_order.action_pos_order_paid()
  51. order_ids += [refund_order.id, resale_order.id]
  52. return order_ids
  53. @api.multi
  54. def _check_payment_change_allowed(self):
  55. """Return True if the user can change the payment of a POS, depending
  56. of the state of the current session."""
  57. closed_orders = self.filtered(lambda x: x.session_id.state == "closed")
  58. if len(closed_orders):
  59. raise UserError(
  60. _(
  61. "You can not change payments of the POS '%s' because"
  62. " the associated session '%s' has been closed!"
  63. % (
  64. ", ".join(closed_orders.mapped("name")),
  65. ", ".join(closed_orders.mapped("session_id.name")),
  66. )
  67. )
  68. )