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.

126 lines
4.2 KiB

  1. # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models
  4. class AgedPartnerBalanceReport(models.AbstractModel):
  5. _name = "report.account_financial_report.abstract_report"
  6. _description = "Abstract Report"
  7. @api.model
  8. def _get_move_lines_domain_not_reconciled(
  9. self, company_id, account_ids, partner_ids, only_posted_moves, date_from
  10. ):
  11. domain = [
  12. ("account_id", "in", account_ids),
  13. ("company_id", "=", company_id),
  14. ("reconciled", "=", False),
  15. ]
  16. if partner_ids:
  17. domain += [("partner_id", "in", partner_ids)]
  18. if only_posted_moves:
  19. domain += [("move_id.state", "=", "posted")]
  20. else:
  21. domain += [("move_id.state", "in", ["posted", "draft"])]
  22. if date_from:
  23. domain += [("date", ">", date_from)]
  24. return domain
  25. @api.model
  26. def _get_new_move_lines_domain(
  27. self, new_ml_ids, account_ids, company_id, partner_ids, only_posted_moves
  28. ):
  29. domain = [
  30. ("account_id", "in", account_ids),
  31. ("company_id", "=", company_id),
  32. ("id", "in", new_ml_ids),
  33. ]
  34. if partner_ids:
  35. domain += [("partner_id", "in", partner_ids)]
  36. if only_posted_moves:
  37. domain += [("move_id.state", "=", "posted")]
  38. else:
  39. domain += [("move_id.state", "in", ["posted", "draft"])]
  40. return domain
  41. def _recalculate_move_lines(
  42. self,
  43. move_lines,
  44. debit_ids,
  45. credit_ids,
  46. debit_amount,
  47. credit_amount,
  48. ml_ids,
  49. account_ids,
  50. company_id,
  51. partner_ids,
  52. only_posted_moves,
  53. ):
  54. debit_ids = set(debit_ids)
  55. credit_ids = set(credit_ids)
  56. in_credit_but_not_in_debit = credit_ids - debit_ids
  57. reconciled_ids = list(debit_ids) + list(in_credit_but_not_in_debit)
  58. reconciled_ids = set(reconciled_ids)
  59. ml_ids = set(ml_ids)
  60. new_ml_ids = reconciled_ids - ml_ids
  61. new_ml_ids = list(new_ml_ids)
  62. new_domain = self._get_new_move_lines_domain(
  63. new_ml_ids, account_ids, company_id, partner_ids, only_posted_moves
  64. )
  65. ml_fields = [
  66. "id",
  67. "name",
  68. "date",
  69. "move_id",
  70. "journal_id",
  71. "account_id",
  72. "partner_id",
  73. "amount_residual",
  74. "date_maturity",
  75. "ref",
  76. "debit",
  77. "credit",
  78. "reconciled",
  79. "currency_id",
  80. "amount_currency",
  81. "amount_residual_currency",
  82. ]
  83. new_move_lines = self.env["account.move.line"].search_read(
  84. domain=new_domain, fields=ml_fields
  85. )
  86. move_lines = move_lines + new_move_lines
  87. for move_line in move_lines:
  88. ml_id = move_line["id"]
  89. if ml_id in debit_ids:
  90. move_line["amount_residual"] += debit_amount[ml_id]
  91. if ml_id in credit_ids:
  92. move_line["amount_residual"] -= credit_amount[ml_id]
  93. return move_lines
  94. def _get_accounts_data(self, accounts_ids):
  95. accounts = self.env["account.account"].browse(accounts_ids)
  96. accounts_data = {}
  97. for account in accounts:
  98. accounts_data.update(
  99. {
  100. account.id: {
  101. "id": account.id,
  102. "code": account.code,
  103. "name": account.name,
  104. "hide_account": False,
  105. "group_id": account.group_id.id,
  106. "currency_id": account.currency_id or False,
  107. "currency_name": account.currency_id.name,
  108. "centralized": account.centralized,
  109. }
  110. }
  111. )
  112. return accounts_data
  113. def _get_journals_data(self, journals_ids):
  114. journals = self.env["account.journal"].browse(journals_ids)
  115. journals_data = {}
  116. for journal in journals:
  117. journals_data.update({journal.id: {"id": journal.id, "code": journal.code}})
  118. return journals_data