bug 1616: handle NULL in damaged, wthdrawn, or itemlost columns
[koha.git] / catalogue / 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 CGI;
22 use C4::Context;
23 use C4::Biblio;
24 use C4::Output;
25 use C4::Circulation;
26 use C4::Accounts;
27 use C4::Reserves;
28
29 my $cgi= new CGI;
30
31 my $biblionumber=$cgi->param('biblionumber');
32 my $itemnumber=$cgi->param('itemnumber');
33 my $biblioitemnumber=$cgi->param('biblioitemnumber');
34 my $itemlost=$cgi->param('itemlost');
35 my $wthdrawn=$cgi->param('wthdrawn');
36 my $damaged=$cgi->param('damaged');
37
38 my $confirm=$cgi->param('confirm');
39 my $dbh = C4::Context->dbh;
40 # get the rest of this item's information
41 my $item_data_hashref = GetItem($itemnumber, undef);
42
43 # modify bib MARC
44 if ((not defined($item_data_hashref->{'itemlost'})) or ($itemlost ne $item_data_hashref->{'itemlost'})) {
45     ModItemInMarconefield($biblionumber, $itemnumber, 'items.itemlost', $itemlost);
46 }
47 if ((not defined($item_data_hashref->{'wthdrawn'})) or ($wthdrawn ne $item_data_hashref->{'wthdrawn'})) {
48     ModItemInMarconefield($biblionumber, $itemnumber, 'items.wthdrawn', $wthdrawn);
49 }
50 if ((not defined($item_data_hashref->{'damaged'})) or ($damaged ne $item_data_hashref->{'damaged'})) {
51     ModItemInMarconefield($biblionumber, $itemnumber, 'items.damaged', $damaged);
52 }
53
54 # check reservations
55 my ($status, $reserve) = CheckReserves($itemnumber, $item_data_hashref->{'barcode'});
56 if ($reserve){
57         #print $cgi->header;
58         warn "Reservation found on item $itemnumber";
59         #exit;
60 }
61 # check issues
62 my $sth=$dbh->prepare("SELECT * FROM issues WHERE (itemnumber=? AND returndate IS NULL)");
63 $sth->execute($itemnumber);
64 my $issues=$sth->fetchrow_hashref();
65
66 # if a borrower lost the item, add a replacement cost to the their record
67 if ( ($issues->{borrowernumber}) && ($itemlost==1) ){
68
69                 # first make sure the borrower hasn't already been charged for this item
70                 my $sth1=$dbh->prepare("SELECT * from accountlines
71                 WHERE borrowernumber=? AND itemnumber=?");
72                 $sth1->execute($issues->{'borrowernumber'},$itemnumber);
73                 my $existing_charge_hashref=$sth1->fetchrow_hashref();
74
75                 # OK, they haven't
76                 unless ($existing_charge_hashref) {
77                         # This item is on issue ... add replacement cost to the borrower's record and mark it returned
78                         my $accountno = getnextacctno('',$issues->{'borrowernumber'},$dbh);
79                         my $sth2=$dbh->prepare("INSERT INTO accountlines
80                         (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
81                         VALUES
82                         (?,?,now(),?,?,'L',?,?)");
83                         $sth2->execute($issues->{'borrowernumber'},$accountno,$item_data_hashref->{'replacementprice'},
84                         "Lost Item $item_data_hashref->{'title'} $item_data_hashref->{'barcode'}",
85                         $item_data_hashref->{'replacementprice'},$itemnumber);
86                         $sth2->finish;
87                 }
88 }
89 $sth->finish;
90
91 # FIXME: eventually we'll use Biblio.pm, but it's currently too buggy
92 #ModItem( $dbh,'',$biblionumber,$itemnumber,'',$item_hashref );
93 $sth = $dbh->prepare("UPDATE items SET wthdrawn=?,itemlost=?,damaged=? WHERE itemnumber=?");
94 $sth->execute($wthdrawn,$itemlost,$damaged,$itemnumber);
95
96 print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");