Bug 23846: Add a check to the data inconsistencies script
[koha.git] / catalogue / moredetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2003 Katipo Communications
4 # parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 use Modern::Perl;
23 use C4::Koha;
24 use CGI qw ( -utf8 );
25 use HTML::Entities;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Acquisition;
29 use C4::Output;
30 use C4::Auth;
31 use C4::Serials;
32 use C4::Search;         # enabled_staff_search_views
33
34 use Koha::Acquisition::Booksellers;
35 use Koha::AuthorisedValues;
36 use Koha::Biblios;
37 use Koha::DateUtils;
38 use Koha::Items;
39 use Koha::Patrons;
40
41 my $query=new CGI;
42
43 my ($template, $loggedinuser, $cookie) = get_template_and_user(
44     {
45         template_name   => 'catalogue/moredetail.tt',
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1 },
50     }
51 );
52
53 if($query->cookie("holdfor")){ 
54     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
55     $template->param(
56         holdfor => $query->cookie("holdfor"),
57         holdfor_surname => $holdfor_patron->surname,
58         holdfor_firstname => $holdfor_patron->firstname,
59         holdfor_cardnumber => $holdfor_patron->cardnumber,
60     );
61 }
62
63 # get variables
64
65 my $biblionumber=$query->param('biblionumber');
66 $biblionumber = HTML::Entities::encode($biblionumber);
67 my $title=$query->param('title');
68 my $bi=$query->param('bi');
69 $bi = $biblionumber unless $bi;
70 my $itemnumber = $query->param('itemnumber');
71 my $data = &GetBiblioData($biblionumber);
72 my $dewey = $data->{'dewey'};
73 my $showallitems = $query->param('showallitems');
74
75 #coping with subscriptions
76 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
77
78 # FIXME Dewey is a string, not a number, & we should use a function
79 # $dewey =~ s/0+$//;
80 # if ($dewey eq "000.") { $dewey = "";};
81 # if ($dewey < 10){$dewey='00'.$dewey;}
82 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
83 # if ($dewey <= 0){
84 #      $dewey='';
85 # }
86 # $dewey=~ s/\.$//;
87 # $data->{'dewey'}=$dewey;
88
89 my $fw = GetFrameworkCode($biblionumber);
90 my @all_items= GetItemsInfo($biblionumber);
91 my @items;
92 my $patron = Koha::Patrons->find( $loggedinuser );
93 for my $itm (@all_items) {
94     push @items, $itm unless ( $itm->{itemlost} && 
95                                $patron->category->hidelostitems &&
96                                !$showallitems && 
97                                ($itemnumber != $itm->{itemnumber}));
98 }
99
100 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
101
102 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
103     unless $record;
104
105 my $hostrecords;
106 # adding items linked via host biblios
107 my @hostitems = GetHostItemsInfo($record);
108 if (@hostitems){
109         $hostrecords =1;
110         push (@items,@hostitems);
111 }
112
113 my $biblio = Koha::Biblios->find( $biblionumber );
114
115 my $totalcount=@all_items;
116 my $showncount=@items;
117 my $hiddencount = $totalcount - $showncount;
118 $data->{'count'}=$totalcount;
119 $data->{'showncount'}=$showncount;
120 $data->{'hiddencount'}=$hiddencount;  # can be zero
121
122 my $ccodes =
123   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
124 my $copynumbers =
125   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
126
127 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
128
129 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
130   if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
131 foreach ( keys %{$data} ) {
132     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
133 }
134
135 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
136 foreach my $item (@items){
137     $item->{object} = Koha::Items->find( $item->{itemnumber} );
138     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
139     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
140     $item->{'replacementprice'}        = $item->{'replacementprice'};
141     if ( defined $item->{'copynumber'} ) {
142         $item->{'displaycopy'} = 1;
143         if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
144             $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
145         }
146         else {
147             $item->{'copyvol'} = $item->{'copynumber'};
148         }
149     }
150
151     # item has a host number if its biblio number does not match the current bib
152     if ($item->{biblionumber} ne $biblionumber){
153         $item->{hostbiblionumber} = $item->{biblionumber};
154         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
155     }
156
157     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
158     $item->{'ordernumber'}             = $order->{'ordernumber'};
159     $item->{'basketno'}                = $order->{'basketno'};
160     $item->{'orderdate'}               = $order->{'entrydate'};
161     if ($item->{'basketno'}){
162             my $basket = GetBasket($item->{'basketno'});
163         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
164         $item->{'vendor'} = $bookseller->name;
165     }
166     $item->{'invoiceid'}               = $order->{'invoiceid'};
167     if($item->{invoiceid}) {
168         my $invoice = GetInvoice($item->{invoiceid});
169         $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
170     }
171     $item->{'datereceived'}            = $order->{'datereceived'};
172
173     if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
174         $item->{status_advisory} = 1;
175     }
176
177     # Add paidfor info
178     if ( $item->{itemlost} ) {
179         my $accountlines = Koha::Account::Lines->search(
180             {
181                 itemnumber        => $item->{itemnumber},
182                 debit_type_code   => 'LOST',
183                 status            => [ undef, { '<>' => 'RETURNED' } ],
184                 amountoutstanding => 0
185             },
186             {
187                 order_by => { '-desc' => 'date' },
188                 rows     => 1
189             }
190         );
191
192         if ( my $accountline = $accountlines->next ) {
193             my $payment_offsets = Koha::Account::Offsets->search(
194                 {
195                     debit_id  => $accountline->id,
196                     credit_id => { '!=' => undef }, # it is not the debit itself
197                     type => { '!=' => [ 'Writeoff', 'Forgiven' ] },
198                     amount => { '<' => 0 }    # credits are negative on the DB
199                 },
200                 { order_by => { '-desc' => 'created_on' } }
201             );
202
203             if ($payment_offsets->count) {
204                 my $patron = $accountline->patron;
205                 my $payment_offset = $payment_offsets->next;
206                 $item->{paidfor} =
207                     $patron->firstname . " "
208                   . $patron->surname . " "
209                   . $patron->cardnumber . " "
210                   . $payment_offset->created_on;
211             }
212         }
213     }
214
215     if (C4::Context->preference("IndependentBranches")) {
216         #verifying rights
217         my $userenv = C4::Context->userenv();
218         unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
219                 $item->{'nomod'}=1;
220         }
221     }
222     if ($item->{'datedue'}) {
223         $item->{'issue'}= 1;
224     } else {
225         $item->{'issue'}= 0;
226     }
227
228     if ( $item->{'borrowernumber'} ) {
229         my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
230         $item->{patron} = $curr_borrower;
231     }
232 }
233
234 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
235 if ( $mss->count ) {
236     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
237 }
238 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
239 if ( $mss->count ) {
240     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
241 }
242 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
243 if ( $mss->count ) {
244     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
245 }
246
247 $template->param(count => $data->{'count'},
248         subscriptionsnumber => $subscriptionsnumber,
249     subscriptiontitle   => $data->{title},
250         C4::Search::enabled_staff_search_views,
251 );
252
253 $template->param(
254     ITEM_DATA           => \@items,
255     moredetailview      => 1,
256     loggedinuser        => $loggedinuser,
257     biblionumber        => $biblionumber,
258     biblioitemnumber    => $bi,
259     itemnumber          => $itemnumber,
260     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
261     biblio              => $biblio->unblessed,
262 );
263 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
264 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
265
266 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
267 my @deletedorders_using_biblio;
268 my @orders_using_biblio;
269 my @baskets_orders;
270 my @baskets_deletedorders;
271
272 foreach my $myorder (@allorders_using_biblio) {
273     my $basket = $myorder->{'basketno'};
274     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
275         push @deletedorders_using_biblio, $myorder;
276         unless (grep(/^$basket$/, @baskets_deletedorders)){
277             push @baskets_deletedorders,$myorder->{'basketno'};
278         }
279     }
280     else {
281         push @orders_using_biblio, $myorder;
282         unless (grep(/^$basket$/, @baskets_orders)){
283             push @baskets_orders,$myorder->{'basketno'};
284             }
285     }
286 }
287
288 my $count_orders_using_biblio = scalar @orders_using_biblio ;
289 $template->param (countorders => $count_orders_using_biblio);
290
291 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
292 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
293
294 my $holds = $biblio->holds;
295 $template->param( holdcount => $holds->count );
296
297 output_html_with_http_headers $query, $cookie, $template->output;
298