Bug 16794: Group the 2 action buttons into the same column
[koha.git] / labels / label-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 use strict;
22 use warnings;
23 use vars qw($debug);
24
25 use CGI qw ( -utf8 );
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Items qw(GetItem GetItemnumberFromBarcode);
30 use C4::Creators;
31 use C4::Labels;
32
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "labels/label-edit-batch.tt",
37         query           => $cgi,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { catalogue => 1 },
41         debug           => 1,
42     }
43 );
44
45 my $err = 0;
46 my $errstr = undef;
47 my $duplicate_count = undef;
48 my $duplicate_message = undef;
49 my $db_rows = {};
50 my $batch = undef;
51 my $display_columns = [ {_label_number  => {label => 'Label Number', link_field => 0}},
52                         {_summary       => {label => 'Summary', link_field => 0}},
53                         {_item_type     => {label => 'Item Type', link_field => 0}},
54                         {_barcode       => {label => 'Barcode', link_field => 0}},
55                         {_delete        => {label => 'Actions', link_field => 0}},
56                         {select         => {label => 'Select', value => '_label_id'}},
57                       ];
58 my $op = $cgi->param('op') || 'edit';
59 my @label_ids;
60 my @item_numbers;
61 my $number_list;
62 my $number_type = $cgi->param('number_type') || "barcode";
63 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
64 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
65 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
66 $number_list = $cgi->param('number_list') if $cgi->param('number_list');
67
68 my $branch_code = C4::Context->userenv->{'branch'};
69
70 if ($op eq 'remove') {
71     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
72     foreach my $label_id (@label_ids) {
73     $err = $batch->remove_item($label_id);
74     }
75     $errstr = "item(s) not removed from batch $batch_id." if $err;
76 #    Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
77 #    print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
78 #    exit;
79 }
80 elsif ($op eq 'delete') {
81     $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
82     $errstr = "batch $batch_id was not deleted." if $err;
83 }
84 elsif ($op eq 'add') {
85     if ($number_list) {
86         my @numbers_list = split /\n/, $number_list; # Entries are effectively passed in as a <cr> separated list
87         foreach my $number (@numbers_list) {
88             $number =~ s/\r$//; # strip any naughty return chars
89             if( $number_type eq "itemnumber" && GetItem($number) ) {
90                 push @item_numbers, $number;
91             }
92             elsif ($number_type eq "barcode" ) {  # we must test in case an invalid barcode is passed in; we effectively disgard them atm
93                 if( my $item_number = GetItemnumberFromBarcode($number) ){
94                     push @item_numbers, $item_number;
95                 }
96             }
97         }
98     }
99     if ($batch_id != 0) {$batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);}
100     if ($batch_id == 0 || $batch == -2) {$batch = C4::Labels::Batch->new(branch_code => $branch_code);}
101     if ($branch_code){
102         foreach my $item_number (@item_numbers) {
103             $err = $batch->add_item($item_number);
104         }
105         $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
106         $errstr = "item(s) not added to batch $batch_id." if $err;
107     }
108     else {
109         $err = 1;
110         $errstr = "items(s) not added, the error was: Branch is not set, you please set your branch before adding items to a batch";
111     }
112 }
113 elsif ($op eq 'new') {
114     $batch = C4::Labels::Batch->new(branch_code => $branch_code);
115     $batch_id = $batch->get_attr('batch_id');
116 }
117 elsif ($op eq 'de_duplicate') {
118     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
119     $duplicate_count = $batch->remove_duplicates();
120     $duplicate_message = 1 if $duplicate_count != -1;
121     $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
122 }
123 else { # edit
124     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
125 }
126
127 my $items = $batch->get_attr('items');
128 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
129
130 my $table = html_table($display_columns, $db_rows);
131
132 $template->param(
133                 err         => $err,
134                 errstr      => $errstr,
135                 ) if ($err ne 0);
136
137 $template->param(
138                 op                      => $op,
139                 batch_id                => $batch_id,
140                 table_loop              => $table,
141                 duplicate_message       => $duplicate_message,
142                 duplicate_count         => $duplicate_count,
143                 );
144
145 output_html_with_http_headers $cgi, $cookie, $template->output;