'Virtual Field to show the status of the unit by available capacity
- used to colour features on the map
0: Full
1: Partial
2: Empty
3: Not Available'

@staticmethod
def cr_shelter_unit_status(row):

    if hasattr(row, 'cr_shelter_unit'):
        row = row.cr_shelter_unit
    if hasattr(row, 'status'):
        status = row.status
    else:
        status = None
    if (status == 2):
        return 3
    if hasattr(row, 'available_capacity_day'):
        actual = row.available_capacity_day
    else:
        actual = None
    if ((status is not None) and (actual is not None)):
        if (actual <= 0):
            return 0
    if hasattr(row, 'capacity_day'):
        total = row.capacity_day
        if (total == 0):
            return 0
    else:
        total = None
    if ((status is not None) and (total is not None) and (actual is not None)):
        if (actual == total):
            return 2
        else:
            return 1
    if hasattr(row, 'id'):
        s3_debug('Reloading    cr_shelter_unit    record')
        table = current.s3db.cr_shelter_unit
        r = current.db((table.id == row.id)).select(table.status, table.capacity_day, table.available_capacity_day, limitby=(0, 1)).first()
        if r:
            status = r.status
            if (status == 2):
                return 3
            actual = r.available_capacity_day
            if (actual <= 0):
                return 0
            total = r.capacity_day
            if (total == 0):
                return 0
            elif (actual == total):
                return 2
            else:
                return 1
    return current.messages['NONE']
