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.

200 lines
7.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  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. interest_closing_fy = fields.Many2one(
  21. comodel_name="account.move",
  22. string="Interest closing fiscal year account move"
  23. )
  24. interest_opening_fy = fields.Many2one(
  25. comodel_name="account.move",
  26. string="Interest opening fiscal year account move"
  27. )
  28. @api.multi
  29. def get_move_line(self, move_id, partner=None):
  30. self.ensure_one()
  31. move_line = {
  32. "date_maturity": self.due_date,
  33. "date": self.due_date,
  34. "move_id": move_id.id,
  35. }
  36. if partner:
  37. move_line["partner_id"] = partner.id
  38. return move_line
  39. @api.multi
  40. def create_move(self, date=None):
  41. self.ensure_one()
  42. if date:
  43. due_date = date
  44. else:
  45. due_date = self.due_date
  46. return self.env["account.move"].create({
  47. "ref": self.name,
  48. "date": due_date,
  49. "journal_id": self.company_id.loan_journal.id,
  50. })
  51. @api.multi
  52. def generate_payment_move_lines(self):
  53. for line in self:
  54. if not self.loan_reimbursment_move:
  55. company = line.company_id
  56. move = line.create_move()
  57. debit_vals = line.get_move_line(move, line.partner_id)
  58. loaner_vals = line.get_move_line(move, line.partner_id)
  59. debit_vals["debit"] = line.interest
  60. debit_vals["account_id"] = company.interest_account.id
  61. if line.due_loan_amount > 0 and line.net_interest > 0:
  62. loaner_vals["credit"] = line.due_amount
  63. elif line.due_loan_amount > 0:
  64. loaner_vals["credit"] = line.due_loan_amount
  65. elif line.net_interest > 0:
  66. loaner_vals["credit"] = line.net_interest
  67. loaner_vals["account_id"] = company.loaner_account
  68. vals_list = [debit_vals, loaner_vals]
  69. if line.taxes_amount > 0:
  70. tax_vals = line.get_move_line(move)
  71. tax_vals["credit"] = line.taxes_amount
  72. tax_vals["account_id"] = company.tax_account.id
  73. vals_list.append(tax_vals)
  74. self.env["account.move.line"].create(vals_list)
  75. line.write({"loan_reimbursment_move": move.id})
  76. @api.multi
  77. def generate_interest_move_lines_fy(self, date, next_fy):
  78. aml_obj = self.env["account.move.line"]
  79. for line in self:
  80. if not self.interest_closing_fy:
  81. company = line.company_id
  82. # compute the prorata interest for the fiscal year
  83. prorata_date = line.due_date - relativedelta(years=-1)
  84. diff_days = (prorata_date - date).days
  85. days = line.issue_line.get_number_of_days(date.year)
  86. previous_interest = line.accrued_interest - line.interest
  87. prorata_interest = line.interest * (diff_days / days)
  88. interest_fy = previous_interest + prorata_interest
  89. # create interest closing account move lines
  90. close_fy_move = line.create_move(date)
  91. deb_vals = line.get_move_line(close_fy_move, line.partner_id)
  92. cred_vals = line.get_move_line(close_fy_move, line.partner_id)
  93. deb_vals["debit"] = interest_fy
  94. deb_vals["date"] = date
  95. deb_vals["account_id"] = company.interest_account.id
  96. cred_vals["credit"] = interest_fy
  97. cred_vals["date"] = date
  98. cred_vals["account_id"] = company.expense_account.id
  99. aml_obj.create([deb_vals, cred_vals])
  100. line.write({"interest_closing_fy": close_fy_move.id})
  101. # create interest opening account move lines
  102. opening_date = next_fy.date_from
  103. open_fy_move = line.create_move(opening_date)
  104. deb_vals = line.get_move_line(open_fy_move, line.partner_id)
  105. cred_vals = line.get_move_line(open_fy_move, line.partner_id)
  106. deb_vals["debit"] = interest_fy
  107. deb_vals["date"] = opening_date
  108. deb_vals["account_id"] = company.expense_account.id
  109. cred_vals["credit"] = interest_fy
  110. cred_vals["date"] = opening_date
  111. cred_vals["account_id"] = company.interest_account.id
  112. aml_obj.create([deb_vals, cred_vals])
  113. line.write({"interest_opening_fy": open_fy_move.id})
  114. @api.multi
  115. def generate_loan_due_fy(self, date):
  116. for line in self:
  117. if not self.loan_due_fy_move:
  118. company = line.company_id
  119. move = line.create_move(date)
  120. deb_vals = line.get_move_line(move, line.partner_id)
  121. cred_vals = line.get_move_line(move, line.partner_id)
  122. deb_vals["debit"] = line.due_loan_amount
  123. deb_vals["date"] = date
  124. deb_vals["account_id"] = company.debt_long_term_account.id
  125. cred_vals["credit"] = line.due_loan_amount
  126. cred_vals["date"] = date
  127. cred_vals["account_id"] = company.debt_long_term_fy_account.id
  128. self.env["account.move.line"].create([deb_vals, cred_vals])
  129. line.write({"loan_due_fy_move": move.id,
  130. "state": "due_fy"})
  131. @api.multi
  132. def generate_loan_due_now(self):
  133. for line in self:
  134. if line.loan_due_move:
  135. company = line.company_id
  136. move = line.create_move(fields.Date.today())
  137. debit_vals = line.get_move_line(move, line.partner_id)
  138. credit_vals = line.get_move_line(move, line.partner_id)
  139. debit_vals["debit"] = line.due_loan_amount
  140. debit_vals["account_id"] = company.debt_long_term_fy_account.id
  141. credit_vals["credit"] = line.due_loan_amount
  142. credit_vals["account_id"] = company.debt_long_term_due_account
  143. self.env["account.move.line"].create([debit_vals, credit_vals])
  144. line.write({"loan_due_move": move.id,
  145. "state": "due"})
  146. @api.model
  147. def _generate_payment_move(self):
  148. # TODO configure how many days before you want generate the move lines
  149. fy = self.env["account.fiscal.year"].get_next_fiscal_year()
  150. interest_lines = self.search([
  151. ('due_date', '>=', fy.date_from),
  152. ('due_date', '<=', fy.date_to),
  153. ('due_amount', '>', 0),
  154. ])
  155. interest_lines.generate_payment_move_lines()
  156. return True