Detailing the three latest issues on OPAC for serials
[koha-ffzg.git] / C4 / Bull.pm
1 package C4::Bull; #assumes C4/Bull.pm
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 use C4::Date;
23 use Date::Manip;
24 use C4::Suggestions;
25 require Exporter;
26
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Bull - Give functions for serializing.
35
36 =head1 SYNOPSIS
37
38   use C4::Bull;
39
40 =head1 DESCRIPTION
41
42 Give all XYZ functions
43
44 =cut
45
46 @ISA = qw(Exporter);
47 @EXPORT = qw(&newsubscription &modsubscription &delsubscription &getsubscriptions &getsubscription 
48                         &getsubscriptionfrombiblionumber &get_subscription_list_from_biblionumber
49                         &modsubscriptionhistory &newissue
50                         &getserials &serialchangestatus
51                         &Find_Next_Date, &Get_Next_Seq
52                         &hassubscriptionexpired &subscriptionexpirationdate &subscriptionrenew
53                         &getSupplierListWithLateIssues &GetLateIssues &serialdelete &getlatestserials);
54
55 sub getSupplierListWithLateIssues {
56         my $dbh = C4::Context->dbh;
57         my $sth = $dbh->prepare("SELECT DISTINCT id, name
58                                                         FROM subscription, serial
59                                                         LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
60                                                         WHERE subscription.subscriptionid = serial.subscriptionid AND
61                                                         (planneddate < now( ) OR serial.STATUS = 3)
62                                                         ");
63         $sth->execute;
64         my %supplierlist;
65         while (my ($id,$name) = $sth->fetchrow) {
66                 $supplierlist{$id} = $name;
67         }
68         return %supplierlist;
69 }
70 sub GetLateIssues {
71         my ($supplierid) = @_;
72         my $dbh = C4::Context->dbh;
73         my $sth;
74         if ($supplierid) {
75                 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
76                                                         FROM subscription, serial, biblio
77                                                         LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
78                                                         WHERE subscription.subscriptionid = serial.subscriptionid AND
79                                                         ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
80                                                         subscription.aqbooksellerid=$supplierid and
81                                                         biblio.biblionumber = subscription.biblionumber
82                                                         ");
83         } else {
84                 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
85                                                         FROM subscription, serial, biblio
86                                                         LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
87                                                         WHERE subscription.subscriptionid = serial.subscriptionid AND
88                                                         ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
89                                                         biblio.biblionumber = subscription.biblionumber
90                                                         ");
91         }
92         $sth->execute;
93         my @issuelist;
94         my $last_title;
95         while (my $line = $sth->fetchrow_hashref) {
96                 $line->{title} = "" if $line->{title} eq $last_title;
97                 $last_title = $line->{title} if ($line->{title});
98                 $line->{planneddate} = format_date($line->{planneddate});
99                 push @issuelist,$line;
100         }
101         return @issuelist;
102 }
103 sub newsubscription {
104         my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
105                 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
106                 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
107                 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
108                 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
109                 $numberingmethod, $status, $notes) = @_;
110         my $dbh = C4::Context->dbh;
111         #save subscription
112         my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
113                                                         startdate,periodicity,dow,numberlength,weeklength,monthlength,
114                                                         add1,every1,whenmorethan1,setto1,lastvalue1,
115                                                         add2,every2,whenmorethan2,setto2,lastvalue2,
116                                                         add3,every3,whenmorethan3,setto3,lastvalue3,
117                                                         numberingmethod, status, notes) values 
118                                                         (?,?,?,?,?,?,?,?,?,
119                                                          ?,?,?,?,?,?,?,?,?,?,
120                                                          ?,?,?,?,?,?,?,?,?,?)");
121         $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
122                                         format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
123                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
124                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
125                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
126                                         $numberingmethod, $status, $notes);
127         #then create the 1st waited number
128         my $subscriptionid = $dbh->{'mysql_insertid'};
129         $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
130         $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
131         # reread subscription to get a hash (for calculation of the 1st issue number)
132         $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
133         $sth->execute($subscriptionid);
134         my $val = $sth->fetchrow_hashref;
135
136         # calculate issue number
137         my $serialseq = Get_Seq($val);
138         $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
139         $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
140         return $subscriptionid;
141 }
142 sub getsubscription {
143         my ($subscriptionid) = @_;
144         my $dbh = C4::Context->dbh;
145         my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle 
146                                                         from subscription 
147                                                         left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
148                                                         left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid 
149                                                         left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
150                                                         left join biblio on biblio.biblionumber=subscription.biblionumber 
151                                                         where subscription.subscriptionid = ?');
152         $sth->execute($subscriptionid);
153         my $subs = $sth->fetchrow_hashref;
154         return $subs;
155 }
156
157 sub getsubscriptionfrombiblionumber {
158         my ($biblionumber) = @_;
159         my $dbh = C4::Context->dbh;
160         my $sth = $dbh->prepare('select count(*) from subscription where biblionumber=?');
161         $sth->execute($biblionumber);
162         my $subscriptionsnumber = $sth->fetchrow;
163         return $subscriptionsnumber;
164 }
165
166 sub get_subscription_list_from_biblionumber {
167         my ($biblionumber) = @_;
168         my $dbh = C4::Context->dbh;
169         my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle 
170                                                         from subscription 
171                                                         left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
172                                                         left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid 
173                                                         left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
174                                                         left join biblio on biblio.biblionumber=subscription.biblionumber 
175                                                         where subscription.biblionumber = ?');
176         $sth->execute($biblionumber);
177         my @res;
178         while (my $subs = $sth->fetchrow_hashref) {
179                 $subs->{startdate} = format_date($subs->{startdate});
180                 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
181                 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
182                 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
183                 push @res,$subs;
184         }
185         return \@res;
186 }
187
188
189 sub modsubscription {
190         my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
191                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
192                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
193                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
194                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
195                                         $numberingmethod, $status, $biblionumber, $notes, $subscriptionid)= @_;
196         my $dbh = C4::Context->dbh;
197         my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
198                                                  periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
199                                                 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
200                                                 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
201                                                 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
202                                                 numberingmethod=?, status=?, biblionumber=?, notes=? where subscriptionid = ?");
203         $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
204                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
205                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
206                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
207                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
208                                         $numberingmethod, $status, $biblionumber, $notes, $subscriptionid);
209         $sth->finish;
210
211 }
212
213 sub delsubscription {
214         my ($subscriptionid) = @_;
215         # check again there is only one issue (the subscription is new)
216         my ($totalissues) = getserials($subscriptionid);
217         if ($totalissues eq 1) {
218                 my $dbh = C4::Context->dbh;
219                 $subscriptionid=$dbh->quote($subscriptionid);
220                 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
221                 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
222                 $dbh->do("delete from serial where subscriptionid=$subscriptionid");
223         }
224 }
225 sub getsubscriptions {
226         my ($title,$ISSN,$biblionumber) = @_;
227         return unless $title or $ISSN or $biblionumber;
228         my $dbh = C4::Context->dbh;
229         my $sth;
230         if ($biblionumber) {
231                 $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=?");
232                 $sth->execute($biblionumber);
233         } else {
234                 if ($ISSN and $title)
235                 {
236                         $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 = ? )");
237                         $sth->execute("%$title%",$ISSN);
238                 }
239                 else
240                 {
241                         if ($ISSN)
242                         {
243                                 $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 = ?");
244                                 $sth->execute($ISSN);
245                         }
246                         else
247                         {
248                                 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where  biblio.biblionumber = biblioitems.biblionumber and
249  biblio.biblionumber=subscription.biblionumber and biblio.title like ? ");
250                                 $sth->execute("%$title%");
251                         }
252                 }
253         }
254                 my @results;
255         while (my $line = $sth->fetchrow_hashref) {
256                 push @results, $line;
257         }
258         return @results;
259 }
260
261 sub modsubscriptionhistory {
262         my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
263         my $dbh=C4::Context->dbh;
264         my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
265         $recievedlist =~ s/^,//g;
266         $missinglist =~ s/^,//g;
267         $opacnote =~ s/^,//g;
268         $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
269 }
270 # get every serial not arrived for a given subscription
271 # as well as the number of issues registered in the database (all types)
272 # this number is used to see if a subscription can be deleted (=it must have only 1 issue)
273 sub getserials {
274         my ($subscriptionid) = @_;
275         my $dbh = C4::Context->dbh;
276         # status = 2 is "arrived"
277         my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5");
278         $sth->execute($subscriptionid);
279         my @serials;
280         while(my $line = $sth->fetchrow_hashref) {
281                 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
282                 $line->{"planneddate"} = format_date($line->{"planneddate"});
283                 push @serials,$line;
284         }
285         $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
286         $sth->execute($subscriptionid);
287         my ($totalissues) = $sth->fetchrow;
288         return ($totalissues,@serials);
289 }
290 sub getlatestserials{
291         my ($subscriptionid,$limit) =@_;
292         my $dbh = C4::Context->dbh;
293         # status = 2 is "arrived"
294         my $strsth="select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and (status =2 or status=4) order by planneddate DESC LIMIT 0,$limit";
295         my $sth=$dbh->prepare($strsth);
296         $sth->execute($subscriptionid);
297         my @serials;
298         while(my $line = $sth->fetchrow_hashref) {
299                 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
300                 $line->{"planneddate"} = format_date($line->{"planneddate"});
301                 push @serials,$line;
302         }
303         return \@serials;
304 }
305
306 sub serialdelete {
307         my ($serialid,$serialseq)=@_;
308         my $dbh = C4::Context->dbh;
309         my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
310         $sth->execute($serialid);
311         my ($subscriptionid,$oldstatus) = $sth->fetchrow;
312         # change status & update subscriptionhistory
313         my $sth = $dbh->prepare("delete * from serial  where serialid = ?");
314         $sth->execute($serialid);
315         $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
316         $sth->execute($subscriptionid);
317         my ($missinglist,$recievedlist) = $sth->fetchrow;
318         $recievedlist =~ s/",$serialseq"//;
319         $missinglist =~ s/",$serialseq"//;
320         $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
321         $sth->execute($recievedlist,$missinglist,$subscriptionid);
322 }
323
324 sub serialchangestatus {
325         my ($serialid,$serialseq,$planneddate,$status)=@_;
326 #       warn "($serialid,$serialseq,$planneddate,$status)";
327         # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
328         my $dbh = C4::Context->dbh;
329         my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
330         $sth->execute($serialid);
331         my ($subscriptionid,$oldstatus) = $sth->fetchrow;
332         # change status & update subscriptionhistory
333         if ($status eq 6){
334                 delissue($serialseq, $subscriptionid); 
335         }else{
336                 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
337                 $sth->execute($serialseq,$planneddate,$status,$serialid);
338                 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
339                 $sth->execute($subscriptionid);
340                 my ($missinglist,$recievedlist) = $sth->fetchrow;
341                 if ($status eq 2) {
342                         $recievedlist .= ",$serialseq";
343                 }
344                 $missinglist .= ",$serialseq" if ($status eq 4) ;
345                 $missinglist .= ",not issued $serialseq" if ($status eq 5);
346                 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
347                 $sth->execute($recievedlist,$missinglist,$subscriptionid);
348         }
349
350         # create new waited entry if needed (ie : was a "waited" and has changed)
351         if ($oldstatus eq 1 && $status ne 1) {
352                 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
353                 $sth->execute($subscriptionid);
354                 my $val = $sth->fetchrow_hashref;
355                 # next issue number
356                 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
357                 # next date (calculated from actual date & frequency parameters)
358                 my $nextplanneddate = Get_Next_Date($planneddate,$val);
359                 newissue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
360                 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
361                                                                                                                 innerloop1=?,innerloop2=?,innerloop3=?
362                                                                                                                 where subscriptionid = ?");
363                 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
364         }
365 }
366
367 sub newissue {
368         my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
369         my $dbh = C4::Context->dbh;
370         my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
371         $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
372 }
373
374 sub delissue {
375         my ($serialseq,$subscriptionid) = @_;
376         my $dbh = C4::Context->dbh;
377         my $sth = $dbh->prepare("delete from serial where serialseq= ? and subscriptionid= ? ");
378         $sth->execute($serialseq,$subscriptionid);
379 }
380
381 sub Get_Next_Date(@) {
382         my ($planneddate,$subscription) = @_;
383         my $resultdate;
384         if ($subscription->{periodicity} == 1) {
385                 $resultdate=DateCalc($planneddate,"1 day");
386         }
387         if ($subscription->{periodicity} == 2) {
388                 $resultdate=DateCalc($planneddate,"1 week");
389         }
390         if ($subscription->{periodicity} == 3) {
391                 $resultdate=DateCalc($planneddate,"2 weeks");
392         }
393         if ($subscription->{periodicity} == 4) {
394                 $resultdate=DateCalc($planneddate,"3 weeks");
395         }
396         if ($subscription->{periodicity} == 5) {
397                 $resultdate=DateCalc($planneddate,"1 month");
398         }
399         if ($subscription->{periodicity} == 6) {
400                 $resultdate=DateCalc($planneddate,"2 months");
401         }
402         if ($subscription->{periodicity} == 7) {
403                 $resultdate=DateCalc($planneddate,"3 months");
404         }
405         if ($subscription->{periodicity} == 8) {
406                 $resultdate=DateCalc($planneddate,"3 months");
407         }
408         if ($subscription->{periodicity} == 9) {
409                 $resultdate=DateCalc($planneddate,"6 months");
410         }
411         if ($subscription->{periodicity} == 10) {
412                 $resultdate=DateCalc($planneddate,"1 year");
413         }
414         if ($subscription->{periodicity} == 11) {
415                 $resultdate=DateCalc($planneddate,"2 years");
416         }
417     return format_date_in_iso($resultdate);
418 }
419
420 sub Get_Seq {
421         my ($val) =@_;
422         my $calculated = $val->{numberingmethod};
423         my $x=$val->{'lastvalue1'};
424         $calculated =~ s/\{X\}/$x/g;
425         my $y=$val->{'lastvalue2'};
426         $calculated =~ s/\{Y\}/$y/g;
427         my $z=$val->{'lastvalue3'};
428         $calculated =~ s/\{Z\}/$z/g;
429         return $calculated;
430 }
431
432 sub Get_Next_Seq {
433         my ($val) =@_;
434         my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
435         $calculated = $val->{numberingmethod};
436         # calculate the (expected) value of the next issue recieved.
437         $newlastvalue1 = $val->{lastvalue1};
438         # check if we have to increase the new value.
439         $newinnerloop1 = $val->{innerloop1}+1;
440         $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
441         $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
442         $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
443         $calculated =~ s/\{X\}/$newlastvalue1/g;
444         
445         $newlastvalue2 = $val->{lastvalue2};
446         # check if we have to increase the new value.
447         $newinnerloop2 = $val->{innerloop2}+1;
448         $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
449         $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
450         $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
451         $calculated =~ s/\{Y\}/$newlastvalue2/g;
452         
453         $newlastvalue3 = $val->{lastvalue3};
454         # check if we have to increase the new value.
455         $newinnerloop3 = $val->{innerloop3}+1;
456         $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
457         $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
458         $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
459         $calculated =~ s/\{Z\}/$newlastvalue3/g;
460         return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
461 }
462
463 # the subscription has expired when the next issue to arrive is out of subscription limit.
464 sub hassubscriptionexpired {
465         my ($subscriptionid) = @_;
466         my $dbh = C4::Context->dbh;
467         my $subscription = getsubscription($subscriptionid);
468         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
469         if ($subscription->{numberlength}) {
470                 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=?  and planneddate>=?");
471                 $sth->execute($subscriptionid,$subscription->{startdate});
472                 my $res = $sth->fetchrow;
473                 if ($subscription->{numberlength}>=$res) {
474                         return 0;
475                 } else {
476                         return 1;
477                 }
478         } else {
479                 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
480                 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
481                 $sth->execute($subscriptionid);
482                 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
483                 my $endofsubscriptiondate;
484                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
485                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
486                 return 1 if ($res >= $endofsubscriptiondate);
487                 return 0;
488         }
489 }
490
491 sub subscriptionexpirationdate {
492         my ($subscriptionid) = @_;
493         my $dbh = C4::Context->dbh;
494         my $subscription = getsubscription($subscriptionid);
495         my $enddate=$subscription->{startdate};
496         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
497         if ($subscription->{numberlength}) {
498                 #calculate the date of the last issue.
499                 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
500                         $enddate = Get_Next_Date($enddate,$subscription);
501                 }
502         } else {
503                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
504                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
505         }
506 #       $enddate=format_date_in_iso($enddate);
507 #       warn "END : $enddate";
508         return $enddate;
509 }
510
511 sub subscriptionrenew {
512         my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
513         my $dbh = C4::Context->dbh;
514         my $subscription = getsubscription($subscriptionid);
515         my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
516         $sth->execute($subscription->{biblionumber});
517         my $biblio = $sth->fetchrow_hashref;
518         newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
519         # renew subscription
520         $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
521         $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
522 }
523 END { }       # module clean-up code here (global destructor)