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.

178 lines
7.5 KiB

  1. # Copyright 2019 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. import logging
  5. from odoo import api, fields, models, _
  6. _logger = logging.getLogger(__name__)
  7. class LoanIssue(models.Model):
  8. _name = 'loan.issue'
  9. _description = 'Loan Issue'
  10. @api.multi
  11. def _compute_subscribed_amount(self):
  12. for issue in self:
  13. susbscribed_amount = 0.0
  14. for line in issue.loan_issue_lines.filtered(
  15. lambda record: record.state != 'cancelled'):
  16. susbscribed_amount += line.amount
  17. issue.subscribed_amount = susbscribed_amount
  18. name = fields.Char(string="Name",
  19. translate=True)
  20. default_issue = fields.Boolean(string="Default issue")
  21. subscription_start_date = fields.Date(string="Start date subscription period")
  22. subscription_end_date = fields.Date(string="End date subscription period")
  23. user_id = fields.Many2one('res.users',
  24. string="Responsible")
  25. loan_start_date = fields.Date(string="Loan start date")
  26. term_date = fields.Date(string="Term date")
  27. loan_term = fields.Float(string="Duration of the loan in month")
  28. rate = fields.Float(string="Interest rate")
  29. face_value = fields.Monetary(string="Facial value",
  30. currency_field='company_currency_id',
  31. required=True)
  32. minimum_amount = fields.Monetary(string="Minimum amount of issue",
  33. currency_field='company_currency_id')
  34. maximum_amount = fields.Monetary(string="Maximum amount of issue",
  35. currency_field='company_currency_id')
  36. min_amount_company = fields.Monetary(string="Minimum amount for a company",
  37. currency_field='company_currency_id')
  38. max_amount_company = fields.Monetary(string="Maximum amount for a company",
  39. currency_field='company_currency_id')
  40. min_amount_person = fields.Monetary(string="Minimum amount for a person",
  41. currency_field='company_currency_id')
  42. max_amount_person = fields.Monetary(string="Maximum amount for a person",
  43. currency_field='company_currency_id')
  44. subscribed_amount = fields.Monetary(string="Subscribed amount",
  45. compute="_compute_subscribed_amount",
  46. currency_field='company_currency_id')
  47. interest_payment = fields.Selection([('end', 'End'),
  48. ('yearly', 'Yearly')],
  49. string="Interest payment")
  50. interest_payment_info = fields.Char(string="Yearly payment on")
  51. loan_issue_lines = fields.One2many('loan.issue.line',
  52. 'loan_issue_id',
  53. string="Loan issue lines")
  54. state = fields.Selection([('draft', 'Draft'),
  55. ('confirmed', 'Confirmed'),
  56. ('cancelled', 'Cancelled'),
  57. ('ongoing', 'Ongoing'),
  58. ('closed', 'Closed')],
  59. string="State",
  60. default='draft')
  61. company_currency_id = fields.Many2one('res.currency',
  62. related='company_id.currency_id',
  63. string="Company Currency",
  64. readonly=True)
  65. company_id = fields.Many2one('res.company',
  66. string='Company',
  67. required=True,
  68. readonly=True,
  69. default=lambda self: self.env['res.company']._company_default_get()) # noqa
  70. by_company = fields.Boolean(string="By company")
  71. by_individual = fields.Boolean(string='By individuals')
  72. display_on_website = fields.Boolean(sting='Display on website')
  73. taxes_rate = fields.Float(string="Taxes on interest",
  74. required=True)
  75. @api.multi
  76. def get_max_amount(self, partner):
  77. lines = self.loan_issue_lines.filtered(
  78. lambda r: r.partner_id == partner and r.state != 'cancelled')
  79. already_subscribed = sum(line.amount for line in lines)
  80. if partner.is_company:
  81. max_amount = self.max_amount_company - already_subscribed
  82. else:
  83. max_amount = self.max_amount_person - already_subscribed
  84. return max_amount
  85. @api.multi
  86. def get_web_issues(self, is_company):
  87. bond_issues = self.search([
  88. ('display_on_website', '=', True),
  89. ('state', '=', 'ongoing')
  90. ])
  91. if is_company is True:
  92. return bond_issues.filtered('by_company')
  93. else:
  94. return bond_issues.filtered('by_company')
  95. @api.multi
  96. def action_confirm(self):
  97. self.ensure_one()
  98. self.write({'state': 'confirmed'})
  99. @api.multi
  100. def action_open(self):
  101. self.ensure_one()
  102. self.write({'state': 'ongoing'})
  103. @api.multi
  104. def action_draft(self):
  105. self.ensure_one()
  106. self.write({'state': 'draft'})
  107. @api.multi
  108. def action_cancel(self):
  109. self.ensure_one()
  110. self.write({'state': 'cancelled'})
  111. @api.multi
  112. def action_close(self):
  113. self.ensure_one()
  114. self.write({'state': 'closed'})
  115. def get_interest_vals(self, line, vals):
  116. interest_obj = self.env['loan.interest.line']
  117. accrued_amount = line.amount
  118. accrued_interest = 0
  119. accrued_net_interest = 0
  120. accrued_taxes = 0
  121. for year in range(1, int(self.loan_term) + 1):
  122. interest = accrued_amount * (line.loan_issue_id.rate / 100)
  123. accrued_amount += interest
  124. taxes_amount = interest * (self.taxes_rate / 100)
  125. net_interest = interest - taxes_amount
  126. accrued_interest += interest
  127. accrued_net_interest += net_interest
  128. accrued_taxes += taxes_amount
  129. vals['interest'] = interest
  130. vals['net_interest'] = net_interest
  131. vals['taxes_amount'] = taxes_amount
  132. vals['accrued_amount'] = accrued_amount
  133. vals['accrued_interest'] = accrued_interest
  134. vals['accrued_net_interest'] = accrued_net_interest
  135. vals['accrued_taxes'] = accrued_taxes
  136. vals['name'] = year
  137. interest_obj.create(vals)
  138. @api.multi
  139. def compute_loan_interest(self):
  140. self.ensure_one()
  141. if self.interest_payment == 'end':
  142. due_date = self.term_date
  143. else:
  144. raise NotImplementedError(_("Interest payment by year hasn't been "
  145. "implemented yet"))
  146. for line in self.loan_issue_lines:
  147. # TODO remove this line
  148. line.interest_lines.unlink()
  149. # Please Do not Forget
  150. vals = {
  151. 'issue_line': line.id,
  152. 'due_date': due_date,
  153. 'taxes_rate': self.taxes_rate
  154. }
  155. self.get_interest_vals(line, vals)
  156. rounded_term = int(self.loan_term)
  157. if self.loan_term - rounded_term > 0:
  158. # TODO Handle this case
  159. raise NotImplementedError(_("Calculation on non entire year "
  160. "hasn't been implemented yet"))