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

(-)a/Koha/REST/V1/Biblios.pm (+108 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
<<<<<<< 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;
(-)a/Makefile.PL (+1 lines)
Lines 351-356 my $target_map = { Link Here
351
  './README.txt'                => 'NONE',
351
  './README.txt'                => 'NONE',
352
  './patroncards'               => 'INTRANET_CGI_DIR',
352
  './patroncards'               => 'INTRANET_CGI_DIR',
353
  './patron_lists'              => 'INTRANET_CGI_DIR',
353
  './patron_lists'              => 'INTRANET_CGI_DIR',
354
  './perltidyrc'                => 'NONE',
354
  './plugins'                   => 'INTRANET_CGI_DIR',
355
  './plugins'                   => 'INTRANET_CGI_DIR',
355
  './pos'                       => 'INTRANET_CGI_DIR',
356
  './pos'                       => 'INTRANET_CGI_DIR',
356
  './recalls'                   => 'INTRANET_CGI_DIR',
357
  './recalls'                   => 'INTRANET_CGI_DIR',
(-)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