Merge remote branch 'kc/new/bug_4908' into kcmaster
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23 use vars qw($debug);
24
25 use CGI;
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Branch qw(get_branch_code_from_name);
30 use C4::Items qw(GetItemnumberFromBarcode);
31 use C4::Creators 1.000000;
32 use C4::Labels 1.000000;
33
34 my $cgi = new CGI;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "labels/label-edit-batch.tmpl",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { catalogue => 1 },
42         debug           => 1,
43     }
44 );
45
46 my $err = 0;
47 my $errstr = undef;
48 my $duplicate_count = undef;
49 my $duplicate_message = undef;
50 my $db_rows = {};
51 my $batch = 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                         {_barcode       => {label => 'Barcode', link_field => 0}},
56                         {select         => {label => 'Select', value => '_label_id'}},
57                       ];
58 my $op = $cgi->param('op') || 'edit';
59 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
60 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
61 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
62 my $barcode = $cgi->param('barcode') if $cgi->param('barcode');
63
64 my $branch_code = get_branch_code_from_name($template->param('LoginBranchname'));
65
66 if ($op eq 'remove') {
67     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
68     foreach my $label_id (@label_ids) {
69     $err = $batch->remove_item($label_id);
70     }
71     $errstr = "item(s) not removed from batch $batch_id." if $err;
72 #    Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
73 #    print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
74 #    exit;
75 }
76 elsif ($op eq 'delete') {
77     $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
78     $errstr = "batch $batch_id was not deleted." if $err;
79 }
80 elsif ($op eq 'add') {
81     if ($barcode) {
82         my @barcodes = split /\n/, $barcode; # $barcode is effectively passed in as a <cr> separated list
83         foreach my $number (@barcodes) {
84             $number =~ s/\r$//; # strip any naughty return chars
85             if (my $item_number = GetItemnumberFromBarcode($number)) {  # we must test in case an invalid barcode is passed in; we effectively disgard them atm
86                 push @item_numbers, $item_number;
87             }
88         }
89     }
90     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
91     $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
92     if ($branch_code){
93         foreach my $item_number (@item_numbers) {
94             $err = $batch->add_item($item_number);
95         }
96         $errstr = "item(s) not added to batch $batch_id." if $err;
97     }
98     else {
99         $err = 1;
100         $errstr = "items(s) not added, the error was: Branch is not set, you please set your branch before adding items to a batch";
101     }
102 }
103 elsif ($op eq 'new') {
104     $batch = C4::Labels::Batch->new(branch_code => $branch_code);
105     $batch_id = $batch->get_attr('batch_id');
106 }
107 elsif ($op eq 'de_duplicate') {
108     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
109     $duplicate_count = $batch->remove_duplicates();
110     $duplicate_message = 1 if $duplicate_count != -1;
111     $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
112 }
113 else { # edit
114     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
115 }
116
117 my $items = $batch->get_attr('items');
118 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
119
120 my $table = html_table($display_columns, $db_rows);
121
122 $template->param(
123                 err         => $err,
124                 errstr      => $errstr,
125                 ) if ($err ne 0);
126
127 $template->param(
128                 op                      => $op,
129                 batch_id                => $batch_id,
130                 table_loop              => $table,
131                 duplicate_message       => $duplicate_message,
132                 duplicate_count         => $duplicate_count,
133                 );
134
135 output_html_with_http_headers $cgi, $cookie, $template->output;