Add larger values to numerical limit.
[koha.git] / misc / cronjobs / notifyMailsOp.pl
1 #!/usr/bin/perl
2 use strict;
3 BEGIN {
4     # find Koha's Perl modules
5     # test carefully before changing this
6     use FindBin;
7     eval { require "$FindBin::Bin/../kohalib.pl" };
8 }
9 use C4::Context;
10 use C4::Dates qw/format_date/;
11 use Mail::Sendmail;  # comment out if not doing e-mail notices
12 use Getopt::Long;
13 use C4::Circulation;
14 # use C4::Members;
15 #  this module will notify only the mail case
16 # Now it's only programmed for ouest provence, you can modify it for yourself
17 # sub function for get all notifications are not sends
18 sub GetNotifys {
19 #       my($branch) = @_;
20         my $dbh = C4::Context->dbh;
21         my $sth=$dbh->prepare("SELECT DISTINCT notifys.borrowernumber , borrowers.surname , borrowers.firstname , borrowers.title AS borrower_title , categories.category_type AS categorycode , borrowers.email , borrowers.contacttitle , borrowers.contactname , borrowers.contactfirstname ,
22         notifys.notify_level , notifys.method
23         FROM notifys,borrowers,categories WHERE (notifys.borrowernumber=borrowers.borrowernumber) AND (notifys.notify_send_date IS NULL) AND (borrowers.categorycode = categories.categorycode)");
24         
25         $sth->execute();
26                 my @getnotifys;
27                 my $i=0;
28                 while (my $data=$sth->fetchrow_hashref){
29                         $getnotifys[$i]=$data;
30                         $i++;   
31                 }
32                 $sth->finish;
33                 return(@getnotifys);
34
35 }
36
37 sub GetBorrowerNotifys{
38         my ($borrowernumber) = @_;
39         my $dbh = C4::Context->dbh;
40         my @getnotifys2;
41         my $sth2=$dbh->prepare("SELECT notifys.itemnumber,notifys.notify_level,biblio.title ,itemtypes.description,
42                         issues.date_due
43                         FROM notifys,biblio,items,itemtypes,biblioitems,issues 
44                         WHERE
45                         (items.itemnumber=notifys.itemnumber
46                         AND biblio.biblionumber=items.biblionumber)
47                         AND (itemtypes.itemtype=biblioitems.itemtype AND biblioitems.biblionumber=biblio.biblionumber)
48                         AND
49                         (notifys.borrowernumber=issues.borrowernumber AND notifys.itemnumber=issues.itemnumber)
50                         AND
51                         notifys.borrowernumber=?
52                         AND notify_send_date IS NULL");
53                         $sth2->execute($borrowernumber);
54                         my $j=0;
55                         while (my $data2=$sth2->fetchrow_hashref){
56                                 $getnotifys2[$j]=$data2;
57                                 $j++;
58                         }
59                         $sth2->finish;
60                         return(@getnotifys2);
61
62 }
63
64 sub GetOverduerules{
65         my($category,$notify_level) = @_;
66         my $dbh = C4::Context->dbh;
67         my $sth=$dbh->prepare("SELECT letter".$notify_level.",debarred".$notify_level." FROM overduerules WHERE categorycode=?");
68         $sth->execute($category);
69         my (@overduerules)=$sth->fetchrow_array;
70         $sth->finish;
71         return(@overduerules);
72
73 }
74
75 sub GetLetter{
76
77         my($letterid) = @_;
78         my $dbh = C4::Context->dbh;
79         my $sth=$dbh->prepare("SELECT title,content FROM letter WHERE code=?");
80         $sth->execute($letterid);
81         my (@getletter)=$sth->fetchrow_array;
82         $sth->finish;
83         return(@getletter);
84
85 }
86
87 sub UpdateBorrowerDebarred{
88         my($borrowernumber) = @_;
89         my $dbh = C4::Context->dbh;
90         my $sth=$dbh->prepare("UPDATE borrowers SET debarred='1' WHERE borrowernumber=?");
91         $sth->execute($borrowernumber);
92         $sth->finish;
93         return 1;
94 }
95
96 sub UpdateNotifySendDate{
97         my($borrowernumber,$itemnumber,$notifyLevel) = @_;
98         my $dbh = C4::Context->dbh;
99         my $sth=$dbh->prepare("UPDATE notifys SET notify_send_date=now() 
100         WHERE borrowernumber=? AND itemnumber=? AND notify_send_date IS NULL AND notify_level=?");
101         $sth->execute($borrowernumber,$itemnumber,$notifyLevel);
102         $sth->finish;
103         return 1;
104
105 }
106
107 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
108
109 # work with get notifys
110 my $smtpserver = 'smtp.yoursmtpserver'; # your smtp server (the server who sent mails)
111 my $from = 'your@librarymailadress'; # all the mails sent to the borrowers will appear coming from here.
112
113
114 # initiate file for wrong_mails
115 my $outfile = 'wrong_mails.txt';
116 open( OUT, ">$outfile" );
117 binmode(OUT, 'utf8');
118
119 my @getnofifys = GetNotifys();
120 foreach my $num (@getnofifys) {
121         my %notify;     
122 #       if we have a method mail, we check witch mail letter we launch
123         if ($num->{'method'} eq 'mail'){
124                 my ($letterid,$debarred) = GetOverduerules($num->{'categorycode'},$num->{'notify_level'});
125 #                       now, we get the letter associated to letterid
126                         my($title,$content) = GetLetter($letterid);
127                         my $email = $num->{'email'};
128                         #my $email = 'alaurin@ouestprovence.fr';
129                         my $mailtitle = $title; # the title of the mails
130 # Work with the adult category code
131                                 if ($num->{'categorycode'} eq 'A') {
132         #                       now deal with $content
133                                         $content =~ s/\<<borrowers.title>\>/$num->{'borrower_title'}/g ;
134                                         $content =~ s/\<<borrowers.surname>\>/$num->{'surname'}/g ;
135                                         $content =~ s/\<<borrowers.firstname>\>/$num->{'firstname'}/g ;
136                                         
137                                         my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
138                                         my $overdueitems;
139                                         foreach my $notif(@getborrowernotify){
140                                                 my $date=format_date($notif->{'date_due'});
141                                                 if ($notif->{'notify_level'} eq $num->{'notify_level'}){
142                                                 $overdueitems .= " - <b>".$notif->{'title'}."</b>" ;
143                                                 $overdueitems .= "  ( ".$notif->{'description'}." )  " ;
144                                                 $overdueitems .= "emprunté le :".$date;
145                                                 $overdueitems .= "<br>";
146                                                 
147 # FIXME at this time, the program consider the mail is send (in notify_send_date) but with no real check must be improved , we don't know if the mail was really to a real adress, and if there is a problem, we don't know how to return the notification to koha...
148         UpdateNotifySendDate($num->{'borrowernumber'},$notif->{'itemnumber'},$num->{'notify_level'});
149 }
150                                         }
151                                 # if we don't have overdueitem replace content by nonotifys value, deal with it later
152                                         if ($overdueitems){     
153                                         $content =~ s/\<<items.content>\>/$overdueitems/g;
154                                 }
155                                 else {
156                                 $content = 'nonotifys';
157                                 }
158                         }
159 # Work with the child category code (we add the parents infos)
160                                 if ($num->{'categorycode'} eq 'C') {
161                                         $content =~ s/\<<borrowers.contacttitle>\>/$num->{'contacttitle'}/g ;
162                                         $content =~ s/\<<borrowers.contactname>\>/$num->{'contactname'}/g ;
163                                         $content =~ s/\<<borrowers.contactfirstname>\>/$num->{'contactfirstname'}/g ;
164                                         $content =~ s/\<<borrowers.title>\>/$num->{'borrower_title'}/g ;
165                                         $content =~ s/\<<borrowers.surname>\>/$num->{'surname'}/g ;
166                                         $content =~ s/\<<borrowers.firstname>\>/$num->{'firstname'}/g ;
167                                         
168                                         my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
169                                         my $overdueitems;
170                                         foreach my $notif(@getborrowernotify){
171                                                 my $date=format_date($notif->{'date_due'});
172                                                 
173                                                 $overdueitems .= " - <b>".$notif->{'title'}."</b>" ;
174                                                 $overdueitems .= "  ( ".$notif->{'description'}." )  " ;
175                                                 $overdueitems .= "emprunté le :".$date;
176                                                 $overdueitems .= "<br>";
177 # FIXME at this time, the program consider the mail is send (in notify_send_date) but with no real check must be improved ...
178                                 UpdateNotifySendDate($num->{'borrowernumber'},$notif->{'itemnumber'},$num->{'notify_level'});
179                                                 }
180                                         
181                                         if ($overdueitems){
182                                                 $content =~ s/\<<items.content>\>/$overdueitems/g;
183                                         }
184                                         else {
185                                         $content = 'nonotifys';
186                                         }
187                                 }
188 # initiate the send mail
189
190 #       decoding mailtitle for lisibility of mailtitle (bug with utf-8 values, so decoding it)
191         utf8::decode($mailtitle);
192
193                         my $mailtext = $content;
194                                 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
195 #                                         set your own mail server name here
196                                         my %mail = ( To      => $email,
197                                                                 From    => $from,
198                                                                 Subject => $mailtitle,
199                                                                 Message => $mailtext,
200                                                                 'content-type' => 'text/html; charset="utf-8"',
201                                         );
202                                 # if we don't have any content for the mail, we don't launch mail, but notify it in a file
203                                         if ($mailtext ne 'nonotifys') {
204                                         sendmail(%mail);
205                                         }
206                                         else {
207                                         print OUT $email ;
208                                         }
209                                         
210 # now deal with the debarred mode
211 #               if ($debarred eq 1) {
212 #               �ajouter : si le lecteur est en mode debarred, ajouter la fonction qui nous permettra cela
213 #               UpdateBorrowerDebarred($num->{'borrowernumber'});
214 #               }
215         close(OUT);
216         }
217 }