Bug 17829: Move GetMember to Koha::Patron
[koha.git] / offline_circ / service.pl
1 #!/usr/bin/perl
2
3 # 2009 BibLibre <jeanandre.santoni@biblibre.com>
4
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Circulation;
27 use Koha::DateUtils;
28 use DateTime::TimeZone;
29
30 my $cgi = CGI->new;
31
32 # get the status of the user, this will check his credentials and rights
33 my ($status, $cookie, $sessionId) = C4::Auth::check_api_auth($cgi, undef);
34 ($status, $sessionId) = C4::Auth::check_cookie_auth($cgi, undef) if ($status ne 'ok');
35
36 my $result;
37
38 if ($status eq 'ok') { # if authentication is ok
39
40     my $userid     = $cgi->param('userid')     || '';
41     my $branchcode = $cgi->param('branchcode') || '';
42     my $timestamp  = $cgi->param('timestamp')  || '';
43     my $action     = $cgi->param('action')     || '';
44     my $barcode    = $cgi->param('barcode')    || '';
45     my $amount     = $cgi->param('amount')     || 0;
46     $barcode    =~ s/^\s+//;
47     $barcode    =~ s/\s+$//;
48     my $cardnumber = $cgi->param('cardnumber') || '';
49     $cardnumber =~ s/^\s+//;
50     $cardnumber =~ s/\s+$//;
51
52     # KOCT send UTC timestamp, it should be converted to local timezone
53     my $dt = dt_from_string($timestamp, 'iso', DateTime::TimeZone->new(name => 'UTC'));
54     $dt->set_time_zone(C4::Context->tz);
55     $timestamp = $dt->ymd('-') . ' ' . $dt->hms(':');
56
57     if ( $cgi->param('pending') eq 'true' ) { # if the 'pending' flag is true, we store the operation in the db instead of directly processing them
58         $result = AddOfflineOperation(
59             $userid,
60             $branchcode,
61             $timestamp,
62             $action,
63             $barcode,
64             $cardnumber,
65             $amount
66         );
67     } else {
68         $result = ProcessOfflineOperation(
69             {
70                 'userid'      => $userid,
71                 'branchcode'  => $branchcode,
72                 'timestamp'   => $timestamp,
73                 'action'      => $action,
74                 'barcode'     => $barcode,
75                 'cardnumber'  => $cardnumber,
76                 'amount'      => $amount
77             }
78         );
79     }
80 } else {
81     $result = "Authentication failed."
82 }
83
84 print CGI::header('-type'=>'text/plain', '-charset'=>'utf-8');
85 print $result;