71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationStudentRefund(models.Model):
|
|
_name = 'education.student.refund'
|
|
_description = 'Student Fee Refund'
|
|
_order = 'id desc'
|
|
|
|
student_id = fields.Many2one('education.student', string='Student', compute='_compute_student', store=True, index=True)
|
|
payment_id = fields.Many2one('education.student.payment', string='Original Payment', required=True, domain="[('status', '=', 'confirmed')]", index=True)
|
|
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
string='Currency',
|
|
required=True,
|
|
default=lambda self: self.env.company.currency_id,
|
|
)
|
|
amount = fields.Monetary(string='Refund Amount', required=True, currency_field='currency_id', default=0.0)
|
|
reason = fields.Text(string='Reason for Refund', required=True)
|
|
status = fields.Selection([
|
|
('draft', 'Draft'),
|
|
('approved', 'Approved'),
|
|
('rejected', 'Rejected')
|
|
], 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.depends('payment_id')
|
|
def _compute_student(self):
|
|
for rec in self:
|
|
rec.student_id = rec.payment_id.student_id if rec.payment_id else False
|
|
|
|
@api.constrains('amount', 'payment_id')
|
|
def _check_refund_amount(self):
|
|
for rec in self:
|
|
if rec.amount <= 0:
|
|
raise ValidationError('Refund amount must be positive.')
|
|
if rec.payment_id and rec.amount > rec.payment_id.amount:
|
|
raise ValidationError(f'Refund amount ({rec.amount}) cannot exceed original payment amount ({rec.payment_id.amount}).')
|
|
|
|
def action_approve(self):
|
|
for rec in self:
|
|
if rec.status == 'approved':
|
|
continue
|
|
|
|
# Reversing credit (refund debit)
|
|
self.env['education.student.ledger'].create({
|
|
'student_id': rec.student_id.id,
|
|
'date': fields.Date.today(),
|
|
'ref': f"REF/{rec.id}",
|
|
'description': f"Refund Approved - Original Payment Receipt: {rec.payment_id.receipt_number}",
|
|
'debit': rec.amount,
|
|
'credit': 0.0,
|
|
'status': 'outstanding',
|
|
'company_id': rec.company_id.id,
|
|
})
|
|
rec.status = 'approved'
|
|
|
|
def action_reject(self):
|
|
for rec in self:
|
|
rec.status = 'rejected'
|