Index: WAeUP_SRP/base/skins/waeup_accommodation/change_bed.py
===================================================================
--- WAeUP_SRP/base/skins/waeup_accommodation/change_bed.py	(revision 3155)
+++ WAeUP_SRP/base/skins/waeup_accommodation/change_bed.py	(revision 3156)
@@ -13,6 +13,7 @@
 """
 import logging
+from Products.AdvancedQuery import Eq, Between, Le,In
+    
 logger = logging.getLogger('Skins.change_bed')
-
 request = context.REQUEST
 redirect = request.RESPONSE.redirect
@@ -38,35 +39,44 @@
     res = beds(student=student_id)
     status = info['student_status']
-    if len(res) == 0:
-        logger.info('No bed allocated for %s' % (student_id))
-        #return redirect("%s/%s" % (students.absolute_url(),student_id))
-    else:
+    while True:
+        if len(res) == 0:
+            psm = 'No bed allocated for %s' % (student_id)
+            logger.info(psm)
+            break
         allocated_bed = res[0]
-    code,bed = beds.searchAndReserveBed(student_id,status)
-    if code > 0:
-        if res:
-            if allocated_bed.bed_type == status:
-                logger.info('Status %s of %s has not changed' % (status,student_id))
-                psm='Student status has not changed!'
-                return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
-            logger.info('Bed status %s of %s has changed to %s' % (allocated_bed.bed_type,student_id,status))
-            beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied)
-            logger.info('Bed %s released' % (allocated_bed.bed))    
-        d = {}
-        d['bed'] = bed
-        d['student_status'] = status
-        acco_info = context.waeup_tool.getHallInfo(bed)
-        d['acco_maint_code'] = acco_info.get('maintenance_code')
-        d['acco_maint_fee'] = acco_info.get('maintenance_fee')
-        acco_doc = info['acco_doc']
-        acco_doc.edit(mapping=d)
-        logger.info('Bed %s allocated to %s' % (bed,student_id))
-        psm='Bed changed!'
-        return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
-    #acco_doc = info['acco_doc']
-    #acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"})
-    logger.info('New bed allocation for %s failed, code = %s' % (student_id,code))
-    #logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id))
-    psm='Bed change failed!'
+        if allocated_bed.bed_type == status:
+            logger.info('Status %s of %s has not changed' % (status,student_id))
+            psm='Student status has not changed!'
+            break
+            #return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
+        logger.info('Bed status %s of %s has changed to %s' % (allocated_bed.bed_type,student_id,status))
+        query = Eq('bed_type',status) & Eq('student',context.portal_accommodation.not_occupied)
+        records = context.portal_accommodation.evalAdvancedQuery(query)
+        if len(records) < 1:
+            psm=''New bed allocation for %s failed, No bed free in category %s' % (student_id,status)
+            logger.info(psm)
+            psm='Bed change failed!'
+            break
+        beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied)
+        logger.info('Bed %s released' % (allocated_bed.bed))    
+        code,bed = beds.searchAndReserveBed(student_id,status)
+        if code > 0:
+            d = {}
+            d['bed'] = bed
+            d['student_status'] = status
+            acco_info = context.waeup_tool.getHallInfo(bed)
+            d['acco_maint_code'] = acco_info.get('maintenance_code')
+            d['acco_maint_fee'] = acco_info.get('maintenance_fee')
+            acco_doc = info['acco_doc']
+            acco_doc.edit(mapping=d)
+            logger.info('Bed %s allocated to %s' % (bed,student_id))
+            psm='Bed changed!'
+            break
+            #return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
+        #acco_doc = info['acco_doc']
+        #acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"})
+        logger.info('New bed allocation for %s failed, code = %s' % (student_id,code))
+        #logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id))
+        psm='Bed change failed!'
     return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
 
