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
58 sub getSupplierListWithLateIssues {
59 my $dbh = C4::Context->dbh;
60 my $sth = $dbh->prepare("SELECT DISTINCT id, name
61 FROM subscription, serial
62 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
63 WHERE subscription.subscriptionid = serial.subscriptionid AND
64 (planneddate < now( ) OR serial.STATUS = 3)
68 while (my ($id,$name) = $sth->fetchrow) {
69 $supplierlist{$id} = $name;
74 my ($supplierid) = @_;
75 my $dbh = C4::Context->dbh;
78 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
79 FROM subscription, serial, biblio
80 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
81 WHERE subscription.subscriptionid = serial.subscriptionid AND
82 ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
83 subscription.aqbooksellerid=$supplierid and
84 biblio.biblionumber = subscription.biblionumber order by title
87 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
88 FROM subscription, serial, biblio
89 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
90 WHERE subscription.subscriptionid = serial.subscriptionid AND
91 ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
92 biblio.biblionumber = subscription.biblionumber order by title
99 while (my $line = $sth->fetchrow_hashref) {
100 $odd++ unless $line->{title} eq $last_title;
101 $line->{title} = "" if $line->{title} eq $last_title;
102 $last_title = $line->{title} if ($line->{title});
103 $line->{planneddate} = format_date($line->{planneddate});
104 $line->{'odd'} = 1 if $odd %2 ;
105 push @issuelist,$line;
110 sub newsubscription {
111 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
112 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
113 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
114 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
115 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
116 $numberingmethod, $status, $notes,$letter) = @_;
117 my $dbh = C4::Context->dbh;
119 my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
120 startdate,periodicity,dow,numberlength,weeklength,monthlength,
121 add1,every1,whenmorethan1,setto1,lastvalue1,
122 add2,every2,whenmorethan2,setto2,lastvalue2,
123 add3,every3,whenmorethan3,setto3,lastvalue3,
124 numberingmethod, status, notes, letter) values
126 ?,?,?,?,?,?,?,?,?,?,
127 ?,?,?,?,?,?,?,?,?,?,?)");
128 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
129 format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
130 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
131 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
132 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
133 $numberingmethod, $status, $notes,$letter);
134 #then create the 1st waited number
135 my $subscriptionid = $dbh->{'mysql_insertid'};
136 $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
137 $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
138 # reread subscription to get a hash (for calculation of the 1st issue number)
139 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
140 $sth->execute($subscriptionid);
141 my $val = $sth->fetchrow_hashref;
143 # calculate issue number
144 my $serialseq = Get_Seq($val);
145 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
146 $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
147 return $subscriptionid;
150 sub getsubscription {
151 my ($subscriptionid) = @_;
152 my $dbh = C4::Context->dbh;
153 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
155 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
156 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
157 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
158 left join biblio on biblio.biblionumber=subscription.biblionumber
159 where subscription.subscriptionid = ?');
160 $sth->execute($subscriptionid);
161 my $subs = $sth->fetchrow_hashref;
165 sub getsubscriptionfrombiblionumber {
166 my ($biblionumber) = @_;
167 my $dbh = C4::Context->dbh;
168 my $sth = $dbh->prepare('select count(*) from subscription where biblionumber=?');
169 $sth->execute($biblionumber);
170 my $subscriptionsnumber = $sth->fetchrow;
171 return $subscriptionsnumber;
174 sub get_subscription_list_from_biblionumber {
175 my ($biblionumber) = @_;
176 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*, aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
179 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
180 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
181 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
182 left join biblio on biblio.biblionumber=subscription.biblionumber
183 where subscription.biblionumber = ?');
184 $sth->execute($biblionumber);
186 while (my $subs = $sth->fetchrow_hashref) {
187 $subs->{startdate} = format_date($subs->{startdate});
188 $subs->{histstartdate} = format_date($subs->{histstartdate});
189 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
190 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
191 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
192 $subs->{"periodicity".$subs->{periodicity}} = 1;
193 $subs->{"status".$subs->{'status'}} = 1;
194 if ($subs->{enddate} eq '0000-00-00') {
197 $subs->{enddate} = format_date($subs->{enddate});
204 sub get_full_subscription_list_from_biblionumber {
205 my ($biblionumber) = @_;
206 my $dbh = C4::Context->dbh;
207 my $sth = $dbh->prepare('select serial.serialseq, serial.planneddate, serial.status, serial.notes, year(serial.planneddate) as year, aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
208 from serial left join subscription on (serial.subscriptionid=subscription.subscriptionid and subscription.biblionumber=serial.biblionumber)
209 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
210 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
211 left join biblio on biblio.biblionumber=subscription.biblionumber
212 where subscription.biblionumber = ? order by year,serial.subscriptionid,serial.planneddate');
213 $sth->execute($biblionumber);
217 my $aqbooksellername;
222 while (my $subs = $sth->fetchrow_hashref) {
223 # my $sth2 = $dbh->prepare('select * from serial where serial.biblionumber = ? and serial.subscriptionid=? order by serial.planneddate');
224 # $sth2->execute($biblionumber,$subs->{'subscriptionid'});
225 # while (my $issues = $sth2->fetchrow_hashref){
226 # warn "planneddate ".$issues->{'planneddate'};
227 # warn "serialseq".$issues->{'serialseq'};
229 if ($year and ($year==$subs->{year})){
230 if ($first eq 1){$first=0;}
231 my $temp=$res[scalar(@res)-1]->{'serials'};
233 {'planneddate' => format_date($subs->{'planneddate'}),
234 'serialseq' => $subs->{'serialseq'},
235 "status".$subs->{'status'} => 1,
236 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
239 $first=1 if (not $year);
240 $year= $subs->{'year'};
241 $startdate= format_date($subs->{'startdate'});
242 $aqbooksellername= $subs->{'aqbooksellername'};
243 $bibliotitle= $subs->{'bibliotitle'};
246 {'planneddate' => format_date($subs->{'planneddate'}),
247 'serialseq' => $subs->{'serialseq'},
248 "status".$subs->{'status'} => 1,
249 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
254 'startdate'=>$startdate,
255 'aqbooksellername'=>$aqbooksellername,
256 'bibliotitle'=>$bibliotitle,
261 $previousnote=$subs->{notes};
267 sub modsubscription {
268 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
269 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
270 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
271 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
272 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
273 $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid)= @_;
274 my $dbh = C4::Context->dbh;
275 my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
276 periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
277 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
278 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
279 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
280 numberingmethod=?, status=?, biblionumber=?, notes=?, letter=? where subscriptionid = ?");
281 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
282 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
283 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
284 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
285 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
286 $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid);
291 sub delsubscription {
292 my ($subscriptionid) = @_;
293 my $dbh = C4::Context->dbh;
294 $subscriptionid=$dbh->quote($subscriptionid);
295 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
296 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
297 $dbh->do("delete from serial where subscriptionid=$subscriptionid");
299 sub getsubscriptions {
300 my ($title,$ISSN,$biblionumber) = @_;
301 return unless $title or $ISSN or $biblionumber;
302 my $dbh = C4::Context->dbh;
305 $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=? order by title");
306 $sth->execute($biblionumber);
308 if ($ISSN and $title)
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.title like ? or biblioitems.issn = ? order by title )");
311 $sth->execute("%$title%",$ISSN);
317 $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 = ? order by title");
318 $sth->execute($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
323 biblio.biblionumber=subscription.biblionumber and biblio.title like ? order by title");
324 $sth->execute("%$title%");
329 my $previoustitle="";
331 while (my $line = $sth->fetchrow_hashref) {
332 if ($previoustitle eq $line->{title}) {
335 $line->{toggle} = 1 if $odd==1;
337 $previoustitle=$line->{title};
339 $line->{toggle} = 1 if $odd==1;
341 push @results, $line;
346 sub modsubscriptionhistory {
347 my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
348 my $dbh=C4::Context->dbh;
349 my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
350 $recievedlist =~ s/^,//g;
351 $missinglist =~ s/^,//g;
352 $opacnote =~ s/^,//g;
353 $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 # OK, now add the last 5 issues arrives/missing
363 my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate,notes from serial where subscriptionid = ? and (status in (2,4,5)) order by serialid desc");
364 $sth->execute($subscriptionid);
367 while((my $line = $sth->fetchrow_hashref) && $counter <5) {
369 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
370 $line->{"planneddate"} = format_date($line->{"planneddate"});
374 # status = 2 is "arrived"
375 $sth=$dbh->prepare("select serialid,serialseq, status, planneddate,notes from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5");
376 $sth->execute($subscriptionid);
377 while(my $line = $sth->fetchrow_hashref) {
378 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
379 $line->{"planneddate"} = format_date($line->{"planneddate"});
382 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
383 $sth->execute($subscriptionid);
384 my ($totalissues) = $sth->fetchrow;
385 return ($totalissues,@serials);
388 # get the $limit's latest serials arrived or missing for a given subscription
389 sub getlatestserials{
390 my ($subscriptionid,$limit) =@_;
391 my $dbh = C4::Context->dbh;
392 # status = 2 is "arrived"
393 my $strsth="select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and (status =2 or status=4) order by planneddate DESC LIMIT 0,$limit";
394 my $sth=$dbh->prepare($strsth);
395 $sth->execute($subscriptionid);
397 while(my $line = $sth->fetchrow_hashref) {
398 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
399 $line->{"planneddate"} = format_date($line->{"planneddate"});
402 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
403 $sth->execute($subscriptionid);
404 my ($totalissues) = $sth->fetchrow;
408 sub serialchangestatus {
409 my ($serialid,$serialseq,$planneddate,$status,$notes)=@_;
410 # warn "($serialid,$serialseq,$planneddate,$status)";
411 # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
412 my $dbh = C4::Context->dbh;
413 my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
414 $sth->execute($serialid);
415 my ($subscriptionid,$oldstatus) = $sth->fetchrow;
416 # change status & update subscriptionhistory
418 delissue($serialseq, $subscriptionid)
420 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=?,notes=? where serialid = ?");
421 $sth->execute($serialseq,$planneddate,$status,$notes,$serialid);
422 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
423 $sth->execute($subscriptionid);
424 my ($missinglist,$recievedlist) = $sth->fetchrow;
426 $recievedlist .= ",$serialseq";
428 $missinglist .= ",$serialseq" if ($status eq 4) ;
429 $missinglist .= ",not issued $serialseq" if ($status eq 5);
430 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
431 $sth->execute($recievedlist,$missinglist,$subscriptionid);
433 # create new waited entry if needed (ie : was a "waited" and has changed)
434 if ($oldstatus eq 1 && $status ne 1) {
435 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
436 $sth->execute($subscriptionid);
437 my $val = $sth->fetchrow_hashref;
439 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
440 # next date (calculated from actual date & frequency parameters)
441 my $nextplanneddate = Get_Next_Date($planneddate,$val);
442 newissue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
443 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
444 innerloop1=?,innerloop2=?,innerloop3=?
445 where subscriptionid = ?");
446 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
451 my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
452 my $dbh = C4::Context->dbh;
453 my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
454 $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
455 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
456 $sth->execute($subscriptionid);
457 my ($missinglist,$recievedlist) = $sth->fetchrow;
459 $recievedlist .= ",$serialseq";
462 $missinglist .= ",$serialseq";
464 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
465 $sth->execute($recievedlist,$missinglist,$subscriptionid);
469 my ($serialseq,$subscriptionid) = @_;
470 my $dbh = C4::Context->dbh;
471 my $sth = $dbh->prepare("delete from serial where serialseq= ? and subscriptionid= ? ");
472 $sth->execute($serialseq,$subscriptionid);
475 sub Get_Next_Date(@) {
476 my ($planneddate,$subscription) = @_;
478 if ($subscription->{periodicity} == 1) {
479 $resultdate=DateCalc($planneddate,"1 day");
481 if ($subscription->{periodicity} == 2) {
482 $resultdate=DateCalc($planneddate,"1 week");
484 if ($subscription->{periodicity} == 3) {
485 $resultdate=DateCalc($planneddate,"2 weeks");
487 if ($subscription->{periodicity} == 4) {
488 $resultdate=DateCalc($planneddate,"3 weeks");
490 if ($subscription->{periodicity} == 5) {
491 $resultdate=DateCalc($planneddate,"1 month");
493 if ($subscription->{periodicity} == 6) {
494 $resultdate=DateCalc($planneddate,"2 months");
496 if ($subscription->{periodicity} == 7) {
497 $resultdate=DateCalc($planneddate,"3 months");
499 if ($subscription->{periodicity} == 8) {
500 $resultdate=DateCalc($planneddate,"3 months");
502 if ($subscription->{periodicity} == 9) {
503 $resultdate=DateCalc($planneddate,"6 months");
505 if ($subscription->{periodicity} == 10) {
506 $resultdate=DateCalc($planneddate,"1 year");
508 if ($subscription->{periodicity} == 11) {
509 $resultdate=DateCalc($planneddate,"2 years");
511 return format_date_in_iso($resultdate);
516 my $calculated = $val->{numberingmethod};
517 my $x=$val->{'lastvalue1'};
518 $calculated =~ s/\{X\}/$x/g;
519 my $y=$val->{'lastvalue2'};
520 $calculated =~ s/\{Y\}/$y/g;
521 my $z=$val->{'lastvalue3'};
522 $calculated =~ s/\{Z\}/$z/g;
528 my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
529 $calculated = $val->{numberingmethod};
530 # calculate the (expected) value of the next issue recieved.
531 $newlastvalue1 = $val->{lastvalue1};
532 # check if we have to increase the new value.
533 $newinnerloop1 = $val->{innerloop1}+1;
534 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
535 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
536 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
537 $calculated =~ s/\{X\}/$newlastvalue1/g;
539 $newlastvalue2 = $val->{lastvalue2};
540 # check if we have to increase the new value.
541 $newinnerloop2 = $val->{innerloop2}+1;
542 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
543 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
544 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
545 $calculated =~ s/\{Y\}/$newlastvalue2/g;
547 $newlastvalue3 = $val->{lastvalue3};
548 # check if we have to increase the new value.
549 $newinnerloop3 = $val->{innerloop3}+1;
550 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
551 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
552 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
553 $calculated =~ s/\{Z\}/$newlastvalue3/g;
554 return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
557 # the subscription has expired when the next issue to arrive is out of subscription limit.
558 sub hassubscriptionexpired {
559 my ($subscriptionid) = @_;
560 my $dbh = C4::Context->dbh;
561 my $subscription = getsubscription($subscriptionid);
562 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
563 if ($subscription->{numberlength}) {
564 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=? and planneddate>=?");
565 $sth->execute($subscriptionid,$subscription->{startdate});
566 my $res = $sth->fetchrow;
567 if ($subscription->{numberlength}>=$res) {
573 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
574 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
575 $sth->execute($subscriptionid);
576 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
577 my $endofsubscriptiondate;
578 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
579 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
580 return 1 if ($res >= $endofsubscriptiondate);
585 sub subscriptionexpirationdate {
586 my ($subscriptionid) = @_;
587 my $dbh = C4::Context->dbh;
588 my $subscription = getsubscription($subscriptionid);
589 my $enddate=$subscription->{startdate};
590 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
591 if ($subscription->{numberlength}) {
592 #calculate the date of the last issue.
593 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
594 $enddate = Get_Next_Date($enddate,$subscription);
597 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
598 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
600 # $enddate=format_date_in_iso($enddate);
601 # warn "END : $enddate";
605 sub subscriptionrenew {
606 my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
607 my $dbh = C4::Context->dbh;
608 my $subscription = getsubscription($subscriptionid);
609 my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
610 $sth->execute($subscription->{biblionumber});
611 my $biblio = $sth->fetchrow_hashref;
612 newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
614 $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
615 $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
617 END { } # module clean-up code here (global destructor)