Bug 19233: Add ability to send itemnumbers in report results to batch modification
[koha.git] / serials / claims.pl
1 #!/usr/bin/perl
2
3 # Parts Copyright 2010 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 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth;
23 use C4::Serials;
24 use C4::Acquisition;
25 use C4::Output;
26 use C4::Context;
27 use C4::Letters;
28 use C4::Koha qw( GetAuthorisedValues );
29
30 use Koha::AdditionalField;
31 use Koha::CsvProfiles;
32
33 my $input = CGI->new;
34
35 my $serialid = $input->param('serialid');
36 my $op = $input->param('op');
37 my $claimletter = $input->param('claimletter');
38 my $supplierid = $input->param('supplierid');
39 my $suppliername = $input->param('suppliername');
40
41 # open template first (security & userenv set here)
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => 'serials/claims.tt',
44             query => $input,
45             type => 'intranet',
46             authnotrequired => 0,
47             flagsrequired => {serials => 'claim_serials'},
48             debug => 1,
49             });
50
51 # supplierlist is returned in name order
52 my $supplierlist = GetSuppliersWithLateIssues();
53 for my $s (@{$supplierlist} ) {
54     $s->{count} = scalar  GetLateOrMissingIssues($s->{id});
55     if ($supplierid && $s->{id} == $supplierid) {
56         $s->{selected} = 1;
57     }
58 }
59
60 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription', searchable => 1 } );
61 for my $field ( @$additional_fields ) {
62     if ( $field->{authorised_value_category} ) {
63         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
64     }
65 }
66
67
68 my @serialnums=$input->multi_param('serialid');
69 if (@serialnums) { # i.e. they have been flagged to generate claims
70     my $err;
71     eval {
72         $err = SendAlerts('claimissues',\@serialnums,$input->param("letter_code"));
73         if ( not ref $err or not exists $err->{error} ) {
74             C4::Serials::updateClaim( \@serialnums );
75         }
76     };
77     if ( $@ ) {
78         $template->param(error_claim => $@);
79     } elsif ( ref $err and exists $err->{error} ) {
80         if ( $err->{error} eq "no_email" ) {
81             $template->param( error_claim => 'no_vendor_email' );
82         } elsif ( $err->{error} =~ m|Bad or missing From address| ) {
83             $template->param( error_claim => 'bad_or_missing_sender' );
84         }
85     } else {
86         $template->param( info_claim => 1 );
87     }
88 }
89
90 my $letters = GetLetters({ module => 'claimissues' });
91
92 my @missingissues;
93 if ($supplierid) {
94     @missingissues = GetLateOrMissingIssues($supplierid);
95     foreach my $issue (@missingissues) {
96         $issue->{cannot_claim} = 1
97           unless C4::Serials::can_claim_subscription($issue);
98     }
99 }
100
101 $template->param(
102         suploop => $supplierlist,
103         missingissues => \@missingissues,
104         supplierid => $supplierid,
105         claimletter => $claimletter,
106         additional_fields_for_subscription => $additional_fields,
107         csv_profiles => [ Koha::CsvProfiles->search({ type => 'sql', used_for => 'late_issues' }) ],
108         letters => $letters,
109         (uc(C4::Context->preference("marcflavour"))) => 1
110         );
111 output_html_with_http_headers $input, $cookie, $template->output;