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.

98 lines
3.3 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. old_line_ids = fields.One2many(
  14. comodel_name="pos.payment.change.wizard.old.line",
  15. inverse_name="wizard_id",
  16. string="Old Payment Lines",
  17. readonly=True,
  18. )
  19. new_line_ids = fields.One2many(
  20. comodel_name="pos.payment.change.wizard.new.line",
  21. inverse_name="wizard_id",
  22. string="New Payment Lines",
  23. )
  24. amount_total = fields.Float(string="Total", readonly=True)
  25. # View Section
  26. @api.model
  27. def default_get(self, fields):
  28. PosOrder = self.env["pos.order"]
  29. res = super().default_get(fields)
  30. order = PosOrder.browse(self._context.get("active_id"))
  31. old_lines_vals = []
  32. for statement_line in order.statement_ids:
  33. old_lines_vals.append((0, 0, {
  34. "old_journal_id": statement_line.statement_id.journal_id.id,
  35. "amount": statement_line.amount
  36. }
  37. ))
  38. res.update({
  39. "order_id": order.id,
  40. "amount_total": order.amount_total,
  41. "old_line_ids": old_lines_vals,
  42. })
  43. return res
  44. # View section
  45. @api.multi
  46. def button_change_payment(self):
  47. self.ensure_one()
  48. order = self.order_id
  49. # Check if the total is correct
  50. total = sum(self.mapped("new_line_ids.amount"))
  51. if total != self.amount_total:
  52. raise UserError(
  53. _(
  54. "Differences between the two values for the POS"
  55. " Order '%s':\n\n"
  56. " * Total of all the new payments %s;\n"
  57. " * Total of the POS Order %s;\n\n"
  58. "Please change the payments."
  59. % (order.name, total, order.amount_total)
  60. )
  61. )
  62. # Change payment
  63. new_payments = [{
  64. "journal": line.new_journal_id.id,
  65. "amount": line.amount,
  66. "payment_date": fields.Date.context_today(self),
  67. } for line in self.new_line_ids]
  68. orders = order.change_payment(new_payments)
  69. # Note. Because of the poor design of the closing session process
  70. # in Odoo, we call _check_pos_session_balance() that sets
  71. # balance_end_real with balance_end for "non cash" journals
  72. if order.session_id.state == "closing_control":
  73. order.session_id._check_pos_session_balance()
  74. if len(orders) == 1:
  75. # if policy is 'update', only close the pop up
  76. action = {'type': 'ir.actions.act_window_close'}
  77. else:
  78. # otherwise (refund policy), displays the 3 orders
  79. action = self.env.ref(
  80. "point_of_sale.action_pos_pos_form"
  81. ).read()[0]
  82. action['domain'] = [('id', 'in', orders.ids)]
  83. return action