Vandelay copy-overlay support
authorBill Erickson <berick@esilibrary.com>
Fri, 27 Jul 2012 19:04:27 +0000 (15:04 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 27 Jul 2012 19:04:27 +0000 (15:04 -0400)
Adds support of for updating fields on an existing copy via vandelay
holdings import.  If a vandelay item has an "internal_id" value pointing
to the ID of a real copy (asset.copy), any values found within the
vandelay item will replace values in the real copy, including the
callnumber (label).

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm

index 89a7d8d..484a7e2 100644 (file)
@@ -1574,76 +1574,184 @@ sub import_record_asset_list_impl {
         for my $item_id (@$item_ids) {
             my $e = new_editor(requestor => $requestor, xact => 1);
             my $item = $e->retrieve_vandelay_import_item($item_id);
+            my ($copy, $vol, $evt);
+
             $$report_args{import_item} = $item;
             $$report_args{e} = $e;
             $$report_args{import_error} = undef;
             $$report_args{evt} = undef;
 
-            # --------------------------------------------------------------------------------
-            # Find or create the volume
-            # --------------------------------------------------------------------------------
-            my ($vol, $evt) =
-                OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
-                    $e, $item->call_number, $rec->imported_as, $item->owning_lib);
+            if ($item->internal_id) {
+                # copy matches an existing copy.  Overlay instead of create.
 
-            if($evt) {
+                $copy = $e->search_asset_copy([
+                    {id => $item->internal_id, deleted => 'f'},
+                    {flesh => 1, flesh_fields => {acp => ['call_number']}}
+                ]);
 
-                $$report_args{evt} = $evt;
-                respond_with_status($report_args);
-                next;
-            }
+                if (!$copy) {
+                    $$report_args{evt} = $e->die_event;
+                    respond_with_status($report_args);
+                    next;
+                }
 
-            # --------------------------------------------------------------------------------
-            # Create the new copy
-            # --------------------------------------------------------------------------------
-            my $copy = Fieldmapper::asset::copy->new;
-            $copy->loan_duration(2);
-            $copy->fine_level(2);
-            $copy->barcode($item->barcode);
-            $copy->location($item->location);
-            $copy->circ_lib($item->circ_lib || $item->owning_lib);
-            $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
-            $copy->circulate($item->circulate);
-            $copy->deposit($item->deposit);
-            $copy->deposit_amount($item->deposit_amount);
-            $copy->ref($item->ref);
-            $copy->holdable($item->holdable);
-            $copy->price($item->price);
-            $copy->circ_as_type($item->circ_as_type);
-            $copy->alert_message($item->alert_message);
-            $copy->opac_visible($item->opac_visible);
-            $copy->circ_modifier($item->circ_modifier);
+                # overlaying copies requires an extra permission
+                if (!$e->allowed("IMPORT_OVERLAY_COPY", $copy->call_number->owning_lib)) {
+                    $$report_args{evt} = $e->die_event;
+                    respond_with_status($report_args);
+                    next;
+                }
 
-            # --------------------------------------------------------------------------------
-            # Check for dupe barcode
-            # --------------------------------------------------------------------------------
-            if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
-                $$report_args{evt} = $evt;
-                $$report_args{import_error} = 'import.item.duplicate.barcode'
-                    if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
-                respond_with_status($report_args);
-                next;
-            }
+                # are we updating the call-number?
+                if ($item->call_number and $item->call_number ne $copy->call_number->label) {
+
+                    my $count = $e->json_query({
+                        select => {acp => [{
+                            alias => 'count', 
+                            column => 'id', 
+                            transform => 'count', 
+                            aggregate => 1
+                        }]},
+                        from => 'acp',
+                        where => {
+                            deleted => 'f',
+                            call_number => $copy->call_number->id
+                        }
+                    })->[0]->{count};
 
-            # --------------------------------------------------------------------------------
-            # create copy notes
-            # --------------------------------------------------------------------------------
-            $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
-                $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
+                    if ($count == 1) {
 
-            if($evt) {
-                $$report_args{evt} = $evt;
-                respond_with_status($report_args);
-                next;
-            }
+                        # if this is the only copy attached to this 
+                        # callnumber, just update the callnumber
 
-            $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
-                $e, $copy, '', $item->priv_note) if $item->priv_note;
+                        $copy->call_number->label($item->call_number);
+                        if (!$e->udpate_asset_call_number($copy->call_number)) {
+                            $$report_args{evt} = $e->die_event;
+                            respond_with_status($report_args);
+                            next;
+                        }
 
-            if($evt) {
-                $$report_args{evt} = $evt;
-                respond_with_status($report_args);
-                next;
+                    } else {
+
+                        # otherwise, move the copy to a new/existing 
+                        # call-number with the given label/owner
+                        # note that overlay does not allow the owning_lib 
+                        # to be changed.  Should it?
+
+                        ($vol, $evt) =
+                            OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
+                                $e, $item->call_number, 
+                                $copy->call_number->record, 
+                                $copy->call_number->owning_lib
+                            );
+
+                        if($evt) {
+                            $$report_args{evt} = $evt;
+                            respond_with_status($report_args);
+                            next;
+                        }
+
+                        $copy->call_number($vol);
+                    }
+                } # cn-update
+
+                # for any field that has a defined value, replace the value on the copy
+                $copy->barcode($item->barcode) if defined $item->barcode;
+                $copy->location($item->location) if defined $item->location;
+                $copy->circ_lib($item->circ_lib) if defined $item->circ_lib;
+                $copy->status($item->status) if defined $item->status;
+                $copy->circulate($item->circulate) if defined $item->circulate;
+                $copy->deposit($item->deposit) if $item->deposit;
+                $copy->deposit_amount($item->deposit_amount) if defined $item->deposit_amount;
+                $copy->ref($item->ref) if defined $item->ref;
+                $copy->holdable($item->holdable) if defined $item->holdable;
+                $copy->price($item->price) if defined $item->price;
+                $copy->circ_as_type($item->circ_as_type) if defined $item->circ_as_type;
+                $copy->alert_message($item->alert_message) if defined $item->alert_message;
+                $copy->opac_visible($item->opac_visible) if defined $item->opac_visible;
+                $copy->circ_modifier($item->circ_modifier) if defined $item->circ_modifier;
+
+                # de-flesh for update
+                $copy->call_number($copy->call_number->id);
+
+                $evt = OpenILS::Application::Cat::AssetCommon->
+                    update_fleshed_copies($e, {all => 1}, undef, [$copy]);
+
+                if($evt) {
+                    $$report_args{evt} = $evt;
+                    respond_with_status($report_args);
+                    next;
+                }
+
+            } else { 
+
+                # Creating a new copy
+
+                # --------------------------------------------------------------------------------
+                # Find or create the volume
+                # --------------------------------------------------------------------------------
+                my ($vol, $evt) =
+                    OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
+                        $e, $item->call_number, $rec->imported_as, $item->owning_lib);
+
+                if($evt) {
+                    $$report_args{evt} = $evt;
+                    respond_with_status($report_args);
+                    next;
+                }
+
+                # --------------------------------------------------------------------------------
+                # Create the new copy
+                # --------------------------------------------------------------------------------
+                $copy = Fieldmapper::asset::copy->new;
+                $copy->loan_duration(2);
+                $copy->fine_level(2);
+                $copy->barcode($item->barcode);
+                $copy->location($item->location);
+                $copy->circ_lib($item->circ_lib || $item->owning_lib);
+                $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
+                $copy->circulate($item->circulate);
+                $copy->deposit($item->deposit);
+                $copy->deposit_amount($item->deposit_amount);
+                $copy->ref($item->ref);
+                $copy->holdable($item->holdable);
+                $copy->price($item->price);
+                $copy->circ_as_type($item->circ_as_type);
+                $copy->alert_message($item->alert_message);
+                $copy->opac_visible($item->opac_visible);
+                $copy->circ_modifier($item->circ_modifier);
+
+                # --------------------------------------------------------------------------------
+                # Check for dupe barcode
+                # --------------------------------------------------------------------------------
+                if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
+                    $$report_args{evt} = $evt;
+                    $$report_args{import_error} = 'import.item.duplicate.barcode'
+                        if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
+                    respond_with_status($report_args);
+                    next;
+                }
+
+                # --------------------------------------------------------------------------------
+                # create copy notes
+                # --------------------------------------------------------------------------------
+                $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
+                    $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
+
+                if($evt) {
+                    $$report_args{evt} = $evt;
+                    respond_with_status($report_args);
+                    next;
+                }
+
+                $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
+                    $e, $copy, '', $item->priv_note) if $item->priv_note;
+
+                if($evt) {
+                    $$report_args{evt} = $evt;
+                    respond_with_status($report_args);
+                    next;
+                }
             }
 
             # set the import data on the import item