1 package C4::Bull; #assumes C4/Bull.pm
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::Bull - Give functions for serializing.
43 Give all XYZ functions
48 @EXPORT = qw(&newsubscription &modsubscription &delsubscription &getsubscriptions &getsubscription
49 &getsubscriptionfrombiblionumber &get_subscription_list_from_biblionumber
50 &get_full_subscription_list_from_biblionumber
51 &modsubscriptionhistory &newissue
52 &getserials &getlatestserials &serialchangestatus
53 &Find_Next_Date, &Get_Next_Seq
54 &hassubscriptionexpired &subscriptionexpirationdate &subscriptionrenew
55 &getSupplierListWithLateIssues &GetLateIssues &serialdelete &getlatestserials);
57 sub getSupplierListWithLateIssues {
58 my $dbh = C4::Context->dbh;
59 my $sth = $dbh->prepare("SELECT DISTINCT id, name
60 FROM subscription, serial
61 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
62 WHERE subscription.subscriptionid = serial.subscriptionid AND
63 (planneddate < now( ) OR serial.STATUS = 3)
67 while (my ($id,$name) = $sth->fetchrow) {
68 $supplierlist{$id} = $name;
73 my ($supplierid) = @_;
74 my $dbh = C4::Context->dbh;
77 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
78 FROM subscription, serial, biblio
79 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
80 WHERE subscription.subscriptionid = serial.subscriptionid AND
81 ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
82 subscription.aqbooksellerid=$supplierid and
83 biblio.biblionumber = subscription.biblionumber
86 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
87 FROM subscription, serial, biblio
88 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
89 WHERE subscription.subscriptionid = serial.subscriptionid AND
90 ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
91 biblio.biblionumber = subscription.biblionumber
97 while (my $line = $sth->fetchrow_hashref) {
98 $line->{title} = "" if $line->{title} eq $last_title;
99 $last_title = $line->{title} if ($line->{title});
100 $line->{planneddate} = format_date($line->{planneddate});
101 push @issuelist,$line;
106 sub newsubscription {
107 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
108 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
109 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
110 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
111 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
112 $numberingmethod, $status, $notes,$letter) = @_;
113 my $dbh = C4::Context->dbh;
115 my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
116 startdate,periodicity,dow,numberlength,weeklength,monthlength,
117 add1,every1,whenmorethan1,setto1,lastvalue1,
118 add2,every2,whenmorethan2,setto2,lastvalue2,
119 add3,every3,whenmorethan3,setto3,lastvalue3,
120 numberingmethod, status, notes, letter) values
122 ?,?,?,?,?,?,?,?,?,?,
123 ?,?,?,?,?,?,?,?,?,?,?)");
124 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
125 format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
126 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
127 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
128 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
129 $numberingmethod, $status, $notes,$letter);
130 #then create the 1st waited number
131 my $subscriptionid = $dbh->{'mysql_insertid'};
132 $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
133 $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
134 # reread subscription to get a hash (for calculation of the 1st issue number)
135 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
136 $sth->execute($subscriptionid);
137 my $val = $sth->fetchrow_hashref;
139 # calculate issue number
140 my $serialseq = Get_Seq($val);
141 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
142 $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
143 return $subscriptionid;
146 sub getsubscription {
147 my ($subscriptionid) = @_;
148 my $dbh = C4::Context->dbh;
149 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
151 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
152 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
153 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
154 left join biblio on biblio.biblionumber=subscription.biblionumber
155 where subscription.subscriptionid = ?');
156 $sth->execute($subscriptionid);
157 my $subs = $sth->fetchrow_hashref;
161 sub getsubscriptionfrombiblionumber {
162 my ($biblionumber) = @_;
163 my $dbh = C4::Context->dbh;
164 my $sth = $dbh->prepare('select count(*) from subscription where biblionumber=?');
165 $sth->execute($biblionumber);
166 my $subscriptionsnumber = $sth->fetchrow;
167 return $subscriptionsnumber;
170 sub get_subscription_list_from_biblionumber {
171 my ($biblionumber) = @_;
172 my $dbh = C4::Context->dbh;
173 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*, aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
175 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
176 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
177 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
178 left join biblio on biblio.biblionumber=subscription.biblionumber
179 where subscription.biblionumber = ?');
180 $sth->execute($biblionumber);
182 while (my $subs = $sth->fetchrow_hashref) {
183 $subs->{startdate} = format_date($subs->{startdate});
184 $subs->{histstartdate} = format_date($subs->{histstartdate});
185 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
186 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
187 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
188 $subs->{"periodicity".$subs->{periodicity}} = 1;
189 if ($subs->{enddate} eq '0000-00-00') {
192 $subs->{enddate} = format_date($subs->{enddate});
199 sub get_full_subscription_list_from_biblionumber {
200 my ($biblionumber) = @_;
201 my $dbh = C4::Context->dbh;
202 my $sth = $dbh->prepare('select serial.serialseq, serial.planneddate, serial.status, year(serial.planneddate) as year,subscription.*, aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
203 from serial left join subscription on (serial.subscriptionid=subscription.subscriptionid and subscription.biblionumber=serial.biblionumber)
204 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
205 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
206 left join biblio on biblio.biblionumber=subscription.biblionumber
207 where subscription.biblionumber = ? order by year,serial.subscriptionid,serial.planneddate');
208 $sth->execute($biblionumber);
212 my $aqbooksellername;
217 while (my $subs = $sth->fetchrow_hashref) {
218 # my $sth2 = $dbh->prepare('select * from serial where serial.biblionumber = ? and serial.subscriptionid=? order by serial.planneddate');
219 # $sth2->execute($biblionumber,$subs->{'subscriptionid'});
220 # while (my $issues = $sth2->fetchrow_hashref){
221 # warn "planneddate ".$issues->{'planneddate'};
222 # warn "serialseq".$issues->{'serialseq'};
224 if ($year and ($year==$subs->{year})){
225 if ($first eq 1){$first=0;}
226 my $temp=$res[scalar(@res)-1]->{'serials'};
228 {'planneddate' => format_date($subs->{'planneddate'}),
229 'serialseq' => $subs->{'serialseq'},
230 'status1' => $subs->{'status'}==1,
231 'status2' => $subs->{'status'}==2,
232 'status3' => $subs->{'status'}==3,
233 'status4' => $subs->{'status'}==4,
234 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
237 $first=1 if (not $year);
238 $year= $subs->{'year'};
239 $startdate= format_date($subs->{'startdate'});
240 $aqbooksellername= $subs->{'aqbooksellername'};
241 $bibliotitle= $subs->{'bibliotitle'};
244 {'planneddate' => format_date($subs->{'planneddate'}),
245 'serialseq' => $subs->{'serialseq'},
246 'status1' => $subs->{'status'}==1,
247 'status2' => $subs->{'status'}==2,
248 'status3' => $subs->{'status'}==3,
249 'status4' => $subs->{'status'}==4,
250 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
255 'startdate'=>$startdate,
256 'aqbooksellername'=>$aqbooksellername,
257 'bibliotitle'=>$bibliotitle,
262 $previousnote=$subs->{notes};
268 sub modsubscription {
269 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
270 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
271 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
272 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
273 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
274 $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid)= @_;
275 my $dbh = C4::Context->dbh;
276 my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
277 periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
278 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
279 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
280 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
281 numberingmethod=?, status=?, biblionumber=?, notes=?, letter=? where subscriptionid = ?");
282 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
283 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
284 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
285 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
286 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
287 $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid);
292 sub delsubscription {
293 my ($subscriptionid) = @_;
294 # check again there is only one issue (the subscription is new)
295 my ($totalissues) = getserials($subscriptionid);
296 if ($totalissues eq 1) {
297 my $dbh = C4::Context->dbh;
298 $subscriptionid=$dbh->quote($subscriptionid);
299 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
300 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
301 $dbh->do("delete from serial where subscriptionid=$subscriptionid");
304 sub getsubscriptions {
305 my ($title,$ISSN,$biblionumber) = @_;
306 return unless $title or $ISSN or $biblionumber;
307 my $dbh = C4::Context->dbh;
310 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes,biblio.biblionumber from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblio.biblionumber=?");
311 $sth->execute($biblionumber);
313 if ($ISSN and $title)
315 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes,biblio.biblionumber from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and (biblio.title like ? or biblioitems.issn = ? )");
316 $sth->execute("%$title%",$ISSN);
322 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes,biblio.biblionumber from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblioitems.issn = ?");
323 $sth->execute($ISSN);
327 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes,biblio.biblionumber from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and
328 biblio.biblionumber=subscription.biblionumber and biblio.title like ? ");
329 $sth->execute("%$title%");
334 my $previoustitle="";
335 while (my $line = $sth->fetchrow_hashref) {
336 if ($previoustitle eq $line->{title}) {
340 $previoustitle=$line->{title};
342 push @results, $line;
347 sub modsubscriptionhistory {
348 my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
349 my $dbh=C4::Context->dbh;
350 my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
351 $recievedlist =~ s/^,//g;
352 $missinglist =~ s/^,//g;
353 $opacnote =~ s/^,//g;
354 $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
356 # get every serial not arrived for a given subscription
357 # as well as the number of issues registered in the database (all types)
358 # this number is used to see if a subscription can be deleted (=it must have only 1 issue)
360 my ($subscriptionid) = @_;
361 my $dbh = C4::Context->dbh;
362 # status = 2 is "arrived"
363 my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate, notes from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5");
365 $sth->execute($subscriptionid);
367 while(my $line = $sth->fetchrow_hashref) {
368 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
369 $line->{"planneddate"} = format_date($line->{"planneddate"});
372 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
373 $sth->execute($subscriptionid);
374 my ($totalissues) = $sth->fetchrow;
375 return ($totalissues,@serials);
378 # get the $limit's latest serials arrived or missing for a given subscription
379 sub getlatestserials{
380 my ($subscriptionid,$limit) =@_;
381 my $dbh = C4::Context->dbh;
382 # status = 2 is "arrived"
383 my $strsth="select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and (status =2 or status=4) order by planneddate DESC LIMIT 0,$limit";
384 my $sth=$dbh->prepare($strsth);
385 $sth->execute($subscriptionid);
387 while(my $line = $sth->fetchrow_hashref) {
388 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
389 $line->{"planneddate"} = format_date($line->{"planneddate"});
392 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
393 $sth->execute($subscriptionid);
394 my ($totalissues) = $sth->fetchrow;
398 sub serialchangestatus {
399 my ($serialid,$serialseq,$planneddate,$status,$note)=@_;
400 # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
401 my $dbh = C4::Context->dbh;
402 my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
403 $sth->execute($serialid);
404 my ($subscriptionid,$oldstatus) = $sth->fetchrow;
405 # change status & update subscriptionhistory
407 delissue($serialseq, $subscriptionid)
409 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=?, notes=? where serialid = ?");
410 $sth->execute($serialseq,$planneddate,$status,$note,$serialid);
411 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
412 $sth->execute($subscriptionid);
413 my ($missinglist,$recievedlist) = $sth->fetchrow;
415 $recievedlist .= ",$serialseq";
417 $missinglist .= ",$serialseq" if ($status eq 4) ;
418 $missinglist .= ",not issued $serialseq" if ($status eq 5);
419 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
420 $sth->execute($recievedlist,$missinglist,$subscriptionid);
422 # create new waited entry if needed (ie : was a "waited" and has changed)
423 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
424 $sth->execute($subscriptionid);
425 my $subscription = $sth->fetchrow_hashref;
426 if ($oldstatus eq 1 && $status ne 1) {
428 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($subscription);
429 # next date (calculated from actual date & frequency parameters)
430 my $nextplanneddate = Get_Next_Date($planneddate,$subscription);
431 newissue($newserialseq, $subscriptionid, $subscription->{'biblionumber'}, 1, $nextplanneddate);
432 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
433 innerloop1=?,innerloop2=?,innerloop3=?
434 where subscriptionid = ?");
435 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
437 # check if an alert must be sent... (= a letter is defined & status became "arrived"
438 if ($subscription->{letter} && $status eq 2) {
439 sendalerts('issue',$subscription->{subscriptionid},$subscription->{letter});
444 my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
445 my $dbh = C4::Context->dbh;
446 my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
447 $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
448 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
449 $sth->execute($subscriptionid);
450 my ($missinglist,$recievedlist) = $sth->fetchrow;
452 $recievedlist .= ",$serialseq";
455 $missinglist .= ",$serialseq";
457 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
458 $sth->execute($recievedlist,$missinglist,$subscriptionid);
462 my ($serialseq,$subscriptionid) = @_;
463 my $dbh = C4::Context->dbh;
464 my $sth = $dbh->prepare("delete from serial where serialseq= ? and subscriptionid= ? ");
465 $sth->execute($serialseq,$subscriptionid);
468 sub Get_Next_Date(@) {
469 my ($planneddate,$subscription) = @_;
471 if ($subscription->{periodicity} == 1) {
472 $resultdate=DateCalc($planneddate,"1 day");
474 if ($subscription->{periodicity} == 2) {
475 $resultdate=DateCalc($planneddate,"1 week");
477 if ($subscription->{periodicity} == 3) {
478 $resultdate=DateCalc($planneddate,"2 weeks");
480 if ($subscription->{periodicity} == 4) {
481 $resultdate=DateCalc($planneddate,"3 weeks");
483 if ($subscription->{periodicity} == 5) {
484 $resultdate=DateCalc($planneddate,"1 month");
486 if ($subscription->{periodicity} == 6) {
487 $resultdate=DateCalc($planneddate,"2 months");
489 if ($subscription->{periodicity} == 7) {
490 $resultdate=DateCalc($planneddate,"3 months");
492 if ($subscription->{periodicity} == 8) {
493 $resultdate=DateCalc($planneddate,"3 months");
495 if ($subscription->{periodicity} == 9) {
496 $resultdate=DateCalc($planneddate,"6 months");
498 if ($subscription->{periodicity} == 10) {
499 $resultdate=DateCalc($planneddate,"1 year");
501 if ($subscription->{periodicity} == 11) {
502 $resultdate=DateCalc($planneddate,"2 years");
504 return format_date_in_iso($resultdate);
509 my $calculated = $val->{numberingmethod};
510 my $x=$val->{'lastvalue1'};
511 $calculated =~ s/\{X\}/$x/g;
512 my $y=$val->{'lastvalue2'};
513 $calculated =~ s/\{Y\}/$y/g;
514 my $z=$val->{'lastvalue3'};
515 $calculated =~ s/\{Z\}/$z/g;
521 my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
522 $calculated = $val->{numberingmethod};
523 # calculate the (expected) value of the next issue recieved.
524 $newlastvalue1 = $val->{lastvalue1};
525 # check if we have to increase the new value.
526 $newinnerloop1 = $val->{innerloop1}+1;
527 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
528 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
529 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
530 $calculated =~ s/\{X\}/$newlastvalue1/g;
532 $newlastvalue2 = $val->{lastvalue2};
533 # check if we have to increase the new value.
534 $newinnerloop2 = $val->{innerloop2}+1;
535 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
536 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
537 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
538 $calculated =~ s/\{Y\}/$newlastvalue2/g;
540 $newlastvalue3 = $val->{lastvalue3};
541 # check if we have to increase the new value.
542 $newinnerloop3 = $val->{innerloop3}+1;
543 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
544 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
545 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
546 $calculated =~ s/\{Z\}/$newlastvalue3/g;
547 return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
550 # the subscription has expired when the next issue to arrive is out of subscription limit.
551 sub hassubscriptionexpired {
552 my ($subscriptionid) = @_;
553 my $dbh = C4::Context->dbh;
554 my $subscription = getsubscription($subscriptionid);
555 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
556 if ($subscription->{numberlength}) {
557 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=? and planneddate>=?");
558 $sth->execute($subscriptionid,$subscription->{startdate});
559 my $res = $sth->fetchrow;
560 if ($subscription->{numberlength}>=$res) {
566 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
567 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
568 $sth->execute($subscriptionid);
569 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
570 my $endofsubscriptiondate;
571 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
572 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
573 return 1 if ($res >= $endofsubscriptiondate);
578 sub subscriptionexpirationdate {
579 my ($subscriptionid) = @_;
580 my $dbh = C4::Context->dbh;
581 my $subscription = getsubscription($subscriptionid);
582 my $enddate=$subscription->{startdate};
583 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
584 if ($subscription->{numberlength}) {
585 #calculate the date of the last issue.
586 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
587 $enddate = Get_Next_Date($enddate,$subscription);
590 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
591 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
593 # $enddate=format_date_in_iso($enddate);
594 # warn "END : $enddate";
598 sub subscriptionrenew {
599 my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
600 my $dbh = C4::Context->dbh;
601 my $subscription = getsubscription($subscriptionid);
602 my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
603 $sth->execute($subscription->{biblionumber});
604 my $biblio = $sth->fetchrow_hashref;
605 newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
607 $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
608 $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
610 END { } # module clean-up code here (global destructor)