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.

151 lines
4.6 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. from datetime import datetime
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import ValidationError
  7. class LoanIssueLine(models.Model):
  8. _name = "loan.issue.line"
  9. _description = "Loan Issue Line"
  10. _order = "date desc, id"
  11. @api.multi
  12. @api.depends("quantity", "face_value")
  13. def _compute_amount(self):
  14. for line in self:
  15. line.amount = line.face_value * line.quantity
  16. name = fields.Char(string="Reference")
  17. loan_issue_id = fields.Many2one(
  18. "loan.issue", string="Loan issue", required=True
  19. )
  20. interest_lines = fields.One2many(
  21. "loan.interest.line", "issue_line", string="Interest lines"
  22. )
  23. quantity = fields.Integer(string="quantity", required=True)
  24. face_value = fields.Monetary(
  25. related="loan_issue_id.face_value",
  26. currency_field="company_currency_id",
  27. store=True,
  28. readonly=True,
  29. )
  30. partner_id = fields.Many2one(
  31. "res.partner", string="Subscriber", required=True
  32. )
  33. date = fields.Date(
  34. string="Subscription date",
  35. default=lambda self: datetime.strftime(datetime.now(), "%Y-%m-%d"),
  36. required=True,
  37. )
  38. amount = fields.Monetary(
  39. string="Subscribed amount",
  40. currency_field="company_currency_id",
  41. compute="_compute_amount",
  42. store=True,
  43. )
  44. state = fields.Selection(
  45. [
  46. ("draft", "Draft"),
  47. ("subscribed", "Subscribed"),
  48. ("waiting", "Waiting payment"),
  49. ("paid", "paid"),
  50. ("cancelled", "Cancelled"),
  51. ("ended", "Ended"),
  52. ],
  53. string="State",
  54. required=True,
  55. default="draft",
  56. )
  57. company_currency_id = fields.Many2one(
  58. "res.currency",
  59. related="company_id.currency_id",
  60. string="Company Currency",
  61. readonly=True,
  62. )
  63. company_id = fields.Many2one(
  64. "res.company",
  65. related="loan_issue_id.company_id",
  66. string="Company",
  67. readonly=True,
  68. )
  69. def get_loan_sub_mail_template(self):
  70. return self.env.ref(
  71. "easy_my_coop_loan.loan_subscription_confirmation", False
  72. )
  73. def get_loan_pay_req_mail_template(self):
  74. return self.env.ref(
  75. "easy_my_coop_loan.loan_issue_payment_request", False
  76. )
  77. @api.model
  78. def create(self, vals):
  79. line = super(LoanIssueLine, self).create(vals)
  80. confirmation_mail_template = line.get_loan_sub_mail_template()
  81. confirmation_mail_template.send_mail(line.id)
  82. return line
  83. @api.multi
  84. def action_draft(self):
  85. if self.filtered(lambda l: l.state != "cancelled"):
  86. raise ValidationError(
  87. _("You can only set cancelled loans to draft")
  88. )
  89. self.write({"state": "draft"})
  90. @api.multi
  91. def action_validate(self):
  92. if self.filtered(lambda l: l.state != "draft"):
  93. raise ValidationError(_("You can only validate draft loans"))
  94. self.write({"state": "subscribed"})
  95. @api.multi
  96. def action_request_payment(self):
  97. if self.filtered(lambda l: l.state != "subscribed"):
  98. raise ValidationError(
  99. _("You can only request payment for validated loans")
  100. )
  101. for line in self:
  102. pay_req_mail_template = line.get_loan_pay_req_mail_template()
  103. pay_req_mail_template.send_mail(line.id)
  104. line.write({"state": "waiting"})
  105. @api.multi
  106. def action_cancel(self):
  107. allowed_states = ["draft", "subscribed", "waiting"]
  108. if self.filtered(lambda l: l.state not in allowed_states):
  109. raise ValidationError(
  110. _(
  111. "You can only cancel loans in states draft, "
  112. "subscribed or waiting for payment."
  113. )
  114. )
  115. self.write({"state": "cancelled"})
  116. @api.multi
  117. def get_confirm_paid_email_template(self):
  118. self.ensure_one()
  119. return self.env.ref(
  120. "easy_my_coop_loan.email_template_loan_confirm_paid"
  121. )
  122. @api.multi
  123. def action_paid(self):
  124. if self.filtered(lambda l: l.state != "waiting"):
  125. raise ValidationError(
  126. _("You can only park as paid loans waiting for payment")
  127. )
  128. for line in self:
  129. loan_email_template = line.get_confirm_paid_email_template()
  130. loan_email_template.sudo().send_mail(line.id, force_send=False)
  131. line.write({"state": "paid"})