from odoo import api, fields, models from odoo.exceptions import ValidationError class AccountMove(models.Model): _inherit = "account.move" dance_student_id = fields.Many2one("res.partner", string="Student", ondelete="restrict") dance_batch_id = fields.Many2one("dance.batch", string="Batch", ondelete="restrict") dance_enrollment_id = fields.Many2one( "dance.enrollment", string="Enrollment", ondelete="restrict", ) dance_fee_structure_id = fields.Many2one( "dance.fee.structure", string="Fee Structure", ondelete="restrict", ) dance_period_start = fields.Date(string="Fee Period Start") dance_period_end = fields.Date(string="Fee Period End") @api.constrains("move_type", "dance_enrollment_id", "dance_period_start", "dance_period_end", "state") def _check_duplicate_fee_invoice(self): for move in self: if ( move.move_type != "out_invoice" or not move.dance_enrollment_id or not move.dance_period_start or not move.dance_period_end or move.state == "cancel" ): continue duplicate = self.search( [ ("id", "!=", move.id), ("move_type", "=", "out_invoice"), ("dance_enrollment_id", "=", move.dance_enrollment_id.id), ("dance_period_start", "=", move.dance_period_start), ("dance_period_end", "=", move.dance_period_end), ("state", "!=", "cancel"), ], limit=1, ) if duplicate: raise ValidationError( "Only one active invoice is allowed per enrollment and fee period." )