## Script (Python) "change_bed"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: change_bed.py 1206 2007-01-05 18:24:56Z joachim $
"""
relocate to a new bed if allocated bed is wrong
"""
import logging
logger = logging.getLogger('Student.Accommodation.change_bed')

request = context.REQUEST
redirect = request.RESPONSE.redirect
mtool = context.portal_membership
wf = context.portal_workflow
member = mtool.getAuthenticatedMember()
member_id = str(member)
path_info = request.get('PATH_INFO').split('/')

if mtool.isAnonymousUser():
    return None
students = context.portal_url.getPortalObject().campus.students
beds = context.portal_accommodation
studs = context.students_catalog
student_id = context.getStudentId()

if student_id is not None:
    logger.info('"%s", "requests bed_change for", "%s"' % (member_id,student_id))
    info = context.getAccommodationInfo(student_id)
    res = beds(student=student_id)
    if len(res) == 0:
        logger.info('"%s", "no bed found"' % (student_id))
        redirect("%s/%s" % (students.absolute_url(),student_id))
    allocated_bed = res[0]
    status = info['student_status']
    student = student_id
    if allocated_bed.bed_type == status:
        logger.info('"%s", "correct bed allocated","%s"' % (student_id,status))
        redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))
    logger.info('"%s", "change bed", "%s/%s"' % (student_id,allocated_bed.bed_type,status))
    beds.modifyRecord(bed=allocated_bed.bed,student='')
    code,bed = beds.searchAndReserveBed(student_id,status)
    if code > 0:
        #from Products.zdb import set_trace; set_trace()
        d = {}
        d['bed'] = bed
        d['student_status'] = status
        acco_info = context.waeup_tool.getAccommodationInfo(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)
        return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))
    logger.info('"%s", "new bed allocation failed","%s"' % (student_id,code))
    redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))

info = {}
records = [r for r in beds() if  r.student]
list = []
to_modify = []
for r in records:
    info = context.getAccommodationInfo(r.student)
    sbt = info.get('student_status',None)
    if sbt is None:
        continue
    elif r.bed_type == sbt:
        list.append("Student %s bed_type %s ok" % (r.student,r.bed_type))
        continue
    list.append("Student %s bed_type %s != %s" % (r.student,
                                                  r.bed_type,
                                                  info['student_status']))
    to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc']))
for former_bed, student, status,acco_doc in to_modify:
    beds.modifyRecord(bed=former_bed,student='')
    code,bed = beds.searchAndReserveBed(student,status)
    if code > 0:
        #from Products.zdb import set_trace; set_trace()
        d = {}
        d['bed'] = bed
        d['student_status'] = status
        acco_info = context.waeup_tool.getAccommodationInfo(bed)
        d['acco_maint_code'] = acco_info.get('maintenance_code')
        d['acco_maint_fee'] = acco_info.get('maintenance_fee')
        acco_doc.edit(mapping=d)
        list.append("Student %s new bed %s assigned code = %s" % (student,
                                                              bed,
                                                              code))
return "\r".join(list)
