Bug 14695 - Tidy C4::Reserves::CanItemBeReserved
[koha.git] / catalogue / labeledMARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008-2009 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 ); 
23 use HTML::Entities;
24 use MARC::Record;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Members; # to use GetMember
31 use C4::Search;         # enabled_staff_search_views
32 use C4::Acquisition qw(GetOrdersByBiblionumber);
33 use C4::Koha qw( GetFrameworksLoop );
34
35 my $query        = new CGI;
36 my $dbh          = C4::Context->dbh;
37 my $biblionumber = $query->param('biblionumber');
38 $biblionumber = HTML::Entities::encode($biblionumber);
39 my $frameworkcode = $query->param('frameworkcode');
40 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
41 my $popup        =
42   $query->param('popup')
43   ;    # if set to 1, then don't insert links, it's just to show the biblio
44
45 # open template
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {
48         template_name   => "catalogue/labeledMARCdetail.tt",
49         query           => $query,
50         type            => "intranet",
51         authnotrequired => 0,
52         flagsrequired   => { catalogue => 1 },
53         debug           => 1,
54     }
55 );
56
57 my $record = GetMarcBiblio($biblionumber);
58 if ( not defined $record ) {
59     # biblionumber invalid -> report and exit
60     $template->param( unknownbiblionumber => 1,
61                 biblionumber => $biblionumber
62     );
63     output_html_with_http_headers $query, $cookie, $template->output;
64     exit;
65 }
66
67 my $tagslib = GetMarcStructure(1,$frameworkcode);
68 my $biblio = GetBiblioData($biblionumber);
69
70 if($query->cookie("holdfor")){ 
71     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
72     $template->param(
73         holdfor => $query->cookie("holdfor"),
74         holdfor_surname => $holdfor_patron->{'surname'},
75         holdfor_firstname => $holdfor_patron->{'firstname'},
76         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
77     );
78 }
79
80 #count of item linked
81 my $itemcount = GetItemsCount($biblionumber);
82 $template->param( count => $itemcount,
83                                         bibliotitle => $biblio->{title}, );
84
85 #Getting framework loop
86 $template->param(frameworkloop => GetFrameworksLoop( $frameworkcode ) );
87
88 my @marc_data;
89 my $prevlabel = '';
90 for my $field ($record->fields)
91 {
92         my $tag = $field->tag;
93         next if ! exists $tagslib->{$tag}->{lib};
94         my $label = $tagslib->{$tag}->{lib};
95         if ($label eq $prevlabel)
96         {
97                 $label = '';
98         }
99         else
100         {
101                 $prevlabel = $label;
102         }
103         my $value = $tag < 10
104                 ? $field->data
105                 : join ' ', map { $_->[1] } $field->subfields;
106         push @marc_data, {
107                 label => $label,
108                 value => $value,
109         };
110 }
111
112 $template->param (
113         marc_data                               => \@marc_data,
114     biblionumber            => $biblionumber,
115     popup                   => $popup,
116         labeledmarcview => 1,
117         z3950_search_params             => C4::Search::z3950_search_args($biblio),
118         C4::Search::enabled_staff_search_views,
119     searchid            => scalar $query->param('searchid'),
120 );
121
122 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
123 my @deletedorders_using_biblio;
124 my @orders_using_biblio;
125 my @baskets_orders;
126 my @baskets_deletedorders;
127
128 foreach my $myorder (@allorders_using_biblio) {
129     my $basket = $myorder->{'basketno'};
130     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
131         push @deletedorders_using_biblio, $myorder;
132         unless (grep(/^$basket$/, @baskets_deletedorders)){
133             push @baskets_deletedorders,$myorder->{'basketno'};
134         }
135     }
136     else {
137         push @orders_using_biblio, $myorder;
138         unless (grep(/^$basket$/, @baskets_orders)){
139             push @baskets_orders,$myorder->{'basketno'};
140             }
141     }
142 }
143
144 my $count_orders_using_biblio = scalar @orders_using_biblio ;
145 $template->param (countorders => $count_orders_using_biblio);
146
147 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
148 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
149
150 my $holds= C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
151 my $holdcount = scalar( @$holds );
152 $template->param( holdcount => scalar ( @$holds ) );
153
154 output_html_with_http_headers $query, $cookie, $template->output;