strict_add_sf9
[migration-tools.git] / unicorn / unicorn_charges_to_tsv.pl
1 #!/usr/bin/perl -w
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 # Converts a Unicorn users.data, bill.data, or charge.data file to a tab-separated file.
20 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
21
22 my @records;
23 my $serial = -1;
24 my $line = 0;
25 my $section = '';
26 my $field = '';
27 my %unique_fields;
28 my @output_fields = qw( l_form l_user_id l_item_id l_charging_lib l_date_charged l_date_due l_date_claim_returned ); # in that order
29
30 # Load each record
31 while (<>) {
32     s/\r\n/\n/g;
33 # print STDERR "Loaded this line: " . $_;
34
35         # Is this the start of a new record?
36         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
37                 $line = 0;
38                 $serial++;
39                 $section = ''; # just in case this didn't get reset in the previous record
40                 # print STDERR "Processing record $serial.\n";
41                 next;
42         }
43
44         # Is this a FORM= line?
45         if ( /^FORM=(.*)/ ) {
46                 $records[$serial]{'l_form'} = $1;
47                 next;
48         }
49
50         # If this isn't the start of the new record, it's a new line in the present record.
51         $line++;
52
53         # Is this line the beginning of a block of data (typically an address or a note)?
54         if ( /^\.(.*?)_BEGIN.$/ ) {
55                 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
56                 $section = "$1.";
57                 next;
58         }
59
60         # Is this line the beginning of a block of data (typically an address or a note)?
61         if ( /^\.(.*?)_END.$/ ) {
62                 if ("$1." ne $section) {
63                         print STDERR "Error in record $serial, line $line (input line $.): got an end-of-$1 but I thought I was in a $section block!\n";
64                 }
65                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
66                 $section = '';
67                 next;
68         }
69
70         # Looks like we've got some actual data!  Let's store it.
71         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
72         if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
73
74                 # Build the name of this field (taking note of whether we're in a named section of data)
75                 $field = '';
76                 if ($section ne '') { 
77                         $field .= $section;
78                 }
79                 $field .= $1;
80
81                 # Now we can actually store this line of data!
82
83                 if ($field eq 'USER_ID')           { $records[$serial]{'l_user_id'}      = $3; }
84                 if ($field eq 'ITEM_ID')           { $records[$serial]{'l_item_id'}      = $3; }
85                 if ($field eq 'CHRG_DC')           { $records[$serial]{'l_date_charged'} = $3; }
86                 if ($field eq 'CHRG_LIBRARY')      { $records[$serial]{'l_charging_lib'} = $3; }
87                 if ($field eq 'CHRG_DATEDUE')      { $records[$serial]{'l_date_due'}     = $3; }
88                 if ($field eq 'CHRG_DATE_CLMRET')  { $records[$serial]{'l_bill_reason'}  = $3; }
89                 next;
90         }       
91
92         # This is the continuation of the previous line.
93         else {
94                 chomp($_);
95                 $records[$serial]{$field} .= ' ' . $_;
96                 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
97         }
98
99 }
100
101 print STDERR "Loaded " . scalar(@records) . " records.\n";
102
103 # Print a header line
104 foreach $i (@output_fields) {
105         print "$i\t";
106 }
107 print "\n";
108
109
110 # Print the results
111 for (my $u = 0; $u < @records; $u++) {
112         foreach $f (@output_fields) {
113                 if (defined $records[$u]{$f}) {
114                         print $records[$u]{$f};
115                 }
116                 print "\t";
117         }
118         print "\n";
119 }
120
121 print STDERR "Wrote " . scalar(@records) . " records.\n";
122 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS