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
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
34 C4::Bull - Give functions for serializing.
42 Give all XYZ functions
47 @EXPORT = qw(&newsubscription &modsubscription &delsubscription &getsubscriptions &getsubscription
48 &getsubscriptionfrombiblionumber &get_subscription_list_from_biblionumber
49 &get_full_subscription_list_from_biblionumber
50 &modsubscriptionhistory &newissue
51 &getserials &serialchangestatus
52 &Find_Next_Date, &Get_Next_Seq
53 &hassubscriptionexpired &subscriptionexpirationdate &subscriptionrenew
54 &getSupplierListWithLateIssues &GetLateIssues &serialdelete &getlatestserials);
56 sub getSupplierListWithLateIssues {
57 my $dbh = C4::Context->dbh;
58 my $sth = $dbh->prepare("SELECT DISTINCT id, name
59 FROM subscription, serial
60 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
61 WHERE subscription.subscriptionid = serial.subscriptionid AND
62 (planneddate < now( ) OR serial.STATUS = 3)
66 while (my ($id,$name) = $sth->fetchrow) {
67 $supplierlist{$id} = $name;
72 my ($supplierid) = @_;
73 my $dbh = C4::Context->dbh;
76 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
77 FROM subscription, serial, biblio
78 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
79 WHERE subscription.subscriptionid = serial.subscriptionid AND
80 ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
81 subscription.aqbooksellerid=$supplierid and
82 biblio.biblionumber = subscription.biblionumber
85 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
86 FROM subscription, serial, biblio
87 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
88 WHERE subscription.subscriptionid = serial.subscriptionid AND
89 ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
90 biblio.biblionumber = subscription.biblionumber
96 while (my $line = $sth->fetchrow_hashref) {
97 $line->{title} = "" if $line->{title} eq $last_title;
98 $last_title = $line->{title} if ($line->{title});
99 $line->{planneddate} = format_date($line->{planneddate});
100 push @issuelist,$line;
105 sub newsubscription {
106 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
107 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
108 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
109 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
110 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
111 $numberingmethod, $status, $notes) = @_;
112 my $dbh = C4::Context->dbh;
114 my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
115 startdate,periodicity,dow,numberlength,weeklength,monthlength,
116 add1,every1,whenmorethan1,setto1,lastvalue1,
117 add2,every2,whenmorethan2,setto2,lastvalue2,
118 add3,every3,whenmorethan3,setto3,lastvalue3,
119 numberingmethod, status, notes) values
121 ?,?,?,?,?,?,?,?,?,?,
122 ?,?,?,?,?,?,?,?,?,?)");
123 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
124 format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
125 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
126 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
127 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
128 $numberingmethod, $status, $notes);
129 #then create the 1st waited number
130 my $subscriptionid = $dbh->{'mysql_insertid'};
131 $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
132 $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
133 # reread subscription to get a hash (for calculation of the 1st issue number)
134 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
135 $sth->execute($subscriptionid);
136 my $val = $sth->fetchrow_hashref;
138 # calculate issue number
139 my $serialseq = Get_Seq($val);
140 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
141 $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
142 return $subscriptionid;
145 sub getsubscription {
146 my ($subscriptionid) = @_;
147 my $dbh = C4::Context->dbh;
148 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
150 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
151 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
152 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
153 left join biblio on biblio.biblionumber=subscription.biblionumber
154 where subscription.subscriptionid = ?');
155 $sth->execute($subscriptionid);
156 my $subs = $sth->fetchrow_hashref;
160 sub getsubscriptionfrombiblionumber {
161 my ($biblionumber) = @_;
162 my $dbh = C4::Context->dbh;
163 my $sth = $dbh->prepare('select count(*) from subscription where biblionumber=?');
164 $sth->execute($biblionumber);
165 my $subscriptionsnumber = $sth->fetchrow;
166 return $subscriptionsnumber;
169 sub get_subscription_list_from_biblionumber {
170 my ($biblionumber) = @_;
171 my $dbh = C4::Context->dbh;
172 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*, aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
174 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
175 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
176 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
177 left join biblio on biblio.biblionumber=subscription.biblionumber
178 where subscription.biblionumber = ?');
179 $sth->execute($biblionumber);
181 while (my $subs = $sth->fetchrow_hashref) {
182 $subs->{startdate} = format_date($subs->{startdate});
183 $subs->{histstartdate} = format_date($subs->{histstartdate});
184 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
185 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
186 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
187 $subs->{"periodicity".$subs->{periodicity}} = 1;
188 if ($subs->{enddate} eq '0000-00-00') {
191 $subs->{enddate} = format_date($subs->{enddate});
198 sub get_full_subscription_list_from_biblionumber {
199 my ($biblionumber) = @_;
200 my $dbh = C4::Context->dbh;
201 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
202 from serial left join subscription on (serial.subscriptionid=subscription.subscriptionid and subscription.biblionumber=serial.biblionumber)
203 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
204 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
205 left join biblio on biblio.biblionumber=subscription.biblionumber
206 where subscription.biblionumber = ? order by year,serial.planneddate');
207 $sth->execute($biblionumber);
211 my $aqbooksellername;
215 while (my $subs = $sth->fetchrow_hashref) {
216 # my $sth2 = $dbh->prepare('select * from serial where serial.biblionumber = ? and serial.subscriptionid=? order by serial.planneddate');
217 # $sth2->execute($biblionumber,$subs->{'subscriptionid'});
218 # while (my $issues = $sth2->fetchrow_hashref){
219 # warn "planneddate ".$issues->{'planneddate'};
220 # warn "serialseq".$issues->{'serialseq'};
222 if ($year and ($year==$subs->{year})){
223 if ($first eq 1){$first=0;}
224 my $temp=$res[scalar(@res)-1]->{'serials'};
226 {'planneddate' => format_date($subs->{'planneddate'}),
227 'serialseq' => $subs->{'serialseq'},
228 'status1' => $subs->{'status'}==1,
229 'status2' => $subs->{'status'}==2,
230 'status3' => $subs->{'status'}==3,
231 'status4' => $subs->{'status'}==4,
234 $first=1 if (not $year);
235 $year= $subs->{'year'};
236 $startdate= format_date($subs->{'startdate'});
237 $aqbooksellername= $subs->{'aqbooksellername'};
238 $bibliotitle= $subs->{'bibliotitle'};
241 {'planneddate' => format_date($subs->{'planneddate'}),
242 'serialseq' => $subs->{'serialseq'},
243 'status1' => $subs->{'status'}==1,
244 'status2' => $subs->{'status'}==2,
245 'status3' => $subs->{'status'}==3,
246 'status4' => $subs->{'status'}==4,
251 'startdate'=>$startdate,
252 'aqbooksellername'=>$aqbooksellername,
253 'bibliotitle'=>$bibliotitle,
263 sub modsubscription {
264 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
265 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
266 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
267 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
268 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
269 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid)= @_;
270 my $dbh = C4::Context->dbh;
271 my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
272 periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
273 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
274 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
275 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
276 numberingmethod=?, status=?, biblionumber=?, notes=? where subscriptionid = ?");
277 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
278 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
279 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
280 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
281 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
282 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid);
287 sub delsubscription {
288 my ($subscriptionid) = @_;
289 # check again there is only one issue (the subscription is new)
290 my ($totalissues) = getserials($subscriptionid);
291 if ($totalissues eq 1) {
292 my $dbh = C4::Context->dbh;
293 $subscriptionid=$dbh->quote($subscriptionid);
294 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
295 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
296 $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 from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblio.biblionumber=?");
306 $sth->execute($biblionumber);
308 if ($ISSN and $title)
310 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and (biblio.title like ? or biblioitems.issn = ? )");
311 $sth->execute("%$title%",$ISSN);
317 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblioitems.issn = ?");
318 $sth->execute($ISSN);
322 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and
323 biblio.biblionumber=subscription.biblionumber and biblio.title like ? ");
324 $sth->execute("%$title%");
329 while (my $line = $sth->fetchrow_hashref) {
330 push @results, $line;
335 sub modsubscriptionhistory {
336 my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
337 my $dbh=C4::Context->dbh;
338 my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
339 $recievedlist =~ s/^,//g;
340 $missinglist =~ s/^,//g;
341 $opacnote =~ s/^,//g;
342 $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
344 # get every serial not arrived for a given subscription
345 # as well as the number of issues registered in the database (all types)
346 # this number is used to see if a subscription can be deleted (=it must have only 1 issue)
348 my ($subscriptionid) = @_;
349 my $dbh = C4::Context->dbh;
350 # status = 2 is "arrived"
351 my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5");
352 $sth->execute($subscriptionid);
354 while(my $line = $sth->fetchrow_hashref) {
355 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
356 $line->{"planneddate"} = format_date($line->{"planneddate"});
359 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
360 $sth->execute($subscriptionid);
361 my ($totalissues) = $sth->fetchrow;
362 return ($totalissues,@serials);
364 sub getlatestserials{
365 my ($subscriptionid,$limit) =@_;
366 my $dbh = C4::Context->dbh;
367 # status = 2 is "arrived"
368 my $strsth="select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and (status =2 or status=4) order by planneddate DESC LIMIT 0,$limit";
369 my $sth=$dbh->prepare($strsth);
370 $sth->execute($subscriptionid);
372 while(my $line = $sth->fetchrow_hashref) {
373 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
374 $line->{"planneddate"} = format_date($line->{"planneddate"});
377 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
378 $sth->execute($subscriptionid);
379 my ($totalissues) = $sth->fetchrow;
380 return ($totalissues,@serials);
383 sub serialchangestatus {
384 my ($serialid,$serialseq,$planneddate,$status)=@_;
385 # warn "($serialid,$serialseq,$planneddate,$status)";
386 # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
387 my $dbh = C4::Context->dbh;
388 my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
389 $sth->execute($serialid);
390 my ($subscriptionid,$oldstatus) = $sth->fetchrow;
391 # change status & update subscriptionhistory
393 delissue($serialseq, $subscriptionid);
395 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
396 $sth->execute($serialseq,$planneddate,$status,$serialid);
397 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
398 $sth->execute($subscriptionid);
399 my ($missinglist,$recievedlist) = $sth->fetchrow;
401 $recievedlist .= ",$serialseq";
403 $missinglist .= ",$serialseq" if ($status eq 4) ;
404 $missinglist .= ",not issued $serialseq" if ($status eq 5);
405 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
406 $sth->execute($recievedlist,$missinglist,$subscriptionid);
409 # create new waited entry if needed (ie : was a "waited" and has changed)
410 if ($oldstatus eq 1 && $status ne 1) {
411 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
412 $sth->execute($subscriptionid);
413 my $val = $sth->fetchrow_hashref;
415 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
416 # next date (calculated from actual date & frequency parameters)
417 my $nextplanneddate = Get_Next_Date($planneddate,$val);
418 newissue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
419 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
420 innerloop1=?,innerloop2=?,innerloop3=?
421 where subscriptionid = ?");
422 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
427 my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
428 my $dbh = C4::Context->dbh;
429 my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
430 $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
431 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
432 $sth->execute($subscriptionid);
433 my ($missinglist,$recievedlist) = $sth->fetchrow;
435 $recievedlist .= ",$serialseq";
438 $missinglist .= ",$serialseq";
440 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
441 $sth->execute($recievedlist,$missinglist,$subscriptionid);
444 sub Get_Next_Date(@) {
445 my ($planneddate,$subscription) = @_;
447 if ($subscription->{periodicity} == 1) {
448 $resultdate=DateCalc($planneddate,"1 day");
450 if ($subscription->{periodicity} == 2) {
451 $resultdate=DateCalc($planneddate,"1 week");
453 if ($subscription->{periodicity} == 3) {
454 $resultdate=DateCalc($planneddate,"2 weeks");
456 if ($subscription->{periodicity} == 4) {
457 $resultdate=DateCalc($planneddate,"3 weeks");
459 if ($subscription->{periodicity} == 5) {
460 $resultdate=DateCalc($planneddate,"1 month");
462 if ($subscription->{periodicity} == 6) {
463 $resultdate=DateCalc($planneddate,"2 months");
465 if ($subscription->{periodicity} == 7) {
466 $resultdate=DateCalc($planneddate,"3 months");
468 if ($subscription->{periodicity} == 8) {
469 $resultdate=DateCalc($planneddate,"3 months");
471 if ($subscription->{periodicity} == 9) {
472 $resultdate=DateCalc($planneddate,"6 months");
474 if ($subscription->{periodicity} == 10) {
475 $resultdate=DateCalc($planneddate,"1 year");
477 if ($subscription->{periodicity} == 11) {
478 $resultdate=DateCalc($planneddate,"2 years");
480 return format_date_in_iso($resultdate);
485 my $calculated = $val->{numberingmethod};
486 my $x=$val->{'lastvalue1'};
487 $calculated =~ s/\{X\}/$x/g;
488 my $y=$val->{'lastvalue2'};
489 $calculated =~ s/\{Y\}/$y/g;
490 my $z=$val->{'lastvalue3'};
491 $calculated =~ s/\{Z\}/$z/g;
497 my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
498 $calculated = $val->{numberingmethod};
499 # calculate the (expected) value of the next issue recieved.
500 $newlastvalue1 = $val->{lastvalue1};
501 # check if we have to increase the new value.
502 $newinnerloop1 = $val->{innerloop1}+1;
503 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
504 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
505 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
506 $calculated =~ s/\{X\}/$newlastvalue1/g;
508 $newlastvalue2 = $val->{lastvalue2};
509 # check if we have to increase the new value.
510 $newinnerloop2 = $val->{innerloop2}+1;
511 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
512 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
513 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
514 $calculated =~ s/\{Y\}/$newlastvalue2/g;
516 $newlastvalue3 = $val->{lastvalue3};
517 # check if we have to increase the new value.
518 $newinnerloop3 = $val->{innerloop3}+1;
519 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
520 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
521 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
522 $calculated =~ s/\{Z\}/$newlastvalue3/g;
523 return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
526 # the subscription has expired when the next issue to arrive is out of subscription limit.
527 sub hassubscriptionexpired {
528 my ($subscriptionid) = @_;
529 my $dbh = C4::Context->dbh;
530 my $subscription = getsubscription($subscriptionid);
531 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
532 if ($subscription->{numberlength}) {
533 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=? and planneddate>=?");
534 $sth->execute($subscriptionid,$subscription->{startdate});
535 my $res = $sth->fetchrow;
536 if ($subscription->{numberlength}>=$res) {
542 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
543 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
544 $sth->execute($subscriptionid);
545 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
546 my $endofsubscriptiondate;
547 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
548 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
549 return 1 if ($res >= $endofsubscriptiondate);
554 sub subscriptionexpirationdate {
555 my ($subscriptionid) = @_;
556 my $dbh = C4::Context->dbh;
557 my $subscription = getsubscription($subscriptionid);
558 my $enddate=$subscription->{startdate};
559 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
560 if ($subscription->{numberlength}) {
561 #calculate the date of the last issue.
562 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
563 $enddate = Get_Next_Date($enddate,$subscription);
566 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
567 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
569 # $enddate=format_date_in_iso($enddate);
570 # warn "END : $enddate";
574 sub subscriptionrenew {
575 my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
576 my $dbh = C4::Context->dbh;
577 my $subscription = getsubscription($subscriptionid);
578 my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
579 $sth->execute($subscription->{biblionumber});
580 my $biblio = $sth->fetchrow_hashref;
581 newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
583 $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
584 $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
586 END { } # module clean-up code here (global destructor)