diff --git a/C4/Bull.pm b/C4/Bull.pm index ab1668b672..17434bdd32 100755 --- a/C4/Bull.pm +++ b/C4/Bull.pm @@ -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 { diff --git a/C4/Letters.pm b/C4/Letters.pm index f06002c969..f2a5eb84b6 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -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) diff --git a/bull/statecollection.pl b/bull/statecollection.pl index cb5f00e550..b49bc6114f 100755 --- a/bull/statecollection.pl +++ b/bull/statecollection.pl @@ -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 index 0000000000..eabd2c305d --- /dev/null +++ b/bull/viewalerts.pl @@ -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; diff --git a/koha-tmpl/intranet-tmpl/default/en/bull/subscription-detail.tmpl b/koha-tmpl/intranet-tmpl/default/en/bull/subscription-detail.tmpl index 474da4ad35..5dd3fdaa78 100644 --- a/koha-tmpl/intranet-tmpl/default/en/bull/subscription-detail.tmpl +++ b/koha-tmpl/intranet-tmpl/default/en/bull/subscription-detail.tmpl @@ -1,11 +1,14 @@
-

Subscription

+

Subscription for

" class="button bull" title="Modify subscription">Edit " class="button bull" title="Recieve issues">Receive " class="button bull" title="All issues on this title">Issue history " class="button bull" title="all subscriptions on ">Subscriptions + + " class="button bull">Alerted borrowers + " class="button bull" title="go to ">Back to biblio @@ -21,6 +24,13 @@

()

+

+ + + + + +

Planning

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 index 0000000000..ec667548cb --- /dev/null +++ b/koha-tmpl/intranet-tmpl/default/en/bull/viewalerts.tmpl @@ -0,0 +1,28 @@ + + +
+

Alert subscribers for

+
" class="button bull">subscription +
+ + + + + + + + + + + + + +
Borrower name 
" class="button bull">View
+ + Nobody + +
+ + +
+