adding biblio.record_entry to init
[migration-tools.git] / unicorn / unicorn_bills_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
20 # Converts a Unicorn users.data, bill.data, or charge.data file to a tab-separated file.
21 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
22
23 my @records;
24 my $serial = -1;
25 my $line = 0;
26 my $section = '';
27 my $field = '';
28 my %unique_fields;
29 my @output_fields = qw( l_form l_user_id l_item_id l_billing_lib l_bill_date l_bill_amt l_bill_reason ); # in that order
30
31 # Load each record
32 while (<>) {
33     s/\r\n/\n/g;
34 # print STDERR "Loaded this line: " . $_;
35
36         # Is this the start of a new record?
37         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
38                 $line = 0;
39                 $serial++;
40                 $section = ''; # just in case this didn't get reset in the previous record
41                 # print STDERR "Processing record $serial.\n";
42                 next;
43         }
44
45         # Is this a FORM= line?
46         if ( /^FORM=(.*)/ ) {
47                 $records[$serial]{'l_form'} = $1;
48                 next;
49         }
50
51         # If this isn't the start of the new record, it's a new line in the present record.
52         $line++;
53
54         # Is this line the beginning of a block of data (typically an address or a note)?
55         if ( /^\.(.*?)_BEGIN.$/ ) {
56                 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
57                 $section = "$1.";
58                 next;
59         }
60
61         # Is this line the beginning of a block of data (typically an address or a note)?
62         if ( /^\.(.*?)_END.$/ ) {
63                 if ("$1." ne $section) {
64                         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";
65                 }
66                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
67                 $section = '';
68                 next;
69         }
70
71         # Looks like we've got some actual data!  Let's store it.
72         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
73         if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
74
75                 # Build the name of this field (taking note of whether we're in a named section of data)
76                 $field = '';
77                 if ($section ne '') { 
78                         $field .= $section;
79                 }
80                 $field .= $1;
81
82                 # Now we can actually store this line of data!
83
84                 if ($field eq 'USER_ID')      { $records[$serial]{'l_user_id'}     = $3; }
85                 if ($field eq 'ITEM_ID')      { $records[$serial]{'l_item_id'}     = $3; }
86                 if ($field eq 'BILL_DB')      { $records[$serial]{'l_bill_date'}   = $3; }
87                 if ($field eq 'BILL_AMOUNT')  { $records[$serial]{'l_bill_amt'}    = $3; }
88                 if ($field eq 'BILL_LIBRARY') { $records[$serial]{'l_billing_lib'} = $3; }
89                 if ($field eq 'BILL_REASON')  { $records[$serial]{'l_bill_reason'} = $3; }
90
91                 next;
92         }       
93
94         # This is the continuation of the previous line.
95         else {
96                 chomp($_);
97                 $records[$serial]{$field} .= ' ' . $_;
98                 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
99         }
100
101 }
102
103 print STDERR "Loaded " . scalar(@records) . " records.\n";
104
105 # Print a header line
106 foreach $i (@output_fields) {
107         print "$i\t";
108 }
109 print "\n";
110
111
112 # Print the results
113 for (my $u = 0; $u < @records; $u++) {
114         foreach $f (@output_fields) {
115                 if (defined $records[$u]{$f}) {
116                         print $records[$u]{$f};
117                 }
118                 print "\t";
119         }
120         print "\n";
121 }
122
123 print STDERR "Wrote " . scalar(@records) . " records.\n";
124 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS