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.

157 lines
5.3 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Houssine BAKKALI <houssine@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from dateutil.relativedelta import relativedelta
  5. from odoo import api, fields, models
  6. class LoanInterestLine(models.Model):
  7. _inherit = "loan.interest.line"
  8. loan_due_fy_move = fields.Many2one(
  9. comodel_name="account.move",
  10. string="Loan due this fiscal year account move"
  11. )
  12. loan_due_move = fields.Many2one(
  13. comodel_name="account.move",
  14. string="Loan due this fiscal year account move"
  15. )
  16. loan_reimbursment_move = fields.Many2one(
  17. comodel_name="account.move",
  18. string="Loan reimbursement account move"
  19. )
  20. @api.multi
  21. def get_move_line(self, move_id, partner=None):
  22. self.ensure_one()
  23. move_line = {
  24. "date_maturity": self.due_date,
  25. "date": self.due_date,
  26. "move_id": move_id,
  27. }
  28. if partner:
  29. move_line["partner_id"] = partner.id
  30. return move_line
  31. @api.multi
  32. def create_move(self, date=None):
  33. self.ensure_one()
  34. if date:
  35. due_date = date
  36. else:
  37. due_date = self.due_date
  38. return self.env["account.move"].create({
  39. "ref": self.loan_issue_id.reference,
  40. "date": due_date,
  41. "journal_id": self.company_id.loan_journal.id,
  42. })
  43. @api.multi
  44. def generate_payment_move_lines(self):
  45. for line in self:
  46. if not self.loan_reimbursment_move:
  47. company = line.company_id
  48. move = line.create_move()
  49. debit_vals = line.get_move_line(line.partner_id)
  50. loaner_vals = line.get_move_line(line.partner_id)
  51. debit_vals["debit"] = line.interest
  52. debit_vals["account_id"] = company.interest_account.id
  53. if line.due_loan_amount > 0 and line.net_interest > 0:
  54. loaner_vals["credit"] = line.due_amount
  55. elif line.due_loan_amount > 0:
  56. loaner_vals["credit"] = line.due_loan_amount
  57. elif line.net_interest > 0:
  58. loaner_vals["credit"] = line.net_interest
  59. loaner_vals["account_id"] = company.loaner_account
  60. vals_list = [debit_vals, loaner_vals]
  61. if line.taxes_amount > 0:
  62. tax_vals = self.get_move_line()
  63. tax_vals["credit"] = line.taxes_amount
  64. tax_vals["account_id"] = company.tax_account.id
  65. vals_list.append(tax_vals)
  66. self.env["account.move.line"].create(vals_list)
  67. line.write({"loan_reimbursment_move": move.id,
  68. "state": "scheduled"})
  69. @api.multi
  70. def generate_loan_due_fy(self, date):
  71. for line in self:
  72. if not self.loan_due_fy_move:
  73. company = line.line.company_id
  74. move = line.create_move(date)
  75. debit_vals = line.get_move_line(line.partner_id)
  76. credit_vals = line.get_move_line(line.partner_id)
  77. debit_vals["debit"] = line.due_loan_amount
  78. debit_vals["date"] = date
  79. debit_vals["account_id"] = company.debt_long_term_account.id
  80. credit_vals["credit"] = line.due_loan_amount
  81. credit_vals["credit"] = date
  82. credit_vals["account_id"] = company.debt_long_term_fy_account.id
  83. self.env["account.move.line"].create([debit_vals, credit_vals])
  84. line.write({"loan_due_fy_move": move.id,
  85. "state": "due_fy"})
  86. @api.multi
  87. def generate_loan_due_now(self):
  88. for line in self:
  89. if line.loan_due_move:
  90. company = line.company_id
  91. move = line.create_move(fields.Date.today())
  92. debit_vals = line.get_move_line(line)
  93. credit_vals = line.get_move_line(line.partner_id)
  94. debit_vals["debit"] = line.due_loan_amount
  95. debit_vals["account_id"] = company.debt_long_term_fy_account.id
  96. credit_vals["credit"] = line.due_loan_amount
  97. credit_vals["account_id"] = company.debt_long_term_due_account
  98. self.env["account.move.line"].create([debit_vals, credit_vals])
  99. line.write({"loan_due_move": move.id,
  100. "state": "due"})
  101. @api.model
  102. def _generate_move_line_debt_end_fy(self):
  103. fy = self.env["account.fiscal.year"].get_next_fiscal_year()
  104. date = fy.date_from - relativedelta(days=1)
  105. if fy:
  106. interest_lines = self.search([
  107. ('due_date', '>=', fy.date_from),
  108. ('due_date', '<=', fy.date_to),
  109. ('due_loan_amount', '>', 0),
  110. ])
  111. interest_lines.generate_loan_due_fy(date)
  112. @api.model
  113. def _generate_payment_move(self):
  114. # TODO configure how many days before you want generate the move lines
  115. fy = self.env["account.fiscal.year"].get_next_fiscal_year()
  116. interest_lines = self.search([
  117. ('due_date', '>=', fy.date_from),
  118. ('due_date', '<=', fy.date_to),
  119. ('due_amount', '>', 0),
  120. ])
  121. interest_lines.generate_payment_move_lines()
  122. return True