Bug 27655: Add barcode "columnname" to "column_settings.yml"
[koha.git] / labels / label-print.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Data::Dumper;
24
25 use C4::Context;
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators::Lib qw(get_all_templates get_all_layouts get_output_formats);
29 use C4::Labels::Batch;
30
31 my $cgi = CGI->new;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "labels/label-print.tt",
35         query           => $cgi,
36         type            => "intranet",
37         flagsrequired   => { catalogue => 1 },
38         debug           => 1,
39     }
40 );
41
42 my $op = $cgi->param('op') || 'none';
43 my @label_ids;
44 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');   # this will handle individual label printing
45 my @batch_ids;
46 @batch_ids = $cgi->multi_param('batch_id') if $cgi->param('batch_id');
47 my $layout_id = $cgi->param('layout_id') || undef;
48 my $template_id = $cgi->param('template_id') || undef;
49 my $start_label = $cgi->param('start_label') || 1;
50 my @item_numbers;
51 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
52 my $output_format = $cgi->param('output_format') || 'pdf';
53 my $referer = $cgi->param('referer') || undef;
54
55 my $from = $cgi->param('from') || undef;
56 my $to = $cgi->param('to') || undef;
57
58 my $layouts = undef;
59 my $templates = undef;
60 my $output_formats = undef;
61 my @batches = ();
62 my $multi_batch_count = scalar(@batch_ids) || ($from && $to) ? 1 : 0;
63 my $label_count = scalar(@label_ids);
64 my $item_count = scalar(@item_numbers);
65
66 if ($op eq 'export') {
67     if (@label_ids) {
68         my $label_id_param = '&amp;label_id=';
69         $label_id_param .= join ('&amp;label_id=',@label_ids);
70         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
71                          batch_id        => $batch_ids[0],
72                          template_id     => $template_id,
73                          layout_id       => $layout_id,
74                          start_label     => $start_label,
75                          label_ids       => $label_id_param,
76                          label_count     => scalar(@label_ids),
77                         });
78         $template->param(
79                         batches     => \@batches,
80                         referer     => $referer,
81                         );
82     }
83     elsif (@item_numbers) {
84         my $item_number_param = '&amp;item_number=';
85         $item_number_param .= join ('&amp;item_number=',@item_numbers);
86         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
87                          template_id     => $template_id,
88                          layout_id       => $layout_id,
89                          start_label     => $start_label,
90                          item_numbers    => $item_number_param,
91                          label_count     => scalar(@item_numbers),
92                         });
93         $template->param(
94                         batches     => \@batches,
95                         referer     => $referer,
96                         );
97     }
98     elsif (@batch_ids) {
99         foreach my $batch_id (@batch_ids) {
100            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
101                             batch_id        => $batch_id,
102                             template_id     => $template_id,
103                             layout_id       => $layout_id,
104                             start_label     => $start_label,
105                             });
106         }
107         $template->param(
108                         batches     => \@batches,
109                         referer     => $referer,
110                         );
111     }
112     elsif ($from and $to) {
113         my $dbh = C4::Context->dbh;
114
115         my $sth = $dbh->prepare('SELECT COUNT(*) AS has_barcode FROM creator_layouts WHERE printing_type LIKE("%BAR%") AND layout_id = ?;');
116         $sth->execute($layout_id);
117         if ($sth->fetchrow_hashref->{'has_barcode'} == 0) {
118             $sth = $dbh->prepare('SELECT COUNT(*) AS existing_count FROM items WHERE CAST(barcode AS unsigned) BETWEEN ? AND ?;');
119             $sth->execute($from, $to);
120             if ($sth->fetchrow_hashref->{'existing_count'} < ($to - $from + 1)) {
121                 $template->param( warn_empty_range => 1 )
122             }
123         }
124
125         push (@batches, {create_script   => 'label-create-pdf.pl',
126                  from            => $from,
127                  to              => $to,
128                  template_id     => $template_id,
129                  layout_id       => $layout_id,
130                  start_label     => $start_label,
131                  });
132        $template->param(
133                         batches     => \@batches,
134                         referer     => $referer,
135                         );
136     }
137 }
138 elsif ($op eq 'none') {
139     # setup select menus for selecting layout and template for this run...
140     $referer = $ENV{'HTTP_REFERER'};
141     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
142     @batch_ids = map{{batch_id => $_}} @batch_ids;
143     @label_ids = map{{label_id => $_}} @label_ids;
144     @item_numbers = map{{item_number => $_}} @item_numbers;
145     $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
146     $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
147     $output_formats = get_output_formats();
148     $template->param(
149                     batch_ids                   => \@batch_ids,
150                     label_ids                   => \@label_ids,
151                     item_numbers                => \@item_numbers,
152                     templates                   => $templates,
153                     layouts                     => $layouts,
154                     output_formats              => $output_formats,
155                     multi_batch_count           => $multi_batch_count,
156                     label_count                 => $label_count,
157                     item_count                  => $item_count,
158                     referer                     => $referer,
159                     from                        => $from,
160                     to                          => $to
161                     );
162 }
163 output_html_with_http_headers $cgi, $cookie, $template->output;