Head & rel_2_2 merged
[wip/koha-chris_n.git] / acqui / suggestion-select.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use HTML::Template;
6
7 use C4::Auth;       # get_template_and_user
8 use C4::Interface::CGI::Output;
9 use C4::Suggestions;
10 use C4::Biblio;
11 use C4::SearchMarc;
12
13 my $input = new CGI;
14
15 my $basketno = $input->param('basketno');
16 my $supplierid = $input->param('booksellerid');
17
18 my $title = $input->param('title');
19 my $author = $input->param('author');
20 my $note = $input->param('note');
21 my $copyrightdate =$input->param('copyrightdate');
22 my $publishercode = $input->param('publishercode');
23 my $volumedesc = $input->param('volumedesc');
24 my $publicationyear = $input->param('publicationyear');
25 my $place = $input->param('place');
26 my $isbn = $input->param('isbn');
27 my $duplicateNumber = $input->param('duplicateNumber');
28 my $suggestionid = $input->param('suggestionid');
29
30 my $status = 'ACCEPTED';
31 my $suggestedbyme = -1; # search ALL suggestors
32 my $op = $input->param('op');
33 $op = 'else' unless $op;
34
35 my $dbh = C4::Context->dbh;
36 my ($template, $borrowernumber, $cookie)
37     = get_template_and_user({template_name => "acqui/suggestion-select.tmpl",
38                              type => "intranet",
39                              query => $input,
40                              authnotrequired => 1,
41                              flagsrequired => {acquisition => 1},
42                          });
43
44 if ($op eq 'connectDuplicate') {
45         connectSuggestionAndBiblio($suggestionid,$duplicateNumber);
46 }
47 my $suggestions_loop= &searchsuggestion($borrowernumber,$author,$title,$publishercode,$status,$suggestedbyme);
48 foreach (@$suggestions_loop) {
49         unless ($_->{biblionumber}) {
50                 my (@tags, @and_or, @excluding, @operator, @value, $offset,$length);
51                 # search on biblio.title
52                 if ($_->{title}) {
53                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblio.title","");
54                         push @tags, "'".$tag.$subfield."'";
55                         push @and_or, "and";
56                         push @excluding, "";
57                         push @operator, "contains";
58                         push @value, $_->{title};
59                 }
60                 if ($_->{author}) {
61                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblio.author","");
62                         push @tags, "'".$tag.$subfield."'";
63                         push @and_or, "and";
64                         push @excluding, "";
65                         push @operator, "contains";
66                         push @value, $_->{author};
67                 }
68                 # ... and on publicationyear.
69                 if ($_->{publicationyear}) {
70                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblioitems.publicationyear","");
71                         push @tags, "'".$tag.$subfield."'";
72                         push @and_or, "and";
73                         push @excluding, "";
74                         push @operator, "=";
75                         push @value, $_->{publicationyear};
76                 }
77                 # ... and on publisher.
78                 if ($_->{publishercode}) {
79                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblioitems.publishercode","");
80                         push @tags, "'".$tag.$subfield."'";
81                         push @and_or, "and";
82                         push @excluding, "";
83                         push @operator, "=";
84                         push @value, $_->{publishercode};
85                 }
86         
87                 my ($finalresult,$nbresult) = C4::SearchMarc::catalogsearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10);
88                 # there is at least 1 result => return the 1st one
89                 if ($nbresult) {
90         #               warn "$nbresult => ".@$finalresult[0]->{biblionumber},@$finalresult[0]->{bibid},@$finalresult[0]->{title};
91 #                       warn "DUPLICATE ==>".@$finalresult[0]->{biblionumber},@$finalresult[0]->{bibid},@$finalresult[0]->{title};
92                         $_->{duplicateBiblionumber} = @$finalresult[0]->{biblionumber};
93                 }
94         }
95 }
96 $template->param(suggestions_loop => $suggestions_loop,
97                                 title => $title,
98                                 author => $author,
99                                 publishercode => $publishercode,
100                                 status => $status,
101                                 suggestedbyme => $suggestedbyme,
102                                 basketno => $basketno,
103                                 supplierid => $supplierid,
104                                 "op_$op" => 1,
105                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
106                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
107                 IntranetNav => C4::Context->preference("IntranetNav"),
108 );
109 output_html_with_http_headers $input, $cookie, $template->output;