Index: main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser.py	(revision 6996)
@@ -35,5 +35,5 @@
 from waeup.sirp.authentication import get_principal_role_manager
 from waeup.sirp.hostels.container import HostelsContainer
-from waeup.sirp.hostels.hostel import Hostel
+from waeup.sirp.hostels.hostel import Hostel, NOT_OCCUPIED
 from waeup.sirp.hostels.interfaces import IHostelsContainer, IHostel
 
@@ -204,8 +204,13 @@
         'Switch reservation of selected beds',
         'Release selected beds']
+    not_occupied = NOT_OCCUPIED
 
     @property
     def title(self):
         return self.context.hostel_name
+
+    @property
+    def students_url(self):
+        return self.url(grok.getSite(),'students')
 
     def update(self):
Index: main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/hostelmanagepage.pt
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/hostelmanagepage.pt	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/hostelmanagepage.pt	(revision 6996)
@@ -79,10 +79,13 @@
       		  tal:attributes="value value/__name__" />
           </td>
-          <td> <a tal:attributes="href value/__name__">
-          <span tal:content="value/bed_id">Id</span>
-          </a></td>
+          <td tal:content="value/bed_id">Id</td>
           <td tal:content="value/bed_type">Type</td>
           <td tal:content="value/bed_number">Number</td>
-          <td tal:content="value/owner">Owner</td>
+          <td>
+            <a tal:condition="python: value.owner != view.not_occupied"
+               tal:attributes="href python: '%s/%s' % (view.students_url,value.owner)">
+              <span tal:content="value/owner">Owner</span>
+            </a>
+          </td>
         </tr>
       </tbody>
Index: main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py	(revision 6996)
@@ -121,4 +121,8 @@
         return self.bed_id.split('_')
 
+    def bookBed(self, student_id):
+        self.owner = student_id
+        return
+
     def switchReservation(self):
         """Reserves a bed or unreserve bed respectively.
Index: main/waeup.sirp/trunk/src/waeup/sirp/hostels/interfaces.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/hostels/interfaces.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/hostels/interfaces.py	(revision 6996)
@@ -142,4 +142,7 @@
         """Determine the coordinates from bed_id.
         """
+    def bookBed(student_id):
+        """Book a bed for a student.
+        """
 
     def switchReservation():
Index: main/waeup.sirp/trunk/src/waeup/sirp/students/accommodation.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/students/accommodation.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/students/accommodation.py	(revision 6996)
@@ -60,4 +60,5 @@
         super(BedTicket, self).__init__()
         self.booking_date = datetime.now()
+        self.bed = None
         return
 
Index: main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py	(revision 6996)
@@ -19,4 +19,6 @@
 from time import time
 from datetime import date, datetime
+from zope.catalog.interfaces import ICatalog
+from zope.component import queryUtility
 from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
 from zope.component import createObject
@@ -47,6 +49,6 @@
 from waeup.sirp.students.studylevel import StudentStudyLevel, CourseTicket
 from waeup.sirp.students.vocabularies import StudyLevelSource
-from waeup.sirp.students.utils import getPaymentDetails
-from waeup.sirp.students.utils import getAccommodationDetails
+from waeup.sirp.students.utils import (
+    getPaymentDetails, getAccommodationDetails,)
 from waeup.sirp.browser.resources import toggleall
 from waeup.sirp.authentication import get_principal_role_manager
@@ -1089,6 +1091,7 @@
 
     def update(self, SUBMIT=None):
-        acc_details  = self.getAccommodationDetails(self.context.__parent__)
-        if not self.context.getStudent().state in acc_details['allowed_states']:
+        student = self.context.getStudent()
+        acc_details  = self.getAccommodationDetails(student)
+        if not student.state in acc_details['allowed_states']:
             self.flash("Wrong state.")
             self.redirect(self.url(self.context))
@@ -1119,7 +1122,21 @@
         bedticket.booking_code = pin
         bedticket.booking_session = acc_details['booking_session']
-        bedticket.bed = u'Test Bed'
-        self.context[str(acc_details['booking_session'])] = bedticket
-        self.flash('Bed ticket created.')
+        bedticket.bed_type = acc_details['bt']
+        # Search and book bed
+        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
+        entries = cat.searchResults(
+            bed_type=(bedticket.bed_type,bedticket.bed_type))
+        bed = [entry for entry in entries if entry.owner == NOT_OCCUPIED][0] # first bed found
+        bed.bookBed(student.student_id)
+        bedticket.bed = bed # maybe wo don't need the bed object itself
+        hall_title = bed.__parent__.hostel_name
+        coordinates = bed.getBedCoordinates()[1:]
+        block, room_nr, bed_nr = coordinates
+        bedticket.bed_coordinates = '%s, Block %s, Room %s, Bed %s' % (
+            hall_title, block, room_nr, bed_nr)
+        key = str(acc_details['booking_session'])
+        self.context[key] = bedticket
+        self.flash('Bed ticket created and bed booked: %s'
+            % bedticket.bed_coordinates)
         self.redirect(self.url(self.context))
         return
Index: main/waeup.sirp/trunk/src/waeup/sirp/students/interfaces.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/students/interfaces.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/students/interfaces.py	(revision 6996)
@@ -1,4 +1,5 @@
 ##
 ## interfaces.py
+from datetime import datetime
 from zope.interface import Attribute, invariant
 from zope.interface.exceptions import Invalid
@@ -9,5 +10,5 @@
 from waeup.sirp.students.vocabularies import (
   CertificateSource, academic_sessions_vocab, verdicts, StudyLevelSource,
-  contextual_reg_num_source, contextual_mat_num_source,
+  contextual_reg_num_source, contextual_mat_num_source, GenderSource,
   )
 from waeup.sirp.payments.interfaces import IPaymentsContainer, IOnlinePayment
@@ -110,4 +111,11 @@
         )
 
+    sex = schema.Choice(
+        title = u'Sex',
+        source = GenderSource(),
+        default = u'm',
+        required = True,
+        )
+
     reg_number = TextLineChoice(
         title = u'Registration Number',
@@ -132,4 +140,71 @@
         readonly = True,
         )
+
+class IStudentClearance(IWAeUPObject):
+    """Representation of student clearance data.
+    """
+
+    date_of_birth = schema.Date(
+        title = u'Date of Birth',
+        required = True,
+        )
+
+    clearance_locked = schema.Bool(
+        title = u'Clearance form locked',
+        default = False,
+        )
+
+    clr_code = schema.TextLine(
+        title = u'CLR Activation Code',
+        default = u'',
+        required = False,
+        readonly = True,
+        )
+
+class IStudentPersonal(IWAeUPObject):
+    """Representation of student personal data.
+    """
+
+    perm_address = schema.Text(
+        title = u'Permanent Address',
+        required = False,
+        )
+
+class IStudent(IStudentBase,IStudentClearance,IStudentPersonal):
+    """Representation of a student.
+    """
+
+class IStudentUpdateByRegNo(IStudent):
+    """Representation of a student. Skip regular reg_number validation.
+    """
+
+    reg_number = schema.TextLine(
+        title = u'Registration Number',
+        default = None,
+        required = False,
+        )
+
+class IStudentUpdateByMatricNo(IStudent):
+    """Representation of a student. Skip regular matric_number validation.
+    """
+
+    matric_number = schema.TextLine(
+        title = u'Matriculation Number',
+        default = None,
+        required = False,
+        )
+
+class IStudentStudyCourse(IWAeUPObject):
+    """A container for student study levels.
+
+    """
+
+    certificate = schema.Choice(
+        title = u'Certificate',
+        source = CertificateSource(),
+        default = None,
+        required = True,
+        )
+
 
     entry_mode = schema.Choice(
@@ -141,68 +216,10 @@
         )
 
-class IStudentClearance(IWAeUPObject):
-    """Representation of student clearance data.
-    """
-
-    date_of_birth = schema.Date(
-        title = u'Date of Birth',
-        required = True,
-        )
-
-    clearance_locked = schema.Bool(
-        title = u'Clearance form locked',
-        default = False,
-        )
-
-    clr_code = schema.TextLine(
-        title = u'CLR Activation Code',
-        default = u'',
-        required = False,
-        readonly = True,
-        )
-
-class IStudentPersonal(IWAeUPObject):
-    """Representation of student personal data.
-    """
-
-    perm_address = schema.Text(
-        title = u'Permanent Address',
-        required = False,
-        )
-
-class IStudent(IStudentBase,IStudentClearance,IStudentPersonal):
-    """Representation of a student.
-    """
-
-class IStudentUpdateByRegNo(IStudent):
-    """Representation of a student. Skip regular reg_number validation.
-    """
-
-    reg_number = schema.TextLine(
-        title = u'Registration Number',
-        default = None,
-        required = False,
-        )
-
-class IStudentUpdateByMatricNo(IStudent):
-    """Representation of a student. Skip regular matric_number validation.
-    """
-
-    matric_number = schema.TextLine(
-        title = u'Matriculation Number',
-        default = None,
-        required = False,
-        )
-
-class IStudentStudyCourse(IWAeUPObject):
-    """A container for student study levels.
-
-    """
-
-    certificate = schema.Choice(
-        title = u'Certificate',
-        source = CertificateSource(),
-        default = None,
-        required = True,
+    entry_session = schema.Choice(
+        title = u'Entry Session',
+        source = academic_sessions_vocab,
+        default = datetime.now().year,
+        required = True,
+        readonly = False,
         )
 
@@ -212,4 +229,5 @@
         default = None,
         required = True,
+        readonly = False,
         )
 
@@ -219,4 +237,5 @@
         default = None,
         required = False,
+        readonly = False,
         )
 
@@ -320,6 +339,14 @@
     """
 
-    bed = schema.TextLine(
-        title = u'Bed',
+    bed = Attribute('The bed object.')
+
+    bed_coordinates = schema.TextLine(
+        title = u'Bed Coordinates',
+        default = None,
+        required = False,
+        )
+
+    bed_type = schema.TextLine(
+        title = u'Bed Type',
         default = None,
         required = False,
Index: main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py	(revision 6994)
+++ main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py	(revision 6996)
@@ -45,5 +45,5 @@
         d['amount'] = academic_session.fee_2
     elif category == 'bed_allocation':
-        #d['p_item'] = student['studycourse'].certificate.code
+        d['p_item'] = getBedType(student)
         d['amount'] = academic_session.booking_fee
     return d
@@ -53,5 +53,4 @@
 def getAccommodationDetails(student):
     d = {}
-    d['bookin_fee'] = d['maint_fee'] = 0
     d['error'] = u''
     site_confoguration = getSite()['configuration']
@@ -59,3 +58,20 @@
     d['allowed_states'] = site_confoguration.accommodation_states
     session = str(d['booking_session'])
+    # Determine bed type
+    studycourse = student['studycourse']
+    entry_session = studycourse.entry_session
+    current_level = studycourse.current_level
+    end_level = studycourse.certificate.end_level
+    if entry_session == getSite()['configuration'].accommodation_session:
+        bt = 'fr'
+    elif current_level >= end_level:
+        bt = 'fi'
+    else:
+        bt = 're'
+    if student.sex == 'f':
+        sex = 'female'
+    else:
+        sex = 'male'
+    special_handling = 'regular'
+    d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
     return d
