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>.
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);
32 use C4
::Members
qw(GetMember);
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
36 template_name
=> "patroncards/edit-batch.tt",
40 flagsrequired
=> { catalogue
=> 1 },
46 my $duplicate_count = undef;
47 my $duplicate_message = undef;
50 my $display_columns = [ {_summary
=> {label
=> 'Summary', link_field
=> 0}},
51 {borrowernumber
=> {label
=> 'Borrower Number', link_field
=> 0}},
52 {_action
=> {label
=> 'Actions ', link_field
=> 0}},
53 {select => {label
=> 'Select', value
=> '_label_id'}},
55 my $op = $cgi->param('op') || 'new';
56 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
57 my ( @label_ids, @item_numbers, @borrower_numbers );
58 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
59 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
60 @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
61 my $errstr = $cgi->param('error') || '';
62 my $bor_num_list = $cgi->param('bor_num_list') || undef;
63 my $branch_code = C4
::Context
->userenv->{'branch'};
65 if ($op eq 'remove') {
66 $batch = C4
::Patroncards
::Batch
->retrieve(batch_id
=> $batch_id);
67 foreach my $label_id (@label_ids) {
68 $err = $batch->remove_item($label_id);
71 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
75 elsif ($op eq 'delete') {
76 $err = C4
::Creators
::Batch
::delete(batch_id
=> $batch_id, branch_code
=> $branch_code);
78 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=404");
82 elsif ($op eq 'add') {
84 my @bor_nums_unchecked = split /\n/, $bor_num_list; # $bor_num_list is effectively passed in as a <cr> separated list
85 foreach my $number (@bor_nums_unchecked) {
86 $number =~ s/\r$//; # strip any naughty return chars
87 if ( GetMember
(borrowernumber
=> $number)) { # we must test in case an invalid borrowernumber is passed in; we effectively disgard them atm
88 my $borrower_number = $number;
89 push @borrower_numbers, $borrower_number;
93 if ($batch_id != 0) {$batch = C4
::Patroncards
::Batch
->retrieve(batch_id
=> $batch_id);}
94 if ($batch_id == 0 || $batch == -2) {$batch = C4
::Patroncards
::Batch
->new(branch_code
=> $branch_code);}
96 foreach my $borrower_number (@borrower_numbers) {
97 $err = $batch->add_item($borrower_number);
99 $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
101 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=401");
106 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=402");
110 elsif ($op eq 'de_duplicate') {
111 $batch = C4
::Patroncards
::Batch
->retrieve(batch_id
=> $batch_id);
112 $duplicate_count = $batch->remove_duplicates();
113 $duplicate_message = 1 if $duplicate_count != -1;
114 if ($duplicate_count == -1) {
115 print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=405");
119 elsif ($op eq 'edit') {
120 $batch = C4
::Patroncards
::Batch
->retrieve(batch_id
=> $batch_id);
122 elsif ($op eq 'new') {
123 if ($branch_code eq '') {
124 warn sprintf('Batch edit interface called with an invalid/non-existent branch code: %s',$branch_code ?
$branch_code : 'NULL');
125 print $cgi->redirect("manage.pl?card_element=batch&error=203");
128 $batch = C4
::Patroncards
::Batch
->new(branch_code
=> $branch_code);
129 $batch_id = $batch->get_attr('batch_id');
132 warn sprintf('Batch edit interface called an unsupported operation: %s',$op);
133 print $cgi->redirect("manage.pl?card_element=batch&error=202");
137 my $items = $batch->get_attr('items');
138 $db_rows = get_card_summary
(items
=> $items, batch_id
=> $batch_id);
140 my $table = html_table
($display_columns, $db_rows);
144 batch_id
=> $batch_id,
145 table_loop
=> $table,
146 duplicate_message
=> $duplicate_message,
147 duplicate_count
=> $duplicate_count,
151 output_html_with_http_headers
$cgi, $cookie, $template->output;