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.

72 lines
2.3 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2015-Today GRAP (http://www.grap.coop)
  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 _, api, fields, models
  6. from openerp.exceptions import Warning as UserError
  7. class PosChangePaymentsWizard(models.TransientModel):
  8. _name = 'pos.change.payments.wizard'
  9. # Column Section
  10. order_id = fields.Many2one(
  11. comodel_name='pos.order', string='Order', readonly=True)
  12. session_id = fields.Many2one(
  13. comodel_name='pos.session', string='Session', readonly=True)
  14. line_ids = fields.One2many(
  15. comodel_name='pos.change.payments.wizard.line',
  16. inverse_name='wizard_id', string='Payment Lines')
  17. amount_total = fields.Float(string='Total', readonly=True)
  18. # View Section
  19. @api.model
  20. def default_get(self, fields):
  21. order_obj = self.env['pos.order']
  22. res = super(PosChangePaymentsWizard, self).default_get(fields)
  23. order = order_obj.browse(self._context.get('active_id'))
  24. res.update({'order_id': order.id})
  25. res.update({'session_id': order.session_id.id})
  26. res.update({'amount_total': order.amount_total})
  27. return res
  28. # View section
  29. @api.multi
  30. def button_change_payments(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. "Differences between the two values for the POS"
  40. " Order '%s':\n\n"
  41. " * Total of all the new payments %s;\n"
  42. " * Total of the POS Order %s;\n\n"
  43. "Please change the payments." % (
  44. order.name, total, order.amount_total)))
  45. # Check if change payments is allowed
  46. order._allow_change_payments()
  47. # Remove old statements
  48. order.statement_ids.with_context(change_pos_payment=True).unlink()
  49. # Create new payment
  50. for line in self.line_ids:
  51. order.add_payment_v8({
  52. 'journal': line.new_journal_id.id,
  53. 'amount': line.amount,
  54. })
  55. return {
  56. 'type': 'ir.actions.client',
  57. 'tag': 'reload',
  58. }