87 lines
3.4 KiB
Python
87 lines
3.4 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationStudentScholarship(models.Model):
|
|
_name = 'education.student.scholarship'
|
|
_description = 'Student Scholarship'
|
|
_order = 'id desc'
|
|
_rec_name = 'scholarship_name'
|
|
|
|
scholarship_name = fields.Char(string='Scholarship Name', required=True)
|
|
student_id = fields.Many2one('education.student', string='Student', required=True, index=True)
|
|
type = fields.Selection([
|
|
('percentage', 'Percentage'),
|
|
('fixed', 'Fixed Amount')
|
|
], string='Scholarship Type', required=True, default='fixed')
|
|
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
string='Currency',
|
|
required=True,
|
|
default=lambda self: self.env.company.currency_id,
|
|
)
|
|
amount = fields.Monetary(string='Scholarship Value', required=True, currency_field='currency_id', default=0.0)
|
|
sponsor = fields.Char(string='Sponsor')
|
|
validity = fields.Date(string='Validity Date')
|
|
state = fields.Selection([
|
|
('draft', 'Draft'),
|
|
('confirmed', 'Confirmed'),
|
|
('cancelled', 'Cancelled')
|
|
], string='Status', default='draft', required=True, index=True)
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
|
|
@api.constrains('amount')
|
|
def _check_amount(self):
|
|
for rec in self:
|
|
if rec.amount <= 0:
|
|
raise ValidationError('Scholarship value must be greater than zero.')
|
|
if rec.type == 'percentage' and rec.amount > 100:
|
|
raise ValidationError('Scholarship percentage cannot exceed 100%.')
|
|
|
|
def action_confirm(self):
|
|
for rec in self:
|
|
if rec.state == 'confirmed':
|
|
continue
|
|
|
|
scholarship_amount = rec.amount
|
|
if rec.type == 'percentage':
|
|
outstanding = rec.student_id.outstanding_amount
|
|
scholarship_amount = outstanding * (rec.amount / 100.0)
|
|
|
|
if scholarship_amount <= 0:
|
|
raise ValidationError('Calculated scholarship amount must be greater than zero.')
|
|
if scholarship_amount > rec.student_id.outstanding_amount:
|
|
raise ValidationError(f'Cannot apply a scholarship of {scholarship_amount} because it exceeds the student outstanding dues of {rec.student_id.outstanding_amount}.')
|
|
|
|
self.env['education.student.ledger'].create({
|
|
'student_id': rec.student_id.id,
|
|
'date': fields.Date.today(),
|
|
'ref': f"SCH/{rec.id}",
|
|
'description': f"Scholarship Awarded: {rec.scholarship_name} (Sponsor: {rec.sponsor or 'N/A'})",
|
|
'debit': 0.0,
|
|
'credit': scholarship_amount,
|
|
'status': 'paid',
|
|
'company_id': rec.company_id.id,
|
|
})
|
|
rec.state = 'confirmed'
|
|
|
|
def action_cancel(self):
|
|
for rec in self:
|
|
if rec.state == 'confirmed':
|
|
ledger_entries = self.env['education.student.ledger'].search([
|
|
('student_id', '=', rec.student_id.id),
|
|
('ref', '=', f"SCH/{rec.id}"),
|
|
])
|
|
ledger_entries.unlink()
|
|
rec.state = 'cancelled'
|