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.

135 lines
3.8 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. payment_date = fields.Date(
  38. string="Payment date")
  39. amount = fields.Monetary(
  40. string="Subscribed amount",
  41. currency_field="company_currency_id",
  42. compute="_compute_amount",
  43. store=True,
  44. )
  45. state = fields.Selection(
  46. [
  47. ("draft", "Draft"),
  48. ("subscribed", "Subscribed"),
  49. ("waiting", "Waiting payment"),
  50. ("paid", "paid"),
  51. ("cancelled", "Cancelled"),
  52. ("ended", "Ended"),
  53. ],
  54. string="State",
  55. required=True,
  56. default="draft",
  57. )
  58. company_currency_id = fields.Many2one(
  59. "res.currency",
  60. related="company_id.currency_id",
  61. string="Company Currency",
  62. readonly=True,
  63. )
  64. company_id = fields.Many2one(
  65. "res.company",
  66. related="loan_issue_id.company_id",
  67. string="Company",
  68. readonly=True,
  69. )
  70. def get_loan_sub_mail_template(self):
  71. return self.env.ref(
  72. "easy_my_coop_loan.loan_subscription_confirmation", False
  73. )
  74. def get_loan_pay_req_mail_template(self):
  75. return self.env.ref(
  76. "easy_my_coop_loan.loan_issue_payment_request", False
  77. )
  78. @api.model
  79. def create(self, vals):
  80. line = super(LoanIssueLine, self).create(vals)
  81. confirmation_mail_template = line.get_loan_sub_mail_template()
  82. confirmation_mail_template.send_mail(line.id)
  83. return line
  84. @api.multi
  85. def action_draft(self):
  86. for line in self:
  87. line.write({"state": "draft"})
  88. @api.multi
  89. def action_validate(self):
  90. for line in self:
  91. line.write({"state": "subscribed"})
  92. @api.multi
  93. def action_request_payment(self):
  94. pay_req_mail_template = self.get_loan_pay_req_mail_template()
  95. for line in self:
  96. pay_req_mail_template.send_mail(line.id)
  97. line.write({"state": "waiting"})
  98. @api.multi
  99. def action_cancel(self):
  100. for line in self:
  101. line.write({"state": "cancelled"})
  102. @api.multi
  103. def get_confirm_paid_email_template(self):
  104. self.ensure_one()
  105. return self.env.ref(
  106. "easy_my_coop_loan.email_template_loan_confirm_paid"
  107. )
  108. @api.multi
  109. def action_paid(self):
  110. for line in self:
  111. loan_email_template = self.get_confirm_paid_email_template()
  112. loan_email_template.sudo().send_mail(line.id, force_send=False)
  113. vals = {"state": "paid"}
  114. if not line.payment_date:
  115. vals["payement_date"] = fields.Date.today()
  116. line.write(vals)