2eedc274eae06adf686acb0b267a751eccfff401
[koha_gimpoz] / 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                         &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);
55
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)
63                                                         ");
64         $sth->execute;
65         my %supplierlist;
66         while (my ($id,$name) = $sth->fetchrow) {
67                 $supplierlist{$id} = $name;
68         }
69         return %supplierlist;
70 }
71 sub GetLateIssues {
72         my ($supplierid) = @_;
73         my $dbh = C4::Context->dbh;
74         my $sth;
75         if ($supplierid) {
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
83                                                         ");
84         } else {
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
91                                                         ");
92         }
93         $sth->execute;
94         my @issuelist;
95         my $last_title;
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;
101         }
102         return @issuelist;
103 }
104
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;
113         #save subscription
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 
120                                                         (?,?,?,?,?,?,?,?,?,
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;
137
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;
143 }
144
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 
149                                                         from subscription 
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;
157         return $subs;
158 }
159
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;
167 }
168
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 
173                                                         from subscription 
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);
180         my @res;
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') {
189                         $subs->{enddate}='';
190                 } else {
191                         $subs->{enddate} = format_date($subs->{enddate});
192                 }
193                 push @res,$subs;
194         }
195         return \@res;
196 }
197
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);
208         my @res;
209         my $year;
210         my $startdate;
211         my $aqbooksellername;
212         my $bibliotitle;
213         my @loopissues;
214         my $first;
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'};
221 #               }
222                 if ($year and ($year==$subs->{year})){
223                         if ($first eq 1){$first=0;}
224                         my $temp=$res[scalar(@res)-1]->{'serials'};
225                         push @$temp,
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,
232                                 };
233                 }else {
234                         $first=1 if (not $year);
235                         $year= $subs->{'year'};
236                         $startdate= format_date($subs->{'startdate'});
237                         $aqbooksellername= $subs->{'aqbooksellername'};
238                         $bibliotitle= $subs->{'bibliotitle'};
239                         my @temp;
240                         push @temp,
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,
247                                 };
248                         
249                         push @res,{
250                                 'year'=>$year,
251                                 'startdate'=>$startdate,
252                                 'aqbooksellername'=>$aqbooksellername,
253                                 'bibliotitle'=>$bibliotitle,
254                                 'serials'=>\@temp,
255                                 'first'=>$first 
256                         };
257                 }
258         }
259         return \@res;
260 }
261
262
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);
283         $sth->finish;
284
285 }
286
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");
297         }
298 }
299 sub getsubscriptions {
300         my ($title,$ISSN,$biblionumber) = @_;
301         return unless $title or $ISSN or $biblionumber;
302         my $dbh = C4::Context->dbh;
303         my $sth;
304         if ($biblionumber) {
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);
307         } else {
308                 if ($ISSN and $title)
309                 {
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);
312                 }
313                 else
314                 {
315                         if ($ISSN)
316                         {
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);
319                         }
320                         else
321                         {
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%");
325                         }
326                 }
327         }
328                 my @results;
329         while (my $line = $sth->fetchrow_hashref) {
330                 push @results, $line;
331         }
332         return @results;
333 }
334
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);
343 }
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)
347 sub getserials {
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);
353         my @serials;
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"});
357                 push @serials,$line;
358         }
359         $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
360         $sth->execute($subscriptionid);
361         my ($totalissues) = $sth->fetchrow;
362         return ($totalissues,@serials);
363 }
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);
371         my @serials;
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"});
375                 push @serials,$line;
376         }
377         $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
378         $sth->execute($subscriptionid);
379         my ($totalissues) = $sth->fetchrow;
380         return ($totalissues,@serials);
381 }
382
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
392         if ($status eq 6){
393                 delissue($serialseq, $subscriptionid); 
394         }else{
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;
400                 if ($status eq 2) {
401                         $recievedlist .= ",$serialseq";
402                 }
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);
407         }
408
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;
414                 # next issue number
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);
423         }
424 }
425
426 sub newissue {
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;
434         if ($status eq 2) {
435                 $recievedlist .= ",$serialseq";
436         }
437         if ($status eq 4) {
438                 $missinglist .= ",$serialseq";
439         }
440         $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
441         $sth->execute($recievedlist,$missinglist,$subscriptionid);
442 }
443
444 sub Get_Next_Date(@) {
445         my ($planneddate,$subscription) = @_;
446         my $resultdate;
447         if ($subscription->{periodicity} == 1) {
448                 $resultdate=DateCalc($planneddate,"1 day");
449         }
450         if ($subscription->{periodicity} == 2) {
451                 $resultdate=DateCalc($planneddate,"1 week");
452         }
453         if ($subscription->{periodicity} == 3) {
454                 $resultdate=DateCalc($planneddate,"2 weeks");
455         }
456         if ($subscription->{periodicity} == 4) {
457                 $resultdate=DateCalc($planneddate,"3 weeks");
458         }
459         if ($subscription->{periodicity} == 5) {
460                 $resultdate=DateCalc($planneddate,"1 month");
461         }
462         if ($subscription->{periodicity} == 6) {
463                 $resultdate=DateCalc($planneddate,"2 months");
464         }
465         if ($subscription->{periodicity} == 7) {
466                 $resultdate=DateCalc($planneddate,"3 months");
467         }
468         if ($subscription->{periodicity} == 8) {
469                 $resultdate=DateCalc($planneddate,"3 months");
470         }
471         if ($subscription->{periodicity} == 9) {
472                 $resultdate=DateCalc($planneddate,"6 months");
473         }
474         if ($subscription->{periodicity} == 10) {
475                 $resultdate=DateCalc($planneddate,"1 year");
476         }
477         if ($subscription->{periodicity} == 11) {
478                 $resultdate=DateCalc($planneddate,"2 years");
479         }
480     return format_date_in_iso($resultdate);
481 }
482
483 sub Get_Seq {
484         my ($val) =@_;
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;
492         return $calculated;
493 }
494
495 sub Get_Next_Seq {
496         my ($val) =@_;
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;
507         
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;
515         
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);
524 }
525
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) {
537                         return 0;
538                 } else {
539                         return 1;
540                 }
541         } else {
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);
550                 return 0;
551         }
552 }
553
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);
564                 }
565         } else {
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});
568         }
569 #       $enddate=format_date_in_iso($enddate);
570 #       warn "END : $enddate";
571         return $enddate;
572 }
573
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});
582         # renew subscription
583         $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
584         $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
585 }
586 END { }       # module clean-up code here (global destructor)