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