82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationHostelBed(models.Model):
|
|
_name = 'education.hostel.bed'
|
|
_description = 'Hostel Bed'
|
|
_order = 'room_id, bed_number'
|
|
|
|
room_id = fields.Many2one(
|
|
'education.hostel.room',
|
|
string='Room',
|
|
required=True,
|
|
ondelete='cascade',
|
|
index=True,
|
|
)
|
|
hostel_id = fields.Many2one(
|
|
'education.hostel',
|
|
string='Hostel',
|
|
related='room_id.hostel_id',
|
|
store=True,
|
|
readonly=True,
|
|
index=True,
|
|
)
|
|
floor_id = fields.Many2one(
|
|
'education.hostel.floor',
|
|
string='Floor',
|
|
related='room_id.floor_id',
|
|
store=True,
|
|
readonly=True,
|
|
)
|
|
bed_number = fields.Char(string='Bed Number', required=True)
|
|
code = fields.Char(string='Bed Code', required=True, index=True)
|
|
status = fields.Selection([
|
|
('available', 'Available'),
|
|
('reserved', 'Reserved'),
|
|
('occupied', 'Occupied'),
|
|
('maintenance', 'Under Maintenance'),
|
|
('inactive', 'Inactive'),
|
|
], string='Status', default='available', required=True, tracking=True)
|
|
active = fields.Boolean(default=True)
|
|
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
related='room_id.company_id',
|
|
store=True,
|
|
readonly=True,
|
|
index=True,
|
|
)
|
|
|
|
@api.constrains('bed_number', 'room_id')
|
|
def _check_bed_number_room(self):
|
|
for rec in self:
|
|
if self.search_count([('bed_number', '=', rec.bed_number), ('room_id', '=', rec.room_id.id), ('id', '!=', rec.id)]):
|
|
raise ValidationError('Bed number must be unique within the room.')
|
|
|
|
@api.constrains('code')
|
|
def _check_bed_code(self):
|
|
for rec in self:
|
|
if self.search_count([('code', '=', rec.code), ('id', '!=', rec.id)]):
|
|
raise ValidationError('Bed code must be globally unique.')
|
|
|
|
@api.constrains('status')
|
|
def _check_status_vs_allocation(self):
|
|
"""Prevent manually clearing 'occupied' status while an active member holds this bed."""
|
|
for rec in self:
|
|
if rec.status != 'occupied':
|
|
# Check if any active member is still using this bed
|
|
active_member = self.env['education.hostel.member'].search([
|
|
('bed_id', '=', rec.id),
|
|
('status', '=', 'active'),
|
|
], limit=1)
|
|
if active_member:
|
|
raise ValidationError(
|
|
f"Bed '{rec.code}' is currently occupied by student "
|
|
f"'{active_member.student_id.full_name}'. "
|
|
"Please exit the allocation before changing bed status."
|
|
)
|