batch import rework -- implement stage-commit-undo for batch import
[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_sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
42 $item_data_sth->execute($itemnumber);
43 my $item_data_hashref = $item_data_sth->fetchrow_hashref();
44
45 # superimpose the new on the old
46 $item_data_hashref->{'itemlost'} = $itemlost if $itemlost;
47 $item_data_hashref->{'wthdrawn'} = $wthdrawn if $wthdrawn;
48 $item_data_hashref->{'damaged'} = $damaged if $damaged;
49
50 # check reservations
51 my ($status, $reserve) = CheckReserves($itemnumber, $item_data_hashref->{'barcode'});
52 if ($reserve){
53         #print $cgi->header;
54         warn "Reservation found on item $itemnumber";
55         #exit;
56 }
57 # check issues
58 my $sth=$dbh->prepare("SELECT * FROM issues WHERE (itemnumber=? AND returndate IS NULL)");
59 $sth->execute($itemnumber);
60 my $issues=$sth->fetchrow_hashref();
61
62 # if a borrower lost the item, add a replacement cost to the their record
63 if ( ($issues->{borrowernumber}) && ($itemlost==1) ){
64
65                 # first make sure the borrower hasn't already been charged for this item
66                 my $sth1=$dbh->prepare("SELECT * from accountlines
67                 WHERE borrowernumber=? AND itemnumber=?");
68                 $sth1->execute($issues->{'borrowernumber'},$itemnumber);
69                 my $existing_charge_hashref=$sth1->fetchrow_hashref();
70
71                 # OK, they haven't
72                 unless ($existing_charge_hashref) {
73                         # This item is on issue ... add replacement cost to the borrower's record and mark it returned
74                         my $accountno = getnextacctno('',$issues->{'borrowernumber'},$dbh);
75                         my $sth2=$dbh->prepare("INSERT INTO accountlines
76                         (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
77                         VALUES
78                         (?,?,now(),?,?,'L',?,?)");
79                         $sth2->execute($issues->{'borrowernumber'},$accountno,$item_data_hashref->{'replacementprice'},
80                         "Lost Item $item_data_hashref->{'title'} $item_data_hashref->{'barcode'}",
81                         $item_data_hashref->{'replacementprice'},$itemnumber);
82                         $sth2->finish;
83                 }
84 }
85 $sth->finish;
86
87 # FIXME: eventually we'll use Biblio.pm, but it's currently too buggy
88 #ModItem( $dbh,'',$biblionumber,$itemnumber,'',$item_hashref );
89 $sth = $dbh->prepare("UPDATE items SET wthdrawn=?,itemlost=?,damaged=? WHERE itemnumber=?");
90 $sth->execute($wthdrawn,$itemlost,$damaged,$itemnumber);
91
92 print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");