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