Exemple #1
0
def setup_historical_transaction(
    owner_status_chooser, end_date, transaction_counter, data, overrides=None
):
    if (data['category'] == TRANSACTION_CATEGORY.CREDIT and
            data['source'] == TRANSACTION_SOURCE.BANK_TRANSFER):
        is_valid = data.get('prison', None) and not data.get('incomplete_sender_info')
        is_most_recent = data['received_at'].date() == end_date.date()
        if is_valid:
            owner, status = owner_status_chooser(data['prison'])
            if is_most_recent:
                data.update({
                    'owner': None,
                    'credited': False
                })
            else:
                data.update({
                    'owner': owner,
                    'credited': True
                })
        else:
            if is_most_recent or data.get('incomplete_sender_info'):
                data.update({'refunded': False})
            else:
                data.update({'refunded': True})

    if overrides:
        data.update(overrides)
    with MockModelTimestamps(data['created'], data['modified']):
        new_transaction = save_transaction(data)

    return new_transaction
Exemple #2
0
def setup_transaction(
    owner_status_chooser, transaction_counter, data, overrides=None
):
    if data['category'] == TRANSACTION_CATEGORY.CREDIT:
        is_valid = data.get('prison', None) and not data.get('incomplete_sender_info')

        if is_valid:
            owner, status = owner_status_chooser(data['prison'])
            if status == CREDIT_STATUS.CREDIT_PENDING:
                data.update({
                    'owner': None,
                    'credited': False
                })
            elif status == CREDIT_STATUS.CREDITED:
                data.update({
                    'owner': owner,
                    'credited': True
                })
        else:
            if transaction_counter % 2 == 0 or data.get('incomplete_sender_info'):
                data.update({'refunded': False})
            else:
                data.update({'refunded': True})

    if overrides:
        data.update(overrides)
    with MockModelTimestamps(data['created'], data['modified']):
        new_transaction = save_transaction(data)

    return new_transaction
Exemple #3
0
def generate_transactions(
    transaction_batch=100,
    predetermined_transactions=False,
    consistent_history=False,
    include_debits=True,
    include_administrative_credits=True,
    include_unidentified_credits=True,
    days_of_history=7
):
    transactions = []
    if predetermined_transactions:
        for data in generate_predetermined_transactions_data():
            with MockModelTimestamps(data['created'], data['modified']):
                new_transaction = save_transaction(data)
            transactions.append(new_transaction)
        generate_transaction_logs(transactions)

    data_list = generate_initial_transactions_data(
        tot=transaction_batch,
        include_debits=include_debits,
        include_administrative_credits=include_administrative_credits,
        include_unidentified_credits=include_unidentified_credits,
        days_of_history=days_of_history
    )

    transactions += create_transactions(data_list, consistent_history)
    return transactions
Exemple #4
0
def setup_payment(owner_status_chooser, end_date, payment_counter, data):
    complete = bool(payment_counter % 10)
    older_than_yesterday = (data['created'].date() <
                            (end_date.date() - datetime.timedelta(days=1)))
    if complete:
        owner, status = owner_status_chooser(data['prison'])
        data['processor_id'] = str(uuid.uuid1())
        data['status'] = PAYMENT_STATUS.TAKEN
        if older_than_yesterday:
            data.update({'owner': owner, 'credited': True})
        else:
            data.update({'owner': None, 'credited': False})

    else:
        data['status'] = PAYMENT_STATUS.PENDING
        del data['cardholder_name']
        del data['card_number_last_digits']
        del data['card_expiry_date']
        del data['card_brand']
        del data['billing_address']

    with MockModelTimestamps(data['created'], data['modified']):
        new_payment = save_payment(data)

    return new_payment
Exemple #5
0
def create_disbursement_logs(disbursement):
    log_data = {
        'disbursement': disbursement,
    }

    prison_clerks = User.objects.filter(
        groups__name='PrisonClerk',
        prisonusermapping__prisons=disbursement.prison)
    bank_admins = User.objects.filter(groups__name='BankAdmin')

    creating_user = prison_clerks.first()

    with MockModelTimestamps(disbursement.created, disbursement.created):
        log_data['action'] = LOG_ACTIONS.CREATED
        log_data['user'] = creating_user
        Log.objects.create(**log_data)

    confirming_user = prison_clerks.last()

    if disbursement.resolution == DISBURSEMENT_RESOLUTION.SENT:
        sending_user = bank_admins.first()
        confirmed = disbursement.created + timedelta(hours=3)
        with MockModelTimestamps(confirmed, confirmed):
            log_data['action'] = LOG_ACTIONS.CONFIRMED
            log_data['user'] = confirming_user
            Log.objects.create(**log_data)
        with MockModelTimestamps(disbursement.modified, disbursement.modified):
            log_data['action'] = LOG_ACTIONS.SENT
            log_data['user'] = sending_user
            Log.objects.create(**log_data)
    else:
        with MockModelTimestamps(disbursement.modified, disbursement.modified):
            if disbursement.resolution == DISBURSEMENT_RESOLUTION.CONFIRMED:
                log_data['action'] = LOG_ACTIONS.CONFIRMED
                log_data['user'] = confirming_user
                Log.objects.create(**log_data)
            elif disbursement.resolution == DISBURSEMENT_RESOLUTION.REJECTED:
                log_data['action'] = LOG_ACTIONS.REJECTED
                log_data['user'] = confirming_user
                Log.objects.create(**log_data)
def create_credit_log(credit, created, modified):
    with MockModelTimestamps(modified, modified):
        log_data = {
            'credit': credit,
            'user': credit.owner,
        }

        if credit.credited:
            log_data['action'] = LOG_ACTIONS.CREDITED
            Log.objects.create(**log_data)
        elif credit.refunded:
            log_data['action'] = LOG_ACTIONS.REFUNDED
            Log.objects.create(**log_data)
Exemple #7
0
def setup_payment(owner_status_chooser,
                  end_date,
                  payment_counter,
                  data,
                  overrides=None,
                  attach_profiles_to_individual_credits=True):
    older_than_yesterday = (data['created'].date() <
                            (end_date.date() - datetime.timedelta(days=1)))
    if overrides and overrides.get('status'):
        data['status'] = overrides['status']
    elif not bool(payment_counter % 11):  # 1 in 11 is expired
        data['status'] = PAYMENT_STATUS.EXPIRED
    elif not bool(payment_counter % 10):  # 1 in 10 is rejected
        data['status'] = PAYMENT_STATUS.REJECTED
    elif not bool(payment_counter % 4):  # 1 in 4ish is pending
        data['status'] = PAYMENT_STATUS.PENDING
    else:  # otherwise it's taken
        data['status'] = PAYMENT_STATUS.TAKEN

    if data['status'] == PAYMENT_STATUS.PENDING:
        del data['cardholder_name']
        del data['card_number_first_digits']
        del data['card_number_last_digits']
        del data['card_expiry_date']
        del data['card_brand']
        if not bool(payment_counter %
                    12):  # 2 in 3 of pending checks has a billing_address
            del data['billing_address']
    elif data['status'] == PAYMENT_STATUS.TAKEN:
        owner, status = owner_status_chooser(data['prison'])
        data['processor_id'] = str(uuid.uuid1())
        # TODO This is a horrible piece of implicit logic, can we please make it explicit
        # or document it somewhere
        if older_than_yesterday:
            data.update({'owner': owner, 'credited': True})
        else:
            data.update({'owner': None, 'credited': False})

    if overrides:
        data.update(overrides)
    with MockModelTimestamps(data['created'], data['modified']):
        new_payment = save_payment(data, overrides,
                                   attach_profiles_to_individual_credits)

    return new_payment
Exemple #8
0
def setup_disbursement(end_date, disbursement_counter, data):
    if disbursement_counter % 20 == 0:
        data['resolution'] = DISBURSEMENT_RESOLUTION.REJECTED
        data['modified'] = data['created'] + timedelta(hours=3)
    elif data['created'].date(
    ) < latest_disbursement_date().date() - timedelta(days=1):
        data['resolution'] = DISBURSEMENT_RESOLUTION.SENT
        data['modified'] = data['created'] + timedelta(days=1)
    elif data['created'].date(
    ) < latest_disbursement_date().date() - timedelta(hours=4):
        data['resolution'] = DISBURSEMENT_RESOLUTION.CONFIRMED
        data['modified'] = data['created'] + timedelta(hours=3)
    else:
        data['modified'] = data['created']

    with MockModelTimestamps(data['created'], data['modified']):
        new_disbursement = Disbursement.objects.create(**data)
        if new_disbursement.resolution == DISBURSEMENT_RESOLUTION.CONFIRMED:
            new_disbursement.invoice_number = new_disbursement._generate_invoice_number(
            )
            new_disbursement.save()

    create_disbursement_logs(new_disbursement)
    return new_disbursement