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.

77 lines
2.5 KiB

  1. # Copyright (C) 2015-Today GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import Warning as UserError
  6. class PosPaymentChangeWizard(models.TransientModel):
  7. _name = "pos.payment.change.wizard"
  8. _description = "PoS Payment Change Wizard"
  9. # Column Section
  10. order_id = fields.Many2one(
  11. comodel_name="pos.order", string="Order", readonly=True
  12. )
  13. line_ids = fields.One2many(
  14. comodel_name="pos.payment.change.wizard.line",
  15. inverse_name="wizard_id",
  16. string="Payment Lines",
  17. )
  18. amount_total = fields.Float(string="Total", readonly=True)
  19. # View Section
  20. @api.model
  21. def default_get(self, fields):
  22. PosOrder = self.env["pos.order"]
  23. res = super().default_get(fields)
  24. order = PosOrder.browse(self._context.get("active_id"))
  25. res.update({"order_id": order.id})
  26. res.update({"amount_total": order.amount_total})
  27. return res
  28. # View section
  29. @api.multi
  30. def button_change_payment(self):
  31. self.ensure_one()
  32. order = self.order_id
  33. # Check if the total is correct
  34. total = 0
  35. for line in self.line_ids:
  36. total += line.amount
  37. if total != self.amount_total:
  38. raise UserError(
  39. _(
  40. "Differences between the two values for the POS"
  41. " Order '%s':\n\n"
  42. " * Total of all the new payments %s;\n"
  43. " * Total of the POS Order %s;\n\n"
  44. "Please change the payments."
  45. % (order.name, total, order.amount_total)
  46. )
  47. )
  48. # Change payment
  49. new_payments = [{
  50. "journal": line.new_journal_id.id,
  51. "amount": line.amount,
  52. "payment_date": fields.Date.context_today(self),
  53. } for line in self.line_ids]
  54. orders = order.change_payment(new_payments)
  55. if len(orders) == 1:
  56. # if policy is 'update', only close the pop up
  57. action = {'type': 'ir.actions.act_window_close'}
  58. else:
  59. # otherwise (refund policy), displays the 3 orders
  60. action = self.env.ref(
  61. "point_of_sale.action_pos_pos_form"
  62. ).read()[0]
  63. action['domain'] = [('id', 'in', orders.ids)]
  64. return action