-
Notifications
You must be signed in to change notification settings - Fork 1
/
Base.pm
1458 lines (1197 loc) · 42.1 KB
/
Base.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 Koha::Illbackends::ReprintsDesk::Base;
# Copyright PTFS Europe 2022
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use strict;
use warnings;
use JSON qw( to_json from_json );
use File::Basename qw( dirname );
use C4::Installer;
use Koha::Illbackends::ReprintsDesk::Lib::API;
use Koha::Illbackends::ReprintsDesk::Processor::PlaceOrders;
use Koha::Illbackends::ReprintsDesk::Processor::GetOrderHistory;
use Koha::Illbackends::ReprintsDesk::Processor::CheckAvailability;
use Koha::Illbackends::ReprintsDesk::Processor::EnqueueNotices;
use Koha::Libraries;
use Koha::Patrons;
our $VERSION = "1.0.0";
sub new {
my ( $class, $params ) = @_;
my $api = Koha::Illbackends::ReprintsDesk::Lib::API->new($VERSION);
my $self = {
_api => $api,
templates => {
'REPRINTS_DESK_MIGRATE_IN' => dirname(__FILE__) . '/intra-includes/log/reprints_desk_migrate_in.tt',
'REPRINTS_DESK_REQUEST_SUCCEEDED' => dirname(__FILE__)
. '/intra-includes/log/reprints_desk_request_succeeded.tt',
'REPRINTS_DESK_REQUEST_ORDER_UPDATED' => dirname(__FILE__)
. '/intra-includes/log/reprints_desk_request_order_updated.tt',
'REPRINTS_DESK_REQUEST_FAILED' => dirname(__FILE__) . '/intra-includes/log/reprints_desk_request_failed.tt'
}
};
$self->{_logger} = $params->{logger} if ( $params->{logger} );
$self->{backend_wide_processors} = [
Koha::Illbackends::ReprintsDesk::Processor::PlaceOrders->new,
Koha::Illbackends::ReprintsDesk::Processor::GetOrderHistory->new,
Koha::Illbackends::ReprintsDesk::Processor::CheckAvailability->new,
Koha::Illbackends::ReprintsDesk::Processor::EnqueueNotices->new
];
bless( $self, $class );
return $self;
}
=head3 create
Handle the "create" flow
=cut
sub create {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage};
my $response = {
cwd => dirname(__FILE__),
backend => $self->name,
method => "create",
stage => $stage,
branchcode => $other->{branchcode},
cardnumber => $other->{cardnumber},
status => "",
message => "",
error => 0,
field_map => $self->fieldmap_sorted,
field_map_json => to_json( $self->fieldmap() )
};
# Check for borrowernumber, but only if we're not receiving an OpenURL
if ( !$other->{openurl}
&& ( !$other->{borrowernumber} && defined( $other->{cardnumber} ) ) )
{
$response->{cardnumber} = $other->{cardnumber};
# 'cardnumber' here could also be a surname (or in the case of
# search it will be a borrowernumber).
my ( $brw_count, $brw ) =
_validate_borrower( $other->{'cardnumber'}, $stage );
if ( $brw_count == 0 ) {
$response->{status} = "invalid_borrower";
$response->{value} = $params;
$response->{stage} = "init";
$response->{error} = 1;
return $response;
} elsif ( $brw_count > 1 ) {
# We must select a specific borrower out of our options.
$params->{brw} = $brw;
$response->{value} = $params;
$response->{stage} = "borrowers";
$response->{error} = 0;
return $response;
} else {
$other->{borrowernumber} = $brw->borrowernumber;
}
$self->{borrower} = $brw;
}
# Initiate process
if ( !$stage || $stage eq 'init' ) {
# First thing we want to do, is check if we're receiving
# an OpenURL and transform it into something we can
# understand
if ( $other->{openurl} ) {
# We only want to transform once
delete $other->{openurl};
$params = _openurl_to_reprints_desk($params);
}
# Pass the map of form fields in forms that can be used by TT
# and JS
$response->{field_map} = $self->fieldmap_sorted;
$response->{field_map_json} = to_json( $self->fieldmap() );
# We just need to request the snippet that builds the Creation
# interface.
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
}
# Validate form and perform search if valid
elsif ( $stage eq 'validate' || $stage eq 'form' ) {
if ( _fail( $other->{'branchcode'} ) ) {
# Pass the map of form fields in forms that can be used by TT
# and JS
$response->{field_map} = $self->fieldmap_sorted;
$response->{field_map_json} = to_json( $self->fieldmap() );
$response->{status} = "missing_branch";
$response->{error} = 1;
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
} elsif ( !Koha::Libraries->find( $other->{'branchcode'} ) ) {
# Pass the map of form fields in forms that can be used by TT
# and JS
$response->{field_map} = $self->fieldmap_sorted;
$response->{field_map_json} = to_json( $self->fieldmap() );
$response->{status} = "invalid_branch";
$response->{error} = 1;
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
} elsif ( !$self->_validate_metadata($other) ) {
$response->{field_map} = $self->fieldmap_sorted;
$response->{field_map_json} = to_json( $self->fieldmap() );
$response->{status} = "invalid_metadata";
$response->{error} = 1;
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
} else {
my $result = $self->create_submission($params);
$response->{stage} = 'commit';
$response->{next} = "illview";
$response->{params} = $params;
return $response;
}
}
}
=head3 illview
View and manage an ILL request
=cut
sub illview {
my ( $self, $params ) = @_;
return {
field_map_json => to_json( fieldmap() ),
method => "illview"
};
}
=head3 edititem
Edit an item's metadata
=cut
sub edititem {
my ( $self, $params ) = @_;
# Don't allow editing of requested or completed submissions
return {
cwd => dirname(__FILE__),
method => 'illlist'
} if ( $params->{request}->status eq 'REQ' || $params->{request}->status eq 'COMP' );
my $other = $params->{other};
my $stage = $other->{stage};
if ( !$stage || $stage eq 'init' ) {
my $attrs = $params->{request}->illrequestattributes->unblessed;
foreach my $attr ( @{$attrs} ) {
$other->{ $attr->{type} } = $attr->{value};
}
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'edititem',
stage => 'form',
value => $params,
field_map => $self->fieldmap_sorted,
field_map_json => to_json( $self->fieldmap )
};
} elsif ( $stage eq 'form' ) {
# Update submission
my $submission = $params->{request};
$submission->updated( DateTime->now );
$submission->store;
# We may be receiving a submitted form due to the user having
# changed request material type, so we just need to go straight
# back to the form, the type has been changed in the params
if ( defined $other->{change_type} ) {
delete $other->{change_type};
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'edititem',
stage => 'form',
value => $params,
field_map => $self->fieldmap_sorted,
field_map_json => to_json( $self->fieldmap )
};
}
# ...Populate Illrequestattributes
# generate $request_details
# We do this with a 'dump all and repopulate approach' inside
# a transaction, easier than catering for create, update & delete
my $dbh = C4::Context->dbh;
my $schema = Koha::Database->new->schema;
$schema->txn_do(
sub {
# Delete all existing attributes for this request
$dbh->do(
q|
DELETE FROM illrequestattributes WHERE illrequest_id=?
|, undef, $submission->id
);
# Insert all current attributes for this request
my $fields = $self->fieldmap;
# First insert our Reprints Desk fields
foreach my $field ( %{$other} ) {
my $value = $other->{$field};
if ( $other->{$field}
&& length $other->{$field} > 0 )
{
my @bind = (
$submission->id,
column_exists( 'illrequestattributes', 'backend' ) ? "ReprintsDesk" : (),
$field, $value, 0
);
$dbh->do(
q|
INSERT IGNORE INTO illrequestattributes
(illrequest_id,|
. ( column_exists( 'illrequestattributes', 'backend' ) ? q|backend,| : q|| ) . q|
type, value, readonly) VALUES
(?, ?, ?, ?, ?)
|, undef, @bind
);
}
}
# Now insert our core equivalents, if an equivalently named Rapid field
# doesn't already exist
foreach my $field ( %{$other} ) {
my $value = $other->{$field};
if ( $other->{$field}
&& $fields->{$field}->{ill}
&& length $other->{$field} > 0
&& !$fields->{ $fields->{$field}->{ill} } )
{
my @bind = (
$submission->id,
column_exists( 'illrequestattributes', 'backend' ) ? "ReprintsDesk" : (),
$field, $value, 0
);
$dbh->do(
q|
INSERT IGNORE INTO illrequestattributes
(illrequest_id,|
. ( column_exists( 'illrequestattributes', 'backend' ) ? q|backend,| : q|| ) . q|
type, value, readonly) VALUES
(?, ?, ?, ?, ?)
|, undef, @bind
);
}
}
}
);
# Create response
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'create',
stage => 'commit',
next => 'illview',
value => $params,
field_map => $self->fieldmap_sorted,
field_map_json => to_json( $self->fieldmap )
};
}
}
=head3 do_join
If a field should be joined with another field for storage as a core
value or display, then do it
=cut
sub do_join {
my ( $self, $field, $metadata ) = @_;
my $fields = $self->fieldmap;
my $value = $metadata->{$field};
my $join = $fields->{$field}->{join};
if ( $join && $metadata->{$join} && $value ) {
my @to_join = ( $value, $metadata->{$join} );
$value = join " ", @to_join;
}
return $value;
}
=head3 mark_completed
Mark a request as completed (status = COMP).
=cut
sub mark_completed {
my ($self) = @_;
$self->status('COMP')->store;
$self->completed( dt_from_string() )->store;
return {
error => 0,
status => '',
message => '',
method => 'mark_completed',
stage => 'commit',
next => 'illview',
};
}
=head3 ready
Mark this request as 'READY'
=cut
sub ready {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $request = Koha::Illrequests->find( $other->{illrequest_id} );
$request->status('READY');
$request->updated( DateTime->now );
$request->store;
return {
error => 0,
status => '',
message => '',
method => 'ready',
stage => 'commit',
next => 'illview',
value => $params,
};
}
=head3 mark_new
Mark this request as 'NEW'
=cut
sub mark_new {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $request = Koha::Illrequests->find( $other->{illrequest_id} );
$request->status('NEW');
$request->updated( DateTime->now );
$request->store;
return {
error => 0,
status => '',
message => '',
method => 'mark_new',
stage => 'commit',
next => 'illview',
value => $params,
};
}
=head3 migrate
Migrate a request into or out of this backend
=cut
sub migrate {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage};
my $step = $other->{step};
my $fields = $self->fieldmap;
my $request = Koha::Illrequests->find( $other->{illrequest_id} );
# Record where we're migrating from, so we can log that
my $migrating_from = $request->backend;
if ( $request->status eq 'REQ' ) {
# The orderid is no longer applicable
$request->orderid(undef);
}
$request->status('MIG');
$request->backend( $self->name );
$request->updated( DateTime->now );
$request->store;
# Translate the core metadata into our schema
my $all_attrs = $request->illrequestattributes->unblessed;
# For each attribute, if the property name is a core one we change it to the Reprints Desk
# equivalent, otherwise we can skip it as it already exists in the attributes list
foreach my $attr ( @{$all_attrs} ) {
my $rd_field_name = $self->find_reprints_desk_property( $attr->{type} );
# If we've found a Reprints Desk field name and an attribute doesn't already exist
# with this name, create a new one
if ( $rd_field_name && !$self->find_illrequestattribute( $all_attrs, $rd_field_name ) ) {
Koha::Illrequestattribute->new(
{
illrequest_id => $request->illrequest_id,
# Check required for compatibility with installations before bug 33970
column_exists( 'illrequestattributes', 'backend' ) ? ( backend => "ReprintsDesk" ) : (),
type => $rd_field_name,
value => $attr->{value},
}
)->store;
}
}
# Log that the migration took place
if ( $self->_logger ) {
my $payload = {
modulename => 'ILL',
actionname => 'REPRINTS_DESK_MIGRATE_IN',
objectnumber => $request->id,
infos => to_json(
{
log_origin => $self->name,
migrated_from => $migrating_from,
migrated_to => $self->name
}
)
};
$self->_logger->log_something($payload);
}
return {
error => 0,
status => '',
message => '',
method => 'migrate',
stage => 'commit',
next => 'illview',
value => $params,
};
}
=head3 _validate_metadata
Ensure the metadata we've got conforms to the order
API specification
=cut
sub _validate_metadata {
my ( $self, $metadata ) = @_;
return 1;
}
=head3 create_submission
Create a local submission, for later Reprints Desk request creation
=cut
sub create_submission {
my ( $self, $params ) = @_;
my $patron = Koha::Patrons->find( $params->{other}->{borrowernumber} );
my $request = $params->{request};
$request->borrowernumber( $patron->borrowernumber );
$request->branchcode( $params->{other}->{branchcode} );
$request->status('NEW');
$request->batch_id(
$params->{other}->{ill_batch_id} ? $params->{other}->{ill_batch_id} : $params->{other}->{batch_id} );
$request->backend( $self->name );
$request->placed( DateTime->now );
$request->updated( DateTime->now );
$request->store;
$params->{other}->{type} = 'article';
# Store the request attributes
$self->create_illrequestattributes( $request, $params->{other} );
# Now store the core equivalents
$self->create_illrequestattributes( $request, $params->{other}, 1 );
return $request;
}
=head3
Store metadata for a given request for our Reprints Desk fields
=cut
sub create_illrequestattributes {
my ( $self, $request, $metadata, $core ) = @_;
# Get the canonical list of metadata fields
my $fields = $self->fieldmap;
# Get any existing illrequestattributes for this request,
# so we can avoid trying to create duplicates
my $existing_attrs = $request->illrequestattributes->unblessed;
my $existing_hash = {};
foreach my $a ( @{$existing_attrs} ) {
$existing_hash->{ lc $a->{type} } = $a->{value};
}
# Iterate our list of fields
foreach my $field ( keys %{$fields} ) {
if (
# If we're working with core metadata, check if this field
# has a core equivalent
( ( $core && $fields->{$field}->{ill} ) || !$core )
&& $metadata->{$field}
&& length $metadata->{$field} > 0
)
{
my $att_type = $core ? $fields->{$field}->{ill} : $field;
my $att_value = $metadata->{$field};
# If core, we might need to join
if ($core) {
$att_value = $self->do_join( $field, $metadata );
}
# If it doesn't already exist for this request
if ( !exists $existing_hash->{ lc $att_type } ) {
my $data = {
illrequest_id => $request->illrequest_id,
# Check required for compatibility with installations before bug 33970
column_exists( 'illrequestattributes', 'backend' ) ? ( backend => "ReprintsDesk" ) : (),
type => $att_type,
value => $att_value,
readonly => 0
};
Koha::Illrequestattribute->new($data)->store;
}
}
}
}
=head3 prep_submission_metadata
Given a submission's metadata, probably from a form,
but maybe as an Illrequestattributes object,
and a partly constructed hashref, add any metadata that
is appropriate for this material type
=cut
sub prep_submission_metadata {
my ( $self, $metadata, $return ) = @_;
$return = $return //= {};
my $metadata_hashref = {};
if ( ref $metadata eq "Koha::Illrequestattributes" ) {
while ( my $attr = $metadata->next ) {
$metadata_hashref->{ $attr->type } = $attr->value;
}
} else {
$metadata_hashref = $metadata;
}
# Get our canonical field list
my $fields = $self->fieldmap;
# Iterate our list of fields
foreach my $field ( keys %{$fields} ) {
if ( $metadata_hashref->{$field}
&& length $metadata_hashref->{$field} > 0 )
{
$metadata_hashref->{$field} =~ s/ / /g;
if ( $fields->{$field}->{api_max_length} ) {
$return->{$field} = substr( $metadata_hashref->{$field}, 0, $fields->{$field}->{api_max_length} );
} else {
$return->{$field} = $metadata_hashref->{$field};
}
}
}
return $return;
}
=head3 submit_and_request
Creates a local submission, then uses the returned ID to create
a Reprints Desk request
=cut
sub submit_and_request {
my ( $self, $params ) = @_;
# First we create a submission
my $submission = $self->create_submission($params);
# Now use the submission to try and create a request with Reprints Desk
return $self->create_request($submission);
}
=head3 create_request
Take a previously created submission and send it to Reprints Desk
in order to create a request
=cut
sub create_request {
my ( $self, $submission ) = @_;
my $metadata = {};
$metadata = $self->prep_submission_metadata(
$submission->illrequestattributes,
$metadata
);
# We may need to remove fields prior to sending the request
my $fields = fieldmap();
foreach my $field ( keys %{$fields} ) {
if ( $fields->{$field}->{no_submit} ) {
delete $metadata->{$field};
}
}
# Make the request with Reprints Desk via the koha-plugin-reprintsdesk API
my $response =
$self->{_api}->Order_PlaceOrder2( $metadata, $submission->borrowernumber, $submission->illrequest_id );
# If the call to Reprints Desk was successful,
# add the Reprints Desk request ID to our submission's metadata
my $body = from_json( $response->decoded_content );
if ( scalar @{ $body->{errors} } == 0 && $body->{result}->{Order_PlaceOrder2Result} == 1 ) {
my $request_id = $body->{result}->{orderID};
if ( $request_id && length $request_id > 0 ) {
Koha::Illrequestattribute->new(
{
illrequest_id => $submission->illrequest_id,
# Check required for compatibility with installations before bug 33970
column_exists( 'illrequestattributes', 'backend' ) ? ( backend => "ReprintsDesk" ) : (),
type => 'orderId',
value => $request_id
}
)->store;
}
my $rnd_id = $body->{result}->{rndID};
if ( $rnd_id && length $rnd_id > 0 ) {
Koha::Illrequestattribute->new(
{
illrequest_id => $submission->illrequest_id,
# Check required for compatibility with installations before bug 33970
column_exists( 'illrequestattributes', 'backend' ) ? ( backend => "ReprintsDesk" ) : (),
type => 'rndId',
value => $rnd_id
}
)->store;
}
# Add the Reprints Desk ID to the orderid field
$submission->orderid($request_id);
# Update the submission status
$submission->status('REQ')->store;
# Log the outcome
$self->log_request_outcome(
{
outcome => 'REPRINTS_DESK_REQUEST_SUCCEEDED',
request => $submission
}
);
return { success => 1 };
}
# The call to Reprints Desk failed for some reason. Add the message we got back from the API
# to the submission's Staff Notes
my $errors = join '. ', map { $_->{message} . ( $_->{path} ? ' path: ' . $_->{path} : '' ) } @{ $body->{errors} };
$submission->append_to_note("Reprints Desk request failed:\n$errors");
# Log the outcome
$self->log_request_outcome(
{
outcome => 'REPRINTS_DESK_REQUEST_FAILED',
request => $submission,
message => $errors
}
);
$submission->status('ERROR')->store;
# Return the message
return {
success => 0,
message => $errors
};
}
=head3 confirm
A wrapper around create_request allowing us to
provide the "confirm" method required by
the status graph
=cut
sub confirm {
my ( $self, $params ) = @_;
my $return = $self->create_request( $params->{request} );
my $return_value = {
cwd => dirname(__FILE__),
error => 0,
status => "",
message => "",
method => "create",
stage => "commit",
next => "illview",
value => {},
%{$return}
};
return $return_value;
}
=head3 log_request_outcome
Log the outcome of a request to the Reprints Desk API
=cut
sub log_request_outcome {
my ( $self, $params ) = @_;
if ( $self->{_logger} ) {
# TODO: This is a transitionary measure, we have removed set_data
# in Bug 20750, so calls to it won't work. But since 20750 is
# only in 19.05+, they only won't work in earlier
# versions. So we're temporarily going to allow for both cases
my $payload = {
modulename => 'ILL',
actionname => $params->{outcome},
objectnumber => $params->{request}->id,
infos => to_json(
{
log_origin => $self->name,
response => $params->{message}
}
)
};
if ( $self->{_logger}->can('set_data') ) {
$self->{_logger}->set_data($payload);
} else {
$self->{_logger}->log_something($payload);
}
}
}
=head3 get_log_template_path
my $path = $BLDSS->get_log_template_path($action);
Given an action, return the path to the template for displaying
that action log
=cut
sub get_log_template_path {
my ( $self, $action ) = @_;
return $self->{templates}->{$action};
}
=head3 metadata
Return a hashref containing canonical values from the key/value
illrequestattributes store
=cut
sub metadata {
my ( $self, $request ) = @_;
my $attrs = $request->illrequestattributes;
my $fields = $self->fieldmap;
my $metadata = {};
my $metadata_keyed_on_prop = {};
while ( my $attr = $attrs->next ) {
if ( $fields->{ $attr->type } ) {
my $label = $fields->{ $attr->type }->{label};
$metadata->{$label} = $attr->value;
$metadata_keyed_on_prop->{ $attr->type } = $attr->value;
}
}
my $rd_title_key = 'Journal title';
$metadata->{Title} = $metadata->{$rd_title_key} if $metadata->{$rd_title_key};
return $metadata;
}
=head3 capabilities
$capability = $backend->capabilities($name);
Return the sub implementing a capability selected by NAME, or 0 if that
capability is not implemented.
=cut
sub capabilities {
my ( $self, $name ) = @_;
my $capabilities = {
# View and manage a request
illview => sub { illview(@_); },
# Migrate
migrate => sub { $self->migrate(@_); },
# Return whether we can create the request
# i.e. the create form has been submitted
can_create_request => sub { _can_create_request(@_) },
# This is required for compatibility
# with Koha versions prior to bug 33716
should_display_availability => sub { _can_create_request(@_) },
provides_batch_requests => sub { return 1; },
# We can create ILL requests with data passed from the API
create_api => sub { $self->create_api(@_) }
};
return $capabilities->{$name};
}
=head3 _can_create_request
Given the parameters we've been passed, should we create the request
=cut
sub _can_create_request {
my ($params) = @_;
return ( defined $params->{'stage'} ) ? 1 : 0;
}
=head3 status_graph
=cut
sub status_graph {
return {
EDITITEM => {
prev_actions => ['NEW'],
id => 'EDITITEM',
name => 'Edited item metadata',
ui_method_name => 'Edit item metadata',
method => 'edititem',
next_actions => [],
ui_method_icon => 'fa-edit',
},
CIT => {
prev_actions => ['REQ'],
id => 'CIT',
name => 'Citation Verification',
ui_method_name => 0,
method => 0,
next_actions => [ 'COMP', 'MIG', 'KILL' ],
ui_method_icon => 0,
},
SOURCE => {
prev_actions => ['REQ'],
id => 'SOURCE',
name => 'Sourcing',
ui_method_name => 0,
method => 0,
next_actions => [ 'COMP', 'MIG', 'KILL' ],
ui_method_icon => 0,
},
ERROR => {
prev_actions => [],
id => 'ERROR',
name => 'Request error',
ui_method_name => 0,
method => 0,
next_actions => [ 'MARK_NEW', 'COMP', 'EDITITEM', 'STANDBY', 'READY', 'MIG', 'KILL' ],
ui_method_icon => 0,
},
COMP => {
prev_actions => [ 'CIT', 'SOURCE', 'ERROR' ],
id => 'COMP',
name => 'Order Complete',
ui_method_name => 'Mark completed',
method => 'mark_completed',
next_actions => [],
ui_method_icon => 'fa-check',
},
READY => {