add shipping distance to hold copy map
authorLlewellyn Marshall <llewellyn.marshall@ncdcr.gov>
Tue, 23 Aug 2022 20:08:11 +0000 (16:08 -0400)
committerLlewellyn Marshall <llewellyn.marshall@ncdcr.gov>
Tue, 23 Aug 2022 20:17:57 +0000 (16:17 -0400)
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/sql/Pg/090.schema.action.sql
Open-ILS/src/sql/Pg/upgrade/xxxx.function.ahcm_shipping_distance.sql [new file with mode: 0644]

index ed7318a..d6723a2 100644 (file)
@@ -5579,6 +5579,7 @@ SELECT  usr,
                        <field name="id" reporter:datatype="id" />
                        <field name="target_copy" reporter:datatype="link"/>
                        <field name="proximity" reporter:datatype="number"/>
+                       <field name="shipping_distance" reporter:datatype="number"/>
                </fields>
                <links>
                        <link field="hold" reltype="has_a" key="id" map="" class="ahr"/>
index 813ab0b..5cea0e1 100644 (file)
@@ -544,6 +544,7 @@ CREATE TABLE action.hold_copy_map (
        hold            INT     NOT NULL REFERENCES action.hold_request (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
        target_copy     BIGINT  NOT NULL, -- REFERENCES asset.copy (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, -- XXX could be an serial.issuance
        proximity       NUMERIC,
+       shipping_distance       NUMERIC,
        CONSTRAINT copy_once_per_hold UNIQUE (hold,target_copy)
 );
 -- CREATE INDEX acm_hold_idx ON action.hold_copy_map (hold);
@@ -1589,6 +1590,39 @@ BEGIN
 END;
 $f$ LANGUAGE PLPGSQL;
 
+CREATE OR REPLACE FUNCTION action.hold_copy_calculated_shipping_distance(
+    ahr_id INT,
+    acp_id BIGINT
+) RETURNS NUMERIC AS $f$
+DECLARE
+    ahr  action.hold_request%ROWTYPE;
+    acp  asset.copy%ROWTYPE;
+
+    dist NUMERIC;
+    orig_hub NUMERIC;
+    dest_hub NUMERIC;
+BEGIN
+
+    SELECT * INTO ahr FROM action.hold_request WHERE id = ahr_id;
+    SELECT * INTO acp FROM asset.copy WHERE id = acp_id;
+
+    IF copy_context_ou IS NULL THEN
+        copy_context_ou := acp.circ_lib;
+    END IF;
+
+       
+    SELECT hub from actor.list_org_unit_ancestor_shipping_hub(ahr.pickup_lib) 
+       INTO orig_hub;
+    SELECT hub from actor.list_org_unit_ancestor_shipping_hub(acp.circ_lib) 
+       INTO dest_hub;
+    SELECT distance from actor.org_unit_shipping_hub_distance aoushd 
+       where aoushd.orig_hub = orig_hub and aoushd.dest_hub = dest_hub 
+       INTO dist;
+
+    RETURN dist;
+END;
+$f$ LANGUAGE PLPGSQL;
+
 CREATE OR REPLACE FUNCTION action.hold_copy_calculated_proximity_update () RETURNS TRIGGER AS $f$
 BEGIN
     NEW.proximity := action.hold_copy_calculated_proximity(NEW.hold,NEW.target_copy);
@@ -1596,7 +1630,15 @@ BEGIN
 END;
 $f$ LANGUAGE PLPGSQL;
 
+CREATE OR REPLACE FUNCTION action.hold_copy_calculated_shipping_distance_update () RETURNS TRIGGER AS $f$
+BEGIN
+    NEW.shipping_distance := action.hold_copy_calculated_proximity(NEW.hold,NEW.target_copy);
+    RETURN NEW;
+END;
+$f$ LANGUAGE PLPGSQL;
+
 CREATE TRIGGER hold_copy_proximity_update_tgr BEFORE INSERT OR UPDATE ON action.hold_copy_map FOR EACH ROW EXECUTE PROCEDURE action.hold_copy_calculated_proximity_update ();
+CREATE TRIGGER hold_copy_shipping_distance_update_tgr BEFORE INSERT OR UPDATE ON action.hold_copy_map FOR EACH ROW EXECUTE PROCEDURE action.hold_copy_calculated_shipping_distance_update ();
 
 CREATE TABLE action.usr_circ_history (
     id           BIGSERIAL PRIMARY KEY,
diff --git a/Open-ILS/src/sql/Pg/upgrade/xxxx.function.ahcm_shipping_distance.sql b/Open-ILS/src/sql/Pg/upgrade/xxxx.function.ahcm_shipping_distance.sql
new file mode 100644 (file)
index 0000000..d565898
--- /dev/null
@@ -0,0 +1,50 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('xxxx', :eg_version);
+
+ALTER TABLE action.hold_copy_map
+ADD COLUMN     shipping_distance NUMERIC;
+
+CREATE OR REPLACE FUNCTION action.hold_copy_calculated_shipping_distance(
+    ahr_id INT,
+    acp_id BIGINT
+) RETURNS NUMERIC AS $f$
+DECLARE
+    ahr  action.hold_request%ROWTYPE;
+    acp  asset.copy%ROWTYPE;
+
+    dist NUMERIC;
+    orig_hub NUMERIC;
+    dest_hub NUMERIC;
+BEGIN
+
+    SELECT * INTO ahr FROM action.hold_request WHERE id = ahr_id;
+    SELECT * INTO acp FROM asset.copy WHERE id = acp_id;
+
+    IF copy_context_ou IS NULL THEN
+        copy_context_ou := acp.circ_lib;
+    END IF;
+
+       
+    SELECT hub from actor.list_org_unit_ancestor_shipping_hub(ahr.pickup_lib) 
+       INTO orig_hub;
+    SELECT hub from actor.list_org_unit_ancestor_shipping_hub(acp.circ_lib) 
+       INTO dest_hub;
+    SELECT distance from actor.org_unit_shipping_hub_distance aoushd 
+       where aoushd.orig_hub = orig_hub and aoushd.dest_hub = dest_hub 
+       INTO dist;
+
+    RETURN dist;
+END;
+$f$ LANGUAGE PLPGSQL;
+
+CREATE OR REPLACE FUNCTION action.hold_copy_calculated_shipping_distance_update () RETURNS TRIGGER AS $f$
+BEGIN
+    NEW.shipping_distance := action.hold_copy_calculated_proximity(NEW.hold,NEW.target_copy);
+    RETURN NEW;
+END;
+$f$ LANGUAGE PLPGSQL;
+
+CREATE TRIGGER hold_copy_shipping_distance_update_tgr BEFORE INSERT OR UPDATE ON action.hold_copy_map FOR EACH ROW EXECUTE PROCEDURE action.hold_copy_calculated_shipping_distance_update ();
+
+COMMIT;
\ No newline at end of file