View | Details | Raw Unified | Return to bug 18783
Collapse All | Expand All

(-)a/C4/Letters.pm (-3 / +9 lines)
Lines 474-480 sub SendAlerts { Link Here
474
        $letter->{content} =~ s/<order>(.*?)<\/order>/$1/gxms;
474
        $letter->{content} =~ s/<order>(.*?)<\/order>/$1/gxms;
475
475
476
        # ... then send mail
476
        # ... then send mail
477
        my $from;
477
        my $library = Koha::Libraries->find( $userenv->{branch} );
478
        my $library = Koha::Libraries->find( $userenv->{branch} );
479
        if ($library and $library->branchemail) {
480
            $from = $library->branchemail;
481
        } else {
482
            $from = C4::Context->preference('KohaAdminEmailAddress');
483
        }
478
        my $mail = Koha::Email->create(
484
        my $mail = Koha::Email->create(
479
            {
485
            {
480
                to => join( ',', @email ),
486
                to => join( ',', @email ),
Lines 488-495 sub SendAlerts { Link Here
488
                    ? ( bcc => $userenv->{emailaddress} )
494
                    ? ( bcc => $userenv->{emailaddress} )
489
                    : ()
495
                    : ()
490
                ),
496
                ),
491
                from => $library->branchemail
497
                from => $from,
492
                  || C4::Context->preference('KohaAdminEmailAddress'),
493
                subject => "" . $letter->{title},
498
                subject => "" . $letter->{title},
494
            }
499
            }
495
        );
500
        );
Lines 502-508 sub SendAlerts { Link Here
502
        }
507
        }
503
508
504
        my $success = try {
509
        my $success = try {
505
            $mail->send_or_die({ transport => $library->smtp_server->transport });
510
            my $smtp_server = $library ? $library->smtp_server : Koha::SMTP::Servers->get_default;
511
            $mail->send_or_die({ transport => $smtp_server->transport });
506
        }
512
        }
507
        catch {
513
        catch {
508
            # We expect ref($_) eq 'Email::Sender::Failure'
514
            # We expect ref($_) eq 'Email::Sender::Failure'
(-)a/C4/Serials.pm (-3 / +6 lines)
Lines 1318-1324 sub ModSubscription { Link Here
1318
    $biblionumber, $callnumber, $notes, $letter, $manualhistory,
1318
    $biblionumber, $callnumber, $notes, $letter, $manualhistory,
1319
    $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1319
    $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1320
    $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq,
1320
    $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq,
1321
    $itemtype, $previousitemtype, $mana_id
1321
    $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled
1322
    ) = @_;
1322
    ) = @_;
1323
1323
1324
    my $subscription = Koha::Subscriptions->find($subscriptionid);
1324
    my $subscription = Koha::Subscriptions->find($subscriptionid);
Lines 1361-1366 sub ModSubscription { Link Here
1361
            itemtype          => $itemtype,
1361
            itemtype          => $itemtype,
1362
            previousitemtype  => $previousitemtype,
1362
            previousitemtype  => $previousitemtype,
1363
            mana_id           => $mana_id,
1363
            mana_id           => $mana_id,
1364
            auto_claim_enabled => $auto_claim_enabled // 0,
1364
        }
1365
        }
1365
    )->store;
1366
    )->store;
1366
    # FIXME Must be $subscription->serials
1367
    # FIXME Must be $subscription->serials
Lines 1398-1404 sub NewSubscription { Link Here
1398
    $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity,
1399
    $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity,
1399
    $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes,
1400
    $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes,
1400
    $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod,
1401
    $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod,
1401
    $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id
1402
    $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id,
1403
    $auto_claim_enabled
1402
    ) = @_;
1404
    ) = @_;
1403
    my $dbh = C4::Context->dbh;
1405
    my $dbh = C4::Context->dbh;
1404
1406
Lines 1441-1446 sub NewSubscription { Link Here
1441
            itemtype          => $itemtype,
1443
            itemtype          => $itemtype,
1442
            previousitemtype  => $previousitemtype,
1444
            previousitemtype  => $previousitemtype,
1443
            mana_id           => $mana_id,
1445
            mana_id           => $mana_id,
1446
            auto_claim_enabled => $auto_claim_enabled // 0,
1444
        }
1447
        }
1445
    )->store;
1448
    )->store;
1446
    $subscription->discard_changes;
1449
    $subscription->discard_changes;
Lines 1859-1865 sub GetLateOrMissingIssues { Link Here
1859
    if ($supplierid) {
1862
    if ($supplierid) {
1860
        $sth = $dbh->prepare(
1863
        $sth = $dbh->prepare(
1861
            "SELECT
1864
            "SELECT
1862
                serialid,      aqbooksellerid,        name,
1865
                serialid,      serial.aqbooksellerid,        name,
1863
                biblio.title,  biblioitems.issn,      planneddate,    serialseq,
1866
                biblio.title,  biblioitems.issn,      planneddate,    serialseq,
1864
                serial.status, serial.subscriptionid, claimdate, claims_count,
1867
                serial.status, serial.subscriptionid, claimdate, claims_count,
1865
                subscription.branchcode
1868
                subscription.branchcode
(-)a/installer/data/mysql/atomicupdate/bug-18783.pl (+22 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
use C4::Installer qw(column_exists);
4
5
return {
6
    bug_number => "18783",
7
    description => "Automatic claims for serials",
8
    up => sub {
9
        my ($args) = @_;
10
        my ($dbh, $out) = @$args{qw(dbh out)};
11
        unless (column_exists('subscription', 'auto_claim_enabled') ) {
12
            $dbh->do(q{
13
                ALTER TABLE subscription
14
                ADD COLUMN auto_claim_enabled tinyint NOT NULL DEFAULT 0 COMMENT 'enable automatic claiming' AFTER previousitemtype
15
            });
16
17
            say $out "Added column subscription.auto_claim_enabled";
18
        } else {
19
            say $out "Column subscription.auto_claim_enabled already exists";
20
        }
21
    },
22
}
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 4802-4807 CREATE TABLE `subscription` ( Link Here
4802
  `reneweddate` date DEFAULT NULL COMMENT 'date of last renewal for the subscription',
4802
  `reneweddate` date DEFAULT NULL COMMENT 'date of last renewal for the subscription',
4803
  `itemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
4803
  `itemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
4804
  `previousitemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
4804
  `previousitemtype` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
4805
  `auto_claim_enabled` tinyint NOT NULL DEFAULT 0 COMMENT 'enable automatic claiming',
4805
  `mana_id` int(11) DEFAULT NULL,
4806
  `mana_id` int(11) DEFAULT NULL,
4806
  PRIMARY KEY (`subscriptionid`),
4807
  PRIMARY KEY (`subscriptionid`),
4807
  KEY `subscription_ibfk_1` (`periodicity`),
4808
  KEY `subscription_ibfk_1` (`periodicity`),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt (+8 lines)
Lines 231-236 fieldset.rows table { clear: none; margin: 0; } Link Here
231
                                    <label for="graceperiod">Grace period:</label>
231
                                    <label for="graceperiod">Grace period:</label>
232
                                    <input type="text" name="graceperiod" id="graceperiod" value="[% graceperiod | html %]" size="5"/> day(s)
232
                                    <input type="text" name="graceperiod" id="graceperiod" value="[% graceperiod | html %]" size="5"/> day(s)
233
                                </li>
233
                                </li>
234
                                <li>
235
                                    <label for="auto_claim_enabled">Enable automatic claims</label>
236
                                    [% IF auto_claim_enabled %]
237
                                        <input type="checkbox" name="auto_claim_enabled" id="auto_claim_enabled" value="1" checked/>
238
                                    [% ELSE %]
239
                                        <input type="checkbox" name="auto_claim_enabled" id="auto_claim_enabled" value="1"/>
240
                                    [% END %]
241
                                </li>
234
                                <li>
242
                                <li>
235
                                     <label class="widelabel" for="staffdisplaycount">Number of issues to display to staff: </label>
243
                                     <label class="widelabel" for="staffdisplaycount">Number of issues to display to staff: </label>
236
                                     <input type="text" name="staffdisplaycount" id="staffdisplaycount" value="[% staffdisplaycount | html %]" size="4"/>
244
                                     <input type="text" name="staffdisplaycount" id="staffdisplaycount" value="[% staffdisplaycount | html %]" size="4"/>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt (+1 lines)
Lines 135-140 Link Here
135
                                            [% END %]
135
                                            [% END %]
136
                                        </li>
136
                                        </li>
137
                                        <li><span class="label">Grace period:</span> [% graceperiod | html %]</li>
137
                                        <li><span class="label">Grace period:</span> [% graceperiod | html %]</li>
138
                                        <li><span class="label">Automatic claims:</span> [% IF auto_claim_enabled %]Enabled[% ELSE %]Disabled[% END %]</li>
138
                                    </ol>
139
                                    </ol>
139
                                </div> <!-- /.rows -->
140
                                </div> <!-- /.rows -->
140
                            </div> <!-- /.col-sm-6 -->
141
                            </div> <!-- /.col-sm-6 -->
(-)a/misc/cronjobs/serialsClaim.pl (+124 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2017 Biblibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
serialsClaims.pl - Send claim notice to suppliers that have late or missing issues
23
24
=head1 SYNOPSIS
25
26
serialsClaims.pl --letter-code LETTER_CODE [--max-claims MAX_CLAIMS]
27
28
Options:
29
    --letter-code LETTER_CODE   Mandatory. Use letter LETTER_CODE.
30
    --max-claims MAX_CLAIMS     Send up to MAX_CLAIMS notices
31
32
=cut
33
34
use Modern::Perl;
35
36
use DateTime;
37
use Getopt::Long;
38
39
use Koha::Acquisition::Booksellers;
40
use Koha::DateUtils;
41
use Koha::Subscriptions;
42
43
use C4::Context;
44
use C4::Letters;
45
use C4::Serials;
46
47
sub usage {
48
    return <<EOT
49
Usage: $0 --letter-code LETTER_CODE [--max-claims MAX_CLAIMS]
50
EOT
51
}
52
53
my $help;
54
my $letter_code;
55
my $max_claims;
56
57
GetOptions(
58
    'help' => \$help,
59
    'letter-code=s' => \$letter_code,
60
    'max-claims=i' => \$max_claims,
61
) or die usage();
62
63
if ($help) {
64
    say usage();
65
    exit;
66
}
67
68
die usage() unless $letter_code;
69
70
my $suppliers = C4::Serials::GetSuppliersWithLateIssues();
71
foreach my $supplier (@$suppliers) {
72
    my $bookseller = Koha::Acquisition::Booksellers->find($supplier->{id});
73
    my $supplierinfo = $bookseller->name . ' (' . $bookseller->id . ')';
74
75
    my $deliverytime = $bookseller->deliverytime;
76
    unless ($deliverytime) {
77
        say STDERR "Warning: There is no delivery time set for supplier $supplierinfo. Automatic claiming is disabled for this supplier.";
78
        next;
79
    }
80
81
    my @serials = C4::Serials::GetLateOrMissingIssues($bookseller->id);
82
    my @serialids;
83
    foreach my $serial (@serials) {
84
        if ($max_claims && $serial->{claims_count} >= $max_claims) {
85
            say "Maximum claims count reached for serial " . $serial->{serialid};
86
            next;
87
        }
88
89
        my $subscription = Koha::Subscriptions->find($serial->{subscriptionid});
90
        next unless $subscription->auto_claim_enabled;
91
92
        my $claims_count = $serial->{claims_count};
93
        my $date = $serial->{claimdate} ? $serial->{claimdate} : $serial->{planneddate};
94
        my $newclaimdate = dt_from_string($date)->add(days => $deliverytime);
95
        my $now = DateTime->now(time_zone => C4::Context->tz);
96
97
        if ($newclaimdate <= $now) {
98
            push @serialids, $serial->{serialid};
99
        }
100
    }
101
102
    if (@serialids) {
103
        my $err;
104
        eval {
105
            $err = C4::Letters::SendAlerts('claimissues', \@serialids, $letter_code);
106
            if ( not ref $err or not exists $err->{error} ) {
107
                C4::Serials::updateClaim( \@serialids );
108
            }
109
        };
110
        if ( $@ ) {
111
            say STDERR "Error: $@ while claiming serials " . join(',', @serialids);
112
        } elsif ( ref $err and exists $err->{error} ) {
113
            if ( $err->{error} eq "no_email" ) {
114
                say STDERR "Error: Supplier $supplierinfo has no email";
115
            } elsif ( $err->{error} =~ m|Bad or missing From address| ) {
116
                say STDERR "Error: Supplier $supplierinfo has no email";
117
            } else {
118
                say STDERR "Error: " . $err->{error};
119
            }
120
        } else {
121
            say "Claim sent to $supplierinfo for serials " . join(',', @serialids);
122
        }
123
    }
124
}
(-)a/serials/subscription-add.pl (-3 / +4 lines)
Lines 328-333 sub redirect_add_subscription { Link Here
328
    my $itemtype          = $query->param('itemtype');
328
    my $itemtype          = $query->param('itemtype');
329
    my $previousitemtype  = $query->param('previousitemtype');
329
    my $previousitemtype  = $query->param('previousitemtype');
330
    my $skip_serialseq    = $query->param('skip_serialseq');
330
    my $skip_serialseq    = $query->param('skip_serialseq');
331
    my $auto_claim_enabled = $query->param('auto_claim_enabled');
331
332
332
    my $mana_id;
333
    my $mana_id;
333
    if ( $query->param('mana_id') ne "" ) {
334
    if ( $query->param('mana_id') ne "" ) {
Lines 354-360 sub redirect_add_subscription { Link Here
354
        join(";",@irregularity), $numberpattern, $locale, $callnumber,
355
        join(";",@irregularity), $numberpattern, $locale, $callnumber,
355
        $manualhistory, $internalnotes, $serialsadditems,
356
        $manualhistory, $internalnotes, $serialsadditems,
356
        $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
357
        $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
357
        $skip_serialseq, $itemtype, $previousitemtype, $mana_id
358
        $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled
358
    );
359
    );
359
    if ( (C4::Context->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){
360
    if ( (C4::Context->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){
360
        my $result = Koha::SharedContent::send_entity( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription');
361
        my $result = Koha::SharedContent::send_entity( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription');
Lines 441-446 sub redirect_mod_subscription { Link Here
441
    my $itemtype          = $query->param('itemtype');
442
    my $itemtype          = $query->param('itemtype');
442
    my $previousitemtype  = $query->param('previousitemtype');
443
    my $previousitemtype  = $query->param('previousitemtype');
443
    my $skip_serialseq    = $query->param('skip_serialseq');
444
    my $skip_serialseq    = $query->param('skip_serialseq');
445
    my $auto_claim_enabled = $query->param('auto_claim_enabled');
444
446
445
    my $mana_id;
447
    my $mana_id;
446
    if ( $query->param('mana_id') ne "" ) {
448
    if ( $query->param('mana_id') ne "" ) {
Lines 476-482 sub redirect_mod_subscription { Link Here
476
        $status, $biblionumber, $callnumber, $notes, $letter,
478
        $status, $biblionumber, $callnumber, $notes, $letter,
477
        $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
479
        $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
478
        $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
480
        $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
479
        $skip_serialseq, $itemtype, $previousitemtype, $mana_id
481
        $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $auto_claim_enabled
480
    );
482
    );
481
483
482
    my @additional_fields;
484
    my @additional_fields;
483
- 

Return to bug 18783