Exemplo n.º 1
0
def import_csv(filename, account):
    '''
    '''
    # CSVs come in many formats, find a matching profile
    for profile in CsvImportProfile.objects.all().order_by('order'):
        if profile.match(filename):
            logging.info("Found matching CSV profile: %s", profile)
            break
    else:
        raise ValueError, "No profiles matched the file"
    
    with open(filename, 'rb') as csv_file:
        transactions = parse_csv(csv_file, profile.get_fieldnames(), 
                                 dayfirst=profile.date_day_first,
                                 skip_rows=profile.data_start_row)
    logging.info("%d CSV transactions found in file %s", len(transactions), filename)
    for t in transactions:
        # split payee string into parts
        try:
            trans_type, payee = match_transaction_type(t['payee'])
        except ValueError:
            logging.warning("Transaction type cannot be determined: %s", t['payee'])
            trans_type = None
            payee = t['payee']

        # create and save Transactions in DB            
        new_transaction = Transaction(
            account = account,
            name = payee, #TODO: populate or remove as we don't need a name?!
            date = t['date'],
            transaction_type = trans_type,
            payee = payee,
            ##paid_on = paid_on_dt,
            ##import_string = t['import_string'], # DictReader doesn't give us the original line
        )
        new_transaction.save()
            
        t_part = TransactionPart(
            transaction = new_transaction,
            amount = '%.2f' % t['amount'],
            category = match_category(payee),
        )
        t_part.save()
        logging.debug("Imported %s - %s", new_transaction, t_part)
    
    return len(transactions)
Exemplo n.º 2
0
def import_qif(filename, account):
    """
    """
    with open(filename, 'rb') as qif_file:
        transactions = parse_qif(qif_file)
    logging.info("%d QIF transactions found in file %s", len(transactions), filename)
    for t in transactions:
        try:
            trans_type, payee = match_transaction_type(t['payee'])
        except ValueError:
            logging.warning("Transaction type cannot be determined: %s", t['payee'])
            trans_type = None
            payee = t['payee']

        # create and save Transactions in DB            
        new_transaction = Transaction(
            account = account,
            name = payee, #TODO: populate or remove as we don't need a name?!
            date = t['date'],
            transaction_type = trans_type,
            payee = payee,
            ##paid_on = paid_on_dt,
            import_string = t['import_string'],
        )  
        new_transaction.save()
        
        if t.get('amount_in_split'):
            part_list = []
            for n, amount in t['amount_in_split']:
                # create and save Transactions in DB
                if t.get('category_in_split'):
                    # assumed category_in_split has same number of items as amount
                    split_category = _get_category(t['category_in_split'][n])
                else:
                    split_category = match_category(payee)
                    
                t_part = TransactionPart(
                    transaction = new_transaction,
                    amount = '%.2f' % amount,
                    category = split_category,
                    description  = '', #TODO: iterate over t['memo'] field if available
                )
                t_part.save() #TODO: Required again after?
                part_list.append(t_part)
            logging.debug("Imported %s - %s", new_transaction, part_list)
                    
        else:
            # create a single transactionamount
            if t.get('category'):
                # Category from QIF takes priority if available
                category = _get_category(category_name)
            else:
                category = match_category(payee)
            t_part = TransactionPart(
                transaction = new_transaction,
                amount = '%.2f' % t['amount'],
                description = t.get('memo', ''),
                category = category,
            )
            t_part.save() #TODO: Required again after?
            logging.debug("Imported %s - %s", new_transaction, t_part)
    return len(transactions)
Exemplo n.º 3
0
 def test_transfer(self):
     res = match_transaction_type('FASTER PAYMENTS RECEIPT REF SAVOLAINEN RM')
     assert res == ('transfer', 'SAVOLAINEN RM', None)
     
     res = match_transaction_type('REGULAR TRANSFER FROM MR CHRISTOPHER JESSE')        
     assert res == ('transfer', 'MR CHRISTOPHER JESSE', None)
Exemplo n.º 4
0
 def test_card_payment(self):
     res = match_transaction_type('CARD PAYMENT TO CO-OP GROUP 54321234.24 GBP ON 29-05-2011', amount=abs(-34.24))
     assert res == ('card_payment', "CO-OP GROUP 543212", datetime(2011,5,29)) # why does this fail - it strips off the last "2" when you rstrip the numeric and a "."
     res = match_transaction_type('CARD PAYMENT TO ASDA SUPERSTORE,33.64 GBP ON 11-07-2011, 33.64GBP')
     assert res == ('card_payment', 'ASDA SUPERSTORE')
Exemplo n.º 5
0
 def test_direct_debit(self):
     res = match_transaction_type('DIRECT DEBIT PAYMENT TO DIRECT LINE INS REF 54321234/000001/P MANDATE NO 0015')
     assert res == ('direct_debit', 'DIRECT LINE INS REF 54321234/000001/P MANDATE NO 0015', None)
Exemplo n.º 6
0
 def test_no_match(self):
     assert match_transaction_type('NOTHING_HERE!') == None