removing disused C4::Serials functions
[koha.git] / C4 / Serials.pm
1 package C4::Serials;    #assumes C4/Serials.pm
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 use strict;
22 use C4::Dates qw(format_date format_date_in_iso);
23 use Date::Calc qw(:all);
24 use POSIX qw(strftime);
25 use C4::Suggestions;
26 use C4::Koha;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Search;
30 use C4::Letters;
31 use C4::Log; # logaction
32 use C4::Debug;
33
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35
36 BEGIN {
37         $VERSION = 3.01;        # set version for version checking
38         require Exporter;
39         @ISA    = qw(Exporter);
40         @EXPORT = qw(
41     &NewSubscription    &ModSubscription    &DelSubscription    &GetSubscriptions
42     &GetSubscription    &CountSubscriptionFromBiblionumber      &GetSubscriptionsFromBiblionumber
43     &GetFullSubscriptionsFromBiblionumber   &GetFullSubscription &ModSubscriptionHistory
44     &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
45     
46     &GetNextSeq         &NewIssue           &ItemizeSerials    &GetSerials
47     &GetLatestSerials   &ModSerialStatus    &GetNextDate       &GetSerials2
48     &ReNewSubscription  &GetLateIssues      &GetLateOrMissingIssues
49     &GetSerialInformation                   &AddItem2Serial
50     &PrepareSerialsData &GetNextExpected    &ModNextExpected
51     
52     &UpdateClaimdateIssues
53     &GetSuppliersWithLateIssues             &getsupplierbyserialid
54     &getroutinglist     &delroutingmember   &addroutingmember
55     &reorder_members
56     &check_routing &updateClaim &removeMissingIssue
57     
58         );
59 }
60
61 =head2 GetSuppliersWithLateIssues
62
63 =head1 NAME
64
65 C4::Serials - Give functions for serializing.
66
67 =head1 SYNOPSIS
68
69   use C4::Serials;
70
71 =head1 DESCRIPTION
72
73 Give all XYZ functions
74
75 =head1 FUNCTIONS
76
77 =over 4
78
79 %supplierlist = &GetSuppliersWithLateIssues
80
81 this function get all suppliers with late issues.
82
83 return :
84 the supplierlist into a hash. this hash containts id & name of the supplier
85
86 =back
87
88 =cut
89
90 sub GetSuppliersWithLateIssues {
91     my $dbh   = C4::Context->dbh;
92     my $query = qq|
93         SELECT DISTINCT id, name
94         FROM            subscription 
95         LEFT JOIN       serial ON serial.subscriptionid=subscription.subscriptionid
96         LEFT JOIN       aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
97         WHERE           subscription.subscriptionid = serial.subscriptionid
98         AND             (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
99         ORDER BY name
100     |;
101     my $sth = $dbh->prepare($query);
102     $sth->execute;
103     my %supplierlist;
104     while ( my ( $id, $name ) = $sth->fetchrow ) {
105         $supplierlist{$id} = $name;
106     }
107     return %supplierlist;
108 }
109
110 =head2 GetLateIssues
111
112 =over 4
113
114 @issuelist = &GetLateIssues($supplierid)
115
116 this function select late issues on database
117
118 return :
119 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
120 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
121
122 =back
123
124 =cut
125
126 sub GetLateIssues {
127     my ($supplierid) = @_;
128     my $dbh = C4::Context->dbh;
129     my $sth;
130     if ($supplierid) {
131         my $query = qq|
132             SELECT     name,title,planneddate,serialseq,serial.subscriptionid
133             FROM       subscription
134             LEFT JOIN  serial ON subscription.subscriptionid = serial.subscriptionid
135             LEFT JOIN  biblio ON biblio.biblionumber = subscription.biblionumber
136             LEFT JOIN  aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
137             WHERE      ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
138             AND        subscription.aqbooksellerid=$supplierid
139             ORDER BY   title
140         |;
141         $sth = $dbh->prepare($query);
142     }
143     else {
144         my $query = qq|
145             SELECT     name,title,planneddate,serialseq,serial.subscriptionid
146             FROM       subscription
147             LEFT JOIN  serial ON subscription.subscriptionid = serial.subscriptionid
148             LEFT JOIN  biblio ON biblio.biblionumber = subscription.biblionumber
149             LEFT JOIN  aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
150             WHERE      ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
151             ORDER BY   title
152         |;
153         $sth = $dbh->prepare($query);
154     }
155     $sth->execute;
156     my @issuelist;
157     my $last_title;
158     my $odd   = 0;
159     my $count = 0;
160     while ( my $line = $sth->fetchrow_hashref ) {
161         $odd++ unless $line->{title} eq $last_title;
162         $line->{title} = "" if $line->{title} eq $last_title;
163         $last_title = $line->{title} if ( $line->{title} );
164         $line->{planneddate} = format_date( $line->{planneddate} );
165         $count++;
166         push @issuelist, $line;
167     }
168     return $count, @issuelist;
169 }
170
171 =head2 GetSubscriptionHistoryFromSubscriptionId
172
173 =over 4
174
175 $sth = GetSubscriptionHistoryFromSubscriptionId()
176 this function just prepare the SQL request.
177 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
178 return :
179 $sth = $dbh->prepare($query).
180
181 =back
182
183 =cut
184
185 sub GetSubscriptionHistoryFromSubscriptionId() {
186     my $dbh   = C4::Context->dbh;
187     my $query = qq|
188         SELECT *
189         FROM   subscriptionhistory
190         WHERE  subscriptionid = ?
191     |;
192     return $dbh->prepare($query);
193 }
194
195 =head2 GetSerialStatusFromSerialId
196
197 =over 4
198
199 $sth = GetSerialStatusFromSerialId();
200 this function just prepare the SQL request.
201 After this function, don't forget to execute it by using $sth->execute($serialid)
202 return :
203 $sth = $dbh->prepare($query).
204
205 =back
206
207 =cut
208
209 sub GetSerialStatusFromSerialId() {
210     my $dbh   = C4::Context->dbh;
211     my $query = qq|
212         SELECT status
213         FROM   serial
214         WHERE  serialid = ?
215     |;
216     return $dbh->prepare($query);
217 }
218
219 =head2 GetSerialInformation
220
221 =over 4
222
223 $data = GetSerialInformation($serialid);
224 returns a hash containing :
225   items : items marcrecord (can be an array)
226   serial table field
227   subscription table field
228   + information about subscription expiration
229   
230 =back
231
232 =cut
233
234 sub GetSerialInformation {
235     my ($serialid) = @_;
236     my $dbh        = C4::Context->dbh;
237     my $query      = qq|
238         SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
239        if (C4::Context->preference('IndependantBranches') && 
240               C4::Context->userenv && 
241               C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
242                 $query.="
243       , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
244         }
245             $query .= qq|             
246         FROM   serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
247         WHERE  serialid = ?
248     |;
249     my $rq = $dbh->prepare($query);
250     $rq->execute($serialid);
251     my $data = $rq->fetchrow_hashref;
252     # create item information if we have serialsadditems for this subscription
253     if ( $data->{'serialsadditems'} ) {
254         my $queryitem=$dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
255         $queryitem->execute($serialid);
256         my $itemnumbers=$queryitem->fetchall_arrayref([0]);
257         if (scalar(@$itemnumbers)>0){
258             foreach my $itemnum (@$itemnumbers) {
259                 #It is ASSUMED that GetMarcItem ALWAYS WORK...
260                 #Maybe GetMarcItem should return values on failure
261                 $debug and warn "itemnumber :$itemnum->[0], bibnum :".$data->{'biblionumber'};
262                 my $itemprocessed =
263                   PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0] , $data );
264                 $itemprocessed->{'itemnumber'}   = $itemnum->[0];
265                 $itemprocessed->{'itemid'}       = $itemnum->[0];
266                 $itemprocessed->{'serialid'}     = $serialid;
267                 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
268                 push @{ $data->{'items'} }, $itemprocessed;
269             }
270         }
271         else {
272             my $itemprocessed =
273               PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
274             $itemprocessed->{'itemid'}       = "N$serialid";
275             $itemprocessed->{'serialid'}     = $serialid;
276             $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
277             $itemprocessed->{'countitems'}   = 0;
278             push @{ $data->{'items'} }, $itemprocessed;
279         }
280     }
281     $data->{ "status" . $data->{'serstatus'} } = 1;
282     $data->{'subscriptionexpired'} =
283       HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'}==1;
284     $data->{'abouttoexpire'} =
285       abouttoexpire( $data->{'subscriptionid'} );
286     return $data;
287 }
288
289 =head2 AddItem2Serial
290
291 =over 4
292
293 $data = AddItem2Serial($serialid,$itemnumber);
294 Adds an itemnumber to Serial record
295
296 =back
297
298 =cut
299
300 sub AddItem2Serial {
301     my ( $serialid, $itemnumber ) = @_;
302     my $dbh   = C4::Context->dbh;
303     my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
304     $rq->execute($serialid, $itemnumber);
305     return $rq->rows;
306 }
307
308 =head2 UpdateClaimdateIssues
309
310 =over 4
311
312 UpdateClaimdateIssues($serialids,[$date]);
313
314 Update Claimdate for issues in @$serialids list with date $date 
315 (Take Today if none)
316
317 =back
318
319 =cut
320
321 sub UpdateClaimdateIssues {
322     my ( $serialids, $date ) = @_;
323     my $dbh   = C4::Context->dbh;
324     $date = strftime("%Y-%m-%d",localtime) unless ($date);
325     my $query = "
326         UPDATE serial SET claimdate=$date,status=7
327         WHERE  serialid in ".join (",",@$serialids);
328     ;
329     my $rq = $dbh->prepare($query);
330     $rq->execute;
331     return $rq->rows;
332 }
333
334 =head2 GetSubscription
335
336 =over 4
337
338 $subs = GetSubscription($subscriptionid)
339 this function get the subscription which has $subscriptionid as id.
340 return :
341 a hashref. This hash containts
342 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
343
344 =back
345
346 =cut
347
348 sub GetSubscription {
349     my ($subscriptionid) = @_;
350     my $dbh              = C4::Context->dbh;
351     my $query            = qq(
352         SELECT  subscription.*,
353                 subscriptionhistory.*,
354                 subscriptionhistory.enddate as histenddate,
355                 aqbudget.bookfundid,
356                 aqbooksellers.name AS aqbooksellername,
357                 biblio.title AS bibliotitle,
358                 subscription.biblionumber as bibnum);
359        if (C4::Context->preference('IndependantBranches') && 
360               C4::Context->userenv && 
361               C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
362                 $query.="
363       , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
364         }
365             $query .= qq(             
366        FROM subscription
367        LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
368        LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
369        LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
370        LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
371        WHERE subscription.subscriptionid = ?
372     );
373 #     if (C4::Context->preference('IndependantBranches') && 
374 #         C4::Context->userenv && 
375 #         C4::Context->userenv->{'flags'} != 1){
376 # #       $debug and warn "flags: ".C4::Context->userenv->{'flags'};
377 #       $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
378 #     }
379         $debug and warn "query : $query\nsubsid :$subscriptionid";
380     my $sth = $dbh->prepare($query);
381     $sth->execute($subscriptionid);
382     return $sth->fetchrow_hashref;
383 }
384
385 =head2 GetFullSubscription
386
387 =over 4
388
389    \@res = GetFullSubscription($subscriptionid)
390    this function read on serial table.
391
392 =back
393
394 =cut
395
396 sub GetFullSubscription {
397     my ($subscriptionid) = @_;
398     my $dbh            = C4::Context->dbh;
399     my $query          = qq|
400   SELECT    serial.serialid,
401             serial.serialseq,
402             serial.planneddate, 
403             serial.publisheddate, 
404             serial.status, 
405             serial.notes as notes,
406             year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
407             aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,
408             biblio.title as bibliotitle,
409             subscription.branchcode AS branchcode,
410             subscription.subscriptionid AS subscriptionid |;
411     if (C4::Context->preference('IndependantBranches') && 
412         C4::Context->userenv && 
413         C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
414       $query.="
415       , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
416     }
417     $query.=qq|
418   FROM      serial 
419   LEFT JOIN subscription ON 
420           (serial.subscriptionid=subscription.subscriptionid )
421   LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid 
422   LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
423   LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber 
424   WHERE     serial.subscriptionid = ? 
425   ORDER BY year DESC,
426           IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
427           serial.subscriptionid
428           |;
429         $debug and warn "GetFullSubscription query: $query";   
430     my $sth = $dbh->prepare($query);
431     $sth->execute($subscriptionid);
432     return $sth->fetchall_arrayref({});
433 }
434
435
436 =head2 PrepareSerialsData
437
438 =over 4
439
440    \@res = PrepareSerialsData($serialinfomation)
441    where serialinformation is a hashref array
442
443 =back
444
445 =cut
446
447 sub PrepareSerialsData{
448     my ($lines)=@_;
449     my %tmpresults;
450     my $year;
451     my @res;
452     my $startdate;
453     my $aqbooksellername;
454     my $bibliotitle;
455     my @loopissues;
456     my $first;
457     my $previousnote = "";
458     
459     foreach  my $subs ( @$lines ) {
460         $subs->{'publisheddate'} =
461           ( $subs->{'publisheddate'}
462             ? format_date( $subs->{'publisheddate'} )
463             : "XXX" );
464         $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
465         $subs->{ "status" . $subs->{'status'} } = 1;
466
467 #         $subs->{'notes'} = $subs->{'notes'} eq $previousnote?"":$subs->{notes};
468         if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
469             $year = $subs->{'year'};
470         }
471         else {
472             $year = "manage";
473         }
474         if ( $tmpresults{$year} ) {
475             push @{ $tmpresults{$year}->{'serials'} }, $subs;
476         }
477         else {
478             $tmpresults{$year} = {
479                 'year' => $year,
480
481                 #               'startdate'=>format_date($subs->{'startdate'}),
482                 'aqbooksellername' => $subs->{'aqbooksellername'},
483                 'bibliotitle'      => $subs->{'bibliotitle'},
484                 'serials'          => [$subs],
485                 'first'            => $first,
486 #                 'branchcode'       => $subs->{'branchcode'},
487 #                 'subscriptionid'   => $subs->{'subscriptionid'},
488             };
489         }
490
491         #         $previousnote=$subs->{notes};
492     }
493     foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
494         push @res, $tmpresults{$key};
495     }
496     $res[0]->{'first'}=1;  
497     return \@res;
498 }
499
500 =head2 GetSubscriptionsFromBiblionumber
501
502 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
503 this function get the subscription list. it reads on subscription table.
504 return :
505 table of subscription which has the biblionumber given on input arg.
506 each line of this table is a hashref. All hashes containt
507 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
508
509 =cut
510
511 sub GetSubscriptionsFromBiblionumber {
512     my ($biblionumber) = @_;
513     my $dbh            = C4::Context->dbh;
514     my $query          = qq(
515         SELECT subscription.*,
516                branches.branchname,
517                subscriptionhistory.*,
518                subscriptionhistory.enddate as histenddate, 
519                aqbudget.bookfundid,
520                aqbooksellers.name AS aqbooksellername,
521                biblio.title AS bibliotitle
522        FROM subscription
523        LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
524        LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
525        LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
526        LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
527        LEFT JOIN branches ON branches.branchcode=subscription.branchcode
528        WHERE subscription.biblionumber = ?
529     );
530 #     if (C4::Context->preference('IndependantBranches') && 
531 #         C4::Context->userenv && 
532 #         C4::Context->userenv->{'flags'} != 1){
533 #        $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
534 #     }
535     my $sth = $dbh->prepare($query);
536     $sth->execute($biblionumber);
537     my @res;
538     while ( my $subs = $sth->fetchrow_hashref ) {
539         $subs->{startdate}     = format_date( $subs->{startdate} );
540         $subs->{histstartdate} = format_date( $subs->{histstartdate} );
541         $subs->{histenddate} = format_date( $subs->{histenddate} );
542         $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
543         $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
544         $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
545         $subs->{ "periodicity" . $subs->{periodicity} } = 1;
546         $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
547         $subs->{ "status" . $subs->{'status'} } = 1;
548         $subs->{'cannotedit'}=(C4::Context->preference('IndependantBranches') && 
549                 C4::Context->userenv && 
550                 C4::Context->userenv->{flags} % 2 !=1  && 
551                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
552                 (C4::Context->userenv->{branch} ne $subs->{branchcode}));
553         if ( $subs->{enddate} eq '0000-00-00' ) {
554             $subs->{enddate} = '';
555         }
556         else {
557             $subs->{enddate} = format_date( $subs->{enddate} );
558         }
559         $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
560         $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
561         push @res, $subs;
562     }
563     return \@res;
564 }
565
566 =head2 GetFullSubscriptionsFromBiblionumber
567
568 =over 4
569
570    \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
571    this function read on serial table.
572
573 =back
574
575 =cut
576
577 sub GetFullSubscriptionsFromBiblionumber {
578     my ($biblionumber) = @_;
579     my $dbh            = C4::Context->dbh;
580     my $query          = qq|
581   SELECT    serial.serialid,
582             serial.serialseq,
583             serial.planneddate, 
584             serial.publisheddate, 
585             serial.status, 
586             serial.notes as notes,
587             year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
588             aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,
589             biblio.title as bibliotitle,
590             subscription.branchcode AS branchcode,
591             subscription.subscriptionid AS subscriptionid|;
592      if (C4::Context->preference('IndependantBranches') && 
593         C4::Context->userenv && 
594         C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
595       $query.="
596       , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
597      }
598       
599      $query.=qq|      
600   FROM      serial 
601   LEFT JOIN subscription ON 
602           (serial.subscriptionid=subscription.subscriptionid)
603   LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid 
604   LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
605   LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber 
606   WHERE     subscription.biblionumber = ? 
607   ORDER BY year DESC,
608           IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
609           serial.subscriptionid
610           |;
611     my $sth = $dbh->prepare($query);
612     $sth->execute($biblionumber);
613     return $sth->fetchall_arrayref({});
614 }
615
616 =head2 GetSubscriptions
617
618 =over 4
619
620 @results = GetSubscriptions($title,$ISSN,$biblionumber);
621 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
622 return:
623 a table of hashref. Each hash containt the subscription.
624
625 =back
626
627 =cut
628
629 sub GetSubscriptions {
630     my ( $title, $ISSN, $biblionumber ) = @_;
631     #return unless $title or $ISSN or $biblionumber;
632     my $dbh = C4::Context->dbh;
633     my $sth;
634     if ($biblionumber) {
635         my $query = qq(
636             SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
637             FROM   subscription
638             LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
639             LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
640             WHERE biblio.biblionumber=?
641         );
642         $query.=" ORDER BY title";
643         $debug and warn "GetSubscriptions query: $query";
644         $sth = $dbh->prepare($query);
645         $sth->execute($biblionumber);
646     }
647     else {
648         if ( $ISSN and $title ) {
649             my $query = qq|
650                 SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber        
651                 FROM   subscription
652                 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
653                 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
654                 WHERE (biblioitems.issn = ? or|. join('and ',map{"biblio.title LIKE \"%$_%\""}split (" ",$title))." )";
655             $query.=" ORDER BY title";
656                 $debug and warn "GetSubscriptions query: $query";
657             $sth = $dbh->prepare($query);
658             $sth->execute( $ISSN );
659         }
660         else {
661             if ($ISSN) {
662                 my $query = qq(
663                     SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
664                     FROM   subscription
665                     LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
666                     LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
667                     WHERE biblioitems.issn LIKE ?
668                 );
669                 $query.=" ORDER BY title";
670                         $debug and warn "GetSubscriptions query: $query";
671                 $sth = $dbh->prepare($query);
672                 $sth->execute( "%" . $ISSN . "%" );
673             }
674             else {
675                 my $query = qq(
676                     SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
677                     FROM   subscription
678                     LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
679                     LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
680                     WHERE 1
681                     ).($title?" and ":""). join('and ',map{"biblio.title LIKE \"%$_%\""} split (" ",$title) );
682                 
683                 $query.=" ORDER BY title";
684                         $debug and warn "GetSubscriptions query: $query";
685                 $sth = $dbh->prepare($query);
686                 $sth->execute;
687             }
688         }
689     }
690     my @results;
691     my $previoustitle = "";
692     my $odd           = 1;
693     while ( my $line = $sth->fetchrow_hashref ) {
694         if ( $previoustitle eq $line->{title} ) {
695             $line->{title}  = "";
696             $line->{issn}   = "";
697         }
698         else {
699             $previoustitle = $line->{title};
700             $odd           = -$odd;
701         }
702         $line->{toggle} = 1 if $odd == 1;
703         $line->{'cannotedit'}=(C4::Context->preference('IndependantBranches') && 
704                 C4::Context->userenv && 
705                 C4::Context->userenv->{flags} % 2 !=1  && 
706                 C4::Context->userenv->{branch} && $line->{branchcode} &&
707                 (C4::Context->userenv->{branch} ne $line->{branchcode}));
708         push @results, $line;
709     }
710     return @results;
711 }
712
713 =head2 GetSerials
714
715 =over 4
716
717 ($totalissues,@serials) = GetSerials($subscriptionid);
718 this function get every serial not arrived for a given subscription
719 as well as the number of issues registered in the database (all types)
720 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
721
722 FIXME: We should return \@serials.
723
724 =back
725
726 =cut
727
728 sub GetSerials {
729     my ($subscriptionid,$count) = @_;
730     my $dbh = C4::Context->dbh;
731
732     # status = 2 is "arrived"
733     my $counter = 0;
734     $count=5 unless ($count);
735     my @serials;
736     my $query =
737       "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
738                         FROM   serial
739                         WHERE  subscriptionid = ? AND status NOT IN (2,4,5) 
740                         ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
741     my $sth = $dbh->prepare($query);
742     $sth->execute($subscriptionid);
743     while ( my $line = $sth->fetchrow_hashref ) {
744         $line->{ "status" . $line->{status} } =
745           1;    # fills a "statusX" value, used for template status select list
746         $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
747         $line->{"planneddate"}   = format_date( $line->{"planneddate"} );
748         push @serials, $line;
749     }
750     # OK, now add the last 5 issues arrives/missing
751     $query =
752       "SELECT   serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
753        FROM     serial
754        WHERE    subscriptionid = ?
755        AND      (status in (2,4,5))
756        ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
757       ";
758     $sth = $dbh->prepare($query);
759     $sth->execute($subscriptionid);
760     while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
761         $counter++;
762         $line->{ "status" . $line->{status} } =
763           1;    # fills a "statusX" value, used for template status select list
764         $line->{"planneddate"}   = format_date( $line->{"planneddate"} );
765         $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
766         push @serials, $line;
767     }
768
769     $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
770     $sth = $dbh->prepare($query);
771     $sth->execute($subscriptionid);
772     my ($totalissues) = $sth->fetchrow;
773     return ( $totalissues, @serials );
774 }
775
776 =head2 GetSerials2
777
778 =over 4
779
780 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
781 this function get every serial waited for a given subscription
782 as well as the number of issues registered in the database (all types)
783 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
784
785 =back
786
787 =cut
788 sub GetSerials2 {
789     my ($subscription,$status) = @_;
790     my $dbh = C4::Context->dbh;
791     my $query = qq|
792                  SELECT   serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
793                  FROM     serial 
794                  WHERE    subscriptionid=$subscription AND status IN ($status)
795                  ORDER BY publisheddate,serialid DESC
796                     |;
797         $debug and warn "GetSerials2 query: $query";
798     my $sth=$dbh->prepare($query);
799     $sth->execute;
800     my @serials;
801     while(my $line = $sth->fetchrow_hashref) {
802         $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
803         $line->{"planneddate"} = format_date($line->{"planneddate"});
804         $line->{"publisheddate"} = format_date($line->{"publisheddate"});
805         push @serials,$line;
806     }
807     my ($totalissues) = scalar(@serials);
808     return ($totalissues,@serials);
809 }
810
811 =head2 GetLatestSerials
812
813 =over 4
814
815 \@serials = GetLatestSerials($subscriptionid,$limit)
816 get the $limit's latest serials arrived or missing for a given subscription
817 return :
818 a ref to a table which it containts all of the latest serials stored into a hash.
819
820 =back
821
822 =cut
823
824 sub GetLatestSerials {
825     my ( $subscriptionid, $limit ) = @_;
826     my $dbh = C4::Context->dbh;
827
828     # status = 2 is "arrived"
829     my $strsth = "SELECT   serialid,serialseq, status, planneddate, notes
830                         FROM     serial
831                         WHERE    subscriptionid = ?
832                         AND      (status =2 or status=4)
833                         ORDER BY publisheddate DESC LIMIT 0,$limit
834                 ";
835     my $sth = $dbh->prepare($strsth);
836     $sth->execute($subscriptionid);
837     my @serials;
838     while ( my $line = $sth->fetchrow_hashref ) {
839         $line->{ "status" . $line->{status} } =
840           1;    # fills a "statusX" value, used for template status select list
841         $line->{"planneddate"} = format_date( $line->{"planneddate"} );
842         push @serials, $line;
843     }
844
845     #     my $query = qq|
846     #         SELECT count(*)
847     #         FROM   serial
848     #         WHERE  subscriptionid=?
849     #     |;
850     #     $sth=$dbh->prepare($query);
851     #     $sth->execute($subscriptionid);
852     #     my ($totalissues) = $sth->fetchrow;
853     return \@serials;
854 }
855
856 =head2 GetNextSeq
857
858 =over 4
859
860 GetNextSeq($val)
861 $val is a hashref containing all the attributes of the table 'subscription'
862 This function get the next issue for the subscription given on input arg
863 return:
864 all the input params updated.
865
866 =back
867
868 =cut
869
870 # sub GetNextSeq {
871 #     my ($val) =@_;
872 #     my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
873 #     $calculated = $val->{numberingmethod};
874 # # calculate the (expected) value of the next issue recieved.
875 #     $newlastvalue1 = $val->{lastvalue1};
876 # # check if we have to increase the new value.
877 #     $newinnerloop1 = $val->{innerloop1}+1;
878 #     $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
879 #     $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
880 #     $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
881 #     $calculated =~ s/\{X\}/$newlastvalue1/g;
882 #
883 #     $newlastvalue2 = $val->{lastvalue2};
884 # # check if we have to increase the new value.
885 #     $newinnerloop2 = $val->{innerloop2}+1;
886 #     $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
887 #     $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
888 #     $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
889 #     $calculated =~ s/\{Y\}/$newlastvalue2/g;
890 #
891 #     $newlastvalue3 = $val->{lastvalue3};
892 # # check if we have to increase the new value.
893 #     $newinnerloop3 = $val->{innerloop3}+1;
894 #     $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
895 #     $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
896 #     $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
897 #     $calculated =~ s/\{Z\}/$newlastvalue3/g;
898 #     return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
899 # }
900
901 sub GetNextSeq {
902     my ($val) = @_;
903     my (
904         $calculated,    $newlastvalue1, $newlastvalue2, $newlastvalue3,
905         $newinnerloop1, $newinnerloop2, $newinnerloop3
906     );
907     my $pattern = $val->{numberpattern};
908     my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
909     my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
910     $calculated    = $val->{numberingmethod};
911     $newlastvalue1 = $val->{lastvalue1};
912     $newlastvalue2 = $val->{lastvalue2};
913     $newlastvalue3 = $val->{lastvalue3};
914   $newlastvalue1 = $val->{lastvalue1};
915   # check if we have to increase the new value.
916   $newinnerloop1 = $val->{innerloop1} + 1;
917   $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
918   $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
919   $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
920   $calculated =~ s/\{X\}/$newlastvalue1/g;
921   
922   $newlastvalue2 = $val->{lastvalue2};
923   # check if we have to increase the new value.
924   $newinnerloop2 = $val->{innerloop2} + 1;
925   $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
926   $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
927   $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
928   if ( $pattern == 6 ) {
929     if ( $val->{hemisphere} == 2 ) {
930        my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
931        $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
932     }
933     else {
934        my $newlastvalue2seq = $seasons[$newlastvalue2];
935        $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
936     }
937   }
938   else {
939     $calculated =~ s/\{Y\}/$newlastvalue2/g;
940   }
941   
942   
943   $newlastvalue3 = $val->{lastvalue3};
944   # check if we have to increase the new value.
945   $newinnerloop3 = $val->{innerloop3} + 1;
946   $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
947   $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
948   $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
949   $calculated =~ s/\{Z\}/$newlastvalue3/g;
950     
951   return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3 ,
952            $newinnerloop1, $newinnerloop2, $newinnerloop3);
953 }
954
955 =head2 GetSeq
956
957 =over 4
958
959 $calculated = GetSeq($val)
960 $val is a hashref containing all the attributes of the table 'subscription'
961 this function transforms {X},{Y},{Z} to 150,0,0 for example.
962 return:
963 the sequence in integer format
964
965 =back
966
967 =cut
968
969 sub GetSeq {
970     my ($val)      = @_;
971     my $pattern = $val->{numberpattern};
972     my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
973     my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
974     my $calculated = $val->{numberingmethod};
975     my $x          = $val->{'lastvalue1'};
976     $calculated =~ s/\{X\}/$x/g;
977     my $newlastvalue2 = $val->{'lastvalue2'};
978     if ( $pattern == 6 ) {
979         if ( $val->{hemisphere} == 2 ) {
980             my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
981             $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
982         }
983         else {
984             my $newlastvalue2seq = $seasons[$newlastvalue2];
985             $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
986         }
987     }
988     else {
989         $calculated =~ s/\{Y\}/$newlastvalue2/g;
990     }
991     my $z = $val->{'lastvalue3'};
992     $calculated =~ s/\{Z\}/$z/g;
993     return $calculated;
994 }
995
996 =head2 GetExpirationDate
997
998 $sensddate = GetExpirationDate($subscriptionid)
999
1000 this function return the expiration date for a subscription given on input args.
1001
1002 return
1003 the enddate
1004
1005 =cut
1006
1007 sub GetExpirationDate {
1008     my ($subscriptionid) = @_;
1009     my $dbh              = C4::Context->dbh;
1010     my $subscription     = GetSubscription($subscriptionid);
1011     my $enddate          = $subscription->{startdate};
1012
1013 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1014     if (($subscription->{periodicity} % 16) >0){
1015       if ( $subscription->{numberlength} ) {
1016           #calculate the date of the last issue.
1017           my $length = $subscription->{numberlength};
1018           for ( my $i = 1 ; $i <= $length ; $i++ ) {
1019               $enddate = GetNextDate( $enddate, $subscription );
1020           }
1021       }
1022       elsif ( $subscription->{monthlength} ){
1023           my @date=split (/-/,$subscription->{startdate});
1024           my @enddate = Add_Delta_YM($date[0],$date[1],$date[2],0,$subscription->{monthlength});
1025           $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1026       } elsif ( $subscription->{weeklength} ){
1027           my @date=split (/-/,$subscription->{startdate});
1028           my @enddate = Add_Delta_Days($date[0],$date[1],$date[2],$subscription->{weeklength}*7);
1029           $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1030       }
1031       return $enddate;
1032     } else {
1033       return 0;  
1034     }  
1035 }
1036
1037 =head2 CountSubscriptionFromBiblionumber
1038
1039 =over 4
1040
1041 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1042 this count the number of subscription for a biblionumber given.
1043 return :
1044 the number of subscriptions with biblionumber given on input arg.
1045
1046 =back
1047
1048 =cut
1049
1050 sub CountSubscriptionFromBiblionumber {
1051     my ($biblionumber) = @_;
1052     my $dbh = C4::Context->dbh;
1053     my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1054     my $sth   = $dbh->prepare($query);
1055     $sth->execute($biblionumber);
1056     my $subscriptionsnumber = $sth->fetchrow;
1057     return $subscriptionsnumber;
1058 }
1059
1060 =head2 ModSubscriptionHistory
1061
1062 =over 4
1063
1064 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1065
1066 this function modify the history of a subscription. Put your new values on input arg.
1067
1068 =back
1069
1070 =cut
1071
1072 sub ModSubscriptionHistory {
1073     my (
1074         $subscriptionid, $histstartdate, $enddate, $recievedlist,
1075         $missinglist,    $opacnote,      $librariannote
1076     ) = @_;
1077     my $dbh   = C4::Context->dbh;
1078     my $query = "UPDATE subscriptionhistory 
1079                     SET histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1080                     WHERE subscriptionid=?
1081                 ";
1082     my $sth = $dbh->prepare($query);
1083     $recievedlist =~ s/^; //;
1084     $missinglist  =~ s/^; //;
1085     $opacnote     =~ s/^; //;
1086     $sth->execute(
1087         $histstartdate, $enddate,       $recievedlist, $missinglist,
1088         $opacnote,      $librariannote, $subscriptionid
1089     );
1090     return $sth->rows;
1091 }
1092
1093 =head2 ModSerialStatus
1094
1095 =over 4
1096
1097 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1098
1099 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1100 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1101
1102 =back
1103
1104 =cut
1105
1106 sub ModSerialStatus {
1107     my ( $serialid, $serialseq,  $planneddate,$publisheddate, $status, $notes )
1108       = @_;
1109
1110     #It is a usual serial
1111     # 1st, get previous status :
1112     my $dbh   = C4::Context->dbh;
1113     my $query = "SELECT subscriptionid,status FROM serial WHERE  serialid=?";
1114     my $sth   = $dbh->prepare($query);
1115     $sth->execute($serialid);
1116     my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1117
1118     # change status & update subscriptionhistory
1119     my $val;
1120     if ( $status eq 6 ) {
1121         DelIssue( {'serialid'=>$serialid, 'subscriptionid'=>$subscriptionid,'serialseq'=>$serialseq} );
1122     }
1123     else {
1124         my $query =
1125 "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE  serialid = ?";
1126         $sth = $dbh->prepare($query);
1127         $sth->execute( $serialseq, $publisheddate, $planneddate, $status,
1128             $notes, $serialid );
1129         $query = "SELECT * FROM   subscription WHERE  subscriptionid = ?";
1130         $sth = $dbh->prepare($query);
1131         $sth->execute($subscriptionid);
1132         my $val = $sth->fetchrow_hashref;
1133         unless ( $val->{manualhistory} ) {
1134             $query =
1135 "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE  subscriptionid=?";
1136             $sth = $dbh->prepare($query);
1137             $sth->execute($subscriptionid);
1138             my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1139             if ( $status eq 2 ) {
1140
1141                 $recievedlist .= "; $serialseq"
1142                   unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1143             }
1144
1145 #         warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1146             $missinglist .= "; $serialseq"
1147               if ( $status eq 4
1148                 and not index( "$missinglist", "$serialseq" ) >= 0 );
1149             $missinglist .= "; not issued $serialseq"
1150               if ( $status eq 5
1151                 and index( "$missinglist", "$serialseq" ) >= 0 );
1152             $query =
1153 "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE  subscriptionid=?";
1154             $sth = $dbh->prepare($query);
1155             $recievedlist =~ s/^; //;
1156             $missinglist  =~ s/^; //;
1157             $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1158         }
1159     }
1160
1161     # create new waited entry if needed (ie : was a "waited" and has changed)
1162     if ( $oldstatus eq 1 && $status ne 1 ) {
1163         my $query = "SELECT * FROM   subscription WHERE  subscriptionid = ?";
1164         $sth = $dbh->prepare($query);
1165         $sth->execute($subscriptionid);
1166         my $val = $sth->fetchrow_hashref;
1167
1168         # next issue number
1169 #     warn "Next Seq";    
1170         my (
1171             $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
1172             $newinnerloop1, $newinnerloop2, $newinnerloop3
1173         ) = GetNextSeq($val);
1174 #     warn "Next Seq End";    
1175
1176         # next date (calculated from actual date & frequency parameters)
1177 #         warn "publisheddate :$publisheddate ";
1178         my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1179         NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'},
1180             1, $nextpublisheddate, $nextpublisheddate );
1181         $query =
1182 "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1183                     WHERE  subscriptionid = ?";
1184         $sth = $dbh->prepare($query);
1185         $sth->execute(
1186             $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
1187             $newinnerloop2, $newinnerloop3, $subscriptionid
1188         );
1189
1190 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1191         if ( $val->{letter} && $status eq 2 && $oldstatus ne 2 ) {
1192             SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1193         }
1194     }
1195 }
1196
1197 =head2 GetNextExpected
1198
1199 =over 4
1200
1201 $nextexpected = GetNextExpected($subscriptionid)
1202
1203 Get the planneddate for the current expected issue of the subscription.
1204
1205 returns a hashref:
1206
1207 $nextexepected = {
1208     serialid => int
1209     planneddate => C4::Dates object
1210     }
1211
1212 =back
1213
1214 =cut
1215
1216 sub GetNextExpected($) {
1217     my ($subscriptionid) = @_;
1218     my $dbh = C4::Context->dbh;
1219     my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1220     # Each subscription has only one 'expected' issue, with serial.status==1.
1221     $sth->execute( $subscriptionid, 1 );
1222     my ( $nextissue ) = $sth->fetchrow_hashref;
1223     if(not $nextissue){
1224          $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid  = ? ORDER BY planneddate DESC LIMIT 1');
1225          $sth->execute( $subscriptionid );  
1226          $nextissue = $sth->fetchrow_hashref;       
1227     }
1228     $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
1229     return $nextissue;
1230     
1231 }
1232 =head2 ModNextExpected
1233
1234 =over 4
1235
1236 ModNextExpected($subscriptionid,$date)
1237
1238 Update the planneddate for the current expected issue of the subscription.
1239 This will modify all future prediction results.  
1240
1241 C<$date> is a C4::Dates object.
1242
1243 =back
1244
1245 =cut
1246
1247 sub ModNextExpected($$) {
1248     my ($subscriptionid,$date) = @_;
1249     my $dbh = C4::Context->dbh;
1250     #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1251     my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1252     # Each subscription has only one 'expected' issue, with serial.status==1.
1253     $sth->execute( $date->output('iso'),$date->output('iso'), $subscriptionid, 1);
1254     return 0;
1255
1256 }
1257
1258 =head2 ModSubscription
1259
1260 =over 4
1261
1262 this function modify a subscription. Put all new values on input args.
1263
1264 =back
1265
1266 =cut
1267
1268 sub ModSubscription {
1269     my (
1270         $auser,           $branchcode,   $aqbooksellerid, $cost,
1271         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
1272         $dow,             $irregularity, $numberpattern,  $numberlength,
1273         $weeklength,      $monthlength,  $add1,           $every1,
1274         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
1275         $add2,            $every2,       $whenmorethan2,  $setto2,
1276         $lastvalue2,      $innerloop2,   $add3,           $every3,
1277         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
1278         $numberingmethod, $status,       $biblionumber,   $callnumber,
1279         $notes,           $letter,       $hemisphere,     $manualhistory,
1280         $internalnotes,   $serialsadditems,$subscriptionid,
1281         $staffdisplaycount,$opacdisplaycount, $graceperiod, $location
1282     ) = @_;
1283 #     warn $irregularity;
1284     my $dbh   = C4::Context->dbh;
1285     my $query = "UPDATE subscription
1286                     SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1287                         periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1288                         add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1289                         add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1290                         add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1291                         numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1292                     WHERE subscriptionid = ?";
1293      #warn "query :".$query;
1294     my $sth = $dbh->prepare($query);
1295     $sth->execute(
1296         $auser,           $branchcode,   $aqbooksellerid, $cost,
1297         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
1298         $dow,             "$irregularity", $numberpattern,  $numberlength,
1299         $weeklength,      $monthlength,  $add1,           $every1,
1300         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
1301         $add2,            $every2,       $whenmorethan2,  $setto2,
1302         $lastvalue2,      $innerloop2,   $add3,           $every3,
1303         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
1304         $numberingmethod, $status,       $biblionumber,   $callnumber,
1305         $notes,           $letter,       $hemisphere,     ($manualhistory?$manualhistory:0),
1306         $internalnotes,   $serialsadditems,
1307         $staffdisplaycount, $opacdisplaycount, $graceperiod, $location,
1308         $subscriptionid
1309     );
1310     my $rows=$sth->rows;
1311     $sth->finish;
1312     
1313     logaction("SERIAL", "MODIFY", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1314     return $rows;
1315 }
1316
1317 =head2 NewSubscription
1318
1319 =over 4
1320
1321 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1322     $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1323     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1324     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1325     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1326     $numberingmethod, $status, $notes, $serialsadditems,
1327     $staffdisplaycount, $opacdisplaycount, $graceperiod, $location);
1328
1329 Create a new subscription with value given on input args.
1330
1331 return :
1332 the id of this new subscription
1333
1334 =back
1335
1336 =cut
1337
1338 sub NewSubscription {
1339     my (
1340         $auser,         $branchcode,   $aqbooksellerid,  $cost,
1341         $aqbudgetid,    $biblionumber, $startdate,       $periodicity,
1342         $dow,           $numberlength, $weeklength,      $monthlength,
1343         $add1,          $every1,       $whenmorethan1,   $setto1,
1344         $lastvalue1,    $innerloop1,   $add2,            $every2,
1345         $whenmorethan2, $setto2,       $lastvalue2,      $innerloop2,
1346         $add3,          $every3,       $whenmorethan3,   $setto3,
1347         $lastvalue3,    $innerloop3,   $numberingmethod, $status,
1348         $notes,         $letter,       $firstacquidate,  $irregularity,
1349         $numberpattern, $callnumber,   $hemisphere,      $manualhistory,
1350         $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1351         $graceperiod, $location
1352     ) = @_;
1353     my $dbh = C4::Context->dbh;
1354
1355     #save subscription (insert into database)
1356     my $query = qq|
1357         INSERT INTO subscription
1358             (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1359             startdate,periodicity,dow,numberlength,weeklength,monthlength,
1360             add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1361             add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1362             add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1363             numberingmethod, status, notes, letter,firstacquidate,irregularity,
1364             numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1365             staffdisplaycount,opacdisplaycount,graceperiod,location)
1366         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1367         |;
1368     my $sth = $dbh->prepare($query);
1369     $sth->execute(
1370         $auser,                         $branchcode,
1371         $aqbooksellerid,                $cost,
1372         $aqbudgetid,                    $biblionumber,
1373         format_date_in_iso($startdate), $periodicity,
1374         $dow,                           $numberlength,
1375         $weeklength,                    $monthlength,
1376         $add1,                          $every1,
1377         $whenmorethan1,                 $setto1,
1378         $lastvalue1,                    $innerloop1,
1379         $add2,                          $every2,
1380         $whenmorethan2,                 $setto2,
1381         $lastvalue2,                    $innerloop2,
1382         $add3,                          $every3,
1383         $whenmorethan3,                 $setto3,
1384         $lastvalue3,                    $innerloop3,
1385         $numberingmethod,               "$status",
1386         $notes,                         $letter,
1387         format_date_in_iso($firstacquidate),                $irregularity,
1388         $numberpattern,                 $callnumber,
1389         $hemisphere,                    $manualhistory,
1390         $internalnotes,                 $serialsadditems,
1391                 $staffdisplaycount,                             $opacdisplaycount,
1392         $graceperiod,                   $location,
1393     );
1394
1395     #then create the 1st waited number
1396     my $subscriptionid = $dbh->{'mysql_insertid'};
1397     $query             = qq(
1398         INSERT INTO subscriptionhistory
1399             (biblionumber, subscriptionid, histstartdate,  opacnote, librariannote)
1400         VALUES (?,?,?,?,?)
1401         );
1402     $sth = $dbh->prepare($query);
1403     $sth->execute( $biblionumber, $subscriptionid,
1404         format_date_in_iso($startdate),
1405         $notes,$internalnotes );
1406
1407    # reread subscription to get a hash (for calculation of the 1st issue number)
1408     $query = qq(
1409         SELECT *
1410         FROM   subscription
1411         WHERE  subscriptionid = ?
1412     );
1413     $sth = $dbh->prepare($query);
1414     $sth->execute($subscriptionid);
1415     my $val = $sth->fetchrow_hashref;
1416
1417     # calculate issue number
1418     my $serialseq = GetSeq($val);
1419     $query     = qq|
1420         INSERT INTO serial
1421             (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1422         VALUES (?,?,?,?,?,?)
1423     |;
1424     $sth = $dbh->prepare($query);
1425     $sth->execute(
1426         "$serialseq", $subscriptionid, $biblionumber, 1,
1427         format_date_in_iso($firstacquidate),
1428         format_date_in_iso($firstacquidate)
1429     );
1430     
1431     logaction("SERIAL", "ADD", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1432     
1433 #set serial flag on biblio if not already set.
1434     my ($null, ($bib)) = GetBiblio($biblionumber);
1435     if( ! $bib->{'serial'} ) {
1436         my $record = GetMarcBiblio($biblionumber);
1437         my ($tag,$subf) = GetMarcFromKohaField('biblio.serial',$bib->{'frameworkcode'});
1438         if($tag) {
1439             eval {
1440             $record->field($tag)->update( $subf => 1 );
1441             };
1442         }
1443         ModBiblio($record,$biblionumber,$bib->{'frameworkcode'});
1444     }    
1445     return $subscriptionid;
1446 }
1447
1448 =head2 ReNewSubscription
1449
1450 =over 4
1451
1452 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1453
1454 this function renew a subscription with values given on input args.
1455
1456 =back
1457
1458 =cut
1459
1460 sub ReNewSubscription {
1461     my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength,
1462         $monthlength, $note )
1463       = @_;
1464     my $dbh          = C4::Context->dbh;
1465     my $subscription = GetSubscription($subscriptionid);
1466      my $query        = qq|
1467          SELECT *
1468          FROM   biblio 
1469          LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1470          WHERE    biblio.biblionumber=?
1471      |;
1472      my $sth = $dbh->prepare($query);
1473      $sth->execute( $subscription->{biblionumber} );
1474      my $biblio = $sth->fetchrow_hashref;
1475      if (C4::Context->preference("RenewSerialAddsSuggestion")){
1476         NewSuggestion(
1477             $user,             $subscription->{bibliotitle},
1478             $biblio->{author}, $biblio->{publishercode},
1479             $biblio->{note},   '',
1480             '',                '',
1481             '',                '',
1482             $subscription->{biblionumber}
1483         );
1484     }
1485
1486     # renew subscription
1487     $query = qq|
1488         UPDATE subscription
1489         SET    startdate=?,numberlength=?,weeklength=?,monthlength=?
1490         WHERE  subscriptionid=?
1491     |;
1492     $sth = $dbh->prepare($query);
1493     $sth->execute( format_date_in_iso($startdate),
1494         $numberlength, $weeklength, $monthlength, $subscriptionid );
1495         
1496     logaction("SERIAL", "RENEW", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1497 }
1498
1499 =head2 NewIssue
1500
1501 =over 4
1502
1503 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate,  $notes)
1504
1505 Create a new issue stored on the database.
1506 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1507
1508 =back
1509
1510 =cut
1511
1512 sub NewIssue {
1513     my ( $serialseq, $subscriptionid, $biblionumber, $status, 
1514         $planneddate, $publisheddate, $notes )
1515       = @_;
1516     ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1517     
1518     my $dbh   = C4::Context->dbh;
1519     my $query = qq|
1520         INSERT INTO serial
1521             (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1522         VALUES (?,?,?,?,?,?,?)
1523     |;
1524     my $sth = $dbh->prepare($query);
1525     $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status,
1526         $publisheddate, $planneddate,$notes );
1527     my $serialid=$dbh->{'mysql_insertid'};
1528     $query = qq|
1529         SELECT missinglist,recievedlist
1530         FROM   subscriptionhistory
1531         WHERE  subscriptionid=?
1532     |;
1533     $sth = $dbh->prepare($query);
1534     $sth->execute($subscriptionid);
1535     my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1536
1537     if ( $status eq 2 ) {
1538       ### TODO Add a feature that improves recognition and description.
1539       ### As such count (serialseq) i.e. : N18,2(N19),N20
1540       ### Would use substr and index But be careful to previous presence of ()
1541         $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1542     }
1543     if ( $status eq 4 ) {
1544         $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1545     }
1546     $query = qq|
1547         UPDATE subscriptionhistory
1548         SET    recievedlist=?, missinglist=?
1549         WHERE  subscriptionid=?
1550     |;
1551     $sth = $dbh->prepare($query);
1552     $recievedlist =~ s/^; //;
1553     $missinglist  =~ s/^; //;
1554     $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1555     return $serialid;
1556 }
1557
1558 =head2 ItemizeSerials
1559
1560 =over 4
1561
1562 ItemizeSerials($serialid, $info);
1563 $info is a hashref containing  barcode branch, itemcallnumber, status, location
1564 $serialid the serialid
1565 return :
1566 1 if the itemize is a succes.
1567 0 and @error else. @error containts the list of errors found.
1568
1569 =back
1570
1571 =cut
1572
1573 sub ItemizeSerials {
1574     my ( $serialid, $info ) = @_;
1575     my $now = POSIX::strftime( "%Y-%m-%d",localtime );
1576
1577     my $dbh   = C4::Context->dbh;
1578     my $query = qq|
1579         SELECT *
1580         FROM   serial
1581         WHERE  serialid=?
1582     |;
1583     my $sth = $dbh->prepare($query);
1584     $sth->execute($serialid);
1585     my $data = $sth->fetchrow_hashref;
1586     if ( C4::Context->preference("RoutingSerials") ) {
1587
1588         # check for existing biblioitem relating to serial issue
1589         my ( $count, @results ) =
1590           GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1591         my $bibitemno = 0;
1592         for ( my $i = 0 ; $i < $count ; $i++ ) {
1593             if (  $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' ('
1594                 . $data->{'planneddate'}
1595                 . ')' )
1596             {
1597                 $bibitemno = $results[$i]->{'biblioitemnumber'};
1598                 last;
1599             }
1600         }
1601         if ( $bibitemno == 0 ) {
1602
1603     # warn "need to add new biblioitem so copy last one and make minor changes";
1604             my $sth =
1605               $dbh->prepare(
1606 "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC"
1607               );
1608             $sth->execute( $data->{'biblionumber'} );
1609             my $biblioitem = $sth->fetchrow_hashref;
1610             $biblioitem->{'volumedate'} =
1611               format_date_in_iso( $data->{planneddate} );
1612             $biblioitem->{'volumeddesc'} =
1613               $data->{serialseq} . ' ('
1614               . format_date( $data->{'planneddate'} ) . ')';
1615             $biblioitem->{'dewey'} = $info->{itemcallnumber};
1616
1617             #FIXME  HDL : I don't understand why you need to call newbiblioitem, as the biblioitem should already be somewhere.
1618             # so I comment it, we can speak of it when you want
1619             # newbiblioitems has been removed from Biblio.pm, as it has a deprecated API now
1620 #             if ( $info->{barcode} )
1621 #             {    # only make biblioitem if we are going to make item also
1622 #                 $bibitemno = newbiblioitem($biblioitem);
1623 #             }
1624         }
1625     }
1626
1627     my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1628     if ( $info->{barcode} ) {
1629         my @errors;
1630         my $exists = itemdata( $info->{'barcode'} );
1631         push @errors, "barcode_not_unique" if ($exists);
1632         unless ($exists) {
1633             my $marcrecord = MARC::Record->new();
1634             my ( $tag, $subfield ) =
1635               GetMarcFromKohaField( "items.barcode", $fwk );
1636             my $newField =
1637               MARC::Field->new( "$tag", '', '',
1638                 "$subfield" => $info->{barcode} );
1639             $marcrecord->insert_fields_ordered($newField);
1640             if ( $info->{branch} ) {
1641                 my ( $tag, $subfield ) =
1642                   GetMarcFromKohaField( "items.homebranch",
1643                     $fwk );
1644
1645                 #warn "items.homebranch : $tag , $subfield";
1646                 if ( $marcrecord->field($tag) ) {
1647                     $marcrecord->field($tag)
1648                       ->add_subfields( "$subfield" => $info->{branch} );
1649                 }
1650                 else {
1651                     my $newField =
1652                       MARC::Field->new( "$tag", '', '',
1653                         "$subfield" => $info->{branch} );
1654                     $marcrecord->insert_fields_ordered($newField);
1655                 }
1656                 ( $tag, $subfield ) =
1657                   GetMarcFromKohaField( "items.holdingbranch",
1658                     $fwk );
1659
1660                 #warn "items.holdingbranch : $tag , $subfield";
1661                 if ( $marcrecord->field($tag) ) {
1662                     $marcrecord->field($tag)
1663                       ->add_subfields( "$subfield" => $info->{branch} );
1664                 }
1665                 else {
1666                     my $newField =
1667                       MARC::Field->new( "$tag", '', '',
1668                         "$subfield" => $info->{branch} );
1669                     $marcrecord->insert_fields_ordered($newField);
1670                 }
1671             }
1672             if ( $info->{itemcallnumber} ) {
1673                 my ( $tag, $subfield ) =
1674                   GetMarcFromKohaField( "items.itemcallnumber",
1675                     $fwk );
1676
1677                 #warn "items.itemcallnumber : $tag , $subfield";
1678                 if ( $marcrecord->field($tag) ) {
1679                     $marcrecord->field($tag)
1680                       ->add_subfields( "$subfield" => $info->{itemcallnumber} );
1681                 }
1682                 else {
1683                     my $newField =
1684                       MARC::Field->new( "$tag", '', '',
1685                         "$subfield" => $info->{itemcallnumber} );
1686                     $marcrecord->insert_fields_ordered($newField);
1687                 }
1688             }
1689             if ( $info->{notes} ) {
1690                 my ( $tag, $subfield ) =
1691                   GetMarcFromKohaField( "items.itemnotes", $fwk );
1692
1693                 # warn "items.itemnotes : $tag , $subfield";
1694                 if ( $marcrecord->field($tag) ) {
1695                     $marcrecord->field($tag)
1696                       ->add_subfields( "$subfield" => $info->{notes} );
1697                 }
1698                 else {
1699                     my $newField =
1700                       MARC::Field->new( "$tag", '', '',
1701                         "$subfield" => $info->{notes} );
1702                     $marcrecord->insert_fields_ordered($newField);
1703                 }
1704             }
1705             if ( $info->{location} ) {
1706                 my ( $tag, $subfield ) =
1707                   GetMarcFromKohaField( "items.location", $fwk );
1708
1709                 # warn "items.location : $tag , $subfield";
1710                 if ( $marcrecord->field($tag) ) {
1711                     $marcrecord->field($tag)
1712                       ->add_subfields( "$subfield" => $info->{location} );
1713                 }
1714                 else {
1715                     my $newField =
1716                       MARC::Field->new( "$tag", '', '',
1717                         "$subfield" => $info->{location} );
1718                     $marcrecord->insert_fields_ordered($newField);
1719                 }
1720             }
1721             if ( $info->{status} ) {
1722                 my ( $tag, $subfield ) =
1723                   GetMarcFromKohaField( "items.notforloan",
1724                     $fwk );
1725
1726                 # warn "items.notforloan : $tag , $subfield";
1727                 if ( $marcrecord->field($tag) ) {
1728                     $marcrecord->field($tag)
1729                       ->add_subfields( "$subfield" => $info->{status} );
1730                 }
1731                 else {
1732                     my $newField =
1733                       MARC::Field->new( "$tag", '', '',
1734                         "$subfield" => $info->{status} );
1735                     $marcrecord->insert_fields_ordered($newField);
1736                 }
1737             }
1738             if ( C4::Context->preference("RoutingSerials") ) {
1739                 my ( $tag, $subfield ) =
1740                   GetMarcFromKohaField( "items.dateaccessioned",
1741                     $fwk );
1742                 if ( $marcrecord->field($tag) ) {
1743                     $marcrecord->field($tag)
1744                       ->add_subfields( "$subfield" => $now );
1745                 }
1746                 else {
1747                     my $newField =
1748                       MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1749                     $marcrecord->insert_fields_ordered($newField);
1750                 }
1751             }
1752             AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1753             return 1;
1754         }
1755         return ( 0, @errors );
1756     }
1757 }
1758
1759 =head2 HasSubscriptionExpired
1760
1761 =over 4
1762
1763 $has_expired = HasSubscriptionExpired($subscriptionid)
1764
1765 the subscription has expired when the next issue to arrive is out of subscription limit.
1766
1767 return :
1768 0 if the subscription has not expired
1769 1 if the subscription has expired
1770 2 if has subscription does not have a valid expiration date set
1771
1772 =back
1773
1774 =cut
1775
1776 sub HasSubscriptionExpired {
1777     my ($subscriptionid) = @_;
1778     my $dbh              = C4::Context->dbh;
1779     my $subscription     = GetSubscription($subscriptionid);
1780     if (($subscription->{periodicity} % 16)>0){
1781       my $expirationdate   = GetExpirationDate($subscriptionid);
1782       my $query = qq|
1783             SELECT max(planneddate)
1784             FROM   serial
1785             WHERE  subscriptionid=?
1786       |;
1787       my $sth = $dbh->prepare($query);
1788       $sth->execute($subscriptionid);
1789       my ($res) = $sth->fetchrow  ;
1790           return 0 unless $res;
1791       my @res=split (/-/,$res);
1792       my @endofsubscriptiondate=split(/-/,$expirationdate);
1793       return 2 if (scalar(@res)!=3 || scalar(@endofsubscriptiondate)!=3||not check_date(@res) || not check_date(@endofsubscriptiondate));
1794       return 1 if ( (@endofsubscriptiondate && Delta_Days($res[0],$res[1],$res[2],
1795                   $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) <= 0)
1796                   || (!$res));
1797       return 0;
1798     } else {
1799       if ($subscription->{'numberlength'}){
1800         my $countreceived=countissuesfrom($subscriptionid,$subscription->{'startdate'});
1801         return 1 if ($countreceived >$subscription->{'numberlength'});
1802               return 0;
1803       } else {
1804               return 0;
1805       }
1806     }
1807     return 0;   # Notice that you'll never get here.
1808 }
1809
1810 =head2 DelSubscription
1811
1812 =over 4
1813
1814 DelSubscription($subscriptionid)
1815 this function delete the subscription which has $subscriptionid as id.
1816
1817 =back
1818
1819 =cut
1820
1821 sub DelSubscription {
1822     my ($subscriptionid) = @_;
1823     my $dbh = C4::Context->dbh;
1824     $subscriptionid = $dbh->quote($subscriptionid);
1825     $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1826     $dbh->do(
1827         "DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1828     $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1829     
1830     logaction("SERIAL", "DELETE", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1831 }
1832
1833 =head2 DelIssue
1834
1835 =over 4
1836
1837 DelIssue($serialseq,$subscriptionid)
1838 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1839
1840 =back
1841
1842 =cut
1843
1844 sub DelIssue {
1845     my ( $dataissue) = @_;
1846     my $dbh   = C4::Context->dbh;
1847     ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1848     
1849     my $query = qq|
1850         DELETE FROM serial
1851         WHERE       serialid= ?
1852         AND         subscriptionid= ?
1853     |;
1854     my $mainsth = $dbh->prepare($query);
1855     $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'});
1856
1857     #Delete element from subscription history
1858     $query = "SELECT * FROM   subscription WHERE  subscriptionid = ?";
1859     my $sth   = $dbh->prepare($query);
1860     $sth->execute($dataissue->{'subscriptionid'});
1861     my $val = $sth->fetchrow_hashref;
1862     unless ( $val->{manualhistory} ) {
1863         my $query = qq|
1864           SELECT * FROM subscriptionhistory
1865           WHERE       subscriptionid= ?
1866       |;
1867         my $sth = $dbh->prepare($query);
1868         $sth->execute($dataissue->{'subscriptionid'});
1869         my $data = $sth->fetchrow_hashref;
1870         my $serialseq= $dataissue->{'serialseq'};
1871         $data->{'missinglist'}  =~ s/\b$serialseq\b//;
1872         $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1873         my $strsth = "UPDATE subscriptionhistory SET "
1874           . join( ",",
1875             map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data )
1876           . " WHERE subscriptionid=?";
1877         $sth = $dbh->prepare($strsth);
1878         $sth->execute($dataissue->{'subscriptionid'});
1879     }
1880     
1881     return $mainsth->rows;
1882 }
1883
1884 =head2 GetLateOrMissingIssues
1885
1886 =over 4
1887
1888 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1889
1890 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1891
1892 return :
1893 a count of the number of missing issues
1894 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1895 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1896
1897 =back
1898
1899 =cut
1900
1901 sub GetLateOrMissingIssues {
1902     my ( $supplierid, $serialid,$order ) = @_;
1903     my $dbh = C4::Context->dbh;
1904     my $sth;
1905     my $byserial = '';
1906     if ($serialid) {
1907         $byserial = "and serialid = " . $serialid;
1908     }
1909     if ($order){
1910       $order.=", title";
1911     } else {
1912       $order="title";
1913     }
1914     if ($supplierid) {
1915         $sth = $dbh->prepare(
1916 "SELECT
1917    serialid,
1918    aqbooksellerid,
1919    name,
1920    biblio.title,
1921    planneddate,
1922    serialseq,
1923    serial.status,
1924    serial.subscriptionid,
1925    claimdate
1926 FROM      serial 
1927 LEFT JOIN subscription  ON serial.subscriptionid=subscription.subscriptionid 
1928 LEFT JOIN biblio        ON subscription.biblionumber=biblio.biblionumber
1929 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1930 WHERE subscription.subscriptionid = serial.subscriptionid 
1931 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3))
1932 AND subscription.aqbooksellerid=$supplierid
1933 $byserial
1934 ORDER BY $order"
1935         );
1936     }
1937     else {
1938         $sth = $dbh->prepare(
1939 "SELECT 
1940    serialid,
1941    aqbooksellerid,
1942    name,
1943    biblio.title,
1944    planneddate,
1945    serialseq,
1946    serial.status,
1947    serial.subscriptionid,
1948    claimdate
1949 FROM serial 
1950 LEFT JOIN subscription 
1951 ON serial.subscriptionid=subscription.subscriptionid 
1952 LEFT JOIN biblio 
1953 ON subscription.biblionumber=biblio.biblionumber
1954 LEFT JOIN aqbooksellers 
1955 ON subscription.aqbooksellerid = aqbooksellers.id
1956 WHERE 
1957    subscription.subscriptionid = serial.subscriptionid 
1958 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3))
1959 $byserial
1960 ORDER BY $order"
1961         );
1962     }
1963     $sth->execute;
1964     my @issuelist;
1965     my $last_title;
1966     my $odd   = 0;
1967     my $count = 0;
1968     while ( my $line = $sth->fetchrow_hashref ) {
1969         $odd++ unless $line->{title} eq $last_title;
1970         $last_title = $line->{title} if ( $line->{title} );
1971         $line->{planneddate} = format_date( $line->{planneddate} );
1972         $line->{claimdate}   = format_date( $line->{claimdate} );
1973         $line->{"status".$line->{status}}   = 1;
1974         $line->{'odd'} = 1 if $odd % 2;
1975         $count++;
1976         push @issuelist, $line;
1977     }
1978     return $count, @issuelist;
1979 }
1980
1981 =head2 removeMissingIssue
1982
1983 =over 4
1984
1985 removeMissingIssue($subscriptionid)
1986
1987 this function removes an issue from being part of the missing string in 
1988 subscriptionlist.missinglist column
1989
1990 called when a missing issue is found from the serials-recieve.pl file
1991
1992 =back
1993
1994 =cut
1995
1996 sub removeMissingIssue {
1997     my ( $sequence, $subscriptionid ) = @_;
1998     my $dbh = C4::Context->dbh;
1999     my $sth =
2000       $dbh->prepare(
2001         "SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
2002     $sth->execute($subscriptionid);
2003     my $data              = $sth->fetchrow_hashref;
2004     my $missinglist       = $data->{'missinglist'};
2005     my $missinglistbefore = $missinglist;
2006
2007     # warn $missinglist." before";
2008     $missinglist =~ s/($sequence)//;
2009
2010     # warn $missinglist." after";
2011     if ( $missinglist ne $missinglistbefore ) {
2012         $missinglist =~ s/\|\s\|/\|/g;
2013         $missinglist =~ s/^\| //g;
2014         $missinglist =~ s/\|$//g;
2015         my $sth2 = $dbh->prepare(
2016             "UPDATE subscriptionhistory
2017                                        SET missinglist = ?
2018                                        WHERE subscriptionid = ?"
2019         );
2020         $sth2->execute( $missinglist, $subscriptionid );
2021     }
2022 }
2023
2024 =head2 updateClaim
2025
2026 =over 4
2027
2028 &updateClaim($serialid)
2029
2030 this function updates the time when a claim is issued for late/missing items
2031
2032 called from claims.pl file
2033
2034 =back
2035
2036 =cut
2037
2038 sub updateClaim {
2039     my ($serialid) = @_;
2040     my $dbh        = C4::Context->dbh;
2041     my $sth        = $dbh->prepare(
2042         "UPDATE serial SET claimdate = now()
2043                                    WHERE serialid = ?
2044                                    "
2045     );
2046     $sth->execute($serialid);
2047 }
2048
2049 =head2 getsupplierbyserialid
2050
2051 =over 4
2052
2053 ($result) = &getsupplierbyserialid($serialid)
2054
2055 this function is used to find the supplier id given a serial id
2056
2057 return :
2058 hashref containing serialid, subscriptionid, and aqbooksellerid
2059
2060 =back
2061
2062 =cut
2063
2064 sub getsupplierbyserialid {
2065     my ($serialid) = @_;
2066     my $dbh        = C4::Context->dbh;
2067     my $sth        = $dbh->prepare(
2068         "SELECT serialid, serial.subscriptionid, aqbooksellerid
2069          FROM serial 
2070          LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2071          WHERE serialid = ?
2072                                    "
2073     );
2074     $sth->execute($serialid);
2075     my $line   = $sth->fetchrow_hashref;
2076     my $result = $line->{'aqbooksellerid'};
2077     return $result;
2078 }
2079
2080 =head2 check_routing
2081
2082 =over 4
2083
2084 ($result) = &check_routing($subscriptionid)
2085
2086 this function checks to see if a serial has a routing list and returns the count of routingid
2087 used to show either an 'add' or 'edit' link
2088
2089 =back
2090
2091 =cut
2092
2093 sub check_routing {
2094     my ($subscriptionid) = @_;
2095     my $dbh              = C4::Context->dbh;
2096     my $sth              = $dbh->prepare(
2097 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist 
2098                               ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2099                               WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2100                               "
2101     );
2102     $sth->execute($subscriptionid);
2103     my $line   = $sth->fetchrow_hashref;
2104     my $result = $line->{'routingids'};
2105     return $result;
2106 }
2107
2108 =head2 addroutingmember
2109
2110 =over 4
2111
2112 &addroutingmember($borrowernumber,$subscriptionid)
2113
2114 this function takes a borrowernumber and subscriptionid and add the member to the
2115 routing list for that serial subscription and gives them a rank on the list
2116 of either 1 or highest current rank + 1
2117
2118 =back
2119
2120 =cut
2121
2122 sub addroutingmember {
2123     my ( $borrowernumber, $subscriptionid ) = @_;
2124     my $rank;
2125     my $dbh = C4::Context->dbh;
2126     my $sth =
2127       $dbh->prepare(
2128 "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?"
2129       );
2130     $sth->execute($subscriptionid);
2131     while ( my $line = $sth->fetchrow_hashref ) {
2132         if ( $line->{'rank'} > 0 ) {
2133             $rank = $line->{'rank'} + 1;
2134         }
2135         else {
2136             $rank = 1;
2137         }
2138     }
2139     $sth =
2140       $dbh->prepare(
2141 "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)"
2142       );
2143     $sth->execute( $subscriptionid, $borrowernumber, $rank );
2144 }
2145
2146 =head2 reorder_members
2147
2148 =over 4
2149
2150 &reorder_members($subscriptionid,$routingid,$rank)
2151
2152 this function is used to reorder the routing list
2153
2154 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2155 - it gets all members on list puts their routingid's into an array
2156 - removes the one in the array that is $routingid
2157 - then reinjects $routingid at point indicated by $rank
2158 - then update the database with the routingids in the new order
2159
2160 =back
2161
2162 =cut
2163
2164 sub reorder_members {
2165     my ( $subscriptionid, $routingid, $rank ) = @_;
2166     my $dbh = C4::Context->dbh;
2167     my $sth =
2168       $dbh->prepare(
2169 "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC"
2170       );
2171     $sth->execute($subscriptionid);
2172     my @result;
2173     while ( my $line = $sth->fetchrow_hashref ) {
2174         push( @result, $line->{'routingid'} );
2175     }
2176
2177     # To find the matching index
2178     my $i;
2179     my $key = -1;    # to allow for 0 being a valid response
2180     for ( $i = 0 ; $i < @result ; $i++ ) {
2181         if ( $routingid == $result[$i] ) {
2182             $key = $i;    # save the index
2183             last;
2184         }
2185     }
2186
2187     # if index exists in array then move it to new position
2188     if ( $key > -1 && $rank > 0 ) {
2189         my $new_rank = $rank -
2190           1;    # $new_rank is what you want the new index to be in the array
2191         my $moving_item = splice( @result, $key, 1 );
2192         splice( @result, $new_rank, 0, $moving_item );
2193     }
2194     for ( my $j = 0 ; $j < @result ; $j++ ) {
2195         my $sth =
2196           $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '"
2197               . ( $j + 1 )
2198               . "' WHERE routingid = '"
2199               . $result[$j]
2200               . "'" );
2201         $sth->execute;
2202     }
2203 }
2204
2205 =head2 delroutingmember
2206
2207 =over 4
2208
2209 &delroutingmember($routingid,$subscriptionid)
2210
2211 this function either deletes one member from routing list if $routingid exists otherwise
2212 deletes all members from the routing list
2213
2214 =back
2215
2216 =cut
2217
2218 sub delroutingmember {
2219
2220 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2221     my ( $routingid, $subscriptionid ) = @_;
2222     my $dbh = C4::Context->dbh;
2223     if ($routingid) {
2224         my $sth =
2225           $dbh->prepare(
2226             "DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2227         $sth->execute($routingid);
2228         reorder_members( $subscriptionid, $routingid );
2229     }
2230     else {
2231         my $sth =
2232           $dbh->prepare(
2233             "DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2234         $sth->execute($subscriptionid);
2235     }
2236 }
2237
2238 =head2 getroutinglist
2239
2240 =over 4
2241
2242 ($count,@routinglist) = &getroutinglist($subscriptionid)
2243
2244 this gets the info from the subscriptionroutinglist for $subscriptionid
2245
2246 return :
2247 a count of the number of members on routinglist
2248 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2249 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2250
2251 =back
2252
2253 =cut
2254
2255 sub getroutinglist {
2256     my ($subscriptionid) = @_;
2257     my $dbh              = C4::Context->dbh;
2258     my $sth              = $dbh->prepare(
2259         "SELECT routingid, borrowernumber,
2260                               ranking, biblionumber 
2261          FROM subscription 
2262          LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2263          WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2264                               "
2265     );
2266     $sth->execute($subscriptionid);
2267     my @routinglist;
2268     my $count = 0;
2269     while ( my $line = $sth->fetchrow_hashref ) {
2270         $count++;
2271         push( @routinglist, $line );
2272     }
2273     return ( $count, @routinglist );
2274 }
2275
2276 =head2 countissuesfrom
2277
2278 =over 4
2279
2280 $result = &countissuesfrom($subscriptionid,$startdate)
2281
2282
2283 =back
2284
2285 =cut
2286
2287 sub countissuesfrom {
2288     my ($subscriptionid,$startdate) = @_;
2289     my $dbh              = C4::Context->dbh;
2290     my $query = qq|
2291             SELECT count(*)
2292             FROM   serial
2293             WHERE  subscriptionid=?
2294             AND serial.publisheddate>?
2295         |;
2296     my $sth=$dbh->prepare($query);
2297     $sth->execute($subscriptionid, $startdate);
2298     my ($countreceived)=$sth->fetchrow;
2299     return $countreceived;  
2300 }
2301
2302 =head2 abouttoexpire
2303
2304 =over 4
2305
2306 $result = &abouttoexpire($subscriptionid)
2307
2308 this function alerts you to the penultimate issue for a serial subscription
2309
2310 returns 1 - if this is the penultimate issue
2311 returns 0 - if not
2312
2313 =back
2314
2315 =cut
2316
2317 sub abouttoexpire {
2318     my ($subscriptionid) = @_;
2319     my $dbh              = C4::Context->dbh;
2320     my $subscription     = GetSubscription($subscriptionid);
2321     my $per = $subscription->{'periodicity'};
2322     if ($per % 16>0){
2323       my $expirationdate   = GetExpirationDate($subscriptionid);
2324       my $sth =
2325         $dbh->prepare(
2326           "select max(planneddate) from serial where subscriptionid=?");
2327       $sth->execute($subscriptionid);
2328       my ($res) = $sth->fetchrow ;
2329 #        warn "date expiration : ".$expirationdate." date courante ".$res;
2330       my @res=split (/-/,$res);
2331       @res=Date::Calc::Today if ($res[0]*$res[1]==0);
2332       my @endofsubscriptiondate=split(/-/,$expirationdate);
2333       my $x;
2334       if ( $per == 1 ) {$x=7;}
2335       if ( $per == 2 ) {$x=7; }
2336       if ( $per == 3 ) {$x=14;}
2337       if ( $per == 4 ) { $x = 21; }
2338       if ( $per == 5 ) { $x = 31; }
2339       if ( $per == 6 ) { $x = 62; }
2340       if ( $per == 7 || $per == 8 ) { $x = 93; }
2341       if ( $per == 9 )  { $x = 190; }
2342       if ( $per == 10 ) { $x = 365; }
2343       if ( $per == 11 ) { $x = 730; }
2344       my @datebeforeend=Add_Delta_Days(  $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
2345                     - (3 * $x)) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
2346               # warn "DATE BEFORE END: $datebeforeend";
2347       return 1 if ( @res && 
2348                     (@datebeforeend && 
2349                         Delta_Days($res[0],$res[1],$res[2],
2350                         $datebeforeend[0],$datebeforeend[1],$datebeforeend[2]) <= 0) && 
2351                     (@endofsubscriptiondate && 
2352                         Delta_Days($res[0],$res[1],$res[2],
2353                         $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) >= 0) );
2354     return 0;
2355    } elsif ($subscription->{numberlength}>0) {
2356     return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2357    } else {return 0}
2358 }
2359
2360 =head2 GetNextDate
2361
2362 ($resultdate) = &GetNextDate($planneddate,$subscription)
2363
2364 this function is an extension of GetNextDate which allows for checking for irregularity
2365
2366 it takes the planneddate and will return the next issue's date and will skip dates if there
2367 exists an irregularity
2368 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be 
2369 skipped then the returned date will be 2007-05-10
2370
2371 return :
2372 $resultdate - then next date in the sequence
2373
2374 Return 0 if periodicity==0
2375
2376 =cut
2377 sub in_array { # used in next sub down
2378   my ($val,@elements) = @_;
2379   foreach my $elem(@elements) {
2380     if($val == $elem) {
2381             return 1;
2382     }
2383   }
2384   return 0;
2385 }
2386
2387 sub GetNextDate(@) {
2388     my ( $planneddate, $subscription ) = @_;
2389     my @irreg = split( /\,/, $subscription->{irregularity} );
2390
2391     #date supposed to be in ISO.
2392     
2393     my ( $year, $month, $day ) = split(/-/, $planneddate);
2394     $month=1 unless ($month);
2395     $day=1 unless ($day);
2396     my @resultdate;
2397
2398     #       warn "DOW $dayofweek";
2399     if ( $subscription->{periodicity} % 16 == 0 ) {  # 'without regularity' || 'irregular'
2400       return 0;
2401     }  
2402     #   daily : n / week
2403     #   Since we're interpreting irregularity here as which days of the week to skip an issue,
2404     #   renaming this pattern from 1/day to " n / week ".
2405     if ( $subscription->{periodicity} == 1 ) {  
2406         my $dayofweek = eval{Day_of_Week( $year,$month, $day )};
2407         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2408         else {    
2409           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2410               $dayofweek = 0 if ( $dayofweek == 7 ); 
2411               if ( in_array( ($dayofweek + 1), @irreg ) ) {
2412                   ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 1 );
2413                   $dayofweek++;
2414               }
2415           }
2416           @resultdate = Add_Delta_Days($year,$month, $day , 1 );
2417         }    
2418     }
2419     #   1  week
2420     if ( $subscription->{periodicity} == 2 ) {
2421         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2422         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2423         else {    
2424           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2425           #FIXME: if two consecutive irreg, do we only skip one?
2426               if ( $irreg[$i] == (($wkno!=51)?($wkno +1) % 52 :52)) {
2427                   ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 7 );
2428                   $wkno=(($wkno!=51)?($wkno +1) % 52 :52);
2429               }
2430           }
2431           @resultdate = Add_Delta_Days( $year,$month, $day, 7);
2432         }        
2433     }
2434     #   1 / 2 weeks
2435     if ( $subscription->{periodicity} == 3 ) {        
2436         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2437         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2438         else {    
2439           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2440               if ( $irreg[$i] == (($wkno!=50)?($wkno +2) % 52 :52)) {
2441               ### BUGFIX was previously +1 ^
2442                   ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 14 );
2443                   $wkno=(($wkno!=50)?($wkno +2) % 52 :52);
2444               }
2445           }
2446           @resultdate = Add_Delta_Days($year,$month, $day , 14 );
2447         }        
2448     }
2449     #   1 / 3 weeks
2450     if ( $subscription->{periodicity} == 4 ) {
2451         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2452         if ($@){warn "annĂ©e mois jour : $year $month $day $subscription->{subscriptionid} : $@";}
2453         else {    
2454           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2455               if ( $irreg[$i] == (($wkno!=49)?($wkno +3) % 52 :52)) {
2456                   ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 21 );
2457                   $wkno=(($wkno!=49)?($wkno +3) % 52 :52);
2458               }
2459           }
2460           @resultdate = Add_Delta_Days($year,$month, $day , 21 );
2461         }        
2462     }
2463     my $tmpmonth=$month;
2464     if ($year && $month && $day){
2465     if ( $subscription->{periodicity} == 5 ) {
2466           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2467               if ( $irreg[$i] == (($tmpmonth!=11)?($tmpmonth +1) % 12 :12)) {
2468                   ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2469                   $tmpmonth=(($tmpmonth!=11)?($tmpmonth +1) % 12 :12);
2470               }
2471           }        
2472           @resultdate = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2473     }
2474     if ( $subscription->{periodicity} == 6 ) {
2475           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2476               if ( $irreg[$i] == (($tmpmonth!=10)?($tmpmonth +2) % 12 :12)) {
2477                   ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,2,0 );
2478                   $tmpmonth=(($tmpmonth!=10)?($tmpmonth + 2) % 12 :12);
2479               }
2480           }
2481           @resultdate = Add_Delta_YMD($year,$month, $day, 0, 2,0 );
2482     }
2483     if ( $subscription->{periodicity} == 7 ) {
2484         for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2485             if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2486                 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2487                 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2488             }
2489         }
2490         @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2491     }
2492     if ( $subscription->{periodicity} == 8 ) {
2493         for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2494             if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2495                 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2496                 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2497             }
2498         }
2499         @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2500     }
2501     if ( $subscription->{periodicity} == 9 ) {
2502         for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2503             if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2504             ### BUFIX Seems to need more Than One ?
2505                 ($year,$month,$day) = Add_Delta_YM($year,$month, $day, 0, 6 );
2506                 $tmpmonth=(($tmpmonth!=6)?($tmpmonth + 6) % 12 :12);
2507             }
2508         }
2509         @resultdate = Add_Delta_YM($year,$month, $day, 0, 6);
2510     }
2511     if ( $subscription->{periodicity} == 10 ) {
2512         @resultdate = Add_Delta_YM($year,$month, $day, 1, 0 );
2513     }
2514     if ( $subscription->{periodicity} == 11 ) {
2515         @resultdate = Add_Delta_YM($year,$month, $day, 2, 0 );
2516     }
2517     }  
2518     my $resultdate=sprintf("%04d-%02d-%02d",$resultdate[0],$resultdate[1],$resultdate[2]);
2519       
2520 #     warn "dateNEXTSEQ : ".$resultdate;
2521     return "$resultdate";
2522 }
2523
2524 =head2 itemdata
2525
2526   $item = &itemdata($barcode);
2527
2528 Looks up the item with the given barcode, and returns a
2529 reference-to-hash containing information about that item. The keys of
2530 the hash are the fields from the C<items> and C<biblioitems> tables in
2531 the Koha database.
2532
2533 =cut
2534
2535 #'
2536 sub itemdata {
2537     my ($barcode) = @_;
2538     my $dbh       = C4::Context->dbh;
2539     my $sth       = $dbh->prepare(
2540         "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber 
2541         WHERE barcode=?"
2542     );
2543     $sth->execute($barcode);
2544     my $data = $sth->fetchrow_hashref;
2545     $sth->finish;
2546     return ($data);
2547 }
2548
2549 1;
2550 __END__
2551
2552 =head1 AUTHOR
2553
2554 Koha Developement team <info@koha.org>
2555
2556 =cut