From 960f1301f2b39965ffb1b0d4aebdd65b33a423ea Mon Sep 17 00:00:00 2001 From: mxbeaulieu Date: Wed, 12 Aug 2015 15:44:20 -0400 Subject: [PATCH] Bug 12772 - Reworked overdue_notices.pl script modified: misc/cronjobs/overdue_notices.pl --- misc/cronjobs/overdue_notices.pl | 695 ++++++++++++++++---------------------- 1 file changed, 288 insertions(+), 407 deletions(-) diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl index 98b0a25..8a68744 100755 --- a/misc/cronjobs/overdue_notices.pl +++ b/misc/cronjobs/overdue_notices.pl @@ -316,71 +316,43 @@ pod2usage( -verbose => 2 ) if $man; cronlogaction(); -if ( defined $csvfilename && $csvfilename =~ /^-/ ) { - warn qq(using "$csvfilename" as filename, that seems odd); +sub VERBOSE{ + if ($verbose){ + printf STDERR "%s\n", shift; + } } -my @overduebranches = C4::Overdues::GetBranchcodesWithOverdueRules(); # Branches with overdue rules -my @branches; # Branches passed as parameter with overdue rules -my $branchcount = scalar(@overduebranches); - -my $overduebranch_word = scalar @overduebranches > 1 ? 'branches' : 'branch'; -my $branchcodes_word = scalar @branchcodes > 1 ? 'branches' : 'branch'; - -my $PrintNoticesMaxLines = C4::Context->preference('PrintNoticesMaxLines'); - -if ($branchcount) { - $verbose and warn "Found $branchcount $overduebranch_word with first message enabled: " . join( ', ', map { "'$_'" } @overduebranches ), "\n"; -} else { - die 'No branches with active overduerules'; +if ( defined $csvfilename && $csvfilename =~ /^-/ ) { + warn qq(using "$csvfilename" as filename, that seems odd); } -if (@branchcodes) { - $verbose and warn "$branchcodes_word @branchcodes passed on parameter\n"; - - # Getting libraries which have overdue rules - my %seen = map { $_ => 1 } @branchcodes; - @branches = grep { $seen{$_} } @overduebranches; - - - if (@branches) { - - my $branch_word = scalar @branches > 1 ? 'branches' : 'branch'; - $verbose and warn "$branch_word @branches have overdue rules\n"; - - } else { - - $verbose and warn "No active overduerules for $branchcodes_word '@branchcodes'\n"; - ( scalar grep { '' eq $_ } @branches ) - or die "No active overduerules for DEFAULT either!"; - $verbose and warn "Falling back on default rules for @branchcodes\n"; - @branches = (''); - } -} my $date_to_run; my $date; if ( $date_input ){ + $date = $dbh->quote($date); eval { - $date_to_run = dt_from_string( $date_input, 'iso' ); + $date_to_run = dt_from_string( $date_input ); }; - die "$date_input is not a valid date, aborting! Use a date in format YYYY-MM-DD." + die "$date_input is not a valid date, aborting!" if $@ or not $date_to_run; - # It's certainly useless to escape $date_input - # dt_from_string should not return something if $date_input is not correctly set. - $date = $dbh->quote( $date_input ); } else { $date="NOW()"; $date_to_run = dt_from_string(); } +## +## Some file/html preparation ? +## + +my $printNoticesMaxLines = C4::Context->preference('PrintNoticesMaxLines'); + # these are the fields that will be substituted into <> my @item_content_fields = split( /,/, $itemscontent ); binmode( STDOUT, ':encoding(UTF-8)' ); - our $csv; # the Text::CSV_XS object our $csv_fh; # the filehandle to the CSV file. if ( defined $csvfilename ) { @@ -392,26 +364,24 @@ if ( defined $csvfilename ) { } else { open $csv_fh, ">", $csvfilename or die "unable to open $csvfilename: $!"; } - if ( $csv->combine(qw(name surname address1 address2 zipcode city country email phone cardnumber itemcount itemsinfo branchname letternumber)) ) { + if ( $csv->combine(qw(name surname address1 address2 zipcode city country email itemcount itemsinfo)) ) { print $csv_fh $csv->string, "\n"; } else { - $verbose and warn 'combine failed on argument: ' . $csv->error_input; + VERBOSE( 'combine failed on argument: ' . $csv->error_input); } } -@branches = @overduebranches unless @branches; our $fh; if ( defined $htmlfilename ) { if ( $htmlfilename eq '' ) { $fh = *STDOUT; } else { my $today = DateTime->now(time_zone => C4::Context->tz ); - open $fh, ">:encoding(UTF-8)",File::Spec->catdir ($htmlfilename,"notices-".$today->ymd().".html"); + open $fh, ">",File::Spec->catdir ($htmlfilename,"notices-".$today->ymd().".html"); } print $fh "\n"; print $fh "\n"; - print $fh "\n"; print $fh "