4 # find Koha's Perl modules
5 # test carefully before changing this
7 eval { require "$FindBin::Bin/../kohalib.pl" };
10 use C4::Dates qw/format_date/;
11 use Mail::Sendmail; # comment out if not doing e-mail notices
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
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)");
28 while (my $data=$sth->fetchrow_hashref){
29 $getnotifys[$i]=$data;
37 sub GetBorrowerNotifys{
38 my ($borrowernumber) = @_;
39 my $dbh = C4::Context->dbh;
41 my $sth2=$dbh->prepare("SELECT notifys.itemnumber,notifys.notify_level,biblio.title ,itemtypes.description,
43 FROM notifys,biblio,items,itemtypes,biblioitems,issues
45 (items.itemnumber=notifys.itemnumber
46 AND biblio.biblionumber=items.biblionumber)
47 AND (itemtypes.itemtype=biblioitems.itemtype AND biblioitems.biblionumber=biblio.biblionumber)
49 (notifys.borrowernumber=issues.borrowernumber AND notifys.itemnumber=issues.itemnumber)
51 notifys.borrowernumber=?
52 AND notify_send_date IS NULL");
53 $sth2->execute($borrowernumber);
55 while (my $data2=$sth2->fetchrow_hashref){
56 $getnotifys2[$j]=$data2;
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;
71 return(@overduerules);
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;
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);
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);
107 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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.
114 # initiate file for wrong_mails
115 my $outfile = 'wrong_mails.txt';
116 open( OUT, ">$outfile" );
117 binmode(OUT, 'utf8');
119 my @getnofifys = GetNotifys();
120 foreach my $num (@getnofifys) {
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 ;
137 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
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>";
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'});
151 # if we don't have overdueitem replace content by nonotifys value, deal with it later
153 $content =~ s/\<<items.content>\>/$overdueitems/g;
156 $content = 'nonotifys';
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 ;
168 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
170 foreach my $notif(@getborrowernotify){
171 my $date=format_date($notif->{'date_due'});
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'});
182 $content =~ s/\<<items.content>\>/$overdueitems/g;
185 $content = 'nonotifys';
188 # initiate the send mail
190 # decoding mailtitle for lisibility of mailtitle (bug with utf-8 values, so decoding it)
191 utf8::decode($mailtitle);
193 my $mailtext = $content;
194 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
195 # set your own mail server name here
196 my %mail = ( To => $email,
198 Subject => $mailtitle,
199 Message => $mailtext,
200 'content-type' => 'text/html; charset="utf-8"',
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') {
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'});