From e8218aa33f5ec733e089746e1e3b4c6c99e70a55 Mon Sep 17 00:00:00 2001
From: Bill Erickson <berick@esilibrary.com>
Date: Fri, 28 Sep 2012 15:00:37 -0400
Subject: [PATCH] ACQ order upload use copy location from owner

When uploading an ACQ MARC order record, attempt to match copy locations
to copies based on the copy owning location instead of the upload
context org unit.  This allows one branch to order copies for another
and have the copies use the copy location of the other (owning) branch
(when they exist) instead of the ordering branch (or parent org).

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
---
 .../src/perlmods/lib/OpenILS/Application/Acq/Order.pm   | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm
index 7c66162f4a..a297b74208 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm
@@ -1649,17 +1649,28 @@ sub extract_lineitem_detail_data {
     # ---------------------------------------------------------------------
     # Shelving Location
     if( my $name = $compiled{copy_location}) {
-        my $loc = $mgr->cache($base_org, "copy_loc.$name");
+
+        my $cp_base_org = $base_org;
+
+        if ($compiled{owning_lib}) {
+            # start looking for copy locations at the copy 
+            # owning lib instaed of the upload context org
+            $cp_base_org = $compiled{owning_lib};
+        }
+
+        my $loc = $mgr->cache($cp_base_org, "copy_loc.$name");
         unless($loc) {
-            for my $org (@$org_path) {
+            my $org = $cp_base_org;
+            while ($org) {
                 $loc = $mgr->editor->search_asset_copy_location(
                     {owning_lib => $org, name => $name}, {idlist => 1})->[0];
                 last if $loc;
+                $org = $mgr->editor->retrieve_actor_org_unit($org)->parent_ou;
             }
         }
         return $killme->("Invalid copy location $name") unless $loc;
         $compiled{copy_location} = $loc;
-        $mgr->cache($base_org, "copy_loc.$name", $loc);
+        $mgr->cache($cp_base_org, "copy_loc.$name", $loc);
     }
 
     return \%compiled;
-- 
2.11.0