Lazy Circ (AKA partial barcode lookup)
[evergreen-equinox.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.lazy_circ.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('XXXX');
4
5 INSERT INTO config.org_unit_setting_type ( name, label, description, datatype) VALUES ( 'circ.staff_client.actor_on_checkout', 'Load patron from Checkout', 'When scanning barcodes into Checkout auto-detect if a new patron barcode is scanned and auto-load the new patron.', 'bool');
6
7 CREATE TABLE config.barcode_completion (
8     id          SERIAL PRIMARY KEY,
9     active      BOOL NOT NULL DEFAULT true,
10     org_unit    INT NOT NULL, -- REFERENCES actor.org_unit(id) DEFERRABLE INITIALLY DEFERRED,
11     prefix      TEXT,
12     suffix      TEXT,
13     length      INT NOT NULL DEFAULT 0,
14     padding     TEXT,
15     padding_end BOOL NOT NULL DEFAULT false,
16     asset       BOOL NOT NULL DEFAULT true,
17     actor       BOOL NOT NULL DEFAULT true
18 );
19
20 CREATE TYPE evergreen.barcode_set AS (type TEXT, id BIGINT, barcode TEXT);
21
22 CREATE OR REPLACE FUNCTION evergreen.get_barcodes(select_ou INT, type TEXT, in_barcode TEXT) RETURNS SETOF evergreen.barcode_set AS $$
23 DECLARE
24     cur_barcode TEXT;
25     barcode_len INT;
26     completion_len  INT;
27     asset_barcodes  TEXT[];
28     actor_barcodes  TEXT[];
29     do_asset    BOOL = false;
30     do_serial   BOOL = false;
31     do_booking  BOOL = false;
32     do_actor    BOOL = false;
33     completion_set  config.barcode_completion%ROWTYPE;
34 BEGIN
35
36     IF position('asset' in type) > 0 THEN
37         do_asset = true;
38     END IF;
39     IF position('serial' in type) > 0 THEN
40         do_serial = true;
41     END IF;
42     IF position('booking' in type) > 0 THEN
43         do_booking = true;
44     END IF;
45     IF do_asset OR do_serial OR do_booking THEN
46         asset_barcodes = asset_barcodes || in_barcode;
47     END IF;
48     IF position('actor' in type) > 0 THEN
49         do_actor = true;
50         actor_barcodes = actor_barcodes || in_barcode;
51     END IF;
52
53     barcode_len := length(in_barcode);
54
55     FOR completion_set IN
56       SELECT * FROM config.barcode_completion
57         WHERE active
58         AND org_unit IN (SELECT aou.id FROM actor.org_unit_ancestors(select_ou) aou)
59         LOOP
60         IF completion_set.prefix IS NULL THEN
61             completion_set.prefix := '';
62         END IF;
63         IF completion_set.suffix IS NULL THEN
64             completion_set.suffix := '';
65         END IF;
66         IF completion_set.length = 0 OR completion_set.padding IS NULL OR length(completion_set.padding) = 0 THEN
67             cur_barcode = completion_set.prefix || in_barcode || completion_set.suffix;
68         ELSE
69             completion_len = completion_set.length - length(completion_set.prefix) - length(completion_set.suffix);
70             IF completion_len >= barcode_len THEN
71                 IF completion_set.padding_end THEN
72                     cur_barcode = rpad(in_barcode, completion_len, completion_set.padding);
73                 ELSE
74                     cur_barcode = lpad(in_barcode, completion_len, completion_set.padding);
75                 END IF;
76                 cur_barcode = completion_set.prefix || cur_barcode || completion_set.suffix;
77             END IF;
78         END IF;
79         IF completion_set.actor THEN
80             actor_barcodes = actor_barcodes || cur_barcode;
81         END IF;
82         IF completion_set.asset THEN
83             asset_barcodes = asset_barcodes || cur_barcode;
84         END IF;
85     END LOOP;
86
87     IF do_asset AND do_serial THEN
88         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM ONLY asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
89         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
90     ELSIF do_asset THEN
91         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
92     ELSIF do_serial THEN
93         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
94     END IF;
95     IF do_booking THEN
96         RETURN QUERY SELECT 'booking'::TEXT, id::BIGINT, barcode FROM booking.resource WHERE barcode = ANY(asset_barcodes);
97     END IF;
98     IF do_actor THEN
99         RETURN QUERY SELECT 'actor'::TEXT, c.usr::BIGINT, c.barcode FROM actor.card c JOIN actor.usr u ON c.usr = u.id WHERE c.barcode = ANY(actor_barcodes) AND c.active AND NOT u.deleted ORDER BY usr;
100     END IF;
101     RETURN;
102 END;
103 $$ LANGUAGE plpgsql;
104
105 COMMENT ON FUNCTION evergreen.get_barcodes(INT, TEXT, TEXT) IS $$
106 Given user input, find an appropriate barcode in the proper class.
107
108 Will add prefix/suffix information to do so, and return all results.
109 $$;
110
111 ALTER TABLE config.barcode_completion ADD CONSTRAINT config_barcode_completion_org_unit_fkey FOREIGN KEY (org_unit) REFERENCES actor.org_unit (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
112
113 COMMIT;