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.8 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Point Of Sale - Change Payment module for Odoo
  5. # Copyright (C) 2015-Today GRAP (http://www.grap.coop)
  6. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv import fields
  23. from openerp.osv.osv import except_osv
  24. from openerp.osv.orm import TransientModel
  25. from openerp.tools.translate import _
  26. class pos_change_payments_wizard(TransientModel):
  27. _name = 'pos.change.payments.wizard'
  28. # View Section
  29. def default_get(self, cr, uid, fields, context=None):
  30. po_obj = self.pool['pos.order']
  31. if context.get('active_model', False) != 'pos.order':
  32. raise except_osv(_('Error!'), _('Incorrect Call!'))
  33. res = super(pos_change_payments_wizard, self).default_get(
  34. cr, uid, fields, context=context)
  35. po = po_obj.browse(
  36. cr, uid, context.get('active_id'), context=context)
  37. res.update({'order_id': po.id})
  38. res.update({'amount_total': po.amount_total})
  39. return res
  40. # Column Section
  41. _columns = {
  42. 'order_id': fields.many2one(
  43. 'pos.order', 'POS Order', required=True, readonly=True),
  44. 'line_ids': fields.one2many(
  45. 'pos.change.payments.wizard.line', 'wizard_id', 'Wizard Lines'),
  46. 'amount_total': fields.float(
  47. 'Total', readonly=True),
  48. }
  49. # Action section
  50. def button_change_payments(self, cr, uid, ids, context=None):
  51. po_obj = self.pool['pos.order']
  52. absl_obj = self.pool['account.bank.statement.line']
  53. ctx = context.copy()
  54. ctx['change_pos_payment'] = True
  55. for pcpw in self.browse(cr, uid, ids, context=context):
  56. po = po_obj.browse(cr, uid, pcpw.order_id.id, context=context)
  57. # Check if the total is correct
  58. total = 0
  59. for line in pcpw.line_ids:
  60. total += line.amount
  61. if total != pcpw.amount_total:
  62. raise except_osv(
  63. _('Error!'),
  64. _("""Differences between the two values for the POS"""
  65. """ Order '%s':\n\n"""
  66. """ * Total of all the new payments %s;\n"""
  67. """ * Total of the POS Order %s;\n\n"""
  68. """Please change the payments.""" % (
  69. po.name, total, po.amount_total)))
  70. # Check if change payments is allowed
  71. po_obj._allow_change_payments(
  72. cr, uid, [po.id], context=context)
  73. # Remove old statements
  74. absl_obj.unlink(
  75. cr, uid, [x.id for x in po.statement_ids], context=ctx)
  76. # Create new payment
  77. for line in pcpw.line_ids:
  78. po_obj.add_payment(cr, uid, po.id, {
  79. 'journal': int(line.journal_id),
  80. 'amount': line.amount,
  81. }, context=context)
  82. return {
  83. 'type': 'ir.actions.client',
  84. 'tag': 'reload',
  85. }