/*
 * Copyright (C) 2008  Equinox Software, Inc.
 * Mike Rylander <miker@esilibrary.com> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

DROP SCHEMA extend_reporter CASCADE;

BEGIN;

CREATE SCHEMA extend_reporter;

CREATE TABLE extend_reporter.legcay_circ_count (
    id          BIGSERIAL   PRIMARY KEY REFERENCES asset.copy (id),
    circ_count  INT         NOT NULL DEFAULT 0
);

CREATE VIEW extend_reporter.full_circ_count AS
 SELECT cp.id, COALESCE(sum(c.circ_count), 0::bigint) + COALESCE(count(circ.id), 0::bigint) AS circ_count
   FROM asset."copy" cp
   LEFT JOIN extend_reporter.legcay_circ_count c USING (id)
   LEFT JOIN "action".all_circulation circ ON circ.target_copy = c.id
  GROUP BY cp.id;

COMMIT;

