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.

142 lines
4.8 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 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_due_move = fields.Many2one(
  12. comodel_name="account.move",
  13. string="Loan due this fiscal year account move"
  14. )
  15. loan_reimbursment_move = fields.Many2one(
  16. comodel_name="account.move",
  17. string="Loan reimbursement 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_payment_move_lines(self):
  44. for line in self:
  45. if not self.loan_reimbursment_move:
  46. company = line.company_id
  47. move = line.create_move()
  48. debit_vals = line.get_move_line(line.partner_id)
  49. loaner_vals = line.get_move_line(line.partner_id)
  50. debit_vals["debit"] = line.interest
  51. debit_vals["account_id"] = company.interest_account.id
  52. if line.due_loan_amount > 0 and line.net_interest > 0:
  53. loaner_vals["credit"] = line.due_amount
  54. elif line.due_loan_amount > 0:
  55. loaner_vals["credit"] = line.due_loan_amount
  56. elif line.net_interest > 0:
  57. loaner_vals["credit"] = line.net_interest
  58. loaner_vals["account_id"] = company.loaner_account
  59. vals_list = [debit_vals, loaner_vals]
  60. if line.taxes_amount > 0:
  61. tax_vals = self.get_move_line()
  62. tax_vals["credit"] = line.taxes_amount
  63. tax_vals["account_id"] = company.tax_account.id
  64. vals_list.append(tax_vals)
  65. self.env["account.move.line"].create(vals_list)
  66. line.write({"loan_reimbursment_move": move.id,
  67. "state": "scheduled"})
  68. @api.multi
  69. def generate_loan_due_fy(self, date):
  70. for line in self:
  71. if not self.loan_due_fy_move:
  72. company = line.line.company_id
  73. move = line.create_move(date)
  74. deb_vals = line.get_move_line(line.partner_id)
  75. cred_vals = line.get_move_line(line.partner_id)
  76. deb_vals["debit"] = line.due_loan_amount
  77. deb_vals["date"] = date
  78. deb_vals["account_id"] = company.debt_long_term_account.id
  79. cred_vals["credit"] = line.due_loan_amount
  80. cred_vals["credit"] = date
  81. cred_vals["account_id"] = company.debt_long_term_fy_account.id
  82. self.env["account.move.line"].create([deb_vals, cred_vals])
  83. line.write({"loan_due_fy_move": move.id,
  84. "state": "due_fy"})
  85. @api.multi
  86. def generate_loan_due_now(self):
  87. for line in self:
  88. if line.loan_due_move:
  89. company = line.company_id
  90. move = line.create_move(fields.Date.today())
  91. debit_vals = line.get_move_line(line)
  92. credit_vals = line.get_move_line(line.partner_id)
  93. debit_vals["debit"] = line.due_loan_amount
  94. debit_vals["account_id"] = company.debt_long_term_fy_account.id
  95. credit_vals["credit"] = line.due_loan_amount
  96. credit_vals["account_id"] = company.debt_long_term_due_account
  97. self.env["account.move.line"].create([debit_vals, credit_vals])
  98. line.write({"loan_due_move": move.id,
  99. "state": "due"})
  100. @api.model
  101. def _generate_payment_move(self):
  102. # TODO configure how many days before you want generate the move lines
  103. fy = self.env["account.fiscal.year"].get_next_fiscal_year()
  104. interest_lines = self.search([
  105. ('due_date', '>=', fy.date_from),
  106. ('due_date', '<=', fy.date_to),
  107. ('due_amount', '>', 0),
  108. ])
  109. interest_lines.generate_payment_move_lines()
  110. return True