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