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