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