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