Resynching Savannah
[koha.git] / catalogue / moredetail.pl
1 #!/usr/bin/perl
2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
3
4 # $Id$
5
6 # Copyright 2000-2003 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 require Exporter;
25 use C4::Koha;
26 use CGI;
27 use C4::Search;
28 use C4::Acquisition;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31 use C4::Date;
32 use C4::Context;
33 use C4::Biblio;
34 use C4::Accounts2;
35 use C4::Circulation::Circ2;
36
37 my $dbh=C4::Context->dbh;
38 my $query=new CGI;
39
40
41 my ($template, $loggedinuser, $cookie) = get_template_and_user({
42         template_name   => ( 'catalogue/moredetail.tmpl'),
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => {catalogue => 1},
47     });
48
49 # get variables
50 my $op=$query->param('op');
51 my $lost=$query->param('lost');
52 my $withdrawn=$query->param('withdrawn');
53 my $override=$query->param('override');
54 my $itemnumber=$query->param('itemnumber');
55 my $barcode=$query->param('barcode');
56
57 my $title=$query->param('title');
58 my $biblionumber=$query->param('biblionumber');
59 my ($record)=XMLgetbibliohash($dbh,$biblionumber);
60 my $data=XMLmarc2koha_onerecord($dbh,$record,"biblios");
61 my $dewey = $data->{'dewey'};
62 # FIXME Dewey is a string, not a number, & we should use a function
63 $dewey =~ s/0+$//;
64 if ($dewey eq "000.") { $dewey = "";};
65 if ($dewey < 10){$dewey='00'.$dewey;}
66 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
67 if ($dewey <= 0){
68       $dewey='';
69 }
70 $dewey=~ s/\.$//;
71 $data->{'dewey'}=$dewey;
72
73 my @results;
74
75 my @items;
76 if ($op eq "update"){
77 my $env;
78 ##Do Lost or Withdraw here
79 my $flag=0;
80   my ($resbor,$resrec)=C4::Reserves2::CheckReserves($env,$dbh,$itemnumber);
81 if ($override ne "yes"){
82   if ($resbor){
83 #    print $query->header;
84     $template->param(error => "This item   has a reserve on it");
85  $template->param(biblionumber =>$biblionumber);
86  $template->param(itemnumber =>$itemnumber);
87  $template->param(lost =>$lost);
88  $template->param(withdrawn =>$withdrawn);
89     $flag=1;
90   }
91   my $sth=$dbh->prepare("Select * from issues where (itemnumber=?) and (returndate is null)");
92   $sth->execute($itemnumber);
93  
94   if (my $data=$sth->fetchrow_hashref) {
95    $template->param(biblionumber =>$biblionumber);
96  $template->param(itemnumber =>$itemnumber);
97  $template->param(error => "This item   is On Loan to a member");
98  $template->param(lost =>$lost);
99  $template->param(withdrawn =>$withdrawn);
100     $flag=2;
101   }
102 }
103 if ($flag != 0 && $override ne "yes"){
104
105   }else {
106    ##UPDATE here
107
108 XMLmoditemonefield($dbh,$biblionumber,$itemnumber,'wthdrawn',$withdrawn,1);
109 XMLmoditemonefield($dbh,$biblionumber,$itemnumber,'itemlost',$lost);
110
111      if ($lost ==1 && $flag ==2){
112     my $sth=$dbh->prepare("Select * from issues where (itemnumber=?) and (returndate is null)");
113     $sth->execute($itemnumber);
114     my $data=$sth->fetchrow_hashref;
115     if ($data->{'borrowernumber'} ne '') {
116       #item on issue add replacement cost to borrowers record
117       my $accountno=getnextacctno($env,$data->{'borrowernumber'},$dbh);
118       my $item=getiteminformation($env, $itemnumber);
119       my $sth2=$dbh->prepare("Insert into accountlines
120       (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
121       values
122       (?,?,now(),?,?,'L',?,?)");
123       $sth2->execute($data->{'borrowernumber'},$accountno,$item->{'replacementprice'},
124       "Lost Item $item->{'title'} $item->{'barcode'}",
125       $item->{'replacementprice'},$itemnumber);
126       $sth2->finish;
127     }
128     }
129         if ($flag==1){
130         foreach my $res ($resrec){
131         C4::Reserves2::CancelReseve(undef,$res->{itemnumber},$res->{borrowernumber});
132         }
133         }
134     
135   }
136 }
137 my @itemrecords=XMLgetallitems($dbh,$biblionumber);
138 foreach my $itemrecord (@itemrecords){
139 $itemrecord=XML_xml2hash_onerecord($itemrecord);
140 my $items = XMLmarc2koha_onerecord($dbh,$itemrecord,"holdings");
141 $items->{itemtype}=$data->{itemtype};
142 $items->{biblionumber}=$biblionumber;
143 $items=itemissues($dbh,$items,$items->{'itemnumber'});
144 push @items,$items;
145 }
146 my $count=@items;
147 $data->{'count'}=$count;
148 my ($order,$ordernum)=GetOrder($biblionumber,$barcode);
149
150 my $env;
151 $env->{itemcount}=1;
152
153 $results[0]=$data;
154
155 foreach my $item (@items){
156     $item->{'replacementprice'}=sprintf("%.2f", $item->{'replacementprice'});
157     $item->{'datelastborrowed'}= format_date($item->{'datelastborrowed'});
158     $item->{'dateaccessioned'} = format_date($item->{'dateaccessioned'});
159     $item->{'datelastseen'} = format_date($item->{'datelastseen'});
160     $item->{'ordernumber'} = $ordernum;
161     $item->{'booksellerinvoicenumber'} = $order->{'booksellerinvoicenumber'};
162
163     if ($item->{'date_due'} gt '0000-00-00'){
164         $item->{'date_due'} = format_date($item->{'date_due'});         
165 $item->{'issue'}= 1;
166                 $item->{'borrowernumber'} = $item->{'borrower'};
167                 $item->{'cardnumber'} = $item->{'card'};
168                         
169     } else {
170         $item->{'issue'}= 0;
171     }
172 }
173
174 $template->param(BIBITEM_DATA => \@results);
175 $template->param(ITEM_DATA => \@items);
176 $template->param(loggedinuser => $loggedinuser);
177
178 output_html_with_http_headers $query, $cookie, $template->output;
179
180
181 # Local Variables:
182 # tab-width: 8
183 # End: