Clean up moredetail.pl and updateitem.pl
[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 $itemnotes=$cgi->param('itemnotes');
36 my $wthdrawn=$cgi->param('wthdrawn');
37 my $damaged=$cgi->param('damaged');
38
39 my $confirm=$cgi->param('confirm');
40 my $dbh = C4::Context->dbh;
41 # get the rest of this item's information
42 my $item_data_hashref = GetItem($itemnumber, undef);
43 my $newitemdata;
44 # modify MARC item if input differs from items table.
45 if ( $itemnotes ne $item_data_hashref->{'itemnotes'}) {
46     ModItemInMarconefield($biblionumber, $itemnumber, 'items.itemnotes', $itemnotes);
47         $newitemdata->{'itemnotes'} = $itemnotes;
48 } elsif ($itemlost ne $item_data_hashref->{'itemlost'}) {
49     ModItemInMarconefield($biblionumber, $itemnumber, 'items.itemlost', $itemlost);
50         $newitemdata->{'itemlost'} = $itemlost;
51 } elsif ($wthdrawn ne $item_data_hashref->{'wthdrawn'}) {
52     ModItemInMarconefield($biblionumber, $itemnumber, 'items.wthdrawn', $wthdrawn);
53         $newitemdata->{'wthdrawn'} = $wthdrawn;
54 } elsif ($damaged ne $item_data_hashref->{'damaged'}) {
55     ModItemInMarconefield($biblionumber, $itemnumber, 'items.damaged', $damaged);
56         $newitemdata->{'damaged'} = $damaged;
57 } else {
58         #nothings changed, so do nothing.
59         print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");
60 }
61
62 # FIXME: eventually we'll use Biblio.pm, but it's currently too buggy  (is this current ??)
63 #ModItem( $dbh,'',$biblionumber,$itemnumber,'',$item_hashref );
64         $newitemdata->{'itemnumber'} = $itemnumber;
65         &C4::Biblio::_koha_modify_item($dbh,$newitemdata);
66 #$sth = $dbh->prepare("UPDATE items SET wthdrawn=?,itemlost=?,damaged=? WHERE itemnumber=?");
67 #$sth->execute($wthdrawn,$itemlost,$damaged,$itemnumber);
68
69 # check reservations (why ?)
70 #my ($status, $reserve) = CheckReserves($itemnumber, $item_data_hashref->{'barcode'});
71 #if ($reserve){
72 ##      #print $cgi->header;
73 #       warn "Reservation found on item $itemnumber";
74         #exit;
75 #}
76 # check issues iff itemlost.
77  # FIXME : is there documentation or enforcement that itemlost value must be '1'?  if no replacement price, then borrower just doesn't get charged?
78 if ($itemlost==1) {
79         my $sth=$dbh->prepare("SELECT * FROM issues WHERE (itemnumber=? AND returndate IS NULL)");
80         $sth->execute($itemnumber);
81         my $issues=$sth->fetchrow_hashref();
82
83         # if a borrower lost the item, add a replacement cost to the their record
84         if ( ($issues->{borrowernumber}) && ($itemlost==1) ){
85
86                 # first make sure the borrower hasn't already been charged for this item
87                 my $sth1=$dbh->prepare("SELECT * from accountlines
88                 WHERE borrowernumber=? AND itemnumber=?");
89                 $sth1->execute($issues->{'borrowernumber'},$itemnumber);
90                 my $existing_charge_hashref=$sth1->fetchrow_hashref();
91
92                 # OK, they haven't
93                 unless ($existing_charge_hashref) {
94                         # This item is on issue ... add replacement cost to the borrower's record and mark it returned
95                         my $accountno = getnextacctno('',$issues->{'borrowernumber'},$dbh);
96                         my $sth2=$dbh->prepare("INSERT INTO accountlines
97                         (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
98                         VALUES
99                         (?,?,now(),?,?,'L',?,?)");
100                         $sth2->execute($issues->{'borrowernumber'},$accountno,$item_data_hashref->{'replacementprice'},
101                         "Lost Item $item_data_hashref->{'title'} $item_data_hashref->{'barcode'}",
102                         $item_data_hashref->{'replacementprice'},$itemnumber);
103                         $sth2->finish;
104                 # FIXME: Log this ?
105                 }
106         }
107         $sth->finish;
108 }
109
110 print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");