3 # 2008 Kyle Hall <kyle.m.hall@gmail.com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
35 use Date::Calc qw( Add_Delta_Days Date_to_Days );
37 use constant DEBUG => 0;
41 my ($template, $loggedinuser, $cookie)
42 = get_template_and_user( { template_name => "offline_circ/process_koc.tmpl",
50 our $dbh = C4::Context->dbh();
52 our $branchcode = C4::Context->userenv->{branch};
54 warn "Branchcode: $branchcode";
56 our @output; ## For storing messages to be displayed to the user
58 $query::POST_MAX = 1024 * 10000;
60 my $file = $query->param("kocfile");
61 $file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
66 while ( my $line = <$file> ) {
67 my ( $type, $cardnumber, $barcode, $datetime ) = split( /\t/, $line );
68 ( $datetime ) = split( /\+/, $datetime );
69 my ( $date ) = split( / /, $datetime );
72 $circ->{ 'type' } = $type;
73 $circ->{ 'cardnumber' } = $cardnumber;
74 $circ->{ 'barcode' } = $barcode;
75 $circ->{ 'datetime' } = $datetime;
76 $circ->{ 'date' } = $date;
78 if ( $circ->{ 'type' } eq 'issue' ) {
79 kocIssueItem( $circ, $branchcode );
80 } elsif ( $circ->{ 'type' } eq 'return' ) {
81 kocReturnItem( $circ );
82 } elsif ( $circ->{ 'type' } eq 'payment' ) {
83 $circ->{'payment_amount'} = delete $circ->{'barcode'};
84 kocMakePayment( $circ );
89 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
90 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
91 IntranetNav => C4::Context->preference("IntranetNav"),
95 output_html_with_http_headers $query, $cookie, $template->output;
98 my ( $circ, $branchcode ) = @_;
100 my $borrower = GetMember( $circ->{ 'cardnumber' }, 'cardnumber' );
101 my $item = GetBiblioFromItemNumber( undef, $circ->{ 'barcode' } );
102 my $issue = GetItemIssue( $item->{'itemnumber'} );
104 my $issuingrule = GetIssuingRule( $borrower->{ 'categorycode' }, $item->{ 'itemtype' }, $branchcode );
105 my $issuelength = $issuingrule->{ 'issuelength' };
106 my ( $year, $month, $day ) = split( /-/, $circ->{'date'} );
107 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, $issuelength );
108 my $date_due = sprintf("%04d-%02d-%02d", $year, $month, $day);
110 if ( $issue->{ 'date_due' } ) { ## Item is currently checked out to another person.
111 warn "Item Currently Issued.";
112 my $issue = GetOpenIssue( $item->{'itemnumber'} );
114 if ( $issue->{'borrowernumber'} eq $borrower->{'borrowernumber'} ) { ## Issued to this person already, renew it.
115 warn "Item issued to this member already, renewing.";
117 my $date_due_object = C4::Dates->new($date_due ,'iso');
118 C4::Circulation::AddRenewal(
119 $issue->{'borrowernumber'}, # borrowernumber
120 $item->{'itemnumber'}, # itemnumber
122 $date_due_object, # datedue
123 $circ->{'date'}, # issuedate
126 push( @output, { message => "Renewed $item->{ 'title' } ( $item->{ 'barcode' } ) to $borrower->{ 'firstname' } $borrower->{ 'surename' } ( $borrower->{'cardnumber'} ) : $circ->{ 'datetime' }\n" } );
129 warn "Item issued to a different member.";
130 warn "Date of previous issue: $issue->{'issuedate'}";
131 warn "Date of this issue: $circ->{'date'}";
132 my ( $i_y, $i_m, $i_d ) = split( /-/, $issue->{'issuedate'} );
133 my ( $c_y, $c_m, $c_d ) = split( /-/, $circ->{'date'} );
135 if ( Date_to_Days( $i_y, $i_m, $i_d ) < Date_to_Days( $c_y, $c_m, $c_d ) ) { ## Current issue to a different persion is older than this issue, return and issue.
136 C4::Circulation::AddIssue( $borrower, $circ->{'barcode'}, $date_due ) unless ( DEBUG );
137 push( @output, { message => "Issued $item->{ 'title' } ( $item->{ 'barcode' } ) to $borrower->{ 'firstname' } $borrower->{ 'surename' } ( $borrower->{'cardnumber'} ) : $circ->{ 'datetime' }\n" } );
139 } else { ## Current issue is *newer* than this issue, write a 'returned' issue, as the item is most likely in the hands of someone else now.
140 warn "Current issue to another member is newer. Doing nothing";
141 ## This situation should only happen of the Offline Circ data is *really* old.
142 ## FIXME: write line to old_issues and statistics
146 } else { ## Item is not checked out to anyone at the moment, go ahead and issue it
147 C4::Circulation::AddIssue( $borrower, $circ->{'barcode'}, $date_due ) unless ( DEBUG );
148 push( @output, { message => "Issued $item->{ 'title' } ( $item->{ 'barcode' } ) to $borrower->{ 'firstname' } $borrower->{ 'surename' } ( $borrower->{'cardnumber'} ) : $circ->{ 'datetime' }\n" } );
154 my $borrower = GetMember( $circ->{ 'cardnumber' }, 'cardnumber' );
155 my $item = GetBiblioFromItemNumber( undef, $circ->{ 'barcode' } );
156 C4::Circulation::MarkIssueReturned( $borrower->{'borrowernumber'},
157 $item->{'itemnumber'},
161 push( @output, { message => "Returned $item->{ 'title' } ( $item->{ 'barcode' } ) From borrower number $borrower->{'borrowernumber'} : $circ->{ 'datetime' }\n" } );
166 my $borrower = GetMember( $circ->{ 'cardnumber' }, 'cardnumber' );
167 recordpayment( $borrower->{'borrowernumber'}, $circ->{'payment_amount'} );
168 push( @output, { message => "accepted payment ($circ->{'payment_amount'}) from cardnumber ($circ->{'cardnumber'}), borrower ($borrower->{'borrowernumber'})" } );