3 # find Koha's Perl modules
4 # test carefully before changing this
6 eval { require "$FindBin::Bin/../kohalib.pl" };
9 use C4::Dates qw/format_date/;
10 use Mail::Sendmail; # comment out if not doing e-mail notices
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
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)");
27 while (my $data=$sth->fetchrow_hashref){
28 $getnotifys[$i]=$data;
36 sub GetBorrowerNotifys{
37 my ($borrowernumber) = @_;
38 my $dbh = C4::Context->dbh;
40 my $sth2=$dbh->prepare("SELECT notifys.itemnumber,notifys.notify_level,biblio.title ,itemtypes.description,
42 FROM notifys,biblio,items,itemtypes,biblioitems,issues
44 (items.itemnumber=notifys.itemnumber
45 AND biblio.biblionumber=items.biblionumber)
46 AND (itemtypes.itemtype=biblioitems.itemtype AND biblioitems.biblionumber=biblio.biblionumber)
48 (notifys.borrowernumber=issues.borrowernumber AND notifys.itemnumber=issues.itemnumber)
50 issues.returndate IS NULL
52 notifys.borrowernumber=?
53 AND notify_send_date IS NULL");
54 $sth2->execute($borrowernumber);
56 while (my $data2=$sth2->fetchrow_hashref){
57 $getnotifys2[$j]=$data2;
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;
72 return(@overduerules);
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;
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);
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);
108 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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.
115 # initiate file for wrong_mails
116 my $outfile = 'wrong_mails.txt';
117 open( OUT, ">$outfile" );
118 binmode(OUT, 'utf8');
120 my @getnofifys = GetNotifys();
121 foreach my $num (@getnofifys) {
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 ;
138 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
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>";
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'});
152 # if we don't have overdueitem replace content by nonotifys value, deal with it later
154 $content =~ s/\<<items.content>\>/$overdueitems/g;
157 $content = 'nonotifys';
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 ;
169 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
171 foreach my $notif(@getborrowernotify){
172 my $date=format_date($notif->{'date_due'});
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'});
183 $content =~ s/\<<items.content>\>/$overdueitems/g;
186 $content = 'nonotifys';
189 # initiate the send mail
191 # decoding mailtitle for lisibility of mailtitle (bug with utf-8 values, so decoding it)
192 utf8::decode($mailtitle);
194 my $mailtext = $content;
195 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
196 # set your own mail server name here
197 my %mail = ( To => $email,
199 Subject => $mailtitle,
200 Message => $mailtext,
201 'content-type' => 'text/html; charset="utf-8"',
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') {
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'});