def _transaction_sort_key(txn):
    """
    If there are payments and invoices created on the same day
    we want to make sure to process the payments first and then
    the invoices. Otherwise the transaction history on an
    invoice will look like a drunk cow licked it, that's bad.
    :param txn:
    :return: sort key
    """
    txn_date = txn.transacted_on.isoformat()[:10]
    type_weight = "0" if txn.transaction_type == txn.PAYMENT else "1"
    txn_id = str(txn.id)  # sort multiple invoices on same day by primary key id
    return txn_date + type_weight + txn_id