newordersuggestion.pl replaces suggestion-select.pl
[wip/koha-chris_n.git] / acqui / newordersuggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 # $Id$
19
20 =head1 NAME
21 newordersuggestion.pl
22
23 =head1 DESCRIPTION
24 this script allow to add an order from a existing suggestion.
25
26 =head1 CGI PARAMETERS
27
28 =over 4
29
30 =item basketno
31 the number of this basket.
32
33 =item booksellerid
34 the bookseller who sells this record.
35
36 =item title
37 the title of this record suggested.
38
39 =item author
40 the author of this suggestion.
41
42 =item note
43 this param allow to enter a note with this suggestion.
44
45 =item copyrightdate
46 the copyright date for this suggestion.
47
48 =item publishercode
49
50 =item volumedesc
51
52 =item publicationyear
53 the publication year of this record.
54
55 =item place
56
57 =item isbn
58 the isbn of this suggestion.
59
60 =item duplicateNumber
61 is the biblionumber to put to the new suggestion.
62
63 =item suggestionid
64 the id of the suggestion to select.
65
66 =item op
67 can be equal to
68     * connectDuplicate :
69         then call to the function : ConnectSuggestionAndBiblio.
70         i.e set the biblionumber of this suggestion.
71     * else :
72         is the default value.
73 =back
74
75 =cut
76
77 use strict;
78 require Exporter;
79 use CGI;
80 use HTML::Template;
81 use C4::Auth;       # get_template_and_user
82 use C4::Interface::CGI::Output;
83 use C4::Suggestions;
84 use C4::Biblio;
85 use C4::SearchMarc;
86
87 my $input = new CGI;
88
89 my $basketno = $input->param('basketno');
90 my $supplierid = $input->param('booksellerid');
91 my $title = $input->param('title');
92 my $author = $input->param('author');
93 my $note = $input->param('note');
94 my $copyrightdate =$input->param('copyrightdate');
95 my $publishercode = $input->param('publishercode');
96 my $volumedesc = $input->param('volumedesc');
97 my $publicationyear = $input->param('publicationyear');
98 my $place = $input->param('place');
99 my $isbn = $input->param('isbn');
100 my $duplicateNumber = $input->param('duplicateNumber');
101 my $suggestionid = $input->param('suggestionid');
102
103 my $status = 'ACCEPTED';
104 my $suggestedbyme = -1; # search ALL suggestors
105 my $op = $input->param('op');
106 $op = 'else' unless $op;
107
108 my $dbh = C4::Context->dbh;
109 my ($template, $borrowernumber, $cookie)
110     = get_template_and_user({template_name => "acqui/suggestion-select.tmpl",
111                              type => "intranet",
112                              query => $input,
113                              authnotrequired => 1,
114                              flagsrequired => {acquisition => 1},
115                          });
116
117 if ($op eq 'connectDuplicate') {
118         ConnectSuggestionAndBiblio($suggestionid,$duplicateNumber);
119 }
120 my $suggestions_loop= &SearchSuggestion($borrowernumber,$author,$title,$publishercode,$status,$suggestedbyme);
121 foreach (@$suggestions_loop) {
122         unless ($_->{biblionumber}) {
123                 my (@tags, @and_or, @excluding, @operator, @value, $offset,$length);
124                 # search on biblio.title
125                 if ($_->{title}) {
126                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblio.title","");
127                         push @tags, "'".$tag.$subfield."'";
128                         push @and_or, "and";
129                         push @excluding, "";
130                         push @operator, "contains";
131                         push @value, $_->{title};
132                 }
133                 if ($_->{author}) {
134                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblio.author","");
135                         push @tags, "'".$tag.$subfield."'";
136                         push @and_or, "and";
137                         push @excluding, "";
138                         push @operator, "contains";
139                         push @value, $_->{author};
140                 }
141                 # ... and on publicationyear.
142                 if ($_->{publicationyear}) {
143                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblioitems.publicationyear","");
144                         push @tags, "'".$tag.$subfield."'";
145                         push @and_or, "and";
146                         push @excluding, "";
147                         push @operator, "=";
148                         push @value, $_->{publicationyear};
149                 }
150                 # ... and on publisher.
151                 if ($_->{publishercode}) {
152                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblioitems.publishercode","");
153                         push @tags, "'".$tag.$subfield."'";
154                         push @and_or, "and";
155                         push @excluding, "";
156                         push @operator, "=";
157                         push @value, $_->{publishercode};
158                 }
159         
160                 my ($finalresult,$nbresult) = C4::SearchMarc::catalogsearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10);
161                 # there is at least 1 result => return the 1st one
162                 if ($nbresult) {
163         #               warn "$nbresult => ".@$finalresult[0]->{biblionumber},@$finalresult[0]->{bibid},@$finalresult[0]->{title};
164 #                       warn "DUPLICATE ==>".@$finalresult[0]->{biblionumber},@$finalresult[0]->{bibid},@$finalresult[0]->{title};
165                         $_->{duplicateBiblionumber} = @$finalresult[0]->{biblionumber};
166                 }
167         }
168 }
169 $template->param(suggestions_loop => $suggestions_loop,
170                                 title => $title,
171                                 author => $author,
172                                 publishercode => $publishercode,
173                                 status => $status,
174                                 suggestedbyme => $suggestedbyme,
175                                 basketno => $basketno,
176                                 supplierid => $supplierid,
177                                 "op_$op" => 1,
178                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
179                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
180                 IntranetNav => C4::Context->preference("IntranetNav"),
181 );
182 output_html_with_http_headers $input, $cookie, $template->output;