Bug 15172: (follow-up)Serial enumchron/sequence not visible when returning/checking...
[koha.git] / patroncards / 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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use autouse 'Data::Dumper' => qw(Dumper);
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators;
29 use C4::Patroncards;
30
31 my $cgi = new CGI;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "patroncards/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 = $cgi->multi_param('label_id') if $cgi->param('label_id');   # this will handle individual card printing; we use label_id to maintain consistency with the column names in the creator_batches table
45 my @batch_ids = $cgi->multi_param('batch_id') if $cgi->param('batch_id');
46 my $patronlist_id = $cgi->param('patronlist_id') || undef;
47 my $layout_id = $cgi->param('layout_id') || undef;
48 my $template_id = $cgi->param('template_id') || undef;
49 my $start_card = $cgi->param('start_card') || 1;
50 my @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
51 my $output_format = $cgi->param('output_format') || 'pdf';
52 my $referer = $cgi->param('referer') || undef;
53
54 my $layouts = undef;
55 my $templates = undef;
56 my $output_formats = undef;
57 my @batches = ();
58 my $multi_batch_count = scalar(@batch_ids);
59 my $card_count = scalar(@label_ids);
60 my $borrower_count = scalar(@borrower_numbers);
61
62 if ($op eq 'export') {
63     if (@label_ids) {
64         my $label_id_param = '&amp;label_id=';
65         $label_id_param .= join ('&amp;label_id=',@label_ids);
66         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
67                          batch_id        => $batch_ids[0],
68                          template_id     => $template_id,
69                          layout_id       => $layout_id,
70                          start_card      => $start_card,
71                          label_ids       => $label_id_param,
72                          card_count      => scalar(@label_ids),
73                         });
74         $template->param(
75                         batches     => \@batches,
76                         referer     => $referer,
77                         );
78     }
79     elsif (@borrower_numbers) {
80         my $borrower_number_param = '&amp;borrower_number=';
81         $borrower_number_param .= join ('&amp;borrower_number=',@borrower_numbers);
82         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
83                          template_id     => $template_id,
84                          layout_id       => $layout_id,
85                          start_card      => $start_card,
86                          borrower_numbers    => $borrower_number_param,
87                          card_count      => scalar(@borrower_numbers),
88                         });
89         $template->param(
90                         batches     => \@batches,
91                         referer     => $referer,
92                         );
93     }
94     elsif (@batch_ids) {
95         foreach my $batch_id (@batch_ids) {
96            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
97                             batch_id        => $batch_id,
98                             template_id     => $template_id,
99                             layout_id       => $layout_id,
100                             start_card      => $start_card,
101                             });
102         }
103         $template->param(
104                         batches     => \@batches,
105                         referer     => $referer,
106                         );
107     }
108     elsif ($patronlist_id ) {
109         $template->param(
110                          patronlist_id   => $patronlist_id,
111                          template_id     => $template_id,
112                          layout_id       => $layout_id,
113                          start_card      => $start_card,
114                          referer         => $referer,
115                         );
116     }
117 }
118 elsif ($op eq 'none') {
119     # setup select menus for selecting layout and template for this run...
120     $referer = $ENV{'HTTP_REFERER'};
121     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
122     @batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
123     @label_ids = grep{$_ = {label_id => $_}} @label_ids;
124     @borrower_numbers = grep{$_ = {borrower_number => $_}} @borrower_numbers;
125     $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Patroncards"');
126     $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Patroncards"');
127     $output_formats = get_output_formats();
128     $template->param(
129                     batch_ids                   => \@batch_ids,
130                     label_ids                   => \@label_ids,
131                     borrower_numbers            => \@borrower_numbers,
132                     patronlist_id               => $patronlist_id,
133                     templates                   => $templates,
134                     layouts                     => $layouts,
135                     output_formats              => $output_formats,
136                     multi_batch_count           => $multi_batch_count,
137                     card_count                  => $card_count,
138                     borrower_count              => $borrower_count,
139                     referer                     => $referer,
140                     );
141 }
142 output_html_with_http_headers $cgi, $cookie, $template->output;