2 new features :
authortipaul <tipaul>
Fri, 5 Aug 2005 14:47:23 +0000 (14:47 +0000)
committertipaul <tipaul>
Fri, 5 Aug 2005 14:47:23 +0000 (14:47 +0000)
- a mail is sent everytime an issue if recieved in serial module. The mail is sent to all borrowers that have put an alert on the subscription (remember that you can put an alert only if the librarian have defined a "letter" as mail to send)
- the librarian can see, for a given subscription, who has put an alert.

C4/Bull.pm
C4/Letters.pm
bull/statecollection.pl
bull/viewalerts.pl [new file with mode: 0755]
koha-tmpl/intranet-tmpl/default/en/bull/subscription-detail.tmpl
koha-tmpl/intranet-tmpl/default/en/bull/viewalerts.tmpl [new file with mode: 0644]

index ab1668b..17434bd 100755 (executable)
@@ -22,6 +22,7 @@ use strict;
 use C4::Date;
 use Date::Manip;
 use C4::Suggestions;
+use C4::Letters;
 require Exporter;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
@@ -395,7 +396,6 @@ sub getlatestserials{
 
 sub serialchangestatus {
        my ($serialid,$serialseq,$planneddate,$status)=@_;
-#      warn "($serialid,$serialseq,$planneddate,$status)";
        # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
        my $dbh = C4::Context->dbh;
        my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
@@ -419,20 +419,24 @@ sub serialchangestatus {
                $sth->execute($recievedlist,$missinglist,$subscriptionid);
        }
        # create new waited entry if needed (ie : was a "waited" and has changed)
+       $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
+       $sth->execute($subscriptionid);
+       my $subscription = $sth->fetchrow_hashref;
        if ($oldstatus eq 1 && $status ne 1) {
-               $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
-               $sth->execute($subscriptionid);
-               my $val = $sth->fetchrow_hashref;
                # next issue number
-               my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
+               my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($subscription);
                # next date (calculated from actual date & frequency parameters)
-               my $nextplanneddate = Get_Next_Date($planneddate,$val);
-               newissue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
+               my $nextplanneddate = Get_Next_Date($planneddate,$subscription);
+               newissue($newserialseq, $subscriptionid, $subscription->{'biblionumber'}, 1, $nextplanneddate);
                $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
                                                                                                                innerloop1=?,innerloop2=?,innerloop3=?
                                                                                                                where subscriptionid = ?");
                $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
        }
+       # check if an alert must be sent... (= a letter is defined & status became "arrived"
+       if ($subscription->{letter} && $status eq 2) {
+               sendalerts('issue',$subscription->{subscriptionid},$subscription->{letter});
+       }
 }
 
 sub newissue {
index f06002c..f2a5eb8 100644 (file)
@@ -19,9 +19,11 @@ package C4::Letters;
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+use Mail::Sendmail;
 use C4::Date;
 use Date::Manip;
 use C4::Suggestions;
+use C4::Members;
 require Exporter;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
@@ -47,7 +49,7 @@ C4::Letters - Give functions for Letters management
 =cut
 
 @ISA = qw(Exporter);
-@EXPORT = qw(&GetLetterList &addalert &getalert &delalert &findrelatedto);
+@EXPORT = qw(&GetLetterList &getletter &addalert &getalert &delalert &findrelatedto &sendalerts);
 
 =head2 GetLetterList
 
@@ -74,6 +76,15 @@ sub GetLetterList {
        return @result;
 }
 
+sub getletter {
+       my ($module,$code) = @_;
+       my $dbh = C4::Context->dbh;
+       my $sth = $dbh->prepare("select * from letter where module=? and code=?");
+       $sth->execute($module,$code);
+       my $line = $sth->fetchrow_hashref;
+       return $line;
+}
+
 =head2 addalert
 
        parameters : 
@@ -137,7 +148,6 @@ sub getalert {
                push @bind,$externalid;
        }
        $query =~ s/ and$//;
-#      warn "Q : $query";
        my $sth = $dbh->prepare($query);
        $sth->execute(@bind);
        my @result;
@@ -147,6 +157,7 @@ sub getalert {
        return \@result if $#result >=0; # return only if there is one result.
        return;
 }
+
 =head2 findrelatedto
        parameters :
        - $type : the type of alert
@@ -156,16 +167,98 @@ sub getalert {
        When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
        When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
 =cut
+
 sub findrelatedto {
        my ($type,$externalid) = @_;
        my $dbh=C4::Context->dbh;
        my $sth;
-       if ($type eq "issue") {
+       if ($type eq 'issue') {
                $sth=$dbh->prepare("select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?");
        }
+       if ($type eq 'borrower') {
+               $sth=$dbh->prepare("select concat(firstname,' ',surname) from borrowers where borrowernumber=?");
+       }
        $sth->execute($externalid);
        my ($result) = $sth->fetchrow;
        return $result;
 }
 
+=head2 sendalert
+       parameters :
+       - $type : the type of alert
+       - $externalid : the id of the "object" to query
+       - $letter : the letter to send.
+
+       send an alert to all borrowers having put an alert on a given subject.
+
+=cut
+
+sub sendalerts {
+       my ($type,$externalid,$letter)=@_;
+       warn "sendalerts : ($type,$externalid,$letter)";
+       my $dbh=C4::Context->dbh;
+       if ($type eq 'issue') {
+#              warn "sending issues...";
+               my $letter = getletter('serial',$letter);
+               # prepare the letter...
+               # search the biblionumber
+               my $sth=$dbh->prepare("select biblionumber from subscription where subscriptionid=?");
+               $sth->execute($externalid);
+               my ($biblionumber)=$sth->fetchrow;
+               parseletter($letter,'biblio',$biblionumber);
+               parseletter($letter,'biblioitems',$biblionumber);
+               # find the list of borrowers to alert
+               my $alerts = getalert('','issue',$externalid);
+               foreach (@$alerts) {
+                       my $innerletter = $letter;
+                       my $borinfo = getmember('',$_->{'borrowernumber'});
+                       parseletter($innerletter,'borrowers',$_->{'borrowernumber'});
+                       my $userenv = C4::Context->userenv;
+                       if ($borinfo->{emailaddress}) {
+                               my %mail = ( To => $borinfo->{emailaddress},
+                                                       From => 'paul.poulain@free.fr',#.$userenv->{emailaddress},
+                                                       Subject => "".$innerletter->{title},
+                                                       Message => "".$innerletter->{content},
+                                                       );
+                               sendmail(%mail);
+#                              warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
+                       }
+               }
+       }
+}
+
+=head2
+       parameters :
+       - $letter : a hash to letter fields (title & content useful)
+       - $table : the Koha table to parse.
+       - $pk : the primary key to query on the $table table
+       parse all fields from a table, and replace values in title & content with the appropriate value
+=cut
+sub parseletter {
+       my ($letter,$table,$pk) = @_;
+#      warn "Parseletter : ($letter,$table,$pk)";
+       my $dbh=C4::Context->dbh;
+       my $sth;
+       if ($table eq 'biblio') {
+               $sth = $dbh->prepare("select * from biblio where biblionumber=?");
+       } elsif ($table eq 'biblioitems') {
+               $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
+       } elsif ($table eq 'borrowers') {
+               $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
+       }
+       $sth->execute($pk);
+       # store the result in an hash
+       my $values = $sth->fetchrow_hashref;
+       # and get all fields from the table
+       $sth = $dbh->prepare("show columns from $table");
+       $sth->execute;
+       while ((my $field) = $sth->fetchrow_array) {
+               my $replacefield="<<$table.$field>>";
+               my $replacedby = $values->{$field};
+#              warn "REPLACE $replacefield by $replacedby";
+               $letter->{title} =~ s/$replacefield/$replacedby/g;
+               $letter->{content} =~ s/$replacefield/$replacedby/g;
+       }
+}
+
 END { }       # module clean-up code here (global destructor)
index cb5f00e..b49bc61 100755 (executable)
@@ -25,6 +25,16 @@ my @serialids = $query->param('serialid');
 my @serialseqs = $query->param('serialseq');
 my @planneddates = $query->param('planneddate');
 my @status = $query->param('status');
+
+my ($template, $loggedinuser, $cookie)
+= get_template_and_user({template_name => "bull/statecollection.tmpl",
+                               query => $query,
+                               type => "intranet",
+                               authnotrequired => 0,
+                               flagsrequired => {catalogue => 1},
+                               debug => 1,
+                               });
+
 my $hassubscriptionexpired = hassubscriptionexpired($subscriptionid);
 if ($op eq 'modsubscriptionhistory') {
        modsubscriptionhistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
@@ -52,14 +62,6 @@ my ($totalissues,@serialslist) = getserials($subscriptionid);
 my $sth=$dbh->prepare("select * from subscriptionhistory where subscriptionid = ?");
 $sth->execute($subscriptionid);
 my $solhistory = $sth->fetchrow_hashref;
-my ($template, $loggedinuser, $cookie)
-= get_template_and_user({template_name => "bull/statecollection.tmpl",
-                               query => $query,
-                               type => "intranet",
-                               authnotrequired => 0,
-                               flagsrequired => {catalogue => 1},
-                               debug => 1,
-                               });
 
        $template->param(
                        serialslist => \@serialslist,
diff --git a/bull/viewalerts.pl b/bull/viewalerts.pl
new file mode 100755 (executable)
index 0000000..eabd2c3
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+# $Id$
+
+# Copyright 2000-2002 Katipo Communications
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+use strict;
+use C4::Auth;
+use C4::Context;
+use C4::Output;
+use CGI;
+use C4::Interface::CGI::Output;
+use C4::Interface::CGI::Template;
+use C4::Koha;
+use C4::Letters;
+use C4::Bull;
+
+my $dbh = C4::Context->dbh;
+
+my $input = new CGI;
+my $print = $input->param('print');
+my $template_name;
+
+my ($template, $loggedinuser, $cookie)
+    = get_template_and_user({template_name => 'bull/viewalerts.tmpl',
+                            query => $input,
+                            type => "intranet",
+                            authnotrequired => 0,
+                            flagsrequired => {catalogue => 1},
+                            debug => 1,
+                            });
+
+my $subscriptionid=$input->param('subscriptionid');
+
+my $borrowers = getalert('','issue',$subscriptionid);
+my $subscription = getsubscription($subscriptionid);
+
+foreach (@$borrowers) {
+       $_->{name} = findrelatedto('borrower',$_->{borrowernumber});
+}
+$template->param(alertloop => $borrowers,
+                               bibliotitle => $subscription->{bibliotitle},
+                               subscriptionid => $subscriptionid);
+
+output_html_with_http_headers $input, $cookie, $template->output;
index 474da4a..5dd3fda 100644 (file)
@@ -1,11 +1,14 @@
 <!-- TMPL_INCLUDE NAME="bull-top.inc" -->
 <!--------------------------MAIN BODY OF PAGE-------------------------->
 <div id="mainbloc">
-<h1 class="bull">Subscription</h1>
+<h1 class="bull">Subscription for <!-- TMPL_VAR name="bibliotitle" --></h1>
 <a href="subscription-add.pl?op=mod&subscriptionid=<!-- TMPL_VAR name="subscriptionid" -->" class="button bull" title="Modify subscription">Edit</a>
 <a href="statecollection.pl?subscriptionid=<!-- TMPL_VAR name="subscriptionid" -->" class="button bull" title="Recieve issues">Receive</a>
 <a href="serial-issues.pl?biblionumber=<!-- TMPL_VAR name="biblionumber" -->" class="button bull" title="All issues on this title">Issue history</a>
 <a href="/cgi-bin/koha/bull-home.pl?biblionumber=<!-- TMPL_VAR name="biblionumber" -->" class="button bull" title="all subscriptions on <!-- TMPL_VAR name="bibliotitle" -->">Subscriptions</a>
+<!-- TMPL_IF name="letter" -->
+       <a href="viewalerts.pl?subscriptionid=<!-- TMPL_VAR name="subscriptionid" -->" class="button bull">Alerted borrowers</a>
+<!-- /TMPL_IF -->
 <a href="/cgi-bin/koha/MARCdetail.pl?bib=<!-- TMPL_VAR name="biblionumber" -->" class="button bull" title="go to <!-- TMPL_VAR name="bibliotitle" -->">Back to biblio</a>
 <!-- TMPL_UNLESS name="totalissues" -->
                <a href="javascript:confirm_deletion()" class="button bull">
                <p><label>Budget </label><!-- TMPL_VAR name="bookfundid" --></p>
                <p><label>Biblio</label><i>(<!-- TMPL_VAR name="biblionumber" -->)</i> <!-- TMPL_VAR name="bibliotitle" --></p>
                <p><label>Notes:</label><!-- TMPL_VAR name="notes" --></p>
+               <p>
+                       <!-- TMPL_IF name="letter" -->
+                               <label class="label100">Issue alert with letter:</label><!-- TMPL_VAR name="letter" -->
+                       <!-- TMPL_ELSE -->
+                               <label class="label100">Borrowers can't subscribe to issue alerts</label>
+                       <!-- /TMPL_IF -->
+               </p>
 </div>
 <div id="bloc25">
        <h2 class="bull">Planning</h2>
diff --git a/koha-tmpl/intranet-tmpl/default/en/bull/viewalerts.tmpl b/koha-tmpl/intranet-tmpl/default/en/bull/viewalerts.tmpl
new file mode 100644 (file)
index 0000000..ec66754
--- /dev/null
@@ -0,0 +1,28 @@
+<!-- TMPL_INCLUDE NAME="bull-top.inc" -->
+
+<div id="mainbloc">
+               <h1 class="bull">Alert subscribers for <!-- TMPL_VAR name="bibliotitle" --></h1>
+               <a href="subscription-detail.pl?subscriptionid=<!-- TMPL_VAR name="subscriptionid" -->" class="button bull">subscription</a>
+               <div class="bloc25">
+               <!-- TMPL_IF name="alertloop" -->
+                       <table>
+                       <tr>
+                               <th class="bull">Borrower name</th>
+                               <th class="bull">&nbsp;</th>
+                       </tr>
+
+                       <!-- TMPL_LOOP NAME="alertloop" -->
+                               <tr>
+                                       <td><!-- TMPL_VAR name="name" --></td>
+                                       <td><a href="/cgi-bin/koha/members/moremember.pl?bornum=<!-- TMPL_VAR name="borrowernumber">" class="button bull">View</a></td>
+                               </tr>
+                       <!-- /TMPL_LOOP -->
+                       </table>
+               <!-- TMPL_ELSE -->
+                       Nobody
+               <!-- /TMPL_IF -->
+               </div>
+
+       
+</div>
+<!-- TMPL_INCLUDE NAME="bull-bottom.inc" -->