3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
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.
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.
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>.
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;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34 template_name => "labels/label-print.tt",
37 flagsrequired => { catalogue => 1 },
41 my $op = $cgi->param('op') || 'none';
43 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id'); # this will handle individual label printing
45 @batch_ids = $cgi->multi_param('batch_id') if $cgi->param('batch_id');
46 my $layout_id = $cgi->param('layout_id') || undef;
47 my $template_id = $cgi->param('template_id') || undef;
48 my $start_label = $cgi->param('start_label') || 1;
50 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
51 my $output_format = $cgi->param('output_format') || 'pdf';
52 my $referer = $cgi->param('referer') || undef;
54 my $txt_from = $cgi->param('from') || undef;
55 my $txt_to = $cgi->param('to') || undef;
56 my $from = int($txt_from) || undef;
57 my $to = int($txt_to) || undef;
58 my $range = length($txt_from) || undef;
60 warn ("range = " . Data::Dumper::Dumper($range));
63 my $templates = undef;
64 my $output_formats = undef;
66 my $multi_batch_count = scalar(@batch_ids) || ($from && $to) ? 1 : 0;
67 my $label_count = scalar(@label_ids);
68 my $item_count = scalar(@item_numbers);
70 if ($op eq 'export') {
72 my $label_id_param = '&label_id=';
73 $label_id_param .= join ('&label_id=',@label_ids);
74 push (@batches, {create_script => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
75 batch_id => $batch_ids[0],
76 template_id => $template_id,
77 layout_id => $layout_id,
78 start_label => $start_label,
79 label_ids => $label_id_param,
80 label_count => scalar(@label_ids),
87 elsif (@item_numbers) {
88 my $item_number_param = '&item_number=';
89 $item_number_param .= join ('&item_number=',@item_numbers);
90 push (@batches, {create_script => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
91 template_id => $template_id,
92 layout_id => $layout_id,
93 start_label => $start_label,
94 item_numbers => $item_number_param,
95 label_count => scalar(@item_numbers),
103 foreach my $batch_id (@batch_ids) {
104 push (@batches, {create_script => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
105 batch_id => $batch_id,
106 template_id => $template_id,
107 layout_id => $layout_id,
108 start_label => $start_label,
112 batches => \@batches,
116 elsif ($from and $to) {
117 my $dbh = C4::Context->dbh;
119 my $sth = $dbh->prepare('SELECT COUNT(*) AS has_barcode FROM creator_layouts WHERE printing_type LIKE("%BAR%") AND layout_id = ?;');
120 $sth->execute($layout_id);
121 if ($sth->fetchrow_hashref->{'has_barcode'} == 0) {
122 $sth = $dbh->prepare('SELECT COUNT(*) AS existing_count FROM items WHERE CAST(barcode AS unsigned) BETWEEN ? AND ?;');
123 $sth->execute($from, $to);
124 if ($sth->fetchrow_hashref->{'existing_count'} < ($to - $from + 1)) {
125 $template->param( warn_empty_range => 1 )
129 push (@batches, {create_script => 'label-create-pdf.pl',
133 template_id => $template_id,
134 layout_id => $layout_id,
135 start_label => $start_label,
138 batches => \@batches,
143 elsif ($op eq 'none') {
144 # setup select menus for selecting layout and template for this run...
145 $referer = $ENV{'HTTP_REFERER'};
146 $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
147 @batch_ids = map{{batch_id => $_}} @batch_ids;
148 @label_ids = map{{label_id => $_}} @label_ids;
149 @item_numbers = map{{item_number => $_}} @item_numbers;
150 $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
151 $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
152 $output_formats = get_output_formats();
154 batch_ids => \@batch_ids,
155 label_ids => \@label_ids,
156 item_numbers => \@item_numbers,
157 templates => $templates,
159 output_formats => $output_formats,
160 multi_batch_count => $multi_batch_count,
161 label_count => $label_count,
162 item_count => $item_count,
167 txt_from => $txt_from,
171 output_html_with_http_headers $cgi, $cookie, $template->output;