From a57db8d26dbe75686cf201e991b06a2f28a34aa7 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 12 Jun 2017 11:51:43 +0200 Subject: [PATCH] Bug 18783: Allow automatic claims for serials This patch adds a new script misc/cronjobs/serialsClaim.pl which send email notification to all suppliers that have late or missing issues. This is configurable per subscription and is disabled by default. Test plan: 1. Create a new bookseller B1 with a delivery time of 5 days 2. Create a new daily subscription with bookseller B1 and enable automatic claims (new checkbox on the first page of subscription-add.pl) 3. Generate some late serials with an expected date in the past (some with more than 5 days in the past, some with less) 4. Create a notice for module "Claim serial issue" CLAIMSERIAL and write something in the 'Email' template 5. Run `misc/cronjobs/serialsClaims.pl --letter-code CLAIMSERIAL` and verify that it sends an email notification only for serials that were expected more than 5 days ago Rescued-by: Martin Renvoize --- C4/Letters.pm | 5 +- C4/Serials.pm | 23 ++-- .../data/mysql/atomicupdate/bug-18783.pl | 23 ++++ installer/data/mysql/kohastructure.sql | 3 +- .../en/modules/serials/subscription-add.tt | 8 ++ .../en/modules/serials/subscription-detail.tt | 1 + misc/cronjobs/serialsClaim.pl | 127 ++++++++++++++++++ serials/subscription-add.pl | 16 ++- 8 files changed, 186 insertions(+), 20 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug-18783.pl create mode 100755 misc/cronjobs/serialsClaim.pl diff --git a/C4/Letters.pm b/C4/Letters.pm index 8087f6bdeeb..b60fafa6a26 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -496,7 +496,7 @@ sub SendAlerts { ? C4::Context->preference('SerialsDefaultEmailAddress') : C4::Context->preference('AcquisitionsDefaultEmailAddress') ) - || $library->branchemail + || ( $library ? $library->branchemail : undef ) || C4::Context->preference('KohaAdminEmailAddress'), ( $type eq 'claimissues' && C4::Context->preference('SerialsDefaultReplyTo') @@ -520,7 +520,8 @@ sub SendAlerts { } my $success = try { - $mail->send_or_die( { transport => $library->smtp_server->transport } ); + my $smtp_server = $library ? $library->smtp_server : Koha::SMTP::Servers->get_default; + $mail->send_or_die( { transport => $smtp_server->transport } ); } catch { # We expect ref($_) eq 'Email::Sender::Failure' diff --git a/C4/Serials.pm b/C4/Serials.pm index 18f7318e506..2ff83bf46a6 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -1347,7 +1347,8 @@ sub ModSubscription { $biblionumber, $callnumber, $notes, $letter, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq, - $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template + $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template, + $auto_claim_enabled ) = @_; my $subscription = Koha::Subscriptions->find($subscriptionid); @@ -1392,6 +1393,7 @@ sub ModSubscription { mana_id => $mana_id, ccode => $ccode, published_on_template => $published_on_template, + auto_claim_enabled => $auto_claim_enabled // 0, } )->store; @@ -1424,14 +1426,14 @@ the id of this new subscription sub NewSubscription { my ( - $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, - $startdate, $periodicity, $numberlength, $weeklength, $monthlength, - $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, - $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity, - $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, - $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, - $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, - $published_on_template, + $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, + $startdate, $periodicity, $numberlength, $weeklength, $monthlength, + $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, + $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity, + $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, + $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, + $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, + $published_on_template, $auto_claim_enabled ) = @_; my $dbh = C4::Context->dbh; @@ -1476,6 +1478,7 @@ sub NewSubscription { mana_id => $mana_id, ccode => $ccode, published_on_template => $published_on_template, + auto_claim_enabled => $auto_claim_enabled // 0, } )->store; $subscription->discard_changes; @@ -1935,7 +1938,7 @@ sub GetLateOrMissingIssues { if ($supplierid) { $sth = $dbh->prepare( "SELECT - serialid, aqbooksellerid, name, + serialid, serial.aqbooksellerid, name, biblio.title, biblioitems.issn, planneddate, serialseq, serial.status, serial.subscriptionid, claimdate, serial.notes AS notes, claims_count, subscription.branchcode, serial.publisheddate diff --git a/installer/data/mysql/atomicupdate/bug-18783.pl b/installer/data/mysql/atomicupdate/bug-18783.pl new file mode 100755 index 00000000000..1526df88dc0 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-18783.pl @@ -0,0 +1,23 @@ +use Modern::Perl; + +return { + bug_number => "18783", + description => "Automatic claims for serials", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + unless ( column_exists( 'subscription', 'auto_claim_enabled' ) ) { + $dbh->do( + q{ + ALTER TABLE subscription + ADD COLUMN auto_claim_enabled tinyint NOT NULL DEFAULT 0 COMMENT 'enable automatic claiming' AFTER published_on_template + } + ); + + say $out "Added column subscription.auto_claim_enabled"; + } else { + say $out "Column subscription.auto_claim_enabled already exists"; + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 5d7139383c2..f28937205f0 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -6187,6 +6187,7 @@ CREATE TABLE `subscription` ( `mana_id` int(11) DEFAULT NULL, `ccode` varchar(80) DEFAULT NULL COMMENT 'collection code to assign to serial items', `published_on_template` text DEFAULT NULL COMMENT 'Template Toolkit syntax to generate the default "Published on (text)" field when receiving an issue this serial', + `auto_claim_enabled` tinyint NOT NULL DEFAULT 0 COMMENT 'enable automatic claiming', PRIMARY KEY (`subscriptionid`), KEY `subscription_ibfk_1` (`periodicity`), KEY `subscription_ibfk_2` (`numberpattern`), @@ -6798,4 +6799,4 @@ CREATE TABLE `zebraqueue` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2025-05-26 13:26:31 +-- Dump completed on 2024-11-25 12:13:27 diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt index 1e62a1108e4..a56e298b520 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt @@ -262,6 +262,14 @@ day(s) +
  • + + [% IF auto_claim_enabled %] + + [% ELSE %] + + [% END %] +
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt index 6d25533f1ad..fec2cc19017 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt @@ -138,6 +138,7 @@ [% END %]
  • Grace period: [% graceperiod | html %]
  • +
  • Automatic claims: [% IF auto_claim_enabled %]Enabled[% ELSE %]Disabled[% END %]
  • diff --git a/misc/cronjobs/serialsClaim.pl b/misc/cronjobs/serialsClaim.pl new file mode 100755 index 00000000000..817772439e1 --- /dev/null +++ b/misc/cronjobs/serialsClaim.pl @@ -0,0 +1,127 @@ +#!/usr/bin/perl + +# Copyright 2017 Biblibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +serialsClaims.pl - Send claim notice to suppliers that have late or missing issues + +=head1 SYNOPSIS + +serialsClaims.pl --letter-code LETTER_CODE [--max-claims MAX_CLAIMS] + +Options: + --letter-code LETTER_CODE Mandatory. Use letter LETTER_CODE. + --max-claims MAX_CLAIMS Send up to MAX_CLAIMS notices + +=cut + +use Modern::Perl; + +use Koha::Script -cron; + +use Koha::Acquisition::Booksellers; +use Koha::DateUtils qw( dt_from_string ); +use Koha::Subscriptions; + +use C4::Context; +use C4::Letters; +use C4::Serials; + +use Getopt::Long qw( GetOptions ); + +sub usage { + return < \$help, + 'letter-code=s' => \$letter_code, + 'max-claims=i' => \$max_claims, +) or die usage(); + +if ($help) { + say usage(); + exit; +} + +die usage() unless $letter_code; + +cronlogaction( { info => $command_line_options } ); + +my $suppliers = C4::Serials::GetSuppliersWithLateIssues(); +foreach my $supplier (@$suppliers) { + my $bookseller = Koha::Acquisition::Booksellers->find( $supplier->{id} ); + my $supplierinfo = $bookseller->name . ' (' . $bookseller->id . ')'; + + my $deliverytime = $bookseller->deliverytime; + unless ($deliverytime) { + say STDERR + "Warning: There is no delivery time set for supplier $supplierinfo. Automatic claiming is disabled for this supplier."; + next; + } + + my @serials = C4::Serials::GetLateOrMissingIssues( $bookseller->id ); + my @serialids; + foreach my $serial (@serials) { + if ( $max_claims && $serial->{claims_count} >= $max_claims ) { + say "Maximum claims count reached for serial " . $serial->{serialid}; + next; + } + + my $subscription = Koha::Subscriptions->find( $serial->{subscriptionid} ); + next unless $subscription->auto_claim_enabled; + + my $claims_count = $serial->{claims_count}; + my $date = $serial->{claimdate} ? $serial->{claimdate} : $serial->{planneddate}; + my $newclaimdate = dt_from_string($date)->add( days => $deliverytime ); + my $now = dt_from_string; + + if ( $newclaimdate <= $now ) { + push @serialids, $serial->{serialid}; + } + } + + if (@serialids) { + my $err; + eval { + $err = C4::Letters::SendAlerts( 'claimissues', \@serialids, $letter_code ); + if ( not ref $err or not exists $err->{error} ) { + C4::Serials::updateClaim( \@serialids ); + } + }; + if ($@) { + say STDERR "Error: $@ while claiming serials " . join( ',', @serialids ); + } elsif ( ref $err and exists $err->{error} ) { + if ( $err->{error} eq "no_email" ) { + say STDERR "Error: Supplier $supplierinfo has no email"; + } elsif ( $err->{error} =~ m|Bad or missing From address| ) { + say STDERR "Error: Supplier $supplierinfo has no email"; + } else { + say STDERR "Error: " . $err->{error}; + } + } else { + say "Claim sent to $supplierinfo for serials " . join( ',', @serialids ); + } + } +} diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index 8dff9699782..f46c70e5912 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -349,6 +349,7 @@ sub redirect_add_subscription { my $skip_serialseq = $query->param('skip_serialseq'); my $ccode = $query->param('ccode'); my $published_on_template = $query->param('published_on_template'); + my $auto_claim_enabled = $query->param('auto_claim_enabled'); my $mana_id; if ( $query->param('mana_id') ne "" ) { @@ -376,7 +377,7 @@ sub redirect_add_subscription { join( ";", @irregularity ), $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, - $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template + $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template, $auto_claim_enabled ); if ( ( C4::Context->preference('Mana') == 1 ) and ( grep { $_ eq "subscription" } split( /,/, C4::Context->preference('AutoShareWithMana') ) ) ) @@ -458,6 +459,7 @@ sub redirect_mod_subscription { my $skip_serialseq = $query->param('skip_serialseq'); my $ccode = $query->param('ccode'); my $published_on_template = $query->param('published_on_template'); + my $auto_claim_enabled = $query->param('auto_claim_enabled'); my $mana_id; if ( $query->param('mana_id') ne "" ) { @@ -489,12 +491,12 @@ sub redirect_mod_subscription { ModSubscription( $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate, join( ";", @irregularity ), - $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1, - $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, - $status, $biblionumber, $callnumber, $notes, $letter, - $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, - $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid, - $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template + $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1, + $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, + $status, $biblionumber, $callnumber, $notes, $letter, + $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, + $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid, + $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template, $auto_claim_enabled ); my @additional_fields = -- 2.50.0