Managing IndependantBranches when creating a new Biblio
[koha.git] / cataloguing / updateitem.pl
1 #!/usr/bin/perl
2
3 # $Id: updateitem.pl,v 1.9.2.1.2.4 2006/10/05 18:36:50 kados Exp $
4 # Copyright 2006 LibLime
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20 use strict; use warnings;
21 use C4::Context;
22 use C4::Biblio;
23 use C4::Output;
24 use C4::Circulation;
25 use C4::Accounts;
26 use C4::Reserves;
27
28 my $cgi= new CGI;
29
30 my $biblionumber=$cgi->param('biblionumber');
31 my $itemnumber=$cgi->param('itemnumber');
32 my $biblioitemnumber=$cgi->param('biblioitemnumber');
33 my $itemlost=$cgi->param('itemlost');
34 my $wthdrawn=$cgi->param('wthdrawn');
35 my $binding=$cgi->param('binding');
36
37 my $confirm=$cgi->param('confirm');
38 my $dbh = C4::Context->dbh;
39 # get the rest of this item's information
40 my $item_data_sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
41 $item_data_sth->execute($itemnumber);
42 my $item_data_hashref = $item_data_sth->fetchrow_hashref();
43
44 # superimpose the new on the old
45 $item_data_hashref->{'itemlost'} = $itemlost if $itemlost;
46 $item_data_hashref->{'wthdrawn'} = $wthdrawn if $wthdrawn;
47 $item_data_hashref->{'binding'} = $binding if $binding;
48
49 # check reservations
50 my ($status, $reserve) = CheckReserves($itemnumber, $item_data_hashref->{'barcode'});
51 if ($reserve){
52         #print $cgi->header;
53         warn "Reservation found on item $itemnumber";
54         #exit;
55 }
56 # check issues
57 my $sth=$dbh->prepare("SELECT * FROM issues WHERE (itemnumber=? AND returndate IS NULL)");
58 $sth->execute($itemnumber);
59 my $issues=$sth->fetchrow_hashref();
60
61 # if a borrower lost the item, add a replacement cost to the their record
62 if ( ($issues->{borrowernumber}) && ($itemlost==1) ){
63
64                 # first make sure the borrower hasn't already been charged for this item
65                 my $sth1=$dbh->prepare("SELECT * from accountlines
66                 WHERE borrowernumber=? AND itemnumber=?");
67                 $sth1->execute($issues->{'borrowernumber'},$itemnumber);
68                 my $existing_charge_hashref=$sth1->fetchrow_hashref();
69
70                 # OK, they haven't
71                 unless ($existing_charge_hashref) {
72                         # This item is on issue ... add replacement cost to the borrower's record and mark it returned
73                         my $accountno = getnextacctno('',$issues->{'borrowernumber'},$dbh);
74                         my $sth2=$dbh->prepare("INSERT INTO accountlines
75                         (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
76                         VALUES
77                         (?,?,now(),?,?,'L',?,?)");
78                         $sth2->execute($issues->{'borrowernumber'},$accountno,$item_data_hashref->{'replacementprice'},
79                         "Lost Item $item_data_hashref->{'title'} $item_data_hashref->{'barcode'}",
80                         $item_data_hashref->{'replacementprice'},$itemnumber);
81                         $sth2->finish;
82                 }
83 }
84 $sth->finish;
85
86 # FIXME: eventually we'll use Biblio.pm, but it's currently too buggy
87 #ModItem( $dbh,'',$biblionumber,$itemnumber,'',$item_hashref );
88 $sth = $dbh->prepare("UPDATE items SET wthdrawn=?,itemlost=?,binding=? WHERE itemnumber=?");
89 $sth->execute($wthdrawn,$itemlost,$binding,$itemnumber);
90
91 print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");