Lines 25-30
use Koha::Ratings;
Link Here
|
25 |
use Koha::RecordProcessor; |
25 |
use Koha::RecordProcessor; |
26 |
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio ); |
26 |
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio ); |
27 |
use C4::Search qw( FindDuplicate ); |
27 |
use C4::Search qw( FindDuplicate ); |
|
|
28 |
use C4::Serials qw( CountSubscriptionFromBiblionumber ); |
29 |
use C4::Reserves qw( MergeHolds ); |
30 |
use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber ); |
28 |
|
31 |
|
29 |
use C4::Barcodes::ValueBuilder; |
32 |
use C4::Barcodes::ValueBuilder; |
30 |
use C4::Context; |
33 |
use C4::Context; |
Lines 859-862
sub list {
Link Here
|
859 |
}; |
862 |
}; |
860 |
} |
863 |
} |
861 |
|
864 |
|
|
|
865 |
<<<<<<< HEAD |
866 |
<<<<<<< HEAD |
867 |
======= |
868 |
>>>>>>> 2f05489f09... cleanup to make qa script happy |
869 |
=head3 merge |
870 |
|
871 |
Controller function that handles merging two biblios. If an optional |
872 |
MARCXML is provided as the request body, this MARCXML replaces the |
873 |
bibliodata of the merge target biblio. |
874 |
|
875 |
=cut |
876 |
|
877 |
sub merge { |
878 |
my $c = shift->openapi->valid_input or return; |
879 |
my $ref_biblionumber = $c->validation->param('biblio_id'); |
880 |
my $bn_merge = $c->validation->param('biblio_id_to_merge'); |
881 |
my $dbh = C4::Context->dbh; |
882 |
|
883 |
my $body = $c->req->body; |
884 |
my $biblio = Koha::Biblios->find( $ref_biblionumber ); |
885 |
if ( not defined $biblio ) { |
886 |
return $c->render( |
887 |
status => 404, |
888 |
json => { error => sprintf("[%s] biblio to merge into not found", $ref_biblionumber) } |
889 |
); |
890 |
} |
891 |
|
892 |
my $from_biblio = Koha::Biblios->find( $bn_merge ); |
893 |
if ( not defined $from_biblio ) { |
894 |
return $c->render( |
895 |
status => 404, |
896 |
# openapi => { error => sprintf("[%s] biblio to merge from not found", $bn_merge) } |
897 |
json => { error => sprintf("[%s] biblio to merge from not found", $bn_merge) } |
898 |
); |
899 |
} |
900 |
|
901 |
return try { |
902 |
if ($body) { |
903 |
my $record = MARC::Record->new_from_xml($body, 'UTF-8', 'MARC21'); |
904 |
$record->leader($biblio->metadata->record->leader()); |
905 |
<<<<<<< HEAD |
906 |
<<<<<<< HEAD |
907 |
ModBiblio($record, $ref_biblionumber, $biblio->frameworkcode); |
908 |
======= |
909 |
ModBiblio($record, $ref_biblionumber, $biblio->frameworkcode); |
910 |
>>>>>>> 5e0fa69e16... implement API for biblio merge |
911 |
======= |
912 |
ModBiblio($record, $ref_biblionumber, $biblio->frameworkcode); |
913 |
>>>>>>> 2f05489f09... cleanup to make qa script happy |
914 |
} |
915 |
|
916 |
$from_biblio->items->move_to_biblio($biblio); |
917 |
$from_biblio->article_requests->update({ biblionumber => $ref_biblionumber }, { no_triggers => 1 }); |
918 |
|
919 |
my $sth_subscription = $dbh->prepare(" |
920 |
UPDATE subscription SET biblionumber = ? WHERE biblionumber = ? |
921 |
"); |
922 |
|
923 |
my $sth_subscriptionhistory = $dbh->prepare(" |
924 |
UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ? |
925 |
"); |
926 |
my $sth_serial = $dbh->prepare(" |
927 |
UPDATE serial SET biblionumber = ? WHERE biblionumber = ? |
928 |
"); |
929 |
my $sth_suggestions = $dbh->prepare(" |
930 |
UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ? |
931 |
"); |
932 |
|
933 |
# Moving subscriptions from the other record to the reference record |
934 |
my $subcount = CountSubscriptionFromBiblionumber($bn_merge); |
935 |
if ($subcount > 0) { |
936 |
$sth_subscription->execute($ref_biblionumber, $bn_merge); |
937 |
$sth_subscriptionhistory->execute($ref_biblionumber, $bn_merge); |
938 |
} |
939 |
|
940 |
# Moving serials |
941 |
$sth_serial->execute($ref_biblionumber, $bn_merge); |
942 |
|
943 |
# Moving suggestions |
944 |
$sth_suggestions->execute($ref_biblionumber, $bn_merge); |
945 |
|
946 |
# Moving orders (orders linked to items of frombiblio have already been moved by move_to_biblio) |
947 |
my @allorders = GetOrdersByBiblionumber($bn_merge); |
948 |
foreach my $myorder (@allorders) { |
949 |
$myorder->{'biblionumber'} = $ref_biblionumber; |
950 |
ModOrder ($myorder); |
951 |
# TODO : add error control (in ModOrder?) |
952 |
} |
953 |
|
954 |
# Deleting the other records |
955 |
# Move holds |
956 |
MergeHolds($dbh, $ref_biblionumber, $bn_merge); |
957 |
my $error = DelBiblio($bn_merge); |
958 |
if ($error) { |
959 |
$c->render( status => 400, json => {error => $error}); |
960 |
} |
961 |
|
962 |
return $c->render( status => 200, openapi => { message => sprintf("Successfully merged %s into %s", |
963 |
$bn_merge, $ref_biblionumber) |
964 |
}); |
965 |
} catch { |
966 |
$c->render( status => 400, json => {error => $_}); |
967 |
}; |
968 |
} |
969 |
|
862 |
1; |
970 |
1; |