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 C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Items qw(GetItem);
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37 template_name => "labels/label-edit-batch.tt",
41 flagsrequired => { catalogue => 1 },
48 my $duplicate_count = undef;
49 my $duplicate_message = undef;
52 my $display_columns = [ {_label_number => {label => 'Label number', link_field => 0}},
53 {_summary => {label => 'Summary', link_field => 0}},
54 {_item_type => {label => 'Item type', link_field => 0}},
55 {_item_cn => {label => 'Call number', link_field => 0}},
56 {_barcode => {label => 'Barcode', link_field => 0}},
57 {_delete => {label => 'Actions', link_field => 0}},
58 {select => {label => 'Select', value => '_label_id'}},
60 my $op = $cgi->param('op') || 'edit';
64 my $number_type = $cgi->param('number_type') || "barcode";
65 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
66 my $description = $cgi->param('description') || '';
67 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
68 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
69 $number_list = $cgi->param('number_list') if $cgi->param('number_list');
71 my $branch_code = C4::Context->userenv->{'branch'};
73 if ($op eq 'remove') {
74 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
75 foreach my $label_id (@label_ids) {
76 $err = $batch->remove_item($label_id);
78 $errtype = 'ITEM_NOT_REMOVED' if $err;
79 # Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
80 # print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
83 elsif ($op eq 'delete') {
84 $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
85 $errtype = 'BATCH_NOT_DELETED' if $err;
87 elsif ($op eq 'add') {
89 my @numbers_list = split /\n/, $number_list; # Entries are effectively passed in as a <cr> separated list
90 foreach my $number (@numbers_list) {
91 $number =~ s/\r$//; # strip any naughty return chars
92 if( $number_type eq "itemnumber" && GetItem($number) ) {
93 push @item_numbers, $number;
95 elsif ($number_type eq "barcode" ) { # we must test in case an invalid barcode is passed in; we effectively disgard them atm
96 my $item = Koha::Items->find({barcode => $number});
97 push @item_numbers, $item->itemnumber if $item;
101 if ($batch_id != 0) {$batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);}
102 if ($batch_id == 0 || $batch == -2) {$batch = C4::Labels::Batch->new(branch_code => $branch_code);}
103 $template->param( description => $batch->{description} );
105 foreach my $item_number (@item_numbers) {
106 $err = $batch->add_item($item_number);
108 $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
109 $errtype = 'ITEM_NOT_ADDED' if $err;
113 $errtype = 'BRANCH_NOT_SET';
116 elsif ($op eq 'new') {
117 $batch = C4::Labels::Batch->new(branch_code => $branch_code);
118 $batch_id = $batch->get_attr('batch_id');
120 elsif ($op eq 'de_duplicate') {
121 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
122 $duplicate_count = $batch->remove_duplicates();
123 $duplicate_message = 1 if $duplicate_count != -1;
124 $errtype = 'BATCH_NOT_DEDUP' if $duplicate_count == -1;
127 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
128 $template->param( description => $batch->{description} );
131 my $items = $batch->get_attr('items');
132 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
134 my $table = html_table($display_columns, $db_rows);
143 batch_id => $batch_id,
144 table_loop => $table,
145 duplicate_message => $duplicate_message,
146 duplicate_count => $duplicate_count,
149 output_html_with_http_headers $cgi, $cookie, $template->output;