Added &getcredits routine.
[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                 if ($subs->{enddate} eq '0000-00-00') {
190                         $subs->{enddate}='';
191                 } else {
192                         $subs->{enddate} = format_date($subs->{enddate});
193                 }
194                 push @res,$subs;
195         }
196         return \@res;
197 }
198
199 sub get_full_subscription_list_from_biblionumber {
200         my ($biblionumber) = @_;
201         my $dbh = C4::Context->dbh;
202         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 
203                                                         from serial left join subscription on (serial.subscriptionid=subscription.subscriptionid and subscription.biblionumber=serial.biblionumber)
204                                                         left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid 
205                                                         left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
206                                                         left join biblio on biblio.biblionumber=subscription.biblionumber 
207                                                         where subscription.biblionumber = ? order by year,serial.subscriptionid,serial.planneddate');
208         $sth->execute($biblionumber);
209         my @res;
210         my $year;
211         my $startdate;
212         my $aqbooksellername;
213         my $bibliotitle;
214         my @loopissues;
215         my $first;
216         my $previousnote="";
217         while (my $subs = $sth->fetchrow_hashref) {
218 #               my $sth2 = $dbh->prepare('select * from serial where serial.biblionumber = ? and serial.subscriptionid=? order by serial.planneddate');
219 #               $sth2->execute($biblionumber,$subs->{'subscriptionid'});
220 #               while (my $issues = $sth2->fetchrow_hashref){
221 #                               warn "planneddate ".$issues->{'planneddate'};
222 #                               warn "serialseq".$issues->{'serialseq'};
223 #               }
224                 if ($year and ($year==$subs->{year})){
225                         if ($first eq 1){$first=0;}
226                         my $temp=$res[scalar(@res)-1]->{'serials'};
227                         push @$temp,
228                                 {'planneddate' => format_date($subs->{'planneddate'}), 
229                                 'serialseq' => $subs->{'serialseq'},
230                                 'status1' => $subs->{'status'}==1,
231                                 'status2' => $subs->{'status'}==2,
232                                 'status3' => $subs->{'status'}==3,
233                                 'status4' => $subs->{'status'}==4,
234                                 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
235                                 };
236                 }else {
237                         $first=1 if (not $year);
238                         $year= $subs->{'year'};
239                         $startdate= format_date($subs->{'startdate'});
240                         $aqbooksellername= $subs->{'aqbooksellername'};
241                         $bibliotitle= $subs->{'bibliotitle'};
242                         my @temp;
243                         push @temp,
244                                 {'planneddate' => format_date($subs->{'planneddate'}), 
245                                 'serialseq' => $subs->{'serialseq'},
246                                 'status1' => $subs->{'status'}==1,
247                                 'status2' => $subs->{'status'}==2,
248                                 'status3' => $subs->{'status'}==3,
249                                 'status4' => $subs->{'status'}==4,
250                                 'notes' => $subs->{'notes'} eq $previousnote?"":$subs->{notes},
251                                 };
252                         
253                         push @res,{
254                                 'year'=>$year,
255                                 'startdate'=>$startdate,
256                                 'aqbooksellername'=>$aqbooksellername,
257                                 'bibliotitle'=>$bibliotitle,
258                                 'serials'=>\@temp,
259                                 'first'=>$first 
260                         };
261                 }
262                 $previousnote=$subs->{notes};
263         }
264         return \@res;
265 }
266
267
268 sub modsubscription {
269         my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
270                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
271                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
272                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
273                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
274                                         $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid)= @_;
275         my $dbh = C4::Context->dbh;
276         my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
277                                                  periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
278                                                 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
279                                                 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
280                                                 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
281                                                 numberingmethod=?, status=?, biblionumber=?, notes=?, letter=? where subscriptionid = ?");
282         $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
283                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
284                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
285                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
286                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
287                                         $numberingmethod, $status, $biblionumber, $notes, $letter, $subscriptionid);
288         $sth->finish;
289
290 }
291
292 sub delsubscription {
293         my ($subscriptionid) = @_;
294         # check again there is only one issue (the subscription is new)
295         my ($totalissues) = getserials($subscriptionid);
296         if ($totalissues eq 1) {
297                 my $dbh = C4::Context->dbh;
298                 $subscriptionid=$dbh->quote($subscriptionid);
299                 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
300                 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
301                 $dbh->do("delete from serial where subscriptionid=$subscriptionid");
302         }
303 }
304 sub getsubscriptions {
305         my ($title,$ISSN,$biblionumber) = @_;
306         return unless $title or $ISSN or $biblionumber;
307         my $dbh = C4::Context->dbh;
308         my $sth;
309         if ($biblionumber) {
310                 $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=?");
311                 $sth->execute($biblionumber);
312         } else {
313                 if ($ISSN and $title)
314                 {
315                         $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 = ? )");
316                         $sth->execute("%$title%",$ISSN);
317                 }
318                 else
319                 {
320                         if ($ISSN)
321                         {
322                                 $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 = ?");
323                                 $sth->execute($ISSN);
324                         }
325                         else
326                         {
327                                 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes,biblio.biblionumber from subscription,biblio,biblioitems where  biblio.biblionumber = biblioitems.biblionumber and
328  biblio.biblionumber=subscription.biblionumber and biblio.title like ? ");
329                                 $sth->execute("%$title%");
330                         }
331                 }
332         }
333         my @results;
334         my $previoustitle="";
335         while (my $line = $sth->fetchrow_hashref) {
336                 if ($previoustitle eq $line->{title}) {
337                         $line->{title}="";
338                         $line->{issn}="";
339                 } else {
340                         $previoustitle=$line->{title};
341                 }
342                 push @results, $line;
343         }
344         return @results;
345 }
346
347 sub modsubscriptionhistory {
348         my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
349         my $dbh=C4::Context->dbh;
350         my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
351         $recievedlist =~ s/^,//g;
352         $missinglist =~ s/^,//g;
353         $opacnote =~ s/^,//g;
354         $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
355 }
356 # get every serial not arrived for a given subscription
357 # as well as the number of issues registered in the database (all types)
358 # this number is used to see if a subscription can be deleted (=it must have only 1 issue)
359 sub getserials {
360         my ($subscriptionid) = @_;
361         my $dbh = C4::Context->dbh;
362         # status = 2 is "arrived"
363         my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5");
364         $sth->execute($subscriptionid);
365         my @serials;
366         while(my $line = $sth->fetchrow_hashref) {
367                 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
368                 $line->{"planneddate"} = format_date($line->{"planneddate"});
369                 push @serials,$line;
370         }
371         $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
372         $sth->execute($subscriptionid);
373         my ($totalissues) = $sth->fetchrow;
374         return ($totalissues,@serials);
375 }
376
377 # get the $limit's latest serials arrived or missing for a given subscription
378 sub getlatestserials{
379         my ($subscriptionid,$limit) =@_;
380         my $dbh = C4::Context->dbh;
381         # status = 2 is "arrived"
382         my $strsth="select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and (status =2 or status=4) order by planneddate DESC LIMIT 0,$limit";
383         my $sth=$dbh->prepare($strsth);
384         $sth->execute($subscriptionid);
385         my @serials;
386         while(my $line = $sth->fetchrow_hashref) {
387                 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
388                 $line->{"planneddate"} = format_date($line->{"planneddate"});
389                 push @serials,$line;
390         }
391         $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
392         $sth->execute($subscriptionid);
393         my ($totalissues) = $sth->fetchrow;
394         return \@serials;
395 }
396
397 sub serialchangestatus {
398         my ($serialid,$serialseq,$planneddate,$status)=@_;
399         # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
400         my $dbh = C4::Context->dbh;
401         my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
402         $sth->execute($serialid);
403         my ($subscriptionid,$oldstatus) = $sth->fetchrow;
404         # change status & update subscriptionhistory
405         if ($status eq 6){
406                 delissue($serialseq, $subscriptionid) 
407         }else{
408                 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
409                 $sth->execute($serialseq,$planneddate,$status,$serialid);
410                 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
411                 $sth->execute($subscriptionid);
412                 my ($missinglist,$recievedlist) = $sth->fetchrow;
413                 if ($status eq 2) {
414                         $recievedlist .= ",$serialseq";
415                 }
416                 $missinglist .= ",$serialseq" if ($status eq 4) ;
417                 $missinglist .= ",not issued $serialseq" if ($status eq 5);
418                 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
419                 $sth->execute($recievedlist,$missinglist,$subscriptionid);
420         }
421         # create new waited entry if needed (ie : was a "waited" and has changed)
422         $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
423         $sth->execute($subscriptionid);
424         my $subscription = $sth->fetchrow_hashref;
425         if ($oldstatus eq 1 && $status ne 1) {
426                 # next issue number
427                 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($subscription);
428                 # next date (calculated from actual date & frequency parameters)
429                 my $nextplanneddate = Get_Next_Date($planneddate,$subscription);
430                 newissue($newserialseq, $subscriptionid, $subscription->{'biblionumber'}, 1, $nextplanneddate);
431                 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
432                                                                                                                 innerloop1=?,innerloop2=?,innerloop3=?
433                                                                                                                 where subscriptionid = ?");
434                 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
435         }
436         # check if an alert must be sent... (= a letter is defined & status became "arrived"
437         if ($subscription->{letter} && $status eq 2) {
438                 sendalerts('issue',$subscription->{subscriptionid},$subscription->{letter});
439         }
440 }
441
442 sub newissue {
443         my ($serialseq,$subscriptionid,$biblionumber,$status, $planneddate) = @_;
444         my $dbh = C4::Context->dbh;
445         my $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
446         $sth->execute($serialseq,$subscriptionid,$biblionumber,$status, $planneddate);
447         $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
448         $sth->execute($subscriptionid);
449         my ($missinglist,$recievedlist) = $sth->fetchrow;
450         if ($status eq 2) {
451                 $recievedlist .= ",$serialseq";
452         }
453         if ($status eq 4) {
454                 $missinglist .= ",$serialseq";
455         }
456         $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
457         $sth->execute($recievedlist,$missinglist,$subscriptionid);
458 }
459
460 sub delissue {
461         my ($serialseq,$subscriptionid) = @_;
462         my $dbh = C4::Context->dbh;
463         my $sth = $dbh->prepare("delete from serial where serialseq= ? and subscriptionid= ? ");
464         $sth->execute($serialseq,$subscriptionid);
465 }
466
467 sub Get_Next_Date(@) {
468         my ($planneddate,$subscription) = @_;
469         my $resultdate;
470         if ($subscription->{periodicity} == 1) {
471                 $resultdate=DateCalc($planneddate,"1 day");
472         }
473         if ($subscription->{periodicity} == 2) {
474                 $resultdate=DateCalc($planneddate,"1 week");
475         }
476         if ($subscription->{periodicity} == 3) {
477                 $resultdate=DateCalc($planneddate,"2 weeks");
478         }
479         if ($subscription->{periodicity} == 4) {
480                 $resultdate=DateCalc($planneddate,"3 weeks");
481         }
482         if ($subscription->{periodicity} == 5) {
483                 $resultdate=DateCalc($planneddate,"1 month");
484         }
485         if ($subscription->{periodicity} == 6) {
486                 $resultdate=DateCalc($planneddate,"2 months");
487         }
488         if ($subscription->{periodicity} == 7) {
489                 $resultdate=DateCalc($planneddate,"3 months");
490         }
491         if ($subscription->{periodicity} == 8) {
492                 $resultdate=DateCalc($planneddate,"3 months");
493         }
494         if ($subscription->{periodicity} == 9) {
495                 $resultdate=DateCalc($planneddate,"6 months");
496         }
497         if ($subscription->{periodicity} == 10) {
498                 $resultdate=DateCalc($planneddate,"1 year");
499         }
500         if ($subscription->{periodicity} == 11) {
501                 $resultdate=DateCalc($planneddate,"2 years");
502         }
503     return format_date_in_iso($resultdate);
504 }
505
506 sub Get_Seq {
507         my ($val) =@_;
508         my $calculated = $val->{numberingmethod};
509         my $x=$val->{'lastvalue1'};
510         $calculated =~ s/\{X\}/$x/g;
511         my $y=$val->{'lastvalue2'};
512         $calculated =~ s/\{Y\}/$y/g;
513         my $z=$val->{'lastvalue3'};
514         $calculated =~ s/\{Z\}/$z/g;
515         return $calculated;
516 }
517
518 sub Get_Next_Seq {
519         my ($val) =@_;
520         my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
521         $calculated = $val->{numberingmethod};
522         # calculate the (expected) value of the next issue recieved.
523         $newlastvalue1 = $val->{lastvalue1};
524         # check if we have to increase the new value.
525         $newinnerloop1 = $val->{innerloop1}+1;
526         $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
527         $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
528         $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
529         $calculated =~ s/\{X\}/$newlastvalue1/g;
530         
531         $newlastvalue2 = $val->{lastvalue2};
532         # check if we have to increase the new value.
533         $newinnerloop2 = $val->{innerloop2}+1;
534         $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
535         $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
536         $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
537         $calculated =~ s/\{Y\}/$newlastvalue2/g;
538         
539         $newlastvalue3 = $val->{lastvalue3};
540         # check if we have to increase the new value.
541         $newinnerloop3 = $val->{innerloop3}+1;
542         $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
543         $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
544         $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
545         $calculated =~ s/\{Z\}/$newlastvalue3/g;
546         return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
547 }
548
549 # the subscription has expired when the next issue to arrive is out of subscription limit.
550 sub hassubscriptionexpired {
551         my ($subscriptionid) = @_;
552         my $dbh = C4::Context->dbh;
553         my $subscription = getsubscription($subscriptionid);
554         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
555         if ($subscription->{numberlength}) {
556                 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=?  and planneddate>=?");
557                 $sth->execute($subscriptionid,$subscription->{startdate});
558                 my $res = $sth->fetchrow;
559                 if ($subscription->{numberlength}>=$res) {
560                         return 0;
561                 } else {
562                         return 1;
563                 }
564         } else {
565                 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
566                 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
567                 $sth->execute($subscriptionid);
568                 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
569                 my $endofsubscriptiondate;
570                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
571                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
572                 return 1 if ($res >= $endofsubscriptiondate);
573                 return 0;
574         }
575 }
576
577 sub subscriptionexpirationdate {
578         my ($subscriptionid) = @_;
579         my $dbh = C4::Context->dbh;
580         my $subscription = getsubscription($subscriptionid);
581         my $enddate=$subscription->{startdate};
582         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
583         if ($subscription->{numberlength}) {
584                 #calculate the date of the last issue.
585                 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
586                         $enddate = Get_Next_Date($enddate,$subscription);
587                 }
588         } else {
589                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
590                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
591         }
592 #       $enddate=format_date_in_iso($enddate);
593 #       warn "END : $enddate";
594         return $enddate;
595 }
596
597 sub subscriptionrenew {
598         my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
599         my $dbh = C4::Context->dbh;
600         my $subscription = getsubscription($subscriptionid);
601         my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
602         $sth->execute($subscription->{biblionumber});
603         my $biblio = $sth->fetchrow_hashref;
604         newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
605         # renew subscription
606         $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
607         $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
608 }
609 END { }       # module clean-up code here (global destructor)