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