sub getbranches renamed to GetBranches according to the coding guidelines
[koha_fer] / acqui / newbasket2.pl
index 75e1d93..a693d43 100755 (executable)
@@ -2,7 +2,6 @@
 #origninally script to provide intranet (librarian) advanced search facility
 #now script to do searching for acquisitions
 
-
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -24,215 +23,287 @@ use strict;
 use C4::Search;
 use CGI;
 use C4::Output;
-use C4::Catalogue;
+use C4::Acquisition;
 use C4::Biblio;
+use HTML::Template;
+use C4::Auth;
+use C4::Interface::CGI::Output;
+
+#use Data::Dumper;
 
 my $env;
 my $input = new CGI;
-print $input->header;
+
+#print $input->header;
+
 #whether it is called from the opac of the intranet
-my $type=$input->param('type');
-if ($type eq ''){
-  $type = 'intra';
+my $type = $input->param('type');
+if ( $type eq '' ) {
+    $type = 'intra';
 }
-#setup colours
-my $main;
-my $secondary;
-  $main='#cccc99';
-  $secondary='#ffffcc';
-
 
 #print $input->dump;
 my $blah;
 my %search;
+
 #build hash of users input
-my $title=$input->param('search');
-$search{'title'}=$title;
-my $keyword=$input->param('d');
-$search{'keyword'}=$keyword;
-my $author=$input->param('author');
-$search{'author'}=$author;
+my $title = $input->param('search');
+$search{'title'} = $title;
+my $keyword = $input->param('d');
+$search{'keyword'} = $keyword;
+my $author = $input->param('author');
+$search{'author'} = $author;
 
 my @results;
-my $offset=$input->param('offset');
-if ($offset eq ''){
-  $offset=0;
+my $offset = $input->param('offset');
+if ( $offset eq '' ) {
+    $offset = 0;
 }
-my $num=$input->param('num');
-if ($num eq ''){
-  $num=10;
+my $num = $input->param('num');
+if ( $num eq '' ) {
+    $num = 10;
 }
-my $id=$input->param('id');
-my $basket=$input->param('basket');
-my $sub=$input->param('sub');
 my $donation;
-if ($id == 72){
-  $donation='yes';
+my $booksellerid = $input->param('booksellerid');
+if ( $booksellerid == 72 ) {
+    $donation = 'yes';
 }
+my $basketno = $input->param('basketno');
+my $sub      = $input->param('sub');
+
 #print $sub;
-my ($count,@booksellers)=bookseller($id);
+my ( $count, @booksellers ) = bookseller($booksellerid);
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "acqui/newbasket2.tmpl",
+        query           => $input,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { order => 1 },
+        debug           => 1,
+    }
+);
 
-print startpage();
-print startmenu('acquisitions');
-print mkheadr(1,"Shopping Basket For: $booksellers[0]->{'name'}");
+#my $template = gettemplate("acqui/newbasket2.tmpl");
+#print startpage();
+#print startmenu('acquisitions');
+my $invalidsearch;
 
-if ($donation ne 'yes'){
-  print "<a href=newbiblio.pl?id=$id&basket=$basket&sub=$sub>";
-} else {
-  print "<a href=newdonation.pl?id=$id&basket=$basket&sub=$sub>";
+if ( $keyword ne '' ) {
+    ( $count, @results ) =
+      KeywordSearch( undef, 'intra', \%search, $num, $offset );
+}
+elsif ( $search{'front'} ne '' ) {
+    ( $count, @results ) =
+      FrontSearch( undef, 'intra', \%search, $num, $offset );
+}
+elsif ( $search{'author'} || $search{'title'} ) {
+    ( $count, @results ) = CatSearch( undef, 'loose', \%search, $num, $offset );
+}
+else {
+    $invalidsearch = 1;
 }
-print <<printend
-<img src=/images/add-biblio.gif width=187 heigth=42 border=0 align=right alt="Add New Biblio"></a>
-<a href=basket.pl?basket=$basket><img src=/images/view-basket.gif width=187 heigth=42 border=0 align=right alt="View Basket"></a>
-
-<FORM ACTION="/cgi-bin/koha/acqui/newbasket2.pl">
-<input type=hidden name=id value="$id">
-<input type=hidden name=basket value="$basket">
-<b>New Search: </b><INPUT TYPE="text"  SIZE="25"   NAME="search"></form>
-<br clear=all>
-
-printend
-;
-
-print center();
-
-
-    if ($keyword ne ''){
-#      print "hey";
-      ($count,@results)=KeywordSearch(undef,'intra',\%search,$num,$offset);
-    } elsif ($search{'front'} ne '') {
-    ($count,@results)=FrontSearch(undef,'intra',\%search,$num,$offset);
-    }else {
-      ($count,@results)=CatSearch(undef,'loose',\%search,$num,$offset);
-#            print "hey";
+
+my @loopsearch;
+
+while ( my ( $key, $value ) = each %search ) {
+    if ( $value ne '' ) {
+        my %linesearch;
+        $value =~ s/\\//g;
+        $linesearch{key}   = $key;
+        $linesearch{value} = $value;
+        push( @loopsearch, \%linesearch );
     }
+}
+
+my $offset2 = $num + $offset;
+my $dispnum = $offset + 1;
+if ( $offset2 > $count ) {
+    $offset2 = $count;
+}
 
-print "You searched on ";
-while ( my ($key, $value) = each %search) {
-  if ($value ne ''){
-    $value=~ s/\\//g;
-    print bold("$key $value,");
-  }
+my $count2 = @results;
+if ( $keyword ne '' && $offset > 0 ) {
+    $count2 = $count - $offset;
+    if ( $count2 > 10 ) {
+        $count2 = 10;
+    }
 }
-print " $count results found";
-my $offset2=$num+$offset;
-my $dispnum=$offset+1;
-print "<br> Results $dispnum to $offset2 displayed";
-print mktablehdr;
+my $i      = 0;
+my $colour = 0;
 
+my @loopresult;
 
-print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
+while ( $i < $count2 ) {
+    my %lineres;
+    my $toggle;
 
+    my $result = $results[$i];
+    $result->{'title'} =~ s/\`/\\\'/g;
+    my $title2  = $result->{'title'};
+    my $author2 = $result->{'author'};
+    $author2 =~ s/ /%20/g;
+    $title2  =~ s/ /%20/g;
+    $title2  =~ s/\#/\&\#x23;/g;
+    $title2  =~ s/\"/\&quot\;/g;
 
-my $count2=@results;
-if ($keyword ne '' && $offset > 0){
-  $count2=$count-$offset;
-  if ($count2 > 10){
-    $count2=10;
-  }
-}
-#print $count2;
-my $i=0;
-my $colour=1;
-while ($i < $count2){
-#    print $results[$i]."\n";
-#    my @stuff=split('\t',$results[$i]);
-    my $result=$results[$i];
-    $result->{'title'}=~ s/\`/\\\'/g;
-    my $title2=$result->{'title'};
-    my $author2=$result->{'author'};
-    my $copyright=$result->{'copyrightdate'};
-    $author2=~ s/ /%20/g;
-    $title2=~ s/ /%20/g;
-    $title2=~ s/\#/\&\#x23;/g;
-    $title2=~ s/\"/\&quot\;/g;
     my $itemcount;
-    my $location='';
-    if ($donation eq 'yes'){
-      $result->{'title'}=mklink("/cgi-bin/koha/acqui/newdonation.pl?author=$author2&copyright=$copyright&id=$id&basket=$basket&biblio=$result->{'biblionumber'}&title=$title2",$result->{'title'});
-    } else {
-      $result->{'title'}=mklink("/cgi-bin/koha/acqui/newbiblio.pl?sub=$sub&author=$author2&copyright=$copyright&id=$id&basket=$basket&biblio=$result->{'biblionumber'}&title=$title2",$result->{'title'});
-    }
-    my $word=$result->{'author'};
-      $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
-      $word=~ s/  //g;
-      $word=~ s/ /%20/g;
-      $word=~ s/\,/\,%20/g;
-      $word=~ s/\n//g;
-      my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
-      $result->{'author'}=mklink($url,$result->{'author'});
-      my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit)=C4::Search::itemcount($env,$result->{'biblionumber'},$type);
-      $itemcount=$count;
-      if ($nacount > 0){
+    my $location      = '';
+    my $location_only = '';
+    my $word          = $result->{'author'};
+    $word =~ s/([a-z]) +([a-z])/$1%20$2/ig;
+    $word =~ s/  //g;
+    $word =~ s/ /%20/g;
+    $word =~ s/\,/\,%20/g;
+    $word =~ s/\n//g;
+    $lineres{word} = $word;
+    $lineres{type} = $type;
+
+    my ( $counts, $branchcounts ) =
+      C4::Search::itemcount( $env, $result->{'biblionumber'}, $type );
+
+    if ( $counts->{'nacount'} > 0 ) {
         $location .= "On Loan";
-       if ($nacount >1 ){
-         $location .= " ($nacount)";
-         }
-        $location.=" ";
-      }
-      if ($lcount > 0){
-         $location .= "Levin";
-         if ($lcount >1 ){
-         $location .= " ($lcount)";
-         }
-        $location.=" ";
-      }
-      if ($fcount > 0){
-        $location .= "Foxton";
-         if ($fcount >1 ){
-         $location .= " ($fcount)";
-         }
-        $location.=" ";
-      }
-      if ($scount > 0){
-        $location .= "Shannon";
-         if ($scount >1 ){
-         $location .= " ($scount)";
-         }
-        $location.=" ";
-      }
-      if ($lostcount > 0){
+        if ( $counts->{'nacount'} > 1 ) {
+            $location .= "=($counts->{'nacount'})";
+        }
+        $location .= " ";
+        $lineres{'on-loan-p'} = 1;
+    }
+    foreach my $key ( keys %$branchcounts ) {
+        if ( $branchcounts->{$key} > 0 ) {
+            $location      .= $key;
+            $location_only .= $key;
+
+            if ( $branchcounts->{$key} > 1 ) {
+                $location      .= "=$branchcounts->{$key}";
+                $location_only .= "=$branchcounts->{$key}";
+            }
+            $location      .= " ";
+            $location_only .= " ";
+        }
+    }
+    if ( $counts->{'lostcount'} > 0 ) {
         $location .= "Lost";
-         if ($lostcount >1 ){
-         $location .= " ($lostcount)";
-         }
-        $location.=" ";
-      }
-      if ($mending > 0){
+        if ( $counts->{'lostcount'} > 1 ) {
+            $location .= "=($counts->{'lostcount'})";
+        }
+        $location .= " ";
+        $lineres{'lost-p'} = 1;
+    }
+    if ( $counts->{'mending'} > 0 ) {
         $location .= "Mending";
-         if ($mending >1 ){
-         $location .= " ($mending)";
-         }
-        $location.=" ";
-      }
-      if ($transit > 0){
+        if ( $counts->{'mending'} > 1 ) {
+            $location .= "=($counts->{'mending'})";
+        }
+        $location .= " ";
+        $lineres{'mending-p'} = 1;
+    }
+    if ( $counts->{'transit'} > 0 ) {
         $location .= "In Transit";
-         if ($transit >1 ){
-         $location .= " ($transit)";
-         }
-        $location.=" ";
-      }
-
-    if ($colour == 1){
-      print mktablerow(6,$secondary,$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
-      $colour=0;
-    } else{
-      print mktablerow(6,'white',$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
-      $colour=1;
+        if ( $counts->{'transit'} > 1 ) {
+            $location .= "=($counts->{'transit'})";
+        }
+        $location .= " ";
+        $lineres{'in-transit-p'} = 1;
+    }
+    if ( $colour eq 0 ) {
+        $toggle = 1;
+        $colour = 1;
+    }
+    else {
+        $colour = 0;
+        $toggle = 0;
+    }
+    $lineres{author2}         = $author2;
+    $lineres{title2}          = $title2;
+    $lineres{copyright}       = $result->{'copyrightdate'};
+    $lineres{booksellerid}    = $booksellerid;
+    $lineres{basketno}        = $basketno;
+    $lineres{sub}             = $sub;
+    $lineres{biblionumber}    = $result->{biblionumber};
+    $lineres{title}           = $result->{title};
+    $lineres{author}          = $result->{author};
+    $lineres{toggle}          = $toggle;
+    $lineres{itemcount}       = $counts->{'count'};
+    $lineres{location}        = $location;
+    $lineres{'location-only'} = $location_only;
+
+    # lets get a list on existing orders for all bibitems.
+    ( my $count1, my @bibitems ) =
+      getbiblioitembybiblionumber( $result->{biblionumber} );
+
+    my $order, my $ordernumber;
+
+    my $i1 = 0;
+
+    my @ordernumbers;
+    foreach my $bibitem (@bibitems) {
+
+        ( $order, $ordernumber ) =
+          &getorder( $bibitem->{biblioitemnumber}, $result->{biblionumber} );
+
+        #only show order if its current;
+        my %order;
+        $order{'number'} = $ordernumber;
+        if (   ( !$order->{cancelledby} )
+            && ( $order->{quantityreceived} < $order->{quantity} ) )
+        {
+            push @ordernumbers, \%order;
+        }
     }
+    $lineres{existingorder} = \@ordernumbers;
+    push( @loopresult, \%lineres );
     $i++;
 }
-$offset=$num+$offset;
 
- print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
+my $prevoffset = $offset - $num;
+my $offsetprev = 1;
+if ( $prevoffset < 0 ) {
+    $offsetprev = 0;
+}
+
+$offset = $num + $offset;
+
+my @numbers = ();
+if ( $count > 10 ) {
+    for ( my $i = 0 ; $i < ( $count / $num ) ; $i++ ) {
+        my $highlight    = 0;
+        my $numberoffset = $i * $num;
+        if ( ( $numberoffset + $num ) == $offset ) { $highlight = 1 }
 
-print mktableft();
-if ($offset < $count){
-    my $search="num=$num&offset=$offset&type=$type&id=$id&basket=$basket&search=$title&author=$author";
-    my $stuff=mklink("/cgi-bin/koha/acqui/newbasket2.pl?$search",'Next');
-    print $stuff;
+     #       warn "I $i | N $num | O $offset | NO $numberoffset | H $highlight";
+        push @numbers,
+          {
+            number       => ( $i + 1 ),
+            highlight    => $highlight,
+            numberoffset => $numberoffset
+          };
+    }
 }
 
-print endcenter();
-print endmenu('acquisitions');
-print endpage();
+$template->param(
+    bookselname            => $booksellers[0]->{'name'},
+    booksellerid           => $booksellerid,
+    basketno               => $basketno,
+    parsub                 => $sub,
+    count                  => $count,
+    offset2                => $offset2,
+    dispnum                => $dispnum,
+    offsetover             => ( $offset < $count ),
+    num                    => $num,
+    offset                 => $prevoffset,
+    offsetprev             => $offsetprev,
+    type                   => $type,
+    title                  => $title,
+    author                 => $author,
+    donation               => $donation,
+    loopsearch             => \@loopsearch,
+    loopresult             => \@loopresult,
+    numbers                => \@numbers,
+    invalidsearch          => $invalidsearch,
+    'use-location-flags-p' => 1
+);
+
+output_html_with_http_headers $input, $cookie, $template->output;
+