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