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.

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