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.

106 lines
4.0 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('loan.issue',
  17. string="Loan issue",
  18. required=True)
  19. interest_lines = fields.One2many('loan.interest.line',
  20. 'issue_line',
  21. string="Interest lines")
  22. quantity = fields.Integer(string='quantity',
  23. required=True)
  24. face_value = fields.Monetary(related='loan_issue_id.face_value',
  25. currency_field='company_currency_id',
  26. store=True,
  27. readonly=True)
  28. partner_id = fields.Many2one('res.partner',
  29. string="Subscriber",
  30. required=True)
  31. date = fields.Date(string="Subscription date",
  32. default=lambda self: datetime.strftime(datetime.now(),
  33. '%Y-%m-%d'),
  34. required=True)
  35. amount = fields.Monetary(string="Subscribed amount",
  36. currency_field='company_currency_id',
  37. compute='_compute_amount',
  38. store=True)
  39. state = fields.Selection([('draft', 'Draft'),
  40. ('subscribed', 'Subscribed'),
  41. ('waiting', 'Waiting payment'),
  42. ('paid', 'paid'),
  43. ('cancelled', 'Cancelled'),
  44. ('ended', 'Ended')],
  45. string="State",
  46. required=True,
  47. default="draft")
  48. company_currency_id = fields.Many2one('res.currency',
  49. related='company_id.currency_id',
  50. string="Company Currency",
  51. readonly=True)
  52. company_id = fields.Many2one('res.company',
  53. related='loan_issue_id.company_id',
  54. string="Company",
  55. readonly=True)
  56. def get_loan_sub_mail_template(self):
  57. return self.env.ref('easy_my_coop_loan.loan_subscription_confirmation',
  58. False)
  59. def get_loan_pay_req_mail_template(self):
  60. return self.env.ref('easy_my_coop_loan.loan_issue_payment_request',
  61. False)
  62. @api.model
  63. def create(self, vals):
  64. line = super(LoanIssueLine, self).create(vals)
  65. confirmation_mail_template = line.get_loan_sub_mail_template()
  66. confirmation_mail_template.send_mail(line.id)
  67. return line
  68. @api.multi
  69. def action_draft(self):
  70. for line in self:
  71. line.write({'state': 'draft'})
  72. @api.multi
  73. def action_validate(self):
  74. for line in self:
  75. line.write({'state': 'subscribed'})
  76. @api.multi
  77. def action_request_payment(self):
  78. pay_req_mail_template = self.get_loan_pay_req_mail_template()
  79. for line in self:
  80. pay_req_mail_template.send_mail(line.id)
  81. line.write({'state': 'waiting'})
  82. @api.multi
  83. def action_cancel(self):
  84. for line in self:
  85. line.write({'state': 'cancelled'})
  86. @api.multi
  87. def action_paid(self):
  88. for line in self:
  89. line.write({'state': 'paid'})