SPECIAL_TRANSACTION_EDIT_ORGANISATION_ID = 994
SPECIAL_AMOUNT_EDITABLE_PREFIXES = ('dep-', 'p-sh-')


def _normalise_organisation_id(organisation_id):
    try:
        return int(organisation_id)
    except (TypeError, ValueError):
        return None


def can_edit_transaction_amount_for_organisation(transaction, organisation_id):
    if not transaction:
        return False

    if getattr(transaction, 'transaction_type', None) != 'normal':
        return False

    normalised_organisation_id = _normalise_organisation_id(organisation_id)
    if normalised_organisation_id != SPECIAL_TRANSACTION_EDIT_ORGANISATION_ID:
        return False

    reference_no = getattr(transaction, 'reference_no', '') or ''
    return reference_no.startswith(SPECIAL_AMOUNT_EDITABLE_PREFIXES)
