52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationHostelFloor(models.Model):
|
|
_name = 'education.hostel.floor'
|
|
_description = 'Hostel Floor'
|
|
_order = 'hostel_id, sequence, name'
|
|
|
|
hostel_id = fields.Many2one(
|
|
'education.hostel',
|
|
string='Hostel',
|
|
required=True,
|
|
ondelete='cascade',
|
|
index=True,
|
|
)
|
|
name = fields.Char(string='Floor Name', required=True)
|
|
code = fields.Char(string='Floor Code', required=True)
|
|
sequence = fields.Integer(string='Sequence', default=1, required=True)
|
|
description = fields.Text(string='Description')
|
|
active = fields.Boolean(default=True)
|
|
|
|
room_ids = fields.One2many('education.hostel.room', 'floor_id', string='Rooms')
|
|
room_count = fields.Integer(string='Rooms', compute='_compute_room_count')
|
|
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
related='hostel_id.company_id',
|
|
store=True,
|
|
readonly=True,
|
|
index=True,
|
|
)
|
|
|
|
_sql_constraints = [
|
|
('uniq_floor_code_hostel', 'unique(code, hostel_id)', 'Floor code must be unique within the hostel.'),
|
|
('uniq_floor_seq_hostel', 'unique(sequence, hostel_id)', 'Floor sequence must be unique within the hostel.'),
|
|
]
|
|
|
|
@api.depends('room_ids')
|
|
def _compute_room_count(self):
|
|
for rec in self:
|
|
rec.room_count = len(rec.room_ids)
|
|
|
|
@api.constrains('sequence')
|
|
def _check_sequence(self):
|
|
for rec in self:
|
|
if rec.sequence < 0:
|
|
raise ValidationError('Floor sequence cannot be negative.')
|