fa114a18e2499591f3ad7fff8fcffebbed65b28d
[koha.git] / serials / subscription-batchedit.pl
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 use Modern::Perl;
21
22 use CGI qw( -utf8 );
23
24 use C4::Auth;
25 use C4::Output;
26 use C4::Serials;
27 use Koha::Subscriptions;
28 use Koha::Acquisition::Booksellers;
29 use Koha::AdditionalFields;
30 use Koha::DateUtils;
31
32 my $cgi = new CGI;
33
34 my ($template, $loggedinuser, $cookie) = get_template_and_user({
35     template_name => 'serials/subscription-batchedit.tt',
36     query => $cgi,
37     type => 'intranet',
38     authnotrequired => 0,
39     flagsrequired => {serials => 'edit_subscription'},
40 });
41
42 my @subscriptionids = $cgi->multi_param('subscriptionid');
43
44 my @subscriptions;
45 foreach my $subscriptionid (@subscriptionids) {
46     my $subscription = Koha::Subscriptions->find($subscriptionid);
47
48     push @subscriptions, $subscription if $subscription;
49 }
50
51 my @additional_fields = Koha::AdditionalFields->search({tablename => 'subscription'});
52
53 my $batchedit = $cgi->param('batchedit');
54 if ($batchedit) {
55     my %params = (
56         aqbooksellerid => scalar $cgi->param('booksellerid'),
57         location => scalar $cgi->param('location'),
58         branchcode => scalar $cgi->param('branchcode'),
59         itemtype => scalar $cgi->param('itemtype'),
60         notes => scalar $cgi->param('notes'),
61         internalnotes => scalar $cgi->param('internalnotes'),
62         serialsadditems => scalar $cgi->param('serialsadditems'),
63         enddate => dt_from_string(scalar $cgi->param('enddate')),
64     );
65
66     my $field_values = {};
67     foreach my $field (@additional_fields) {
68         my $value = $cgi->param('field_' . $field->id);
69         $field_values->{$field->id} = $value;
70     }
71
72     foreach my $subscription (@subscriptions) {
73         next unless C4::Serials::can_edit_subscription( $subscription->unblessed ); # This should be moved to Koha::Subscription->can_edit
74         while (my ($key, $value) = each %params) {
75             if (defined $value and $value ne '') {
76                 $subscription->$key($value);
77             }
78         }
79
80         my @additional_field_values;
81         foreach my $field (@additional_fields) {
82             my $value = $field_values->{$field->id};
83             if (defined $value and $value ne '') {
84                 push @additional_field_values, {
85                     id => $field->id,
86                     value => $value,
87                 };
88             }
89         }
90         $subscription->set_additional_fields(\@additional_field_values);
91
92         $subscription->store;
93     }
94
95     my $redirect_url = $cgi->param('referrer') // '/cgi-bin/koha/serials/serials-home.pl';
96     print $cgi->redirect($redirect_url);
97     exit;
98 }
99
100 $template->param(
101     subscriptions => \@subscriptions,
102     booksellers => [ Koha::Acquisition::Booksellers->search() ],
103     additional_fields => \@additional_fields,
104     referrer => scalar $cgi->param('referrer'),
105 );
106
107 output_html_with_http_headers $cgi, $cookie, $template->output;