File: export.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 (139 lines) | stat: -rw-r--r-- 3,583 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
#!/usr/local/bin/perl
#  export.pl - functions to implement exporting data to other formats
#
#  Written by Curtis Olson.  Started October 19, 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: export.pl,v 1.1.1.1 1999/12/18 02:04:58 curt Exp $


package CBB;

use strict;   # don't take no guff


# save data in the CBB format
sub save_cbb {
    # in: file base name
    # out: result

    my($file) = @_;
    my($key);

    if ($CBB::calced == 0) {
	&calc_trans();
    }

    open(SAVE, ">$file.new");

    if ($CBB::sorted_keys == 0) {
	&sort_keys();
    }

    foreach $key (@CBB::KEYS) {
	print (SAVE "$CBB::TRANS{$key}\n");
    }

    close(SAVE);

    unlink("$file.bak");
    rename("$file", "$file.bak");
    rename("$file.new", "$file");

    return "ok";
}


# Contributed by Christopher Browne, Oct. 18/94
# export a quicken export file (.qif)
sub export_qif {
    # in: file
    # out: result
    
    my($file) = @_;
    my($key);
    my($date, $check, $desc, $debit, $credit, $cat, $split, $com, $cleared);
    my($amount, $i, @SPLIT, $scom, $scat, $junk);
    my($dy, $mo, $yr);

    $CBB::sorted_keys = 0;
    $CBB::calced = 0;
    
    open(QIF, ">$file");
    
    print QIF "Type:Bank\n";      # This will need to change,
    # eventually, to handle other
    # varieties of QUICKEN accounts.
    # Credit cards, investments, etc.
    # Later.

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

    foreach $key (keys (%CBB::TRANS)) {
        ($date, $check, $desc, $debit, $credit, $cat, $com, $cleared,
	 $junk) = split(/\t/, $CBB::TRANS{$key});
     
	# Handle date
	$yr = substr($date, 2, 2);
	$mo = substr($date, 4, 2);
	$dy = substr($date, 6, 2);
	printf QIF "D%d/%2d/%2d\n", $mo, $dy, $yr;
     
	# Handle amount
	$amount = $credit-$debit;
	printf QIF "T%.2lf\n", $amount;

	printf QIF "C$cleared\n" unless ($cleared eq "");
     
	# Handle Ref. #
	print QIF "N$check\n" unless ($check eq "");
	print QIF "P$desc\n" unless ($desc eq "");

	# Replace underscores by blanks in transfer categories. (BW)  
	while (($cat =~ s/\[([^\]_]*)_([^\]]*)\]/\[$1 $2\]/g) != 0) {
	}

	print QIF "L$cat\n" unless (substr($cat, 0, 1) eq "\|");  # split txn
	# Handle splitting of txn
	if (substr($cat, 0, 1) eq "\|")	{
	    # Take: |Salary|2434.70|Tax-Fed|-0.34|Tax-FICA|-33.25|Tax-State|-5.78|
	    @SPLIT = split(/\|/, $cat);
	    print QIF "L$SPLIT[1]\n";   
	    # Pretend that the initial category is "the one"
	    for ($i = 1 ; $i <= $#SPLIT ; $i += 3) {
		$scat = $SPLIT[$i];
		print QIF "S$scat\n";
		$scom = $SPLIT[$i+1];
		print QIF "E$scom\n";
		$amount = $SPLIT[$i+2];
		print QIF "\$$amount\n";
	    }
	} else {
	}
	print QIF "M$com\n" unless ($com eq "");
	print QIF  "^\n";
    }
    
    close(QIF);

    return "ok";
}


1;				# need to return a true value