<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cancel_block_volume(self, volume_id, reason='No longer needed', immediate=False):
 """Cancels the given block storage volume. :param integer volume_id: The volume ID :param string reason: The reason for cancellation :param boolean immediate_flag: Cancel immediately or on anniversary date """

        block_volume = self.get_block_volume_details(
            volume_id,
            mask='mask[id,billingItem[id,hourlyFlag]]')

        if 'billingItem' not in block_volume:
            raise exceptions.SoftLayerError("Block Storage was already cancelled")

        billing_item_id = block_volume['billingItem']['id']

        if utils.lookup(block_volume, 'billingItem', 'hourlyFlag'):
            immediate = True

        return self.client['Billing_Item'].cancelItem(
            immediate,
            True,
            reason,
            id=billing_item_id)