0d1e0bba19739bdf88f91f26db0b2d21c12d2895
[srvgit] / 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     }
39 );
40
41 my $op = $cgi->param('op') || 'none';
42 my @label_ids;
43 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');   # this will handle individual label printing
44 my @batch_ids;
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;
49 my @item_numbers;
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;
53
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 $barcode_length = length($txt_from) || undef;
59
60 my $layouts = undef;
61 my $templates = undef;
62 my $output_formats = undef;
63 my @batches = ();
64 my $multi_batch_count = scalar(@batch_ids) || ($from && $to) ? 1 : 0;
65 my $label_count = scalar(@label_ids);
66 my $item_count = scalar(@item_numbers);
67
68 if ($op eq 'export') {
69     if (@label_ids) {
70         my $label_id_param = '&amp;label_id=';
71         $label_id_param .= join ('&amp;label_id=',@label_ids);
72         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
73                          batch_id        => $batch_ids[0],
74                          template_id     => $template_id,
75                          layout_id       => $layout_id,
76                          start_label     => $start_label,
77                          label_ids       => $label_id_param,
78                          label_count     => scalar(@label_ids),
79                         });
80         $template->param(
81                         batches     => \@batches,
82                         referer     => $referer,
83                         );
84     }
85     elsif (@item_numbers) {
86         my $item_number_param = '&amp;item_number=';
87         $item_number_param .= join ('&amp;item_number=',@item_numbers);
88         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
89                          template_id     => $template_id,
90                          layout_id       => $layout_id,
91                          start_label     => $start_label,
92                          item_numbers    => $item_number_param,
93                          label_count     => scalar(@item_numbers),
94                         });
95         $template->param(
96                         batches     => \@batches,
97                         referer     => $referer,
98                         );
99     }
100     elsif (@batch_ids) {
101         foreach my $batch_id (@batch_ids) {
102            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
103                             batch_id        => $batch_id,
104                             template_id     => $template_id,
105                             layout_id       => $layout_id,
106                             start_label     => $start_label,
107                             });
108         }
109         $template->param(
110                         batches     => \@batches,
111                         referer     => $referer,
112                         );
113     }
114     elsif ($from and $to) {
115         my $dbh = C4::Context->dbh;
116
117         my $sth = $dbh->prepare('SELECT COUNT(*) AS has_barcode FROM creator_layouts WHERE printing_type LIKE("%BAR%") AND layout_id = ?;');
118         $sth->execute($layout_id);
119         if ($sth->fetchrow_hashref->{'has_barcode'} == 0) {
120             $sth = $dbh->prepare('SELECT COUNT(*) AS existing_count FROM items WHERE CAST(barcode AS unsigned) BETWEEN ? AND ?;');
121             $sth->execute($from, $to);
122             if ($sth->fetchrow_hashref->{'existing_count'} < ($to - $from + 1)) {
123                 $template->param( warn_empty_range => 1 )
124             }
125         }
126
127         push (@batches, {create_script   => 'label-create-pdf.pl',
128                  from            => $from,
129                  to              => $to,
130                  barcode_length  => $barcode_length,
131                  template_id     => $template_id,
132                  layout_id       => $layout_id,
133                  start_label     => $start_label,
134                  });
135        $template->param(
136                         batches     => \@batches,
137                         referer     => $referer,
138                         );
139     }
140 }
141 elsif ($op eq 'none') {
142     # setup select menus for selecting layout and template for this run...
143     $referer = $ENV{'HTTP_REFERER'};
144     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
145     @batch_ids = map{{batch_id => $_}} @batch_ids;
146     @label_ids = map{{label_id => $_}} @label_ids;
147     @item_numbers = map{{item_number => $_}} @item_numbers;
148     $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
149     $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
150     $output_formats = get_output_formats();
151     $template->param(
152                     batch_ids                   => \@batch_ids,
153                     label_ids                   => \@label_ids,
154                     item_numbers                => \@item_numbers,
155                     templates                   => $templates,
156                     layouts                     => $layouts,
157                     output_formats              => $output_formats,
158                     multi_batch_count           => $multi_batch_count,
159                     label_count                 => $label_count,
160                     item_count                  => $item_count,
161                     referer                     => $referer,
162                     from                        => $from,
163                     to                          => $to,
164                     barcode_length              => $barcode_length,
165                     txt_from                    => $txt_from,
166                     txt_to                      => $txt_to
167                     );
168 }
169 output_html_with_http_headers $cgi, $cookie, $template->output;