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.

102 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api, _
  5. from collections import defaultdict
  6. class PosSession(models.Model):
  7. _inherit = 'pos.session'
  8. @api.multi
  9. def _get_move_lines_for_globalization(self):
  10. """ Get all move lines for globalization by journal-account"""
  11. self.ensure_one()
  12. grouped_move_lines = defaultdict(list)
  13. for st in self.statement_ids:
  14. if st.journal_id.pos_payment_globalization:
  15. # One move per journal and account combination
  16. key = (st.journal_id.pos_payment_globalization_account.id,
  17. st.journal_id.pos_payment_globalization_journal.id)
  18. debit_account_id =\
  19. st.journal_id.default_debit_account_id.id
  20. lines = st.move_line_ids.filtered(
  21. lambda r: r.account_id.id == debit_account_id)
  22. grouped_move_lines[key].extend(lines)
  23. return grouped_move_lines
  24. @api.model
  25. def _create_globalization_move(self, journal_id, period_id):
  26. """Create the globalization move"""
  27. entries_vals = {
  28. 'journal_id': journal_id,
  29. 'period_id': period_id,
  30. 'date': fields.Date.today(),
  31. 'name': "%s - %s" % (
  32. self.name, _("Payment globalization")),
  33. }
  34. return self.env['account.move'].create(entries_vals)
  35. @api.model
  36. def _create_globalization_counterpart_line(self, debit, credit, account_id,
  37. move):
  38. """Create the globalization counterpart line"""
  39. item_vals = {
  40. 'name': _("Payment globalization counterpart"),
  41. 'credit': credit,
  42. 'debit': debit,
  43. 'account_id': account_id,
  44. 'move_id': move.id
  45. }
  46. return self.env['account.move.line'].create(item_vals)
  47. @api.model
  48. def _create_reverse_line(self, line_to_reverse, move):
  49. """Create move line the reverse payment line in entries
  50. genereted by pos"""
  51. item_vals = {
  52. 'name': "%s - %s" % (
  53. line_to_reverse.name, _("Payment globalization")),
  54. 'credit': line_to_reverse.debit,
  55. 'debit': line_to_reverse.credit,
  56. 'account_id': line_to_reverse.account_id.id,
  57. 'move_id': move.id
  58. }
  59. return self.env['account.move.line'].create(item_vals)
  60. @api.multi
  61. def _generate_globalization_entries(self):
  62. """Generate globalization moves"""
  63. self.ensure_one()
  64. grouped_move_lines = self._get_move_lines_for_globalization()
  65. to_reconcile = []
  66. period = self.env['account.period'].find()
  67. for key, lines in grouped_move_lines.iteritems():
  68. global_account_id, global_journal_id = key
  69. move = self._create_globalization_move(global_journal_id,
  70. period.id)
  71. counterpart_debit = 0.0
  72. counterpart_credit = 0.0
  73. for line in lines:
  74. counterpart_debit += line.debit
  75. counterpart_credit += line.credit
  76. new_line = self._create_reverse_line(line, move)
  77. # Pair to reconcile : payment line and the reverse line
  78. to_reconcile.append(line + new_line)
  79. if counterpart_debit:
  80. self._create_globalization_counterpart_line(
  81. counterpart_debit, 0.0, global_account_id, move)
  82. if counterpart_credit:
  83. self._create_globalization_counterpart_line(
  84. 0.0, counterpart_credit, global_account_id, move)
  85. for lines in to_reconcile:
  86. lines.reconcile()
  87. @api.multi
  88. def wkf_action_close(self):
  89. res = super(PosSession, self).wkf_action_close()
  90. for record in self:
  91. # Call the method to generate globalization entries
  92. record._generate_globalization_entries()
  93. return res