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