synch'ing 2.2 and head
[koha.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);
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");
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
291 sub serialchangestatus {
292         my ($serialid,$serialseq,$planneddate,$status)=@_;
293 #       warn "($serialid,$serialseq,$planneddate,$status)";
294         # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
295         my $dbh = C4::Context->dbh;
296         my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
297         $sth->execute($serialid);
298         my ($subscriptionid,$oldstatus) = $sth->fetchrow;
299         # change status & update subscriptionhistory
300         $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
301         $sth->execute($serialseq,$planneddate,$status,$serialid);
302         $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
303         $sth->execute($subscriptionid);
304         my ($missinglist,$recievedlist) = $sth->fetchrow;
305         if ($status eq 2) {
306                 $recievedlist .= ",$serialseq";
307         }
308         if ($status eq 4) {
309                 $missinglist .= ",$serialseq";
310         }
311         $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
312         $sth->execute($recievedlist,$missinglist,$subscriptionid);
313         # create new waited entry if needed (ie : was a "waited" and has changed)
314         if ($oldstatus eq 1 && $status ne 1) {
315                 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
316                 $sth->execute($subscriptionid);
317                 my $val = $sth->fetchrow_hashref;
318                 # next issue number
319                 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
320                 # next date (calculated from actual date & frequency parameters)
321                 my $nextplanneddate = Get_Next_Date($planneddate,$val);
322                 newissue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
323                 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
324                                                                                                                 innerloop1=?,innerloop2=?,innerloop3=?
325                                                                                                                 where subscriptionid = ?");
326                 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
327         }
328 }
329
330 sub newissue {
331         my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
332         my $dbh = C4::Context->dbh;
333         my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
334         $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
335 }
336
337 sub Get_Next_Date(@) {
338         my ($planneddate,$subscription) = @_;
339         my $resultdate;
340         if ($subscription->{periodicity} == 1) {
341                 $resultdate=DateCalc($planneddate,"1 day");
342         }
343         if ($subscription->{periodicity} == 2) {
344                 $resultdate=DateCalc($planneddate,"1 week");
345         }
346         if ($subscription->{periodicity} == 3) {
347                 $resultdate=DateCalc($planneddate,"2 weeks");
348         }
349         if ($subscription->{periodicity} == 4) {
350                 $resultdate=DateCalc($planneddate,"3 weeks");
351         }
352         if ($subscription->{periodicity} == 5) {
353                 $resultdate=DateCalc($planneddate,"1 month");
354         }
355         if ($subscription->{periodicity} == 6) {
356                 $resultdate=DateCalc($planneddate,"2 months");
357         }
358         if ($subscription->{periodicity} == 7) {
359                 $resultdate=DateCalc($planneddate,"3 months");
360         }
361         if ($subscription->{periodicity} == 8) {
362                 $resultdate=DateCalc($planneddate,"3 months");
363         }
364         if ($subscription->{periodicity} == 9) {
365                 $resultdate=DateCalc($planneddate,"6 months");
366         }
367         if ($subscription->{periodicity} == 10) {
368                 $resultdate=DateCalc($planneddate,"1 year");
369         }
370         if ($subscription->{periodicity} == 11) {
371                 $resultdate=DateCalc($planneddate,"2 years");
372         }
373     return format_date_in_iso($resultdate);
374 }
375
376 sub Get_Seq {
377         my ($val) =@_;
378         my $calculated = $val->{numberingmethod};
379         my $x=$val->{'lastvalue1'};
380         $calculated =~ s/\{X\}/$x/g;
381         my $y=$val->{'lastvalue2'};
382         $calculated =~ s/\{Y\}/$y/g;
383         my $z=$val->{'lastvalue3'};
384         $calculated =~ s/\{Z\}/$z/g;
385         return $calculated;
386 }
387
388 sub Get_Next_Seq {
389         my ($val) =@_;
390         my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
391         $calculated = $val->{numberingmethod};
392         # calculate the (expected) value of the next issue recieved.
393         $newlastvalue1 = $val->{lastvalue1};
394         # check if we have to increase the new value.
395         $newinnerloop1 = $val->{innerloop1}+1;
396         $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
397         $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
398         $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
399         $calculated =~ s/\{X\}/$newlastvalue1/g;
400         
401         $newlastvalue2 = $val->{lastvalue2};
402         # check if we have to increase the new value.
403         $newinnerloop2 = $val->{innerloop2}+1;
404         $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
405         $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
406         $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
407         $calculated =~ s/\{Y\}/$newlastvalue2/g;
408         
409         $newlastvalue3 = $val->{lastvalue3};
410         # check if we have to increase the new value.
411         $newinnerloop3 = $val->{innerloop3}+1;
412         $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
413         $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
414         $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
415         $calculated =~ s/\{Z\}/$newlastvalue3/g;
416         return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
417 }
418
419 # the subscription has expired when the next issue to arrive is out of subscription limit.
420 sub hassubscriptionexpired {
421         my ($subscriptionid) = @_;
422         my $dbh = C4::Context->dbh;
423         my $subscription = getsubscription($subscriptionid);
424         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
425         if ($subscription->{numberlength}) {
426                 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=?  and planneddate>=?");
427                 $sth->execute($subscriptionid,$subscription->{startdate});
428                 my $res = $sth->fetchrow;
429                 if ($subscription->{numberlength}>=$res) {
430                         return 0;
431                 } else {
432                         return 1;
433                 }
434         } else {
435                 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
436                 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
437                 $sth->execute($subscriptionid);
438                 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
439                 my $endofsubscriptiondate;
440                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
441                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
442                 return 1 if ($res >= $endofsubscriptiondate);
443                 return 0;
444         }
445 }
446
447 sub subscriptionexpirationdate {
448         my ($subscriptionid) = @_;
449         my $dbh = C4::Context->dbh;
450         my $subscription = getsubscription($subscriptionid);
451         my $enddate=$subscription->{startdate};
452         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
453         if ($subscription->{numberlength}) {
454                 #calculate the date of the last issue.
455                 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
456                         $enddate = Get_Next_Date($enddate,$subscription);
457                 }
458         } else {
459                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
460                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
461         }
462 #       $enddate=format_date_in_iso($enddate);
463 #       warn "END : $enddate";
464         return $enddate;
465 }
466
467 sub subscriptionrenew {
468         my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
469         my $dbh = C4::Context->dbh;
470         my $subscription = getsubscription($subscriptionid);
471         my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
472         $sth->execute($subscription->{biblionumber});
473         my $biblio = $sth->fetchrow_hashref;
474         newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
475         # renew subscription
476         $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
477         $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
478 }
479 END { }       # module clean-up code here (global destructor)