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