Bug 30477: Add new UNIMARC installer translation files
[koha.git] / patroncards / edit-batch.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use autouse 'Data::Dumper' => qw(Dumper);
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Creators qw( get_card_summary html_table );
30 use C4::Patroncards;
31 use Koha::Patrons;
32
33 my $cgi = CGI->new;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "patroncards/edit-batch.tt",
37         query           => $cgi,
38         type            => "intranet",
39         flagsrequired   => { tools => 'label_creator' },
40     }
41 );
42
43 my $err = 0;
44 my $duplicate_count = undef;
45 my $duplicate_message = undef;
46 my $db_rows = {};
47 my $batch = undef;
48 my $display_columns = [ {_summary       => {label => 'Summary', link_field => 0}},
49                         {borrowernumber => {label => 'Borrower Number', link_field => 0}},
50                         {_action        => {label => 'Actions ', link_field => 0}},
51                         {select         => {label => 'Select', value => '_label_id'}},
52                       ];
53 my $op = $cgi->param('op') || 'new';
54 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
55 my $description = $cgi->param('description') || '';
56 my ( @label_ids, @item_numbers, @borrower_numbers );
57 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
58 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
59 @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
60 my $errstr = $cgi->param('error') || '';
61 my $bor_num_list = $cgi->param('bor_num_list') || undef;
62 my $branch_code = C4::Context->userenv->{'branch'};
63
64 my @errors = ( 'pdferr', 'errnocards', 'errba', 'errpl', 'errpt', 'errlo', 'errtpl', );
65 foreach my $param (@errors) {
66     my $error = $cgi->param($param) ? 1 : 0;
67     $template->param( 'error_' . $param => $error )
68       if $error;
69 }
70
71 if ($op eq 'remove') {
72     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
73     foreach my $label_id (@label_ids) {
74     $err = $batch->remove_item($label_id);
75     }
76     if ($err) {
77         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=403"); # this allows us to avoid problems with the user hitting their refresh button
78         exit;
79     }
80 }
81 elsif ($op eq 'delete') {
82     $err = C4::Creators::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
83     if ($err) {
84         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=404");
85         exit;
86     }
87 }
88 elsif ($op eq 'add') {
89 if ($bor_num_list) {
90         my @bor_nums_unchecked = split /\n/, $bor_num_list; # $bor_num_list is effectively passed in as a <cr> separated list
91         foreach my $number (@bor_nums_unchecked) {
92             $number =~ s/\r$//; # strip any naughty return chars
93             if ( Koha::Patrons->find( $number )) {  # we must test in case an invalid borrowernumber is passed in; we effectively disgard them atm
94                 my $borrower_number = $number;
95                 push @borrower_numbers, $borrower_number;
96             }
97         }
98     }
99     if ($batch_id != 0) {$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);}
100     if ($batch_id == 0 || $batch == -2) {$batch = C4::Patroncards::Batch->new(branch_code => $branch_code);}
101     $template->param( description => $batch->{'description'} );
102     if ($branch_code){
103         foreach my $borrower_number (@borrower_numbers) {
104             $err = $batch->add_item($borrower_number);
105         }
106         $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
107         if ($err) {
108             print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=401");
109             exit;
110         }
111     }
112     else {
113         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=402");
114         exit;
115     }
116 }
117 elsif ($op eq 'de_duplicate') {
118     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
119     $duplicate_count = $batch->remove_duplicates();
120     $duplicate_message = 1 if $duplicate_count != -1;
121     if ($duplicate_count == -1) {
122         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=405");
123         exit;
124     }
125 }
126 elsif ($op eq 'edit') {
127     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
128     $template->param( description => $batch->{'description'} );
129 }
130 elsif ($op eq 'new') {
131     if ($branch_code eq '') {
132         warn sprintf('Batch edit interface called with an invalid/non-existent branch code: %s',$branch_code ? $branch_code : 'NULL');
133         print $cgi->redirect("manage.pl?card_element=batch&error=203");
134         exit;
135     }
136     $batch = C4::Patroncards::Batch->new(branch_code => $branch_code);
137     $batch_id = $batch->get_attr('batch_id');
138 }
139 else {
140     warn sprintf('Batch edit interface called an unsupported operation: %s',$op);
141     print $cgi->redirect("manage.pl?card_element=batch&error=202");
142     exit;
143 }
144
145 my $items = $batch->get_attr('items');
146 $db_rows = get_card_summary(items => $items, batch_id => $batch_id);
147
148 my $table = html_table($display_columns, $db_rows);
149
150 $template->param(
151                 op                      => $op,
152                 batch_id                => $batch_id,
153                 table_loop              => $table,
154                 duplicate_message       => $duplicate_message,
155                 duplicate_count         => $duplicate_count,
156                 error                   => $errstr,
157                 );
158
159 output_html_with_http_headers $cgi, $cookie, $template->output;