Fixing Database Definitions for Statuses *PARTIAL*
[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; 
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Biblio;
25 use C4::Output;
26 use C4::Circulation;
27 use C4::Accounts;
28 use C4::Reserves;
29
30 my $cgi= new CGI;
31
32 my $biblionumber=$cgi->param('biblionumber');
33 my $itemnumber=$cgi->param('itemnumber');
34 my $biblioitemnumber=$cgi->param('biblioitemnumber');
35 my $itemlost=$cgi->param('itemlost');
36 my $itemnotes=$cgi->param('itemnotes');
37 my $wthdrawn=$cgi->param('wthdrawn');
38 my $damaged=$cgi->param('damaged');
39
40 my $confirm=$cgi->param('confirm');
41 my $dbh = C4::Context->dbh;
42
43 # get the rest of this item's information
44 my $item_data_hashref = GetItem($itemnumber, undef);
45
46 # make sure item statuses are set to 0 if empty or NULL
47 for ($damaged,$itemlost,$wthdrawn) {
48     if (!$_ or $_ eq "") {
49         $_ = 0;
50     }
51 }
52
53 # modify MARC item if input differs from items table.
54 if ( $itemnotes ne $item_data_hashref->{'itemnotes'}) {
55     ModItemInMarconefield($biblionumber, $itemnumber, 'items.itemnotes', $itemnotes);
56     $item_data_hashref->{'itemnotes'} = $itemnotes;
57 } elsif ($itemlost ne $item_data_hashref->{'itemlost'}) {
58     ModItemInMarconefield($biblionumber, $itemnumber, 'items.itemlost', $itemlost);
59     $item_data_hashref->{'itemlost'} = $itemlost;
60 } elsif ($wthdrawn ne $item_data_hashref->{'wthdrawn'}) {
61     ModItemInMarconefield($biblionumber, $itemnumber, 'items.wthdrawn', $wthdrawn);
62     $item_data_hashref->{'wthdrawn'} = $wthdrawn;
63 } elsif ($damaged ne $item_data_hashref->{'damaged'}) {
64     ModItemInMarconefield($biblionumber, $itemnumber, 'items.damaged', $damaged);
65     $item_data_hashref->{'damaged'} = $damaged;
66 } else {
67     #nothings changed, so do nothing.
68     print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");
69 }
70
71 # FIXME: eventually we'll use Biblio.pm, but it's currently too buggy  (is this current ??)
72 # yes as of dec 30 2007, ModItem doesn't update zebra for status changes, it requires
73 # a MARC record be passed in
74 #ModItem( $dbh,'',$biblionumber,$itemnumber,'',$item_hashref );
75 #   &C4::Biblio::_koha_modify_item($dbh,$item_data_hashref);
76     my $sth = $dbh->prepare("UPDATE items SET wthdrawn=?,itemlost=?,damaged=?,itemnotes=? WHERE itemnumber=?");
77     $sth->execute($wthdrawn,$itemlost,$damaged,$itemnotes,$itemnumber);
78     &ModZebra($biblionumber,"specialUpdate","biblioserver");
79     
80 # check issues iff itemlost.
81 # http://wiki.koha.org/doku.php?id=en:development:kohastatuses
82 # lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for
83 # FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that
84 # a charge has been added
85 # FIXME : if no replacement price, borrower just doesn't get charged?
86 if ($itemlost==1) {
87     my $sth=$dbh->prepare("SELECT * FROM issues WHERE (itemnumber=? AND returndate IS NULL)");
88     $sth->execute($itemnumber);
89     my $issues=$sth->fetchrow_hashref();
90
91     # if a borrower lost the item, add a replacement cost to the their record
92     if ( ($issues->{borrowernumber}) && ($itemlost==1) ){
93
94         # first make sure the borrower hasn't already been charged for this item
95         my $sth1=$dbh->prepare("SELECT * from accountlines
96         WHERE borrowernumber=? AND itemnumber=?");
97         $sth1->execute($issues->{'borrowernumber'},$itemnumber);
98         my $existing_charge_hashref=$sth1->fetchrow_hashref();
99
100         # OK, they haven't
101         unless ($existing_charge_hashref) {
102             # This item is on issue ... add replacement cost to the borrower's record and mark it returned
103             my $accountno = getnextacctno('',$issues->{'borrowernumber'},$dbh);
104             my $sth2=$dbh->prepare("INSERT INTO accountlines
105             (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
106             VALUES
107             (?,?,now(),?,?,'L',?,?)");
108             $sth2->execute($issues->{'borrowernumber'},$accountno,$item_data_hashref->{'replacementprice'},
109             "Lost Item $item_data_hashref->{'title'} $item_data_hashref->{'barcode'}",
110             $item_data_hashref->{'replacementprice'},$itemnumber);
111             $sth2->finish;
112         # FIXME: Log this ?
113         }
114     }
115     $sth->finish;
116 }
117
118 print $cgi->redirect("moredetail.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");