Index: /WAeUP_SRP/trunk/skins/waeup_accommodation/allocate_reserved_bed.py
===================================================================
--- /WAeUP_SRP/trunk/skins/waeup_accommodation/allocate_reserved_bed.py	(revision 5619)
+++ /WAeUP_SRP/trunk/skins/waeup_accommodation/allocate_reserved_bed.py	(revision 5619)
@@ -0,0 +1,136 @@
+##parameters=REQUEST
+# $Id$
+"""
+process the the accommodation reservation
+"""
+import DateTime
+current = DateTime.DateTime()
+pr = context.portal_registration
+wftool = context.portal_workflow
+lt = context.portal_layouts
+mtool = context.portal_membership
+member = mtool.getAuthenticatedMember()
+portal_acco_cat = context.portal_accommodation
+import logging
+logger = logging.getLogger('Skins.allocate_reserved_bed')
+
+release = REQUEST.has_key("release")
+allocate = REQUEST.has_key("allocate")
+validate = release or (allocate and REQUEST.get("allocate").startswith('Allocate'))
+mode = 'create'
+d = {}
+rendered,psm,ds = lt.renderLayout(layout_id= 'acco_bed_booking',
+                      schema_id= 'acco_bed_booking',
+                      context=context,
+                      mapping=validate and REQUEST,
+                      ob=d,
+                      layout_mode='edit',
+                      commit = False
+                      )
+if psm == 'invalid':
+    psm = "Please correct your input."
+    return context.allocate_reserved_bed_form(rendered = rendered,
+                                 psm = psm,
+                                 #psm = "%s, %s" % (psm,ds),
+                                 mode = mode,
+                                 formaction = "allocate_reserved_bed",
+                                 button = "Allocate Bed",
+                                 ds = ds,
+                                 )
+elif psm == '':
+    return context.allocate_reserved_bed_form(rendered = rendered,
+                                 psm = psm,
+                                 mode = mode,
+                                 formaction = "allocate_reserved_bed",
+                                 button = "Allocate Bed",
+                                 ds = ds,
+                                 )
+elif psm == 'valid':
+    pass
+#from Products.zdb import set_trace;set_trace()
+hall_id = context.getId()
+doc = context.getContent()
+student = ds.get('student')
+sid = ds.get('student_id')
+br = ds.get('block_room')
+block = br[0]
+bl = ds.get('bed_letter')
+bid = "%s_%s_%s" % (hall_id,br,bl)
+res = portal_acco_cat(bed = bid)
+already = portal_acco_cat(student=sid)
+psm = ''
+
+while True:
+    if not res:
+        psm = "No bed with id %s" % bid
+        break
+    bed_brain = res[0]
+    if allocate:
+        if already:
+            psm = "Student %s has already booked bed %s. This booking must be cancelled first." % (sid, already[0].bed)
+            break
+        if not bed_brain.bed_type.endswith("reserved"):
+            psm = "%s is not a reserved bed" % bid
+            break
+        if bed_brain.student and bed_brain.student != portal_acco_cat.not_occupied:
+            psm = "Bed %s already reserved for %s" % (bid,bed_brain.student)
+            break
+        if student.sex and not block in doc.blocks_for_female:
+            psm = "Sex does not match %s" % bid
+            break
+    if release:
+        if not already:
+            psm = "Student %s has no bed" % (sid)
+            break
+        already_brain = already[0]
+        if already_brain.bed != bid:
+            psm = "Student %s has booked bed %s. Bed can only be released by cancelling the booking record." % (sid, already_brain.bed)
+            break        
+        if not bed_brain.student:
+            psm = "Bed %s not allocated" % (bid)
+            break
+        accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0],reservation_status = 'maintenance_fee_paid')
+        if accommodation_records:
+            psm = "Student %s has already paid for bed %s" % (sid, bid)
+            break
+        if not bed_brain.bed_type.endswith("reserved"):
+            psm = "%s is not a reserved bed" % bid
+            break
+    break
+
+if psm != '':
+    return context.allocate_reserved_bed_form(rendered = rendered,
+                                 psm = psm,
+                                 mode = mode,
+                                 ds = ds,
+                                 formaction = "allocate_reserved_bed",
+                                 button = "Allocate Bed",
+                                 )
+if allocate:
+    portal_acco_cat.modifyRecord(bed = bid, student = sid)
+    accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0])
+    if accommodation_records:
+        d = {}
+        d['bed'] = bid
+        d['catkey'] = sid + '|' + context.getSessionId()[0]
+        context.accommodation_catalog.modifyRecord(**d)        
+    logger.info('%s booked reserved bed %s for %s' % (member,bid,sid))
+    psm = "Bed %s reserved for %s %s" % (bid,sid,student.name)
+elif release:
+    accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0])
+    if accommodation_records and accommodation_records[0].bed == bid:
+        psm = "Removing of %s from bed %s failed. The booking record must be cancelled first." % (student.name,bid)
+    else:
+        portal_acco_cat.modifyRecord(bed=bid,student='')
+        logger.info('%s released bed %s booked by %s' % (member,bid,sid))
+        psm = "%s removed from %s" % (student.name,bid)
+
+mode = 'view'
+return context.allocate_reserved_bed_form(rendered = "",
+                                 psm = psm,
+                                 mode = mode,
+                                 formaction = "allocate_reserved_bed",
+                                 button = "Next Reservation",
+                                 ds = ds,
+                                 )
+
