-
Notifications
You must be signed in to change notification settings - Fork 257
/
Serials.pm
2739 lines (2243 loc) · 91.3 KB
/
Serials.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package C4::Serials;
# Copyright 2000-2002 Katipo Communications
# Parts Copyright 2010 Biblibre
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Carp qw( croak );
use Date::Calc qw(
Add_Delta_Days
Add_Delta_YM
check_date
Delta_Days
N_Delta_YMD
Today
);
use DateTime;
use POSIX qw( strftime );
use Scalar::Util qw( looks_like_number );
use Try::Tiny;
use C4::Auth qw( haspermission );
use C4::Biblio qw( GetMarcFromKohaField ModBiblio );
use C4::Context;
use C4::Log qw( logaction ); # logaction
use C4::Serials::Frequency qw( GetSubscriptionFrequency );
use C4::Serials::Numberpattern;
use Koha::AdditionalFieldValues;
use Koha::Biblios;
use Koha::DateUtils qw( dt_from_string );
use Koha::Serial;
use Koha::SharedContent;
use Koha::Subscription::Histories;
use Koha::Subscription::Routinglists;
use Koha::Subscriptions;
use Koha::Suggestions;
use Koha::TemplateUtils qw( process_tt );
# Define statuses
use constant {
EXPECTED => 1,
ARRIVED => 2,
LATE => 3,
MISSING => 4,
MISSING_NEVER_RECIEVED => 41,
MISSING_SOLD_OUT => 42,
MISSING_DAMAGED => 43,
MISSING_LOST => 44,
NOT_ISSUED => 5,
DELETED => 6,
CLAIMED => 7,
STOPPED => 8,
};
use constant MISSING_STATUSES => (
MISSING, MISSING_NEVER_RECIEVED,
MISSING_SOLD_OUT, MISSING_DAMAGED,
MISSING_LOST
);
our (@ISA, @EXPORT_OK);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
NewSubscription ModSubscription DelSubscription
GetSubscription CountSubscriptionFromBiblionumber GetSubscriptionsFromBiblionumber
SearchSubscriptions
GetFullSubscriptionsFromBiblionumber GetFullSubscription ModSubscriptionHistory
HasSubscriptionStrictlyExpired HasSubscriptionExpired GetExpirationDate abouttoexpire
GetFictiveIssueNumber
GetSubscriptionHistoryFromSubscriptionId
GetNextSeq GetSeq NewIssue GetSerials
GetLatestSerials ModSerialStatus GetNextDate
CloseSubscription ReopenSubscription
subscriptionCurrentlyOnOrder
can_claim_subscription can_edit_subscription can_show_subscription
GetSerials2
GetSubscriptionLength ReNewSubscription GetLateOrMissingIssues
GetSerialInformation AddItem2Serial
PrepareSerialsData GetNextExpected ModNextExpected
GetSubscriptionIrregularities
GetPreviousSerialid
GetSuppliersWithLateIssues
getroutinglist delroutingmember addroutingmember
reorder_members
check_routing updateClaim
CountIssues
HasItems
findSerialsByStatus
);
}
=head1 NAME
C4::Serials - Serials Module Functions
=head1 SYNOPSIS
use C4::Serials;
=head1 DESCRIPTION
Functions for handling subscriptions, claims routing etc.
=head1 SUBROUTINES
=head2 GetSuppliersWithLateIssues
$supplierlist = GetSuppliersWithLateIssues()
this function get all suppliers with late issues.
return :
an array_ref of suppliers each entry is a hash_ref containing id and name
the array is in name order
=cut
sub GetSuppliersWithLateIssues {
my $dbh = C4::Context->dbh;
my $statuses = join(',', ( LATE, MISSING_STATUSES, CLAIMED ) );
my $query = qq|
SELECT DISTINCT id, name
FROM subscription
LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
WHERE id > 0
AND (
(planneddate < now() AND serial.status=1)
OR serial.STATUS IN ( $statuses )
)
AND subscription.closed = 0
ORDER BY name|;
return $dbh->selectall_arrayref($query, { Slice => {} });
}
=head2 GetSubscriptionHistoryFromSubscriptionId
$history = GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
This function returns the subscription history as a hashref
=cut
sub GetSubscriptionHistoryFromSubscriptionId {
my ($subscriptionid) = @_;
return unless $subscriptionid;
my $dbh = C4::Context->dbh;
my $query = qq|
SELECT *
FROM subscriptionhistory
WHERE subscriptionid = ?
|;
my $sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
my $results = $sth->fetchrow_hashref;
$sth->finish;
return $results;
}
=head2 GetSerialInformation
$data = GetSerialInformation($serialid);
returns a hash_ref containing :
items : items marcrecord (can be an array)
serial table field
subscription table field
+ information about subscription expiration
=cut
sub GetSerialInformation {
my ($serialid) = @_;
my $dbh = C4::Context->dbh;
my $query = qq|
SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid
FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
WHERE serialid = ?
|;
my $rq = $dbh->prepare($query);
$rq->execute($serialid);
my $data = $rq->fetchrow_hashref;
# create item information if we have serialsadditems for this subscription
if ( $data->{'serialsadditems'} ) {
my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
$queryitem->execute($serialid);
my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
require C4::Items;
if ( scalar(@$itemnumbers) > 0 ) {
foreach my $itemnum (@$itemnumbers) {
#It is ASSUMED that GetMarcItem ALWAYS WORK...
#Maybe GetMarcItem should return values on failure
my $itemprocessed = C4::Items::PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
$itemprocessed->{'itemnumber'} = $itemnum->[0];
$itemprocessed->{'itemid'} = $itemnum->[0];
$itemprocessed->{'serialid'} = $serialid;
$itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
push @{ $data->{'items'} }, $itemprocessed;
}
} else {
my $itemprocessed = C4::Items::PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
$itemprocessed->{'itemid'} = "N$serialid";
$itemprocessed->{'serialid'} = $serialid;
$itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
$itemprocessed->{'countitems'} = 0;
push @{ $data->{'items'} }, $itemprocessed;
}
}
$data->{ "status" . $data->{'serstatus'} } = 1;
$data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
$data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
$data->{cannotedit} = not can_edit_subscription( $data );
return $data;
}
=head2 AddItem2Serial
$rows = AddItem2Serial($serialid,$itemnumber);
Adds an itemnumber to Serial record
returns the number of rows affected
=cut
sub AddItem2Serial {
my ( $serialid, $itemnumber ) = @_;
return unless ($serialid and $itemnumber);
my $dbh = C4::Context->dbh;
my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
$rq->execute( $serialid, $itemnumber );
return $rq->rows;
}
=head2 GetSubscription
$subs = GetSubscription($subscriptionid)
this function returns the subscription which has $subscriptionid as id.
return :
a hashref. This hash contains
subscription, subscriptionhistory, aqbooksellers.name, biblio.title
=cut
sub GetSubscription {
my ($subscriptionid) = @_;
my $dbh = C4::Context->dbh;
my $query = qq(
SELECT subscription.*,
subscriptionhistory.*,
aqbooksellers.name AS aqbooksellername,
biblio.title AS bibliotitle,
biblio.subtitle AS bibliosubtitle,
subscription.biblionumber as bibnum
FROM subscription
LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
WHERE subscription.subscriptionid = ?
);
my $sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
my $subscription = $sth->fetchrow_hashref;
return unless $subscription;
$subscription->{cannotedit} = not can_edit_subscription( $subscription );
if ( my $mana_id = $subscription->{mana_id} ) {
my $mana_subscription = Koha::SharedContent::get_entity_by_id(
'subscription', $mana_id, {usecomments => 1});
$subscription->{comments} = $mana_subscription->{data}->{comments};
}
return $subscription;
}
=head2 GetFullSubscription
$array_ref = GetFullSubscription($subscriptionid)
this function reads the serial table.
=cut
sub GetFullSubscription {
my ($subscriptionid) = @_;
return unless ($subscriptionid);
my $dbh = C4::Context->dbh;
my $query = qq|
SELECT serial.serialid,
serial.serialseq,
serial.planneddate,
serial.publisheddate,
serial.publisheddatetext,
serial.status,
serial.notes as notes,
year(IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate)) as year,
aqbooksellers.name as aqbooksellername,
biblio.title as bibliotitle,
subscription.branchcode AS branchcode,
subscription.subscriptionid AS subscriptionid
FROM serial
LEFT JOIN subscription ON
(serial.subscriptionid=subscription.subscriptionid )
LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
WHERE serial.subscriptionid = ?
ORDER BY year DESC,
IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate) DESC,
serial.subscriptionid
|;
my $sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
my $subscriptions = $sth->fetchall_arrayref( {} );
if (scalar @$subscriptions) {
my $cannotedit = not can_edit_subscription( $subscriptions->[0] );
for my $subscription ( @$subscriptions ) {
$subscription->{cannotedit} = $cannotedit;
}
}
return $subscriptions;
}
=head2 PrepareSerialsData
$array_ref = PrepareSerialsData($serialinfomation)
where serialinformation is a hashref array
=cut
sub PrepareSerialsData {
my ($lines) = @_;
return unless ($lines);
my %tmpresults;
my $year;
my @res;
my $startdate;
my $first;
my $previousnote = "";
foreach my $subs (@{$lines}) {
$subs->{ "status" . $subs->{'status'} } = 1;
if ( grep { $_ == $subs->{status} } ( EXPECTED, LATE, MISSING_STATUSES, CLAIMED ) ) {
$subs->{"checked"} = 1;
}
if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
$year = $subs->{'year'};
} else {
$year = "manage";
}
if ( $tmpresults{$year} ) {
push @{ $tmpresults{$year}->{'serials'} }, $subs;
} else {
$tmpresults{$year} = {
'year' => $year,
'aqbooksellername' => $subs->{'aqbooksellername'},
'bibliotitle' => $subs->{'bibliotitle'},
'serials' => [$subs],
'first' => $first,
};
}
}
foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
push @res, $tmpresults{$key};
}
return \@res;
}
=head2 GetSubscriptionsFromBiblionumber
$array_ref = GetSubscriptionsFromBiblionumber($biblionumber)
this function get the subscription list. it reads the subscription table.
return :
reference to an array of subscriptions which have the biblionumber given on input arg.
each element of this array is a hashref containing
startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
=cut
sub GetSubscriptionsFromBiblionumber {
my ($biblionumber) = @_;
return unless ($biblionumber);
my $dbh = C4::Context->dbh;
my $query = qq(
SELECT subscription.*,
branches.branchname,
subscriptionhistory.*,
aqbooksellers.name AS aqbooksellername,
biblio.title AS bibliotitle
FROM subscription
LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
LEFT JOIN branches ON branches.branchcode=subscription.branchcode
WHERE subscription.biblionumber = ?
);
my $sth = $dbh->prepare($query);
$sth->execute($biblionumber);
my @res;
while ( my $subs = $sth->fetchrow_hashref ) {
$subs->{opacnote} //= "";
$subs->{ "periodicity" . $subs->{periodicity} } = 1;
$subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
$subs->{ "status" . $subs->{'status'} } = 1;
$subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
$subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
$subs->{cannotedit} = not can_edit_subscription( $subs );
push @res, $subs;
}
return \@res;
}
=head2 GetFullSubscriptionsFromBiblionumber
$array_ref = GetFullSubscriptionsFromBiblionumber($biblionumber)
this function reads the serial table.
=cut
sub GetFullSubscriptionsFromBiblionumber {
my ($biblionumber) = @_;
my $dbh = C4::Context->dbh;
my $query = qq|
SELECT serial.serialid,
serial.serialseq,
serial.planneddate,
serial.publisheddate,
serial.publisheddatetext,
serial.status,
serial.notes as notes,
year(IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate)) as year,
biblio.title as bibliotitle,
subscription.branchcode AS branchcode,
subscription.subscriptionid AS subscriptionid,
subscription.location AS location
FROM serial
LEFT JOIN subscription ON
(serial.subscriptionid=subscription.subscriptionid)
LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
WHERE subscription.biblionumber = ?
ORDER BY year DESC,
IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate) DESC,
serial.subscriptionid
|;
my $sth = $dbh->prepare($query);
$sth->execute($biblionumber);
my $subscriptions = $sth->fetchall_arrayref( {} );
if (scalar @$subscriptions) {
my $cannotedit = not can_edit_subscription( $subscriptions->[0] );
for my $subscription ( @$subscriptions ) {
$subscription->{cannotedit} = $cannotedit;
}
}
return $subscriptions;
}
=head2 SearchSubscriptions
@results = SearchSubscriptions($args);
This function returns a list of hashrefs, one for each subscription
that meets the conditions specified by the $args hashref.
The valid search fields are:
biblionumber
title
issn
ean
callnumber
location
publisher
bookseller
branch
expiration_date
closed
routinglist
The expiration_date search field is special; it specifies the maximum
subscription expiration date.
=cut
sub SearchSubscriptions {
my ( $args, $params ) = @_;
$params //= {};
my $additional_fields = $args->{additional_fields} // [];
my $matching_record_ids_for_additional_fields = [];
if ( @$additional_fields ) {
my @subscriptions = Koha::Subscriptions->filter_by_additional_fields($additional_fields)->as_list;
return () unless @subscriptions;
$matching_record_ids_for_additional_fields = [ map {
$_->subscriptionid
} @subscriptions ];
}
my $query = q|
SELECT
subscription.notes AS publicnotes,
subscriptionhistory.*,
subscription.*,
biblio.notes AS biblionotes,
biblio.title,
biblio.subtitle,
biblio.part_number,
biblio.part_name,
biblio.author,
biblio.biblionumber,
aqbooksellers.name AS vendorname,
biblioitems.issn
FROM subscription
LEFT JOIN subscriptionhistory USING(subscriptionid)
LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
LEFT JOIN biblioitems ON biblioitems.biblionumber = subscription.biblionumber
LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
|;
if ( $args->{routinglist} ) {
$query .=
q| INNER JOIN (SELECT DISTINCT subscriptionid FROM subscriptionroutinglist) srl ON srl.subscriptionid = subscription.subscriptionid|;
}
$query .= q| WHERE 1|;
my @where_strs;
my @where_args;
if( $args->{biblionumber} ) {
push @where_strs, "biblio.biblionumber = ?";
push @where_args, $args->{biblionumber};
}
if( $args->{title} ){
my @words = split / /, $args->{title};
my (@strs, @args);
foreach my $word (@words) {
push @strs, "biblio.title LIKE ?";
push @args, "%$word%";
}
if (@strs) {
push @where_strs, '(' . join (' AND ', @strs) . ')';
push @where_args, @args;
}
}
if( $args->{issn} ){
push @where_strs, "biblioitems.issn LIKE ?";
push @where_args, "%$args->{issn}%";
}
if( $args->{ean} ){
push @where_strs, "biblioitems.ean LIKE ?";
push @where_args, "%$args->{ean}%";
}
if ( $args->{callnumber} ) {
push @where_strs, "subscription.callnumber LIKE ?";
push @where_args, "%$args->{callnumber}%";
}
if( $args->{publisher} ){
push @where_strs, "biblioitems.publishercode LIKE ?";
push @where_args, "%$args->{publisher}%";
}
if( $args->{bookseller} ){
push @where_strs, "aqbooksellers.name LIKE ?";
push @where_args, "%$args->{bookseller}%";
}
if( $args->{branch} ){
push @where_strs, "subscription.branchcode = ?";
push @where_args, "$args->{branch}";
}
if ( $args->{location} ) {
push @where_strs, "subscription.location = ?";
push @where_args, "$args->{location}";
}
if ( $args->{expiration_date} ) {
push @where_strs, "subscription.enddate <= ?";
push @where_args, "$args->{expiration_date}";
}
if( defined $args->{closed} ){
push @where_strs, "subscription.closed = ?";
push @where_args, "$args->{closed}";
}
if(@where_strs){
$query .= ' AND ' . join(' AND ', @where_strs);
}
if ( @$additional_fields ) {
$query .= ' AND subscriptionid IN ('
. join( ', ', @$matching_record_ids_for_additional_fields )
. ')';
}
$query .= " ORDER BY " . $args->{orderby} if $args->{orderby};
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute(@where_args);
my $results = $sth->fetchall_arrayref( {} );
my $total_results = @{$results};
if ( $params->{results_limit} && $total_results > $params->{results_limit} ) {
$results = [ splice( @{$results}, 0, $params->{results_limit} ) ];
}
for my $subscription ( @$results ) {
$subscription->{cannotedit} = not can_edit_subscription( $subscription );
$subscription->{cannotdisplay} = not can_show_subscription( $subscription );
my $subscription_object = Koha::Subscriptions->find($subscription->{subscriptionid});
$subscription->{additional_field_values} = $subscription_object->get_additional_field_values_for_template;
}
return wantarray ? @{$results} : { results => $results, total => $total_results };
}
=head2 GetSerials
($totalissues,@serials) = GetSerials($subscriptionid);
this function gets every serial not arrived for a given subscription
as well as the number of issues registered in the database (all types)
this number is used to see if a subscription can be deleted (=it must have only 1 issue)
FIXME: We should return \@serials.
=cut
sub GetSerials {
my ( $subscriptionid, $count ) = @_;
return unless $subscriptionid;
my $dbh = C4::Context->dbh;
# status = 2 is "arrived"
my $counter = 0;
$count = 5 unless ($count);
my @serials;
my $statuses = join( ',', ( ARRIVED, MISSING_STATUSES, NOT_ISSUED ) );
my $query = "SELECT serialid,serialseq, status, publisheddate,
publisheddatetext, planneddate,notes, routingnotes
FROM serial
WHERE subscriptionid = ? AND status NOT IN ( $statuses )
ORDER BY IF(publisheddate IS NULL,planneddate,publisheddate) DESC";
my $sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
while ( my $line = $sth->fetchrow_hashref ) {
$line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
push @serials, $line;
}
# OK, now add the last 5 issues arrives/missing
$query = "SELECT serialid,serialseq, status, planneddate, publisheddate,
publisheddatetext, notes, routingnotes
FROM serial
WHERE subscriptionid = ?
AND status IN ( $statuses )
ORDER BY IF(publisheddate IS NULL,planneddate,publisheddate) DESC
";
$sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
$counter++;
$line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
push @serials, $line;
}
$query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
$sth = $dbh->prepare($query);
$sth->execute($subscriptionid);
my ($totalissues) = $sth->fetchrow;
return ( $totalissues, @serials );
}
=head2 GetSerials2
@serials = GetSerials2($subscriptionid,$statuses);
this function returns every serial waited for a given subscription
as well as the number of issues registered in the database (all types)
this number is used to see if a subscription can be deleted (=it must have only 1 issue)
$statuses is an arrayref of statuses and is mandatory.
=cut
sub GetSerials2 {
my ( $subscription, $statuses ) = @_;
return unless ($subscription and @$statuses);
my $dbh = C4::Context->dbh;
my $query = q|
SELECT serialid,serialseq, status, planneddate, publisheddate,
publisheddatetext, notes, routingnotes
FROM serial
WHERE subscriptionid=?
|
. q| AND status IN (| . join( ",", ('?') x @$statuses ) . q|)|
. q|
ORDER BY publisheddate,serialid DESC
|;
my $sth = $dbh->prepare($query);
$sth->execute( $subscription, @$statuses );
my @serials;
while ( my $line = $sth->fetchrow_hashref ) {
$line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
push @serials, $line;
}
return @serials;
}
=head2 GetLatestSerials
\@serials = GetLatestSerials($subscriptionid,$limit)
get the $limit's latest serials arrived or missing for a given subscription
return :
a ref to an array which contains all of the latest serials stored into a hash.
=cut
sub GetLatestSerials {
my ( $subscriptionid, $limit ) = @_;
return unless ($subscriptionid and $limit);
my $dbh = C4::Context->dbh;
my $statuses = join( ',', ( ARRIVED, MISSING_STATUSES ) );
my $strsth = "SELECT serialid,serialseq, status, planneddate, publisheddate, publisheddatetext, notes
FROM serial
WHERE subscriptionid = ?
AND status IN ($statuses)
ORDER BY publisheddate DESC LIMIT 0,$limit
";
my $sth = $dbh->prepare($strsth);
$sth->execute($subscriptionid);
my @serials;
while ( my $line = $sth->fetchrow_hashref ) {
$line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
push @serials, $line;
}
return \@serials;
}
=head2 GetPreviousSerialid
$serialid = GetPreviousSerialid($subscriptionid, $nth)
get the $nth's previous serial for the given subscriptionid
return :
the serialid
=cut
sub GetPreviousSerialid {
my ( $subscriptionid, $nth ) = @_;
$nth ||= 1;
my $dbh = C4::Context->dbh;
my $return = undef;
# Status 2: Arrived
my $strsth = "SELECT serialid
FROM serial
WHERE subscriptionid = ?
AND status = 2
ORDER BY serialid DESC LIMIT $nth,1
";
my $sth = $dbh->prepare($strsth);
$sth->execute($subscriptionid);
my @serials;
my $line = $sth->fetchrow_hashref;
$return = $line->{'serialid'} if ($line);
return $return;
}
=head2 GetNextSeq
my (
$nextseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
$newinnerloop1, $newinnerloop2, $newinnerloop3
) = GetNextSeq( $subscription, $pattern, $frequency, $planneddate, $count_forward );
$subscription is a hashref containing all the attributes of the table
'subscription'.
$pattern is a hashref containing all the attributes of the table
'subscription_numberpatterns'.
$frequency is a hashref containing all the attributes of the table 'subscription_frequencies'
$planneddate is a date string in iso format.
$count_forward is the number of issues to count forward, defaults to 1 if omitted
This function get the next issue for the subscription given on input arg
=cut
sub GetNextSeq {
my ($subscription, $pattern, $frequency, $planneddate, $count_forward) = @_;
return unless ($subscription and $pattern);
my ( $newlastvalue1, $newlastvalue2, $newlastvalue3,
$newinnerloop1, $newinnerloop2, $newinnerloop3 );
my $count = $count_forward || 1;
if ($subscription->{'skip_serialseq'}) {
my @irreg = split /;/, $subscription->{'irregularity'};
if(@irreg > 0) {
my $irregularities = {};
$irregularities->{$_} = 1 foreach(@irreg);
my $issueno = GetFictiveIssueNumber($subscription, $planneddate, $frequency) + 1;
while($irregularities->{$issueno}) {
$count++;
$issueno++;
}
}
}
my $numberingmethod = $pattern->{numberingmethod};
my $calculated = "";
if ($numberingmethod) {
$calculated = $numberingmethod;
my $locale = $subscription->{locale};
$newlastvalue1 = $subscription->{lastvalue1} || 0;
$newlastvalue2 = $subscription->{lastvalue2} || 0;
$newlastvalue3 = $subscription->{lastvalue3} || 0;
$newinnerloop1 = $subscription->{innerloop1} || 0;
$newinnerloop2 = $subscription->{innerloop2} || 0;
$newinnerloop3 = $subscription->{innerloop3} || 0;
my %calc;
foreach(qw/X Y Z/) {
$calc{$_} = 1 if ($numberingmethod =~ /\{$_\}/);
}
for(my $i = 0; $i < $count; $i++) {
if($calc{'X'}) {
# check if we have to increase the new value.
$newinnerloop1 += 1;
if ($newinnerloop1 >= $pattern->{every1}) {
$newinnerloop1 = 0;
$newlastvalue1 += $pattern->{add1};
}
# reset counter if needed.
$newlastvalue1 = $pattern->{setto1} if ($newlastvalue1 > $pattern->{whenmorethan1});
}
if($calc{'Y'}) {
# check if we have to increase the new value.
$newinnerloop2 += 1;
if ($newinnerloop2 >= $pattern->{every2}) {
$newinnerloop2 = 0;
$newlastvalue2 += $pattern->{add2};
}
# reset counter if needed.
$newlastvalue2 = $pattern->{setto2} if ($newlastvalue2 > $pattern->{whenmorethan2});
}
if($calc{'Z'}) {
# check if we have to increase the new value.
$newinnerloop3 += 1;
if ($newinnerloop3 >= $pattern->{every3}) {
$newinnerloop3 = 0;
$newlastvalue3 += $pattern->{add3};
}
# reset counter if needed.
$newlastvalue3 = $pattern->{setto3} if ($newlastvalue3 > $pattern->{whenmorethan3});
}
}
if($calc{'X'}) {
my $newlastvalue1string = _numeration( $newlastvalue1, $pattern->{numbering1}, $locale );
$calculated =~ s/\{X\}/$newlastvalue1string/g;
}
if($calc{'Y'}) {
my $newlastvalue2string = _numeration( $newlastvalue2, $pattern->{numbering2}, $locale );
$calculated =~ s/\{Y\}/$newlastvalue2string/g;
}
if($calc{'Z'}) {
my $newlastvalue3string = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale );
$calculated =~ s/\{Z\}/$newlastvalue3string/g;
}
my $dt = dt_from_string($planneddate);
$calculated =~ s/\{Month\}/$dt->month/eg;
$calculated =~ s/\{MonthName\}/$dt->month_name/eg;
$calculated =~ s/\{Year\}/$dt->year/eg;
$calculated =~ s/\{Day\}/$dt->day/eg;
$calculated =~ s/\{DayName\}/$dt->day_name/eg;
}
return ($calculated,
$newlastvalue1, $newlastvalue2, $newlastvalue3,
$newinnerloop1, $newinnerloop2, $newinnerloop3);
}
=head2 GetSeq
$calculated = GetSeq($subscription, $pattern)
$subscription is a hashref containing all the attributes of the table 'subscription'
$pattern is a hashref containing all the attributes of the table 'subscription_numberpatterns'
this function transforms {X},{Y},{Z} to 150,0,0 for example.
return:
the sequence in string format
=cut
sub GetSeq {
my ($subscription, $pattern) = @_;
return unless ($subscription and $pattern);
my $locale = $subscription->{locale};
my $calculated = $pattern->{numberingmethod};
my $newlastvalue1 = $subscription->{'lastvalue1'} || 0;
$newlastvalue1 = _numeration($newlastvalue1, $pattern->{numbering1}, $locale) if ($pattern->{numbering1}); # reset counter if needed.
$calculated =~ s/\{X\}/$newlastvalue1/g;
my $newlastvalue2 = $subscription->{'lastvalue2'} || 0;
$newlastvalue2 = _numeration($newlastvalue2, $pattern->{numbering2}, $locale) if ($pattern->{numbering2}); # reset counter if needed.
$calculated =~ s/\{Y\}/$newlastvalue2/g;
my $newlastvalue3 = $subscription->{'lastvalue3'} || 0;
$newlastvalue3 = _numeration($newlastvalue3, $pattern->{numbering3}, $locale) if ($pattern->{numbering3}); # reset counter if needed.
$calculated =~ s/\{Z\}/$newlastvalue3/g;
my $dt = dt_from_string( $subscription->{firstacquidate} );
$calculated =~ s/\{Month\}/$dt->month/eg;
$calculated =~ s/\{MonthName\}/$dt->month_name/eg;
$calculated =~ s/\{Year\}/$dt->year/eg;
$calculated =~ s/\{Day\}/$dt->day/eg;
$calculated =~ s/\{DayName\}/$dt->day_name/eg;
return $calculated;
}
=head2 GetExpirationDate
$enddate = GetExpirationDate($subscriptionid, [$startdate])
this function return the next expiration date for a subscription given on input args.
return
the enddate or undef
=cut
sub GetExpirationDate {
my ( $subscriptionid, $startdate ) = @_;
return unless ($subscriptionid);
my $dbh = C4::Context->dbh;
my $subscription = GetSubscription($subscriptionid);
my $enddate;
# we don't do the same test if the subscription is based on X numbers or on X weeks/months
$enddate = $startdate || $subscription->{startdate};
my @date = split( /-/, $enddate );
return if ( scalar(@date) != 3 || not check_date(@date) );
my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
if ( $frequency and $frequency->{unit} ) {