[24/40] Adding single/multiple label printing to label export code and interface.
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
23
24 use CGI;
25 use HTML::Template::Pro;
26 use List::Util qw( max min );
27 use POSIX qw(ceil);
28
29 use C4::Auth qw(get_template_and_user);
30 use C4::Output qw(output_html_with_http_headers);
31 use C4::Context;
32 use C4::Dates;
33 use C4::Search qw(SimpleSearch);
34 use C4::Biblio qw(TransformMarcToKoha);
35 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
36 use C4::Koha qw(GetItemTypes);    # XXX subfield_is_koha_internal_p
37 use C4::Labels::Lib qw(html_table);
38 use C4::Debug;
39
40 BEGIN {
41     $debug = $debug || $cgi_debug;
42     if ($debug) {
43         require Data::Dumper;
44         import Data::Dumper qw(Dumper);
45     }
46 }
47
48 my $query = new CGI;
49
50 my $type      = $query->param('type');
51 my $op        = $query->param('op');
52 my $batch_id  = $query->param('batch_id');
53 my $ccl_query = $query->param('ccl_query');
54 my $startfrom = $query->param('startfrom') || 1;
55 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
56 my (
57     $total_hits,  $orderby, $results,  $total,  $error,
58     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
59 );
60 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
61 my $show_results = 0;
62 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
63                         {_item_call_number      => {label => "Call Number", link_field => 0}},
64                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
65                         {_barcode               => {label => "Barcode", link_field => 0}},
66                         {select                 => {label => "Select", value => "_item_number"}},
67                       ];
68
69 if ( $op eq "do_search" ) {
70     $idx         = $query->param('idx');
71     $ccl_textbox = $query->param('ccl_textbox');
72     if ( $ccl_textbox && $idx ) {
73         $ccl_query = "$idx=$ccl_textbox";
74     }
75
76     $datefrom = $query->param('datefrom');
77     $dateto   = $query->param('dateto');
78
79     if ($datefrom) {
80         $datefrom = C4::Dates->new($datefrom);
81         $ccl_query .= ' and ' if $ccl_textbox;
82         $ccl_query .=
83           "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
84     }
85
86     if ($dateto) {
87         $dateto = C4::Dates->new($dateto);
88         $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
89         $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto->output("iso");
90     }
91
92     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
93     ( $error, $marcresults, $total_hits ) =
94       SimpleSearch( $ccl_query, $offset, $resultsperpage );
95
96     if (scalar($marcresults) > 0) {
97         $show_results = scalar @$marcresults;
98     }
99     else {
100         $debug and warn "ERROR label-item-search: no results from SimpleSearch";
101
102         # leave $show_results undef
103     }
104 }
105
106 if ($show_results) {
107     my $hits = $show_results;
108     my @results_set = ();
109     my @items =();
110     # This code needs to be refactored using these subs...
111     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
112     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
113     for ( my $i = 0 ; $i < $hits ; $i++ ) {
114         my @row_data= ();
115         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
116         my $marcrecord = MARC::File::USMARC::decode( $marcresults->[$i] );
117         #DEBUG Notes: Transform it to Koha form...
118         my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
119         #DEBUG Notes: Stuff the bib into @biblio_data...
120         push (@results_set, $biblio);
121         my $biblionumber = $biblio->{'biblionumber'};
122         #DEBUG Notes: Grab the item numbers associated with this MARC record...
123         my $itemnums = get_itemnumbers_of($biblionumber);
124         #DEBUG Notes: Retrieve the item data for each number...
125         if (my $iii = $itemnums->{$biblionumber}) {
126             my $item_results = GetItemInfosOf(@$iii);
127             foreach my $item ( keys %$item_results ) {
128                 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
129                 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
130                     my $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
131                     $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
132                     $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
133                     $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
134                     $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
135                     unshift (@row_data, $item_data);    # item numbers are given to us in descending order by get_itemnumbers_of()...
136                 }
137             }
138             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
139         }
140         else {
141             # FIXME: Some error trapping code needed
142             syslog("LOG_ERR", "labels/label-item-search.pl : No item numbers retrieved for biblio number: %s", $biblionumber);
143         }
144     }
145
146     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
147         {
148             template_name   => "labels/result.tmpl",
149             query           => $query,
150             type            => "intranet",
151             authnotrequired => 0,
152             flagsrequired   => { borrowers => 1 },
153             flagsrequired   => { catalogue => 1 },
154             debug           => 1,
155         }
156     );
157
158     # build page nav stuff.
159     my ( @field_data, @numbers );
160     $total = $total_hits;
161
162     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
163         $displayprev );
164
165     if ( $total > $resultsperpage ) {
166         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
167         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
168             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
169             push @numbers,
170               {
171                 number    => $page,
172                 startfrom => $startfrm
173               };
174         }
175
176         $from          = $startfrom;
177         $startfromprev = $startfrom - $resultsperpage;
178         $startfromnext = $startfrom + $resultsperpage;
179
180         $to =
181             $startfrom + $resultsperpage > $total
182           ? $total
183           : $startfrom + $resultsperpage - 1;
184
185         # multi page display
186         $displaynext = 0;
187         $displayprev = $startfrom > 1 ? $startfrom : 0;
188
189         $displaynext = 1 if $to < $total_hits;
190
191     }
192     else {
193         $displayprev = 0;
194         $displaynext = 0;
195     }
196
197     $template->param(
198         total          => $total_hits,
199         from           => $from,
200         to             => $to,
201         startfromnext  => $startfromnext,
202         startfromprev  => $startfromprev,
203         startfrom      => $startfrom,
204         displaynext    => $displaynext,
205         displayprev    => $displayprev,
206         resultsperpage => $resultsperpage,
207         numbers        => \@numbers,
208     );
209
210     $template->param(
211         results   => ($show_results ? 1 : 0),
212         result_set=> \@results_set,
213         batch_id  => $batch_id,
214         type      => $type,
215         idx       => $idx,
216         ccl_query => $ccl_query,
217     );
218 }
219
220 #
221 #   search section
222 #
223
224 else {
225     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
226         {
227             template_name   => "labels/search.tmpl",
228             query           => $query,
229             type            => "intranet",
230             authnotrequired => 0,
231             flagsrequired   => { catalogue => 1 },
232             debug           => 1,
233         }
234     );
235     my $itemtypes = GetItemTypes;
236     my @itemtypeloop;
237     foreach my $thisitemtype ( keys %$itemtypes ) {
238         my %row = (
239             value       => $thisitemtype,
240             description => $itemtypes->{$thisitemtype}->{'description'},
241         );
242         push @itemtypeloop, \%row;
243     }
244     $template->param(
245         itemtypeloop => \@itemtypeloop,
246         batch_id     => $batch_id,
247         type         => $type,
248     );
249
250 }
251
252 # Print the page
253 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
254 output_html_with_http_headers $query, $cookie, $template->output;