--- /dev/null
+BEGIN;
+
+SELECT plan(2);
+
+INSERT INTO asset.copy (id,circ_lib,creator,loan_duration,fine_level,call_number,editor,barcode,dummy_title,dummy_author)
+ VALUES (8765309,1,1,2,2,-1,1,'8765309','Spooky Dance','Steve Bachman');
+
+INSERT INTO action.circulation (id,usr,target_copy,circ_lib,circ_staff,renewal_remaining,
+ duration_rule,recurring_fine_rule,max_fine_rule,stop_fines,grace_period)
+ VALUES (8765309,1,8765309,1,1,0,
+ 1,1,1,'LONGOVERDUE','0 hours');
+
+SELECT is(asset.copy_state(8765309),'LONGOVERDUE', 'Copy state is LONGOVERDUE when stop_fines='LONGOVERDUE' and checkin_time is null');
+
+-- Our long overdue item has returned!
+UPDATE action.circulation SET checkin_time=now() WHERE id=8765309;
+
+SELECT is(asset.copy_state(8765309),'NORMAL', 'Copy state is NORMAL when stop_fines='LONGOVERDUE' and checkin_time is not null');
+
+ROLLBACK;
--- /dev/null
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+CREATE OR REPLACE FUNCTION asset.copy_state (cid BIGINT) RETURNS TEXT AS $$
+DECLARE
+ last_circ_stop TEXT;
+ the_copy asset.copy%ROWTYPE;
+BEGIN
+
+ SELECT * INTO the_copy FROM asset.copy WHERE id = cid;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+
+ IF the_copy.status = 3 THEN -- Lost
+ RETURN 'LOST';
+ ELSIF the_copy.status = 4 THEN -- Missing
+ RETURN 'MISSING';
+ ELSIF the_copy.status = 14 THEN -- Damaged
+ RETURN 'DAMAGED';
+ ELSIF the_copy.status = 17 THEN -- Lost and paid
+ RETURN 'LOST_AND_PAID';
+ END IF;
+
+ SELECT stop_fines INTO last_circ_stop
+ FROM action.circulation
+ WHERE target_copy = cid AND checkin_time IS NULL
+ ORDER BY xact_start DESC LIMIT 1;
+
+ IF FOUND THEN
+ IF last_circ_stop IN (
+ 'CLAIMSNEVERCHECKEDOUT',
+ 'CLAIMSRETURNED',
+ 'LONGOVERDUE'
+ ) THEN
+ RETURN last_circ_stop;
+ END IF;
+ END IF;
+
+ RETURN 'NORMAL';
+END;
+$$ LANGUAGE PLPGSQL;
+
+COMMIT;
+