3 use C4::Dates qw/format_date/;
4 use Mail::Sendmail; # comment out if not doing e-mail notices
8 # this module will notify only the mail case
9 # Now it's only programmed for ouest provence, you can modify it for yourself
10 # sub function for get all notifications are not sends
13 my $dbh = C4::Context->dbh;
14 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 ,
15 notifys.notify_level , notifys.method
16 FROM notifys,borrowers,categories WHERE (notifys.borrowernumber=borrowers.borrowernumber) AND (notifys.notify_send_date IS NULL) AND (borrowers.categorycode = categories.categorycode)");
21 while (my $data=$sth->fetchrow_hashref){
22 $getnotifys[$i]=$data;
30 sub GetBorrowerNotifys{
31 my ($borrowernumber) = @_;
32 my $dbh = C4::Context->dbh;
34 my $sth2=$dbh->prepare("SELECT notifys.itemnumber,notifys.notify_level,biblio.title ,itemtypes.description,
36 FROM notifys,biblio,items,itemtypes,biblioitems,issues
38 (items.itemnumber=notifys.itemnumber
39 AND biblio.biblionumber=items.biblionumber)
40 AND (itemtypes.itemtype=biblioitems.itemtype AND biblioitems.biblionumber=biblio.biblionumber)
42 (notifys.borrowernumber=issues.borrowernumber AND notifys.itemnumber=issues.itemnumber)
44 issues.returndate IS NULL
46 notifys.borrowernumber=?
47 AND notify_send_date IS NULL");
48 $sth2->execute($borrowernumber);
50 while (my $data2=$sth2->fetchrow_hashref){
51 $getnotifys2[$j]=$data2;
60 my($category,$notify_level) = @_;
61 my $dbh = C4::Context->dbh;
62 my $sth=$dbh->prepare("SELECT letter".$notify_level.",debarred".$notify_level." FROM overduerules WHERE categorycode=?");
63 $sth->execute($category);
64 my (@overduerules)=$sth->fetchrow_array;
66 return(@overduerules);
73 my $dbh = C4::Context->dbh;
74 my $sth=$dbh->prepare("SELECT title,content FROM letter WHERE code=?");
75 $sth->execute($letterid);
76 my (@getletter)=$sth->fetchrow_array;
82 sub UpdateBorrowerDebarred{
83 my($borrowernumber) = @_;
84 my $dbh = C4::Context->dbh;
85 my $sth=$dbh->prepare("UPDATE borrowers SET debarred='1' WHERE borrowernumber=?");
86 $sth->execute($borrowernumber);
91 sub UpdateNotifySendDate{
92 my($borrowernumber,$itemnumber,$notifyLevel) = @_;
93 my $dbh = C4::Context->dbh;
94 my $sth=$dbh->prepare("UPDATE notifys SET notify_send_date=now()
95 WHERE borrowernumber=? AND itemnumber=? AND notify_send_date IS NULL AND notify_level=?");
96 $sth->execute($borrowernumber,$itemnumber,$notifyLevel);
102 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
104 # work with get notifys
105 my $smtpserver = 'smtp.yoursmtpserver'; # your smtp server (the server who sent mails)
106 my $from = 'your@librarymailadress'; # all the mails sent to the borrowers will appear coming from here.
109 # initiate file for wrong_mails
110 my $outfile = 'wrong_mails.txt';
111 open( OUT, ">$outfile" );
112 binmode(OUT, 'utf8');
114 my @getnofifys = GetNotifys();
115 foreach my $num (@getnofifys) {
117 # if we have a method mail, we check witch mail letter we launch
118 if ($num->{'method'} eq 'mail'){
119 my ($letterid,$debarred) = GetOverduerules($num->{'categorycode'},$num->{'notify_level'});
120 # now, we get the letter associated to letterid
121 my($title,$content) = GetLetter($letterid);
122 my $email = $num->{'email'};
123 #my $email = 'alaurin@ouestprovence.fr';
124 my $mailtitle = $title; # the title of the mails
125 # Work with the adult category code
126 if ($num->{'categorycode'} eq 'A') {
127 # now deal with $content
128 $content =~ s/\<<borrowers.title>\>/$num->{'borrower_title'}/g ;
129 $content =~ s/\<<borrowers.surname>\>/$num->{'surname'}/g ;
130 $content =~ s/\<<borrowers.firstname>\>/$num->{'firstname'}/g ;
132 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
134 foreach my $notif(@getborrowernotify){
135 my $date=format_date($notif->{'date_due'});
136 if ($notif->{'notify_level'} eq $num->{'notify_level'}){
137 $overdueitems .= " - <b>".$notif->{'title'}."</b>" ;
138 $overdueitems .= " ( ".$notif->{'description'}." ) " ;
139 $overdueitems .= "emprunté le :".$date;
140 $overdueitems .= "<br>";
142 # 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...
143 UpdateNotifySendDate($num->{'borrowernumber'},$notif->{'itemnumber'},$num->{'notify_level'});
146 # if we don't have overdueitem replace content by nonotifys value, deal with it later
148 $content =~ s/\<<items.content>\>/$overdueitems/g;
151 $content = 'nonotifys';
154 # Work with the child category code (we add the parents infos)
155 if ($num->{'categorycode'} eq 'C') {
156 $content =~ s/\<<borrowers.contacttitle>\>/$num->{'contacttitle'}/g ;
157 $content =~ s/\<<borrowers.contactname>\>/$num->{'contactname'}/g ;
158 $content =~ s/\<<borrowers.contactfirstname>\>/$num->{'contactfirstname'}/g ;
159 $content =~ s/\<<borrowers.title>\>/$num->{'borrower_title'}/g ;
160 $content =~ s/\<<borrowers.surname>\>/$num->{'surname'}/g ;
161 $content =~ s/\<<borrowers.firstname>\>/$num->{'firstname'}/g ;
163 my @getborrowernotify=GetBorrowerNotifys($num->{'borrowernumber'});
165 foreach my $notif(@getborrowernotify){
166 my $date=format_date($notif->{'date_due'});
168 $overdueitems .= " - <b>".$notif->{'title'}."</b>" ;
169 $overdueitems .= " ( ".$notif->{'description'}." ) " ;
170 $overdueitems .= "emprunté le :".$date;
171 $overdueitems .= "<br>";
172 # FIXME at this time, the program consider the mail is send (in notify_send_date) but with no real check must be improved ...
173 UpdateNotifySendDate($num->{'borrowernumber'},$notif->{'itemnumber'},$num->{'notify_level'});
177 $content =~ s/\<<items.content>\>/$overdueitems/g;
180 $content = 'nonotifys';
183 # initiate the send mail
185 # decoding mailtitle for lisibility of mailtitle (bug with utf-8 values, so decoding it)
186 utf8::decode($mailtitle);
188 my $mailtext = $content;
189 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
190 # set your own mail server name here
191 my %mail = ( To => $email,
193 Subject => $mailtitle,
194 Message => $mailtext,
195 'content-type' => 'text/html; charset="utf-8"',
197 # if we don't have any content for the mail, we don't launch mail, but notify it in a file
198 if ($mailtext ne 'nonotifys') {
205 # now deal with the debarred mode
206 # if ($debarred eq 1) {
207 # �ajouter : si le lecteur est en mode debarred, ajouter la fonction qui nous permettra cela
208 # UpdateBorrowerDebarred($num->{'borrowernumber'});