[24/40] Adding single/multiple label printing to label export code and interface.
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23 use vars qw($debug);
24
25 use Sys::Syslog qw(syslog);
26 use CGI;
27 use HTML::Template::Pro;
28
29 use C4::Auth qw(get_template_and_user);
30 use C4::Output qw(output_html_with_http_headers);
31 use C4::Branch qw(get_branch_code_from_name);
32 use C4::Labels::Lib 1.000000 qw(get_label_summary html_table);
33 use C4::Labels::Batch 1.000000;
34
35 my $cgi = new CGI;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "labels/label-edit-batch.tmpl",
39         query           => $cgi,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { catalogue => 1 },
43         debug           => 1,
44     }
45 );
46
47 my $err = 0;
48 my $errstr = undef;
49 my $duplicate_count = undef;
50 my $duplicate_message = undef;
51 my $db_rows = {};
52 my $batch = undef;
53 my $display_columns = [ {_label_number  => {label => 'Label Number', link_field => 0}},
54                         {_summary       => {label => 'Summary', link_field => 0}},
55                         {_item_type     => {label => 'Item Type', link_field => 0}},
56                         {_barcode       => {label => 'Barcode', link_field => 0}},
57                         {select         => {label => 'Select', value => '_label_id'}},
58                       ];
59 my $op = $cgi->param('op') || undef;
60 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
61 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
62 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
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     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
82     $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
83     foreach my $item_number (@item_numbers) {
84         $err = $batch->add_item($item_number);
85     }
86     $errstr = "item(s) not added to batch $batch_id." if $err;
87 }
88 elsif ($op eq 'new') {
89     $batch = C4::Labels::Batch->new(branch_code => $branch_code);
90     $batch_id = $batch->get_attr('batch_id');
91 }
92 elsif ($op eq 'de_duplicate') {
93     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
94     $duplicate_count = $batch->remove_duplicates();
95     $duplicate_message = 1 if $duplicate_count != -1;
96     $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
97 }
98 else { # edit
99     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
100 }
101
102 my $items = $batch->get_attr('items');
103 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
104
105 my $table = html_table($display_columns, $db_rows);
106
107 $template->param(   
108                 err         => $err,
109                 errstr      => $errstr,
110                 ) if ($err ne 0);
111
112 $template->param(
113                 op                      => $op,
114                 batch_id                => $batch_id,
115                 table_loop              => $table,
116                 duplicate_message       => $duplicate_message,
117                 duplicate_count         => $duplicate_count,
118                 );
119
120 output_html_with_http_headers $cgi, $cookie, $template->output;