File: import.pl

package info (click to toggle)
cbb 1%3A0.8.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,868 kB
  • ctags: 551
  • sloc: tcl: 5,221; perl: 4,512; sh: 376; makefile: 152
file content (206 lines) | stat: -rw-r--r-- 5,395 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/local/bin/perl
#  import.pl - functions to implement importing from other formats
#
#  Written by Curtis Olson.  Started August 25, 1994.
#
#  Copyright (C) 1994 - 1999  Curtis L. Olson  - curt@me.umn.edu
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id: import.pl,v 1.1.1.1 1999/12/18 02:05:09 curt Exp $


package CBB;

use strict;   # don't take no guff


# @INC specifies the installed location of the necessary pieces.
# It should already be setup by wrapper.pl

require "common.pl";


# load data from a CBB format file
sub load_cbb {
    # in: file base name
    # out: result

    my($file) = @_;
    $CBB::sorted_keys = 0;
    $CBB::calced = 0;

    open(LOAD, "<$file") || return "error";

    while ( <LOAD> ) {
	if ( m/^\s*#/ ) {
	    # toss the comment (any line whose 1st non-whitespace character is
	    #                   the pound sign.)
	} else {
	    chop;
	    &create_trans($_);
	}
    }

    close(LOAD);

    return "ok";
}


# import a quicken export file (.qif)
sub import_qif {
    # in: file
    # out: result

    my($file) = @_;
    my($date, $check, $desc, $debit, $credit, $cat, $split, $com, $cleared);
    my($amt, $found_split_com, $split_amt);
    my($day, $month, $year);
    my($iend, $incat, $splitsub, $insplit);

    $CBB::sorted_keys = 0;
    $CBB::calced = 0;

    open(QIF, "<$file");

    ($date, $check, $desc, $debit, $credit, $cat, $split, $com, 
    	$cleared) = ("", "", "", "", "", "", "", "", "");

    while ( <QIF> ) {
	chop;			# get rid of that pesky newline.
	# s/:/-/g;		# eliminate our delimiter characters from
	s/\|/-/g;		# the import file.
	s/\r//g;		# strip the dos ^M if needed
	if ( m/^\!/ ) {
	    # Type
	    # print "$_\n";
	} elsif ( m/^D/ ) {
	    # Date
	    ($month, $day, $year) = split(/\/ */, substr($_,1));
	    $month = &pad($month);
	    $day = &pad($day);
	    $date = "$year$month$day";
	    # print "$date\n";
	} elsif ( m/^T/ ) {
	    # Transaction Amount
	    s/,//g;		# remove , from numbers (i.e. thousands)
	    $amt = substr($_,1);
	    if ($amt >= 0) {
		$credit = $amt;
		$debit = 0;
	    } else {
		$debit = substr($amt,1); # remove the '-' to make amt >= 0
		$credit = 0;
	    }
	    # print "credit = $credit  debit = $debit\n";
	} elsif ( m/^C/ ) {
	    # Cleared
	    $cleared = substr($_,1);
+	    $cleared = 'x' if ($cleared eq 'X');
	    # print "Cleared = $cleared\n";
	} elsif ( m/^N/ ) {
	    # check Number
	    $check = substr($_,1);
	    # print "Check # = $check\n";
	} elsif ( m/^P/ ) {
	    # descriPtion
	    $desc = substr($_,1);
	    # print "$desc\n";
	} elsif ( m/^L/ ) {
	    # category
	    #
	    # Check for and replace whitespace in account transfer 
	    # categories with underscores. (Cbb equates account "acct"
	    # with account file name "acct.cbb".) B.W.
	    #
	    $cat = substr($_,1);
	    if ( substr($cat,0,1) eq "[" ) {
		$iend = index($cat,"]");
		if ($iend != -1) {
		    $incat = substr($cat,1,$iend - 1);
		    $incat =~ s/\s/_/g;
		    $cat = "[".$incat."]";
		}
	    }
	    #print "Category = $cat\n";
	} elsif ( m/^S/ ) {
	    # split category
	    #
	    # Check for and replace whitespace in account transfer 
	    # categories with underscores. (Cbb equates account "acct"
	    # with account file name "acct.cbb".) B.W.
	    #
	    $splitsub = substr($_,1);
	    if ( substr($splitsub,0,1) eq "[" ) {
		$iend = index($splitsub,"]");
		if ($iend != -1) {
		    $insplit = substr($splitsub,1,$iend - 1);
		    $insplit =~ s/\s/_/g;
		    $splitsub = "[".$insplit."]";
		}
	    }
	    if ($split eq "") {
		# first split
		$split = "|".$splitsub."|";
	    } else {
		# not first split :)
		$split = $split.$splitsub."|";
	    }
	    $found_split_com = 0;
	    #print "Split category = $split\n";
	} elsif ( m/^E/ ) {
	    # split comment
	    $split = $split.substr($_,1)."|";
	    $found_split_com = 1;
	} elsif ( m/^\$/ ) {
	    # split amount
	    if ( ! $found_split_com ) {
		$split = $split . "|";
	    }
	    $split_amt = substr($_,1);
	    $split_amt =~ s/\,//g;
	    $split = $split . $split_amt . "|";
	} elsif ( m/^M/ ) {
	    # coMment
	    $com = substr($_,1);
	    #print "Comment = $com\n";
	} elsif ( m/^\^/ ) {
	    #print "End of record\n";
	    if ($split ne "") {
		$cat = $split;
	    }
	    &create_trans(
	    	"$date\t$check\t$desc\t$debit\t$credit\t$cat\t$com\t$cleared\t0.00" );
	    ($date, $check, $desc, $debit, $credit, $cat, $split, $com, 
	    	$cleared) = ("", "", "", "", "", "", "", "", "");
	} elsif ( m/^A(\S*)\s*$/ ) {
	    if ($1 ne "") {
		# print "address line: $_\n";
	    }
	} elsif ( $_ eq "" ) {
	    # toss empty lines ...
	} else {
	    print "unknown data: $_\n";
	}
    }

    close(QIF);

    return "ok";
}


1;				# need to return a true value