Index: WAeUP_SRP/base/skins/waeup_accommodation/reserve_accommodation.py
===================================================================
--- WAeUP_SRP/base/skins/waeup_accommodation/reserve_accommodation.py	(revision 2845)
+++ WAeUP_SRP/base/skins/waeup_accommodation/reserve_accommodation.py	(revision 2857)
@@ -1,2 +1,8 @@
+## Script (Python) "reserve_accommodation.py"
+##bind container=container
+##bind context=context
+##bind namespace=
+##bind script=script
+##bind subpath=traverse_subpath
 ##parameters=REQUEST
 # $Id$
@@ -4,4 +10,9 @@
 process the the accommodation reservation
 """
+try:
+    from Products.zdb import set_trace
+except:
+    def set_trace():
+        pass
 import DateTime
 current = DateTime.DateTime()
@@ -26,12 +37,16 @@
     return context.REQUEST.RESPONSE.redirect("%s/no_booking_allowed" % info['student'].absolute_url())
 student = info['student']
+student_id = info['student_id']
 acco_id = info['acco_id']
-session = info['session'][1]
+#session = info['session'][1]
+session = info['session']
 #session = acco_id[-4:]
 validate = REQUEST.has_key("cpsdocument_create_button")
 if info.has_key('acco') and info['acco']:
     validate = REQUEST.has_key("cpsdocument_edit_button")
-    if info['acco_review_state'] == "maintenance_fee_paid":
+    if info['maintenance_paid']:
         return context.REQUEST.RESPONSE.redirect("%s/accommodation_view" % info['acco'].absolute_url())
+    elif info['online_payment']:
+        return context.REQUEST.RESPONSE.redirect("%s/pay_interswitch_acco?paytype=HOM" % info['student'].absolute_url())
     else:
         mode = 'edit'
@@ -61,5 +76,5 @@
     else:
         psm = "Error in data, unable to reserve bed."
-    return context.accommodation_pin_edit_form(rendered = res,
+    return context.accommodation_edit_form(rendered = res,
                                  psm = psm,
                                  #psm = "%s, %s" % (psm,ds),
@@ -68,5 +83,5 @@
                                  )
 elif psm == '':
-    return context.accommodation_pin_edit_form(rendered = res,
+    return context.accommodation_edit_form(rendered = res,
                                  psm = None,
                                  mode = mode,
@@ -74,35 +89,39 @@
                                  )
 elif psm == 'valid':
-    s_id = ds.get('s_id')
     if mode == 'create':
+        set_trace()
         pin = str(ds.get('acco_res_sc_pin'))
-        pa = context.portal_accommodation
-        already = pa(student=s_id)
-        if not already:
-            code,bed = pa.searchAndReserveBed(s_id,"%s" % (info['student_status']))
-            #code,bed = pa.searchAndReserveBed(s_id,"%s_%s" % (info['sex'],ds.get('student_status')))
+        code,bed = context.portal_accommodation.searchAndReserveBed(student_id,
+                                                                    "%s" % (info['student_status']))
+        while True:
+            if code == 1:
+                break
             if code == -1:
-                return context.accommodation_pin_edit_form(rendered = res,
-                                     psm = "%s" % bed,
-                                     mode = mode,
-                                     ds = ds,
-                                     )
+                return context.accommodation_edit_form(rendered = res,
+                                        psm = "%s" % bed,
+                                        mode = mode,
+                                        ds = ds,
+                                        )
             elif code == -2:
-                return context.accommodation_pin_edit_form(rendered = res,
-                                     psm = "No bed available. Your category is already fully booked.",
-                                     mode = mode,
-                                     ds = ds,
-                                     )
-        else:
-            bed = already[0].bed
+                return context.accommodation_edit_form(rendered = res,
+                                        psm = "No bed available. Your category is already fully booked.",
+                                        mode = mode,
+                                        ds = ds,
+                                        )
+            else: # unknown error
+                return context.accommodation_edit_form(rendered = res,
+                                        psm = "Unexpected Error!",
+                                        mode = mode,
+                                        ds = ds,
+                                        )
         student.invokeFactory('StudentAccommodation',acco_id)
-        acco = getattr(student,acco_id)
-        acco_info = context.waeup_tool.getHallInfo(bed)
-        ds.set('acco_maint_code', acco_info.get('maintenance_code'))
-        ds.set('acco_maint_fee', acco_info.get('maintenance_fee'))
+        hall_info = context.waeup_tool.getHallInfo(bed)
+        ds.set('acco_maint_code', hall_info.get('maintenance_code'))
+        ds.set('acco_maint_fee', hall_info.get('maintenance_fee'))
         ds.set('acco_res_date', current)
         ds.set('bed', bed)
         ds.set('session', session)
         ds.set('student_status',info['student_status'])
+        acco = getattr(student,acco_id)
         acco.getContent().edit(mapping=ds)
         #wftool.doActionFor(acco,'pay_maintenance_fee',dest_container=acco)
