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