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.

103 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo 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 _prepare_globalization_move(self, journal_id):
  26. """Create the globalization move"""
  27. entries_vals = {
  28. 'journal_id': journal_id,
  29. 'date': fields.Date.today(),
  30. 'name': "%s - %s" % (
  31. self.name, _("Payment globalization")),
  32. }
  33. return self.env['account.move'].create(entries_vals)
  34. @api.model
  35. def _prepare_globalization_counterpart_line(self, debit, credit,
  36. account_id, move):
  37. """Create the globalization counterpart line"""
  38. item_vals = {
  39. 'name': _("Payment globalization counterpart"),
  40. 'credit': credit,
  41. 'debit': debit,
  42. 'account_id': account_id,
  43. 'move_id': move.id
  44. }
  45. return self.env['account.move.line'].with_context(
  46. {'check_move_validity': False}).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. # case of reverse account move line don't check move validity
  60. # because it will assert balanced move
  61. # that need : sum(debit) - sum(credit) must be gt 0
  62. return self.env['account.move.line'].with_context(
  63. {'check_move_validity': False}).create(item_vals)
  64. @api.multi
  65. def _generate_globalization_entries(self):
  66. """Generate globalization moves"""
  67. self.ensure_one()
  68. grouped_move_lines = self._get_move_lines_for_globalization()
  69. to_reconcile = []
  70. for key, lines in grouped_move_lines.iteritems():
  71. global_account_id, global_journal_id = key
  72. move = self._prepare_globalization_move(global_journal_id)
  73. counterpart_debit = 0.0
  74. counterpart_credit = 0.0
  75. for line in lines:
  76. counterpart_debit += line.debit
  77. counterpart_credit += line.credit
  78. new_line = self._create_reverse_line(line, move)
  79. # Pair to reconcile : payment line and the reverse line
  80. to_reconcile.append(line + new_line)
  81. if counterpart_debit:
  82. self._prepare_globalization_counterpart_line(
  83. counterpart_debit, 0.0, global_account_id, move)
  84. if counterpart_credit:
  85. self._prepare_globalization_counterpart_line(
  86. 0.0, counterpart_credit, global_account_id, move)
  87. for lines in to_reconcile:
  88. lines.reconcile()
  89. @api.multi
  90. def action_pos_session_closing_control(self):
  91. super(PosSession, self).action_pos_session_closing_control()
  92. for record in self:
  93. # Call the method to generate globalization entries
  94. record._generate_globalization_entries()