From: Llewellyn Marshall Date: Tue, 23 Aug 2022 20:08:11 +0000 (-0400) Subject: add shipping distance to hold copy map X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=ed69647526c7a6b905dd4839b5e1f9ed6e4ba634;p=working%2FEvergreen.git add shipping distance to hold copy map --- diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index ed7318a6d6..d6723a2d84 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -5579,6 +5579,7 @@ SELECT usr, + diff --git a/Open-ILS/src/sql/Pg/090.schema.action.sql b/Open-ILS/src/sql/Pg/090.schema.action.sql index 813ab0ba98..5cea0e1ff0 100644 --- a/Open-ILS/src/sql/Pg/090.schema.action.sql +++ b/Open-ILS/src/sql/Pg/090.schema.action.sql @@ -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 index 0000000000..d56589865d --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/xxxx.function.ahcm_shipping_distance.sql @@ -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