From 39f60ed9262b99048a1a800f87ee450240ef36e6 Mon Sep 17 00:00:00 2001 From: Thibaud Guillot Date: Wed, 1 Feb 2023 09:19:59 +0100 Subject: [PATCH] Bug 18064: (follow up) rebase on master and reorder syspref --- ...4-add_LongOverdueNoticeCalendar_syspref.pl | 12 ++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../admin/preferences/circulation.pref | 6 ++ misc/cronjobs/longoverdue.pl | 64 +++++++++++++++++-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_18064-add_LongOverdueNoticeCalendar_syspref.pl diff --git a/installer/data/mysql/atomicupdate/bug_18064-add_LongOverdueNoticeCalendar_syspref.pl b/installer/data/mysql/atomicupdate/bug_18064-add_LongOverdueNoticeCalendar_syspref.pl new file mode 100644 index 0000000000..00ec0eec7e --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_18064-add_LongOverdueNoticeCalendar_syspref.pl @@ -0,0 +1,12 @@ +use Modern::Perl; + +return { + bug_number => "BUG_18064", + description => "Add new system preference LongOverdueNoticeCalendar", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('LongOverdueNoticeCalendar', '0', NULL, 'Take the calendar into consideration when generating long overdue notices', 'Yes/No') }); + }, +}; \ No newline at end of file diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index beebd27a6f..24058b0ec8 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -352,6 +352,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('LocalHoldsPriorityItemControl', 'holdingbranch', 'holdingbranch|homebranch', 'decides if the feature operates using the item''s home or holding library.', 'Choice'), ('LocalHoldsPriorityPatronControl', 'PickupLibrary', 'HomeLibrary|PickupLibrary', 'decides if the feature operates using the library set as the patron''s home library, or the library set as the pickup library for the given hold.', 'Choice'), ('LockExpiredDelay','','','Delay for locking expired patrons (empty means no locking)','Integer'), +('LongOverdueNoticeCalendar',0,NULL,'Take the calendar into consideration when generating long overdue notices','YesNo'), ('makePreviousSerialAvailable','0','','make previous serial automatically available when collecting a new serial. Please note that the item-level_itypes syspref must be set to specific item.','YesNo'), ('Mana','2',NULL,'request to Mana Webservice. Mana centralize common information between other Koha to facilitate the creation of new subscriptions, vendors, report queries etc... You can search, share, import and comment the content of Mana.','Choice'), ('ManInvInNoissuesCharge','1',NULL,'MANUAL_INV charges block checkouts (added to noissuescharge).','YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref index e025fc6e9d..f5ae0f4427 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -403,6 +403,12 @@ Circulation: - "Send all notices as a BCC to this email address:" - pref: NoticeBcc class: email + - + - pref: LongOverdueNoticeCalendar + choices: + 1: Use calendar + 0: Ignore calendar + - "when working out the period for long overdue notices." - - pref: OverdueNoticeCalendar choices: diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 2d313b77b2..d8b1abe2dd 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -30,6 +30,8 @@ use warnings; use Getopt::Long qw( GetOptions ); use Pod::Usage qw( pod2usage ); +use DateTime; +use DateTime::Duration; use C4::Circulation qw( LostItem MarkIssueReturned ); use C4::Context; @@ -38,6 +40,9 @@ use Koha::ItemTypes; use Koha::Patron::Categories; use Koha::Patrons; use Koha::Script -cron; +use Koha::Calendar; +use Koha::DateUtils qw( dt_from_string ); + my $lost; # key=lost value, value=num days. my ($charge, $verbose, $confirm, $quiet); @@ -314,6 +319,36 @@ sub longoverdue_sth { return C4::Context->dbh->prepare($query); } +my @holidays_by_branch; + +sub get_number_of_holidays { + my ( $start_date, $end_date ) = @_; + + $start_date = dt_from_string($start_date); + $end_date = dt_from_string($end_date); + + my @branches = Koha::Libraries->search( {}, { order_by => ['branchcode'] } ); + my $calendar; + + foreach my $branch ( @branches ) { + my $date_to_run = $start_date->clone(); + my $branchcode = $branch->branchcode; + my $i = 0; + + $calendar = Koha::Calendar->new( branchcode => $branchcode ); + + while ( $date_to_run ne $end_date ) { + # printf "\n Treatment of the day : %s \n", $date_to_run; + if ( $calendar->is_holiday( $date_to_run ) ) { + $i++; + } + $date_to_run->add( days => 1 ); + } + push @holidays_by_branch, { 'branchcode' => $branchcode, 'nb_holidays' => $i }; + } + return @holidays_by_branch; +} + my $dbh = C4::Context->dbh; my @available_categories = Koha::Patron::Categories->search()->get_column('categorycode'); @@ -386,11 +421,30 @@ foreach my $startrange (sort keys %$lost) { if( my $lostvalue = $lost->{$startrange} ) { my ($date1) = bounds($startrange); my ($date2) = bounds( $endrange); + + if ( C4::Context->preference( 'LongOverdueNoticeCalendar' ) ) { + get_number_of_holidays( $date2, $date1 ); + + foreach my $branch ( @holidays_by_branch ) { + + my $date2_with_holidays = dt_from_string($date2) + ->subtract( days => $branch->{'nb_holidays'} )->ymd; + my $endrange_with_holidays = + $endrange + $branch->{'nb_holidays'}; + + $verbose and + printf "\nBranchcode %s\nDue %3s - %3s days ago (%s to %s) - with %s holiday(s), lost => %s\n", $branch->{'branchcode'}, + $startrange, $endrange_with_holidays, $date2_with_holidays, $date1, $branch->{'nb_holidays'}, $lostvalue; + $sth_items->execute($startrange, $endrange_with_holidays, $lostvalue); + } + } else { + # print "\nRange ", ++$i, "\nDue $startrange - $endrange days ago ($date2 to $date1), lost => $lostvalue\n" if($verbose); $verbose and printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i, $startrange, $endrange, $date2, $date1, $lostvalue; $sth_items->execute($startrange, $endrange, $lostvalue); + } $count=0; ITEM: while (my $row=$sth_items->fetchrow_hashref) { if( $filter_borrower_categories ) { @@ -440,10 +494,12 @@ sub summarize { } } -if (!$quiet){ - print "\n### LONGOVERDUE SUMMARY ###"; - summarize (\@report, 1); - print "\nTOTAL: $total items\n"; +if (!( C4::Context->preference( 'LongOverdueNoticeCalendar' ) )) { + if (!$quiet){ + print "\n### LONGOVERDUE SUMMARY ###"; + summarize (\@report, 1); + print "\nTOTAL: $total items\n"; + } } cronlogaction({ action => 'End', info => "COMPLETED" }); -- 2.25.1