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