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.

77 lines
3.1 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. from odoo.exceptions import UserError
  6. class LoanEndOfYearOperation(models.TransientModel):
  7. _name = "loan.end.of.year.operation"
  8. operation_type = fields.Selection(
  9. [("eoy_operation", "End of year operation"),
  10. ("loan_due", "Loan payment account move lines")],
  11. required=True,
  12. string="Operation type"
  13. )
  14. ongoing_fy_id = fields.Many2one(
  15. comodel_name="account.fiscal.year",
  16. string="Ongoing fiscal year",
  17. required=True
  18. )
  19. due_date = fields.Date(
  20. string="Due date"
  21. )
  22. @api.multi
  23. def run(self):
  24. self.ensure_one()
  25. afy_obj = self.env["account.fiscal.year"]
  26. interest_line_obj = self.env["loan.interest.line"]
  27. loan_issues = self.env["loan.issue"].browse(
  28. self._context.get("active_ids")
  29. )
  30. last_fy_day = self.ongoing_fy_id.date_to
  31. next_fy = afy_obj.get_next_fiscal_year(last_fy_day)
  32. if self.ongoing_fy_id == "eoy_operation":
  33. if next_fy:
  34. interest_lines_loan = interest_line_obj.search([
  35. ("due_date", ">=", next_fy.date_from),
  36. ("due_date", "<=", next_fy.date_to),
  37. ("due_loan_amount", ">", 0),
  38. ("loan_issue_id", "in", loan_issues.ids),
  39. ("loan_reimbursment_move", "=", False)
  40. ])
  41. interest_lines_loan.generate_loan_due_fy(last_fy_day)
  42. interest_lines = interest_line_obj.search([
  43. ("due_date", ">=", next_fy.date_from),
  44. ("due_date", "<=", next_fy.date_to),
  45. ("interest", ">", 0),
  46. ("loan_issue_id", "in", loan_issues.ids),
  47. ("interest_closing_fy", "=", False),
  48. ("interest_opening_fy", "=", False)
  49. ])
  50. interest_lines.generate_interest_move_lines_fy(last_fy_day,
  51. next_fy)
  52. # interest_lines.write({"state": "scheduled"})
  53. # interest_lines_loan.write({"state": "scheduled"})
  54. (interest_lines + interest_lines_loan).write({"state":
  55. "scheduled"})
  56. if not interest_lines_loan and not interest_lines:
  57. raise UserError("There is no end of year account move"
  58. " lines to generate for the selected loan"
  59. " issue")
  60. elif self.ongoing_fy_id == "loan_due":
  61. interest_lines = interest_line_obj.search([
  62. ("due_date", ">=", next_fy.date_from),
  63. ("due_date", "<=", next_fy.date_to),
  64. ("interest", ">", 0),
  65. ("loan_issue_id", "in", loan_issues.ids),
  66. ("interest_closing_fy", "=", False),
  67. ("interest_opening_fy", "=", False)
  68. ])