Bug 23119: MARC21 added title 246, 730 subfield i displays out of order, and should...
[koha.git] / patroncards / edit-batch.pl
blob236706e1bc33c22d2c466ad5c8c8cea00270c4c3
1 #!/usr/bin/perl
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
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.
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.
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>.
22 use Modern::Perl;
23 use vars qw($debug);
25 use CGI qw ( -utf8 );
26 use autouse 'Data::Dumper' => qw(Dumper);
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Creators;
31 use C4::Patroncards;
32 use Koha::Patrons;
34 my $cgi = new CGI;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37 template_name => "patroncards/edit-batch.tt",
38 query => $cgi,
39 type => "intranet",
40 authnotrequired => 0,
41 flagsrequired => { catalogue => 1 },
42 debug => 1,
46 my $err = 0;
47 my $duplicate_count = undef;
48 my $duplicate_message = undef;
49 my $db_rows = {};
50 my $batch = undef;
51 my $display_columns = [ {_summary => {label => 'Summary', link_field => 0}},
52 {borrowernumber => {label => 'Borrower Number', link_field => 0}},
53 {_action => {label => 'Actions ', link_field => 0}},
54 {select => {label => 'Select', value => '_label_id'}},
56 my $op = $cgi->param('op') || 'new';
57 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
58 my $description = $cgi->param('description') || '';
59 my ( @label_ids, @item_numbers, @borrower_numbers );
60 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
61 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
62 @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
63 my $errstr = $cgi->param('error') || '';
64 my $bor_num_list = $cgi->param('bor_num_list') || undef;
65 my $branch_code = C4::Context->userenv->{'branch'};
67 if ($op eq 'remove') {
68 $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
69 foreach my $label_id (@label_ids) {
70 $err = $batch->remove_item($label_id);
72 if ($err) {
73 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
74 exit;
77 elsif ($op eq 'delete') {
78 $err = C4::Creators::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
79 if ($err) {
80 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=404");
81 exit;
84 elsif ($op eq 'add') {
85 if ($bor_num_list) {
86 my @bor_nums_unchecked = split /\n/, $bor_num_list; # $bor_num_list is effectively passed in as a <cr> separated list
87 foreach my $number (@bor_nums_unchecked) {
88 $number =~ s/\r$//; # strip any naughty return chars
89 if ( Koha::Patrons->find( $number )) { # we must test in case an invalid borrowernumber is passed in; we effectively disgard them atm
90 my $borrower_number = $number;
91 push @borrower_numbers, $borrower_number;
95 if ($batch_id != 0) {$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);}
96 if ($batch_id == 0 || $batch == -2) {$batch = C4::Patroncards::Batch->new(branch_code => $branch_code);}
97 $template->param( description => $batch->{'description'} );
98 if ($branch_code){
99 foreach my $borrower_number (@borrower_numbers) {
100 $err = $batch->add_item($borrower_number);
102 $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
103 if ($err) {
104 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=401");
105 exit;
108 else {
109 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=402");
110 exit;
113 elsif ($op eq 'de_duplicate') {
114 $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
115 $duplicate_count = $batch->remove_duplicates();
116 $duplicate_message = 1 if $duplicate_count != -1;
117 if ($duplicate_count == -1) {
118 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=405");
119 exit;
122 elsif ($op eq 'edit') {
123 $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
124 $template->param( description => $batch->{'description'} );
126 elsif ($op eq 'new') {
127 if ($branch_code eq '') {
128 warn sprintf('Batch edit interface called with an invalid/non-existent branch code: %s',$branch_code ? $branch_code : 'NULL');
129 print $cgi->redirect("manage.pl?card_element=batch&error=203");
130 exit;
132 $batch = C4::Patroncards::Batch->new(branch_code => $branch_code);
133 $batch_id = $batch->get_attr('batch_id');
135 else {
136 warn sprintf('Batch edit interface called an unsupported operation: %s',$op);
137 print $cgi->redirect("manage.pl?card_element=batch&error=202");
138 exit;
141 my $items = $batch->get_attr('items');
142 $db_rows = get_card_summary(items => $items, batch_id => $batch_id);
144 my $table = html_table($display_columns, $db_rows);
146 $template->param(
147 op => $op,
148 batch_id => $batch_id,
149 table_loop => $table,
150 duplicate_message => $duplicate_message,
151 duplicate_count => $duplicate_count,
152 error => $errstr,
155 output_html_with_http_headers $cgi, $cookie, $template->output;