Bug 17583: Use Koha::Patron->is_expired from the circulation page
[koha.git] / sms / sms_listen.pl
blob0ee3f532f55270b15f021826df83f461583d6ee7
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use C4::SMS;
7 use C4::Auth;
8 use C4::Context;
9 use C4::Members;
10 use C4::Circulation;
12 my ($res,$ua);
13 my $message;
14 my $result;
15 my $errorcode;
16 my $smsid;
17 my $wait=600;## 10 mn. wait between sms checking
18 my $dbh=C4::Context->dbh;
20 STARTAGAIN:
21 ($res,$ua)=get_sms_auth();
22 AGAIN:
23 $errorcode=0;
24 if ($res->{pRetCode}==200){
25 $result=read_sms($ua,$res->{pSessionId});
26 $errorcode=$result->{pErrCode};
27 print "connected\n";
28 } else {
29 kill_sms($ua,$res->{pSessionId});
30 warn (error_codes($res->{pErrCode}),$res->{pErrcode}) ;
31 # sleep $wait;
32 goto FINISH;
34 if ($errorcode && $errorcode !=-9005){
35 kill_sms($ua,$res->{pSessionId});
36 warn error_codes($errorcode) ;
37 # sleep $wait;
38 goto FINISH;
39 } elsif ($errorcode ==-9005){
40 print "no more messages to read\n";
41 goto WAITING;
45 #Parse the message to a useful hash
46 my @action=parse_message( $result->{pContent});
47 ## Log the request in our database;
48 $smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
49 print "message logged\n";
50 ##Now do the service required
51 if (uc($action[0]) eq "RN"){
52 print "dealing request\n";
53 my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
54 unless ($ok) {
55 ##wrong user/pass
56 $message="Yanlis kullanici/sifre! :Wrong username/password!";
57 my $send=send_message($result,$message,$smsid);
58 goto AGAIN;
60 my $item=getiteminformation(undef,0,$action[3]);
61 if ($item){
62 my $borrower=getmember($cardnumber);
63 my $status=renewstatus(undef,$borrower->{borrowernumber},$item->{itemnumber});
64 if ($status==1) {
65 my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
66 $message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
67 } elsif($status==2) {
68 $message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
69 } elsif($status==3) {
70 $message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
71 } elsif($status==4) {
72 $message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
73 } elsif($status==0) {
74 $message="Uzatilamaz! :Can not renew:".$item->{barcode};
76 } else {
77 $message="Yanlis barkot! :Wrong barcode!";
79 } else {
80 ## reply about error
81 $message="Yanlis mesaj formati! :Wrong message! :
82 RN usercardno password barcode";
83 } ### wrong service
84 send_message($result,$message,$smsid);
86 goto AGAIN;
89 WAITING:
90 ##Now send the messages waiting in queue
91 my $smssth=$dbh->prepare("SELECT smsid,user_phone,message from sms_messages where date_replied like '0000-00-00%' ");
92 $smssth->execute();
93 my @phones;
94 while (my $data=$smssth->fetchrow_hashref){
95 push @phones,$data;
97 $smssth->finish;
99 foreach my $user(@phones){
100 print "replying $user->{user_phone}";
101 my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
102 my $reply="--failed\n";
103 if ($send->{pRetCode}==200){
104 $reply= "--replied\n";
105 mod_sms($user->{smsid},"Sent");
107 print $reply;
110 sub send_message {
111 my ($mes,$message,$smsid)=@_;
112 my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
113 if ($send->{pRetCode}==200){
114 mod_sms($smsid,$message);
115 } else {
116 my $error=error_codes($send->{pErrCode});
117 mod_sms($smsid,"Not replied error:".$error);
119 return $send;
121 FINISH:
123 __END__