Bug 18336: Convert schema from utf8 to utf8mb4
[koha.git] / acqui / neworderbiblio.pl
1 #!/usr/bin/perl
2
3 #origninally script to provide intranet (librarian) advanced search facility
4 #now script to do searching for acquisitions
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 NAME
25
26 neworderbiblio.pl
27
28 =head1 DESCRIPTION
29
30 this script allows to perform a new order from an existing record.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item search
37 the title the librarian has typed to search an existing record.
38
39 =item q
40 the keyword the librarian has typed to search an existing record.
41
42 =item author
43 the author of the new record.
44
45 =item num
46 the number of result per page to display
47
48 =item booksellerid
49 the id of the bookseller this script has to add an order.
50
51 =item basketno
52 the basket number to know on which basket this script have to add a new order.
53
54 =back
55
56 =cut
57
58 use Modern::Perl;
59
60 use C4::Search;
61 use CGI qw ( -utf8 );
62 use C4::Biblio;
63 use C4::Auth;
64 use C4::Output;
65 use C4::Koha;
66 use C4::Budgets qw/ GetBudgetHierarchy /;
67
68 use Koha::Acquisition::Booksellers;
69 use Koha::SearchEngine;
70 use Koha::SearchEngine::Search;
71 use Koha::SearchEngine::QueryBuilder;
72 use Koha::Patrons;
73
74 my $input = new CGI;
75
76 #getting all CGI params into a hash.
77 my $params = $input->Vars;
78
79 my $page             = $params->{'page'} || 1;
80 my $query            = $params->{'q'};
81 my $results_per_page = $params->{'num'} || 20;
82 my $booksellerid     = $params->{'booksellerid'};
83 my $basketno         = $params->{'basketno'};
84 my $sub              = $params->{'sub'};
85 my $bookseller       = Koha::Acquisition::Booksellers->find( $booksellerid );
86
87 # getting the template
88 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
89     {
90         template_name   => "acqui/neworderbiblio.tt",
91         query           => $input,
92         type            => "intranet",
93         authnotrequired => 0,
94         flagsrequired   => { acquisition => 'order_manage' },
95     }
96 );
97
98 # Searching the catalog.
99
100 my @operands = $query;
101 my $QParser;
102 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
103 my $builtquery;
104 my $builder  = Koha::SearchEngine::QueryBuilder->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
105 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
106 if ($QParser) {
107     $builtquery = $query;
108 } else {
109     ( undef,$builtquery,undef,undef,undef,undef,undef,undef,undef,undef) = $builder->build_query_compat(undef,\@operands);
110 }
111 my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page);
112
113 if (defined $error) {
114     $template->param(
115         query_error => $error,
116         basketno             => $basketno,
117         booksellerid     => $bookseller->id,
118         name             => $bookseller->name,
119     );
120     output_html_with_http_headers $input, $cookie, $template->output;
121     exit;
122 }
123
124 my @results;
125
126 foreach my $result ( @{$marcresults} ) {
127     my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $result );
128     my $biblio = TransformMarcToKoha( $marcrecord, '' );
129     $biblio->{subtitles} = GetRecordValue( 'subtitle', GetMarcBiblio({ biblionumber => $biblio->{biblionumber} }),  GetFrameworkCode( $biblio->{biblionumber} ) );
130
131     $biblio->{booksellerid} = $booksellerid;
132     push @results, $biblio;
133
134 }
135
136 my $patron = Koha::Patrons->find( $loggedinuser );
137 my $budgets = GetBudgetHierarchy(q{},$patron->branchcode,$patron->borrowernumber);
138 my $has_budgets = 0;
139 foreach my $r (@{$budgets}) {
140     if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
141         next;
142     }
143     $has_budgets = 1;
144     last;
145 }
146
147 $template->param(
148     has_budgets          => $has_budgets,
149     basketno             => $basketno,
150     booksellerid         => $bookseller->id,
151     name                 => $bookseller->name,
152     resultsloop          => \@results,
153     total                => $total_hits,
154     query                => $query,
155     pagination_bar       => pagination_bar( "/cgi-bin/koha/acqui/neworderbiblio.pl?q=$query&booksellerid=$booksellerid&basketno=$basketno&", getnbpages( $total_hits, $results_per_page ), $page, 'page' ),
156 );
157
158 # BUILD THE TEMPLATE
159 output_html_with_http_headers $input, $cookie, $template->output;