View | Details | Raw Unified | Return to bug 33036
Collapse All | Expand All

(-)a/Koha/REST/V1/Biblios.pm (+96 lines)
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 => { message => sprintf("Successfully merged %s into %s",
951
            $bn_merge, $ref_biblionumber)
952
        });
953
    } catch {
954
        $c->render( status => 400, json => {error => $_});
955
    };
956
}
957
862
1;
958
1;
(-)a/api/v1/swagger/paths/biblios_merge.yaml (+51 lines)
Line 0 Link Here
1
---
2
"/biblios/{biblio_id}/merge/{biblio_id_to_merge}":
3
  put:
4
    x-mojo-to: Biblios#merge
5
    operationId: mergeBiblios
6
    tags:
7
    - merge_biblios
8
    summary: Merge Biblios
9
    parameters:
10
    - name: biblio_id
11
      in: path
12
      description: Bilblionumber to be merged into
13
      required: true
14
      type: string
15
    - name: biblio_id_to_merge
16
      in: path
17
      required: true
18
      description: Biblionumber from which to merge
19
      type: string
20
    - name: body
21
      in: body
22
      description: MARCXML for final record
23
      required: false
24
      schema:
25
        type: object
26
    responses:
27
      '200':
28
        description: Result message
29
      '404':
30
        description: Biblio not found
31
        schema:
32
          "$ref": "../swagger.yaml#/definitions/error"
33
      '401':
34
        description: Authentication required
35
        schema:
36
          "$ref": "../swagger.yaml#/definitions/error"
37
      '403':
38
        description: Access forbidden
39
        schema:
40
          "$ref": "../swagger.yaml#/definitions/error"
41
      '500':
42
        description: Internal server error
43
        schema:
44
          "$ref": "../swagger.yaml#/definitions/error"
45
      '503':
46
        description: Under maintenance
47
        schema:
48
          "$ref": "../swagger.yaml#/definitions/error"
49
    x-koha-authorization:
50
      permissions:
51
        catalogue: "editcatalogue"
(-)a/api/v1/swagger/swagger.yaml (-1 / +2 lines)
Lines 183-188 paths: Link Here
183
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items"
183
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items"
184
  "/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
184
  "/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
185
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items~1{item_id}"
185
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items~1{item_id}"
186
  "/biblios/{biblio_id}/merge/{biblio_id_to_merge}":
187
    $ref: "./paths/biblios_merge.yaml#/~1biblios~1{biblio_id}~1merge~1{biblio_id_to_merge}"
186
  "/cash_registers/{cash_register_id}/cashups":
188
  "/cash_registers/{cash_register_id}/cashups":
187
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
189
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
188
  "/cashups/{cashup_id}":
190
  "/cashups/{cashup_id}":
189
- 

Return to bug 33036