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.

123 lines
3.9 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 odoo import api, fields, models
  5. class LoanInterestLine(models.Model):
  6. _inherit = "loan.interest.line"
  7. loan_due_fy_move = fields.Many2one(
  8. comodel_name="account.move",
  9. string="Loan due this fiscal year account move"
  10. )
  11. loan_reimbursment_move = fields.Many2one(
  12. comodel_name="account.move",
  13. string="Loan reimbursement account move"
  14. )
  15. interest_payment_move = fields.Many2one(
  16. comodel_name="account.move",
  17. string="Interest payment account move"
  18. )
  19. @api.multi
  20. def get_move_line(self, move_id, partner=None):
  21. self.ensure_one()
  22. move_line = {
  23. "date_maturity": self.due_date,
  24. "date": self.due_date,
  25. "move_id": move_id,
  26. }
  27. if partner:
  28. move_line["partner_id"] = partner.id
  29. return move_line
  30. @api.multi
  31. def create_move(self, date=None):
  32. self.ensure_one()
  33. if date:
  34. due_date = date
  35. else:
  36. due_date = self.due_date
  37. return self.env["account.move"].create({
  38. "ref": self.loan_issue_id.reference,
  39. "date": due_date,
  40. "journal_id": self.company_id.loan_journal.id,
  41. })
  42. @api.multi
  43. def generate_interest_payement_move_lines(self):
  44. self.ensure_one()
  45. move_line_obj = self.env["account.move.line"]
  46. move = self.create_move()
  47. debit_vals = self.get_move_line(self.partner_id)
  48. loaner_vals = self.get_move_line(self.partner_id)
  49. debit_vals["debit"] = self.interest
  50. debit_vals["account_id"] = self.company_id.interest_account.id
  51. loaner_vals["credit"] = self.due_amount
  52. loaner_vals["account_id"] = self.company_id.loaner_account
  53. vals_list = [debit_vals, loaner_vals]
  54. if self.taxes_amount > 0:
  55. tax_vals = self.get_move_line()
  56. tax_vals["credit"] = self.taxes_amount
  57. tax_vals["account_id"] = self.company_id.tax_account.id
  58. vals_list.append(tax_vals)
  59. move_line_obj.create(vals_list)
  60. self.interest_payment_mpve = move
  61. @api.multi
  62. def generate_loan_due_fy(self):
  63. self.ensure_one()
  64. move_line_obj = self.env["account.move.line"]
  65. move = self.create_move(fields.Date.today())
  66. debit_vals = self.get_move_line(self.partner_id)
  67. credit_vals = self.get_move_line(self.partner_id)
  68. debit_vals["debit"] = self.due_amount
  69. debit_vals["account_id"] = self.company_id.debt_long_term_account.id
  70. credit_vals["credit"] = self.due_amount
  71. credit_vals["account_id"] = self.company_id.debt_long_term_fy_account
  72. move_line_obj.create([debit_vals, credit_vals])
  73. self.loan_due_fy_move = move
  74. @api.model
  75. def _generate_move_line_debt_end_fy(self):
  76. fy = self.env["account.fiscal.year"].get_next_fiscal_year()
  77. if fy:
  78. interest_lines = self.search([
  79. ('due_date', '>=', fy.date_from),
  80. ('due_date', '<=', fy.date_to),
  81. ('due_loan_amount', '>', 0),
  82. ])
  83. for line in interest_lines:
  84. if (not line.loan_reimbursment_move
  85. and not line.loan_due_fy_move):
  86. self.generate_loan_due_fy()
  87. @api.model
  88. def _generate_payment_move(self):
  89. # TODO configure how many days before you want generate the move lines
  90. interest_lines = self.search([
  91. ('due_date', '>=', fy.date_from),
  92. ('due_date', '<=', fy.date_to),
  93. ('due_amount', '>', 0),
  94. ])
  95. for line in interest_lines:
  96. if line.interest > 0 and not line.loan_reimbursment_move:
  97. line.generate_interest_payement_move_lines()
  98. return True