· 8 years ago · Mar 10, 2018, 08:38 PM
1sub processPatronRecords {
2
3 # -- verify the number of argument is correct - ideally we should do a lot more checks
4 if ( scalar @_ < 2 ) { die "Too few arguments passed to sub. Please review subroutine for required arguments." }
5
6 # -- assign arguments to vars
7 my ( $arrayref_webprod_db,$arrayref_rec) = @_;
8
9 # -- get and store patron exceptions from webprod database (like most libraries, some patrons get elevated privileges)
10 # -- calls a sub to do the work, must pass db connection info
11 my $hashref_patexcep = getPatronExceptions( $arrayref_webprod_db->[0],$arrayref_webprod_db->[1],$arrayref_webprod_db->[2] );
12
13 # -- store sif segment data in this hash
14 # -- hash value is anonymous array with 4 strings
15 my %all_seg_data;
16
17 # -- store process errors in this array
18 my @process_warn_errors;
19
20 # -- loop thru the data array (sql rows)
21 foreach my $row ( @$arrayref_rec ) {
22
23 # -- change all NULL columns to empty strings to avoid perl warnings
24 foreach ( @$row ) { $_ = '' unless defined }
25
26 # -- split the current row up into vars - the provided record set must include all fields in this order even if empty column
27 my ( $skidmore_id,$iso_number,$barcode1_stat,$patron_type,$stat_cat1,$name_type,$last_name,$first_name,$middle_name,
28 $loc_add1,$loc_add2,$loc_city,$loc_state,$loc_phone,
29$perm_add1,$perm_add2,$perm_city,$perm_state,$perm_zip,$perm_phone,
30 $email,$local_po_box,$enter_in_title,$ext_reg_date,$today,$expire_date,$purge_date) = @$row;
31
32 # -- if no last name then process error and skipped record (last name is required in SIF file)
33 if ( not defined $last_name ) {
34
35 push( @process_warn_errors, data_warning_logger( $skidmore_id,$last_name,$first_name,'no_last_name','skipped','' ) );
36 next;
37
38 }
39
40 # -- remove all commas in city - Voyager update utility will not import record if comma present
41 $loc_city =~ s/\,//g;
42 $perm_city =~ s/\,//g;
43
44 # -- check if patron is a special patron and recode to patron type found in hash value - lookup by skidmore id
45 if (exists $$hashref_patexcep{$skidmore_id}) {
46
47 $patron_type = $$hashref_patexcep{$skidmore_id};
48 # -- record this action
49 push( @process_warn_errors, dataWarningLogger( $skidmore_id,$last_name,$first_name,'patron_recode','included','' ) );
50
51 }
52
53 # -- check if no addresses
54 # -- if local address1, permanent address1, and email empty then process error and skip record
55 if ( ( !$loc_add1 ) and ( !$perm_add1 ) and ( !$email ) ) {
56
57 # -- record this action
58 push( @process_warn_errors, dataWarningLogger( $skidmore_id,$last_name,$first_name,'no_address','skipped','' ) );
59 next;
60
61 }
62
63 # -- these vars are used in all address segments
64 my ( $add_beg_date,$add_end_date ) = ( $today,$expire_date );
65
66 # -- set default number of address segment to zero, we will add to this when required
67 my $number_of_add_segs = 0;
68
69 # ------------------------------------------------------------------------ #
70 # -- collect data from address segments first
71 # -- once these are created we will collect data for the base segment
72 # -- this sounds backwards but the number of address segments is a field needed in the base segment
73 # ------------------------------------------------------------------------ #
74
75 # -- if local address1 has some data in it then create local address segment
76 # -- consider this a temporary address, hence the '2' which is the sif code for "temp"
77 if ( $local_po_box ) {
78
79 # -- fields: address id,address type,address status code,add begin date,add end date,add1,add2,city,state,zip,phone
80 # -- data must be in this exact order and all are required here
81 $all_seg_data{$skidmore_id}{box_add} = [ 0,2,'N',$add_beg_date,$add_end_date,$local_po_box,$loc_add2,$loc_city,$loc_state, '', $loc_phone ];
82 $number_of_add_segs++;
83
84 }
85
86 # -- if permanent address1 has some data in it then create permanent address segment -
87 # -- the address type of '1' indicates it a permanent address
88 if ( $perm_add1 ) {
89
90 $all_seg_data{$skidmore_id}{perm_add} = [ 0,1,'N',$add_beg_date,$add_end_date,$perm_add1,$perm_add2,$perm_city,$perm_state,$perm_zip,$perm_phone ];
91 $number_of_add_segs++;
92
93 }
94
95 # -- if email has some data in it then create email address segment
96 if ( $email ) {
97
98 # -- email is put in add1 according to sif specs - the address type of '3' indicates it is an email address
99 $all_seg_data{$skidmore_id}{email_add} = [ 0, 3,'N',$add_beg_date,$add_end_date,$email,'','','','','' ];
100 $number_of_add_segs++;
101
102 }
103
104 # ------------------------------------------------------------------------ #
105 # -- base address, all records must have this
106 # -- fields: patron id,bc,patron type,bc status,reg date,expire data,purge data,skidmore id,stat cat1,record type,lname,fname,mname,
107 # -- title (class year or other short text),num of add segments
108 # -- must be in this exact order and all are required
109 # ------------------------------------------------------------------------ #
110
111 $all_seg_data{$skidmore_id}{base} = [ 0,$iso_number,$patron_type,$barcode1_stat,$ext_reg_date,$expire_date,$purge_date,
112 $skidmore_id,$stat_cat1,$name_type,$last_name,$first_name,$middle_name,$enter_in_title,$number_of_add_segs ];
113
114 } ## end for each
115
116 # -- return hash and array reference
117 return ( \%all_seg_data, \@process_warn_errors );
118
119} ## end sub
120
121
122# ------------------------------------------------------------------------ #
123# -- retrieves list of patrons where patron_type recode is required
124# -- returns hash ref containing patron skidmore id and new patron type
125# -- must pass sub the following parameters:
126# -- 1) <database name> db dsn
127# -- 2) <database name> db username
128# -- 3) <database name> db password
129# ------------------------------------------------------------------------ #
130sub getPatronExceptions {
131
132 my ( $dsn,$dbuser,$dbpass ) = @_; ## get connection info from
133 my ( $dbh,$sth );
134
135 $dbh = DBI-> connect( 'dbi:Oracle:<database name>',$dbuser,$dbpass, { AutoCommit => 0, RaiseError => 0, PrintError $
136
137 my %patrons; #store id and patron_type from webprod table
138
139 my $sqlquery = "SELECT skidmore_id, new_patron_group FROM patron_exceptions";
140 $sth = $dbh->prepare( $sqlquery ) || die "Error preparing sql query for patron exceptions";
141 $sth->execute;
142
143 my ( $skidmore_id,$new_patron_group );
144 $sth->bind_columns( \( $skidmore_id,$new_patron_group ) );
145
146 # -- loop thru returned records adding to hash
147 while ( $sth->fetch ) {
148
149 $patrons{ $skidmore_id } = $new_patron_group;
150
151 } ## end fetch while
152
153 $sth->finish;
154 $dbh->disconnect;
155
156 # -- return hash reference
157 return \%patrons;
158
159} ## end sub