예제 #1
0
파일: test_bai2.py 프로젝트: MrJaeger/bai2
    def test_write(self):
        bai2_file = Bai2FileWriterTestCase.create_bai2_file()

        output = bai2.write(bai2_file)
        self.assertEqual(
            output,
            (
                '01,CITIDIRECT,8888888,150715,2340,00131100,,,2/\n'
                '02,8888888,CITIGB00,1,150715,2340,GBP,2/\n'
                '03,77777777,GBP,010,10000,,,015,10000,,/\n'
                '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
                '88, BILLS\n'
                '16,399,1000,0,,,OTHER\n'
                '49,23599,5/\n'
                '03,77777777,GBP,010,10000,,,015,10000,,/\n'
                '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
                '88, BILLS\n'
                '16,399,1000,0,,,OTHER\n'
                '49,23599,5/\n'
                '98,47198,2,12/\n'
                '02,8888888,CITIGB00,1,150715,2340,GBP,2/\n'
                '03,77777777,GBP,010,10000,,,015,10000,,/\n'
                '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
                '88, BILLS\n'
                '16,399,1000,0,,,OTHER\n'
                '49,23599,5/\n'
                '03,77777777,GBP,010,10000,,,015,10000,,/\n'
                '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
                '88, BILLS\n'
                '16,399,1000,0,,,OTHER\n'
                '49,23599,5/\n'
                '98,47198,2,12/\n'
                '99,94396,2,26/'
            )
        )
    def test_write(self):
        bai2_file = Bai2FileWriterTestCase.create_bai2_file()

        output = bai2.write(bai2_file)
        self.assertEqual(output, (
            '01,CITIDIRECT,8888888,150715,2340,00131100,,,2/\n'
            '02,8888888,CITIGB00,1,150715,2340,GBP,2/\n'
            '03,77777777,GBP,010,10000,,,015,10000,,/\n'
            '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
            '88, BILLS\n'
            '16,399,1000,0,,,OTHER\n'
            '49,23599,5/\n'
            '03,77777777,GBP,010,10000,,,015,10000,,/\n'
            '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
            '88, BILLS\n'
            '16,399,1000,0,,,OTHER\n'
            '49,23599,5/\n'
            '98,47198,2,12/\n'
            '02,8888888,CITIGB00,1,150715,2340,GBP,2/\n'
            '03,77777777,GBP,010,10000,,,015,10000,,/\n'
            '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
            '88, BILLS\n'
            '16,399,1000,0,,,OTHER\n'
            '49,23599,5/\n'
            '03,77777777,GBP,010,10000,,,015,10000,,/\n'
            '16,399,2599,,,,BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS BILLS\n'
            '88, BILLS\n'
            '16,399,1000,0,,,OTHER\n'
            '49,23599,5/\n'
            '98,47198,2,12/\n'
            '99,94396,2,26/'))
def generate_bank_statement(request, receipt_date):
    start_date, end_date = reconcile_for_date(request, receipt_date)

    transactions = retrieve_all_transactions(
        request,
        received_at__gte=start_date,
        received_at__lt=end_date
    )

    transaction_records = []
    credit_num = 0
    credit_total = 0
    debit_num = 0
    debit_total = 0
    for transaction in transactions:
        transaction_record = models.TransactionDetail([])
        transaction_record.text = get_full_narrative(transaction)

        if transaction['category'] == 'debit':
            transaction_record.type_code = constants.TypeCodes[DEBIT_TYPE_CODE]
            debit_num += 1
            debit_total += transaction['amount']
        else:
            transaction_record.type_code = constants.TypeCodes[CREDIT_TYPE_CODE]
            if transaction.get('ref_code'):
                transaction_record.text = 'BGC ' + str(transaction['ref_code'])
            credit_num += 1
            credit_total += transaction['amount']

        transaction_record.amount = transaction['amount']
        transaction_records.append(transaction_record)

    bai2_file = models.Bai2File()

    file_header = bai2_file.header
    file_header.sender_id = settings.BANK_STMT_SENDER_ID
    file_header.receiver_id = settings.BANK_STMT_RECEIVER_ID
    file_header.creation_date = datetime.date.today()
    file_header.creation_time = datetime.datetime.utcnow().time()
    file_header.file_id = get_daily_file_uid()
    file_header.physical_record_size = RECORD_LENGTH

    group = models.Group()
    group_header = group.header
    group_header.ultimate_receiver_id = settings.BANK_STMT_RECEIVER_ID
    group_header.originator_id = settings.BANK_STMT_SENDER_ID
    group_header.group_status = constants.GroupStatus.update
    group_header.as_of_date = receipt_date
    group_header.as_of_time = datetime.time.max
    group_header.currency = settings.BANK_STMT_CURRENCY
    group_header.as_of_date_modifier = constants.AsOfDateModifier.interim_same_day
    bai2_file.children.append(group)

    # calculate balance values with reference to 0
    opening_balance = 0
    last_balance = retrieve_last_balance(request, receipt_date)
    if last_balance:
        opening_balance = last_balance['closing_balance']
    closing_balance = opening_balance + credit_total - debit_total

    account = models.Account()
    account_header = account.header
    account_header.customer_account_number = settings.BANK_STMT_ACCOUNT_NUMBER
    account_header.currency = settings.BANK_STMT_CURRENCY
    account_header.summary_items = [
        models.Summary(
            type_code=constants.TypeCodes[OPENING_BALANCE_TYPE_CODE],
            amount=opening_balance
        ),
        models.Summary(
            type_code=constants.TypeCodes[CLOSING_BALANCE_TYPE_CODE],
            amount=closing_balance
        ),
        models.Summary(
            type_code=constants.TypeCodes[CLOSING_LEDGER_TYPE_CODE],
            amount=closing_balance
        ),
        models.Summary(
            type_code=constants.TypeCodes[OPENING_LEDGER_TYPE_CODE],
            amount=opening_balance
        ),
        models.Summary(
            type_code=constants.TypeCodes[DEBIT_TOTAL_TYPE_CODE],
            amount=debit_total,
            item_count=debit_num
        ),
        models.Summary(
            type_code=constants.TypeCodes[CREDIT_TOTAL_TYPE_CODE],
            amount=credit_total,
            item_count=credit_num
        )
    ]
    account.children = transaction_records
    group.children.append(account)

    output = bai2.write(bai2_file, clock_format_for_intra_day=True)
    logger.info('{user} downloaded {label} containing {count} records'.format(
        user=request.user.username,
        label=BAI2_STMT_LABEL,
        count=len(transactions)
    ))

    return (receipt_date.strftime(settings.BANK_STMT_OUTPUT_FILENAME),
            output)