From 6ff2d4f261b989cb32a744f2ce430ade0edbddcb 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 --- C4/Letters.pm | 12 +- C4/Serials.pm | 9 +- .../data/mysql/atomicupdate/bug-18783.pl | 22 ++++ installer/data/mysql/kohastructure.sql | 1 + .../en/modules/serials/subscription-add.tt | 8 ++ .../en/modules/serials/subscription-detail.tt | 1 + misc/cronjobs/serialsClaim.pl | 124 ++++++++++++++++++ serials/subscription-add.pl | 6 +- 8 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 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 87b82624c6..821994b8b0 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -474,7 +474,13 @@ sub SendAlerts { $letter->{content} =~ s/(.*?)<\/order>/$1/gxms; # ... then send mail + my $from; my $library = Koha::Libraries->find( $userenv->{branch} ); + if ($library and $library->branchemail) { + $from = $library->branchemail; + } else { + $from = C4::Context->preference('KohaAdminEmailAddress'); + } my $mail = Koha::Email->create( { to => join( ',', @email ), @@ -488,8 +494,7 @@ sub SendAlerts { ? ( bcc => $userenv->{emailaddress} ) : () ), - from => $library->branchemail - || C4::Context->preference('KohaAdminEmailAddress'), + from => $from, subject => "" . $letter->{title}, } ); @@ -502,7 +507,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 d840bf24b7..437d687f7f 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -1318,7 +1318,7 @@ sub ModSubscription { $biblionumber, $callnumber, $notes, $letter, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq, - $itemtype, $previousitemtype, $mana_id + $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled ) = @_; my $subscription = Koha::Subscriptions->find($subscriptionid); @@ -1361,6 +1361,7 @@ sub ModSubscription { itemtype => $itemtype, previousitemtype => $previousitemtype, mana_id => $mana_id, + auto_claim_enabled => $auto_claim_enabled // 0, } )->store; # FIXME Must be $subscription->serials @@ -1398,7 +1399,8 @@ sub NewSubscription { $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, - $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id + $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id, + $auto_claim_enabled ) = @_; my $dbh = C4::Context->dbh; @@ -1441,6 +1443,7 @@ sub NewSubscription { itemtype => $itemtype, previousitemtype => $previousitemtype, mana_id => $mana_id, + auto_claim_enabled => $auto_claim_enabled // 0, } )->store; $subscription->discard_changes; @@ -1859,7 +1862,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, claims_count, subscription.branchcode diff --git a/installer/data/mysql/atomicupdate/bug-18783.pl b/installer/data/mysql/atomicupdate/bug-18783.pl new file mode 100644 index 0000000000..a057f2d4b1 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-18783.pl @@ -0,0 +1,22 @@ +use Modern::Perl; + +use C4::Installer qw(column_exists); + +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 previousitemtype + }); + + 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 02cf864ddd..4e51c5ed0c 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4802,6 +4802,7 @@ CREATE TABLE `subscription` ( `reneweddate` date DEFAULT NULL COMMENT 'date of last renewal for the subscription', `itemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `previousitemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `auto_claim_enabled` tinyint NOT NULL DEFAULT 0 COMMENT 'enable automatic claiming', `mana_id` int(11) DEFAULT NULL, PRIMARY KEY (`subscriptionid`), KEY `subscription_ibfk_1` (`periodicity`), 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 789596e4ec..5ea06a3990 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 @@ -231,6 +231,14 @@ fieldset.rows table { clear: none; margin: 0; } 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 744d52a2b2..700a0df56a 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 @@ -135,6 +135,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 0000000000..05dbc350fd --- /dev/null +++ b/misc/cronjobs/serialsClaim.pl @@ -0,0 +1,124 @@ +#!/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 DateTime; +use Getopt::Long; + +use Koha::Acquisition::Booksellers; +use Koha::DateUtils; +use Koha::Subscriptions; + +use C4::Context; +use C4::Letters; +use C4::Serials; + +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; + +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 = DateTime->now(time_zone => C4::Context->tz); + + 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 39426a6253..3461e2a272 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -328,6 +328,7 @@ sub redirect_add_subscription { my $itemtype = $query->param('itemtype'); my $previousitemtype = $query->param('previousitemtype'); my $skip_serialseq = $query->param('skip_serialseq'); + my $auto_claim_enabled = $query->param('auto_claim_enabled'); my $mana_id; if ( $query->param('mana_id') ne "" ) { @@ -354,7 +355,7 @@ sub redirect_add_subscription { join(";",@irregularity), $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, - $skip_serialseq, $itemtype, $previousitemtype, $mana_id + $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled ); if ( (C4::Context->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){ my $result = Koha::SharedContent::send_entity( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription'); @@ -441,6 +442,7 @@ sub redirect_mod_subscription { my $itemtype = $query->param('itemtype'); my $previousitemtype = $query->param('previousitemtype'); my $skip_serialseq = $query->param('skip_serialseq'); + my $auto_claim_enabled = $query->param('auto_claim_enabled'); my $mana_id; if ( $query->param('mana_id') ne "" ) { @@ -476,7 +478,7 @@ sub redirect_mod_subscription { $status, $biblionumber, $callnumber, $notes, $letter, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid, - $skip_serialseq, $itemtype, $previousitemtype, $mana_id + $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled ); my @additional_fields; -- 2.20.1