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