CREATE TABLE cashbag_conforms
(
    id                      integer DEFAULT NEXTVAL('cashbag_conforms_id_seq'::regclass) NOT NULL
        PRIMARY KEY,
    alive                   boolean                                                      NOT NULL,
    count_coins             numeric(12, 4)                                               NOT NULL,
    count_bills             numeric(12, 4)                                               NOT NULL,
    tokens_number           numeric(12, 4)                                               NOT NULL,
    tokens_total            numeric(12, 4)                                               NOT NULL,
    count_timestamp         timestamp with time zone,
    count_receive_timestamp timestamp with time zone,
    collect                 numeric(12, 4)                                               NOT NULL,
    collect_timestamp       timestamp with time zone,
    diff                    numeric(12, 4)                                               NOT NULL,
    author_info_id          integer                                                      NOT NULL
        UNIQUE
        CONSTRAINT cashbag_conf_author_info_id_1663387ed38e04b8_fk_author_infos_id
            REFERENCES author_infos
            DEFERRABLE INITIALLY DEFERRED,
    cashbag_id              integer
        CONSTRAINT cashbag_conforms_cashbag_id_83139826_fk_cashbags_id
            REFERENCES cashbags
            DEFERRABLE INITIALLY DEFERRED,
    cashflow_collection_id  integer
        CONSTRAINT "D4f92ee700d19ebb5990b785681a01fe"
            REFERENCES cashflow_collections
            DEFERRABLE INITIALLY DEFERRED,
    collect_user_id         integer
        CONSTRAINT ca_collect_user_id_7d633e574f6c25a9_fk_custom_users_user_ptr_id
            REFERENCES custom_users
            DEFERRABLE INITIALLY DEFERRED,
    count_user_id           integer
        CONSTRAINT cash_count_user_id_2601834f194be798_fk_custom_users_user_ptr_id
            REFERENCES custom_users
            DEFERRABLE INITIALLY DEFERRED,
    machine_id              integer
        CONSTRAINT cashbag_conforms_machine_id_246129c5a92a0a91_fk_machines_id
            REFERENCES machines
            DEFERRABLE INITIALLY DEFERRED,
    route_id                integer
        CONSTRAINT cashbag_conforms_route_id_18cfcd0d417870ef_fk_route_route_id
            REFERENCES route_route
            DEFERRABLE INITIALLY DEFERRED,
    external_route_id       bigint,
    external_route_name     varchar(255),
    no_cashbag_reason       integer,
    _ver                    bigint,
    description             text,
    status                  cashbag_conform_status_enum
);

ALTER TABLE cashbag_conforms
    OWNER TO svc_cloud;

CREATE INDEX cashbag_conforms_6788849c
    ON cashbag_conforms (cashbag_id);

CREATE INDEX cashbag_conforms_6fce81cc
    ON cashbag_conforms (cashflow_collection_id);

CREATE INDEX cashbag_conforms_7016d9d6
    ON cashbag_conforms (collect_user_id);

CREATE INDEX cashbag_conforms_b4347999
    ON cashbag_conforms (route_id);

CREATE INDEX cashbag_conforms_d8f07203
    ON cashbag_conforms (count_user_id);

CREATE INDEX cashbag_conforms_external_route_id_12045111
    ON cashbag_conforms (external_route_id);

CREATE INDEX idx_cashbag_conforms_ver
    ON cashbag_conforms (COALESCE(_ver, 0::bigint));

CREATE INDEX idx_cashbag_conforms_collect_timestamp
    ON cashbag_conforms (collect_timestamp);

CREATE INDEX idx_cashbag_conforms_count_timestamp
    ON cashbag_conforms (count_timestamp);

CREATE INDEX idx_cashbag_conforms_machine_collect_timestamp
    ON cashbag_conforms (machine_id, collect_timestamp);

CREATE TRIGGER tr_ver_i_cashbag_conforms
    BEFORE INSERT
    ON cashbag_conforms
    FOR EACH ROW
EXECUTE PROCEDURE tr_ver_cashbag_conforms();

CREATE TRIGGER tr_ver_u_cashbag_conforms
    BEFORE UPDATE
    ON cashbag_conforms
    FOR EACH ROW
    WHEN (old.* IS DISTINCT FROM new.*)
EXECUTE PROCEDURE tr_ver_cashbag_conforms();

GRANT INSERT, UPDATE ON cashbag_conforms TO cloud_write;

GRANT SELECT ON cashbag_conforms TO cloud_read;
