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.

80 lines
3.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Point Of Sale - Change Payment module for Odoo
  5. # Copyright (C) 2013-Today GRAP (http://www.grap.coop)
  6. # @author Julien WESTE
  7. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  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.orm import Model
  23. from openerp.osv.osv import except_osv
  24. from openerp.tools.translate import _
  25. class pos_order(Model):
  26. _inherit = 'pos.order'
  27. def _merge_cash_payment(self, cr, uid, ids, context=None):
  28. absl_obj = self.pool['account.bank.statement.line']
  29. for po in self.browse(cr, uid, ids, context=context):
  30. absl_cash_ids = [
  31. x.id for x in po.statement_ids
  32. if x.statement_id.journal_id.type == 'cash']
  33. new_payments = {}
  34. for line in absl_obj.read(
  35. cr, uid, absl_cash_ids,
  36. ['statement_id', 'amount'],
  37. context=context):
  38. if line['statement_id'][0] in new_payments.keys():
  39. new_payments[line['statement_id'][0]] += line['amount']
  40. else:
  41. new_payments[line['statement_id'][0]] = line['amount']
  42. # Delete all obsolete account bank statement line
  43. absl_obj.unlink(cr, uid, absl_cash_ids, context=context)
  44. # Create a new ones
  45. for k, v in new_payments.items():
  46. self.add_payment(cr, uid, po.id, {
  47. 'statement_id': k,
  48. 'amount': v
  49. }, context=context)
  50. # Overload Section
  51. def action_paid(self, cr, uid, ids, context=None):
  52. """ Merge all cash statement line of the Order"""
  53. context = context or {}
  54. ctx = context.copy()
  55. ctx['change_pos_payment'] = True
  56. self._merge_cash_payment(cr, uid, ids, context=ctx)
  57. return super(pos_order, self).action_paid(
  58. cr, uid, ids, context=context)
  59. # Private Function Section
  60. def _allow_change_payments(
  61. self, cr, uid, ids, context=None):
  62. """Return True if the user can change the payment of a POS, depending
  63. of the state of the current session."""
  64. for po in self.browse(cr, uid, ids, context=context):
  65. if po.session_id.state == 'closed':
  66. raise except_osv(
  67. _('Error!'),
  68. _("""You can not change payments of the POS '%s' because"""
  69. """ the associated session '%s' has been closed!""" % (
  70. po.name, po.session_id.name)))
  71. return True