--
--

\restrict Kh26NNyXt6nMzxmdbja9aPNpARS1DMSXGwm0SRPpM4fW995uDB5aVMZ6At52xSr


SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: sp_act_balance(integer, jsonb, jsonb, jsonb, jsonb); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_act_balance(IN p_id integer, IN p_generales jsonb, IN p_iguales jsonb, IN p_utilidad jsonb, IN p_changelog jsonb)
    LANGUAGE plpgsql
    AS $$
BEGIN
    UPDATE "REP_BALANCE"
    SET
        "TOTALES_GENERALES" = p_generales,
        "TOTALES_IGUALES" = p_iguales,
        "TOTALES_UTILIDAD" = p_utilidad,
        "CHANGELOG" = COALESCE("CHANGELOG", '[]'::jsonb) || p_changelog
    WHERE "ID" = p_id;
END;
$$;


ALTER PROCEDURE public.sp_act_balance(IN p_id integer, IN p_generales jsonb, IN p_iguales jsonb, IN p_utilidad jsonb, IN p_changelog jsonb) OWNER TO my_postgres;

--
-- Name: sp_act_periodo_empresa(integer, integer, character varying, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_act_periodo_empresa(IN p_id integer, IN p_id_empresa integer, IN p_origen character varying, IN p_periodo integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- 🔹 INSERT
    IF p_id IS NULL THEN

        INSERT INTO "PERIODO_EMPRESA" (
            "ID_EMPRESA",
            "DB",
            "PERIODO",
            "ACTIVO",
            "FECHA_APERTURA"
        )
        VALUES (
            p_id_empresa,
            p_origen,
            p_periodo,
            'N',
            NOW()
        );

    -- 🔹 UPDATE (toggle atómico)
    ELSE

        UPDATE "PERIODO_EMPRESA"
        SET "ACTIVO" = CASE
            WHEN "ID" = p_id THEN 'S'
            ELSE 'N'
        END
        WHERE "ID_EMPRESA" = p_id_empresa
        AND "DB" = p_origen;

        IF NOT FOUND THEN
            RAISE EXCEPTION 'No existe registro con ID=%', p_id;
        END IF;

    END IF;
END;
$$;


ALTER PROCEDURE public.sp_act_periodo_empresa(IN p_id integer, IN p_id_empresa integer, IN p_origen character varying, IN p_periodo integer) OWNER TO my_postgres;

--
-- Name: sp_act_socio_cant_acciones(integer, integer, character varying, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_act_socio_cant_acciones(IN p_id_empresa integer, IN p_id_socio integer, IN p_db character varying, IN p_cant_acciones integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
    UPDATE "SOCIOS"
    SET "CANT_ACCIONES" = p_cant_acciones
    WHERE "ID_EMPRESA" = p_id_empresa
      AND "ID_SOCIO"   = p_id_socio
      AND "DB"         = p_db;
END;
$$;


ALTER PROCEDURE public.sp_act_socio_cant_acciones(IN p_id_empresa integer, IN p_id_socio integer, IN p_db character varying, IN p_cant_acciones integer) OWNER TO my_postgres;

--
-- Name: sp_act_socio(integer, integer, date, date, character varying, character varying, character varying, integer, character varying, integer, integer, character varying); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_act_socio(IN p_id_empresa integer, IN p_id_socio integer, IN p_fecha_incorporacion date, IN p_fecha_retiro date, IN p_rut character varying, IN p_nombre character varying, IN p_vigente character varying, IN p_participacion integer, IN p_email character varying, IN p_id_canalisis integer, IN p_id_cuenta integer, IN p_db character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    UPDATE "SOCIOS"
    SET
        "FECHA_INCORPORACION"  = p_fecha_incorporacion,
        "FECHA_DESVINCULACION" = p_fecha_retiro,
        "RUT"                  = p_rut,
        "NOMBRE"               = p_nombre,
        "VIGENTE"              = COALESCE(p_vigente, 'S'),
        "PARTICIPACION"        = p_participacion,
        "EMAIL"                = p_email,
        "ID_CANALISIS"         = p_id_canalisis,
        "ID_CUENTA"            = p_id_cuenta,
        "FECHA_LAST_UPD"       = now()
    WHERE "ID_EMPRESA" = p_id_empresa
      AND "ID_SOCIO"   = p_id_socio
      AND "DB"         = p_db;
END;
$$;


ALTER PROCEDURE public.sp_act_socio(IN p_id_empresa integer, IN p_id_socio integer, IN p_fecha_incorporacion date, IN p_fecha_retiro date, IN p_rut character varying, IN p_nombre character varying, IN p_vigente character varying, IN p_participacion integer, IN p_email character varying, IN p_id_canalisis integer, IN p_id_cuenta integer, IN p_db character varying) OWNER TO my_postgres;

--
-- Name: sp_bor_balance_cuentas(integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_bor_balance_cuentas(IN p_id integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
    DELETE FROM "REP_BALANCE_CUENTAS"
    WHERE "ID" = p_id;
END;
$$;


ALTER PROCEDURE public.sp_bor_balance_cuentas(IN p_id integer) OWNER TO my_postgres;

--
-- Name: sp_bor_periodo_empresa(integer, integer, character varying); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_bor_periodo_empresa(IN p_id integer, IN p_id_empresa integer, IN p_origen character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    DELETE FROM "PERIODO_EMPRESA"
    WHERE "ID" = p_id
    AND "ID_EMPRESA" = p_id_empresa
    AND "DB" = p_origen;
END;
$$;


ALTER PROCEDURE public.sp_bor_periodo_empresa(IN p_id integer, IN p_id_empresa integer, IN p_origen character varying) OWNER TO my_postgres;

--
-- Name: sp_bor_socio_movimientos(integer, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_bor_socio_movimientos(IN p_id_socio integer, IN p_id_periodo integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
    DELETE FROM "SOCIOS_MOVIMIENTOS"
    WHERE "IDFK_SOCIO" = p_id_socio
    AND "ID_PERIODO" = p_id_periodo;
END;
$$;


ALTER PROCEDURE public.sp_bor_socio_movimientos(IN p_id_socio integer, IN p_id_periodo integer) OWNER TO my_postgres;

--
-- Name: sp_new_balance(integer, character varying, integer, integer, jsonb, jsonb, jsonb, jsonb, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_new_balance(IN p_id_empresa integer, IN p_origen character varying, IN p_periodo integer, IN p_id_periodo integer, IN p_generales jsonb, IN p_iguales jsonb, IN p_utilidad jsonb, IN p_changelog jsonb, INOUT cursor_inser refcursor DEFAULT 'cursor_insert'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN

    OPEN cursor_inser FOR
    INSERT INTO "REP_BALANCE"
    (
        "ID_EMPRESA",
        "DB",
        "PERIODO",
        "ID_PERIODO",
        "TOTALES_GENERALES",
        "TOTALES_IGUALES",
        "TOTALES_UTILIDAD",
        "CHANGELOG"
    ) VALUES (
        p_id_empresa,
        p_origen,
        p_periodo,
        p_id_periodo,
        p_generales,
        p_iguales,
        p_utilidad,
        p_changelog
    )
    RETURNING "ID";
END;
$$;


ALTER PROCEDURE public.sp_new_balance(IN p_id_empresa integer, IN p_origen character varying, IN p_periodo integer, IN p_id_periodo integer, IN p_generales jsonb, IN p_iguales jsonb, IN p_utilidad jsonb, IN p_changelog jsonb, INOUT cursor_inser refcursor) OWNER TO my_postgres;

--
-- Name: sp_new_balance_cuentas(integer, integer, integer, character varying, character varying, character varying, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_new_balance_cuentas(IN p_id_balance integer, IN p_id_cuenta integer, IN p_id_tipo_cuenta integer, IN p_codigo character varying, IN p_nombre_cuenta character varying, IN p_nombre_tipo_cuenta character varying, IN p_debitos numeric, IN p_creditos numeric, IN p_deudor numeric, IN p_acreedor numeric, IN p_activo numeric, IN p_pasivo numeric, IN p_perdida numeric, IN p_ganancia numeric, IN p_id_pccuenta integer DEFAULT NULL)
    LANGUAGE plpgsql
    AS $$
BEGIN
    INSERT INTO "REP_BALANCE_CUENTAS"
    (
        "ID_BALANCE",
        "ID_CUENTA",
        "ID_TIPO_CUENTA",
        "CODIGO",
        "NOMBRE_CUENTA",
        "TIPO_CUENTA",
        "DEBITOS",
        "CREDITOS",
        "DEUDOR",
        "ACREEDOR",
        "ACTIVO",
        "PASIVO",
        "PERDIDA",
        "GANANCIA",
        "ID_PCCUENTA"
    ) VALUES (
        p_id_balance,
        p_id_cuenta,
        p_id_tipo_cuenta,
        p_codigo,
        p_nombre_cuenta,
        p_nombre_tipo_cuenta,
        p_debitos,
        p_creditos,
        p_deudor,
        p_acreedor,
        p_activo,
        p_pasivo,
        p_perdida,
        p_ganancia,
        p_id_pccuenta
    );
END;
$$;


ALTER PROCEDURE public.sp_new_balance_cuentas(IN p_id_balance integer, IN p_id_cuenta integer, IN p_id_tipo_cuenta integer, IN p_codigo character varying, IN p_nombre_cuenta character varying, IN p_nombre_tipo_cuenta character varying, IN p_debitos numeric, IN p_creditos numeric, IN p_deudor numeric, IN p_acreedor numeric, IN p_activo numeric, IN p_pasivo numeric, IN p_perdida numeric, IN p_ganancia numeric, IN p_id_pccuenta integer) OWNER TO my_postgres;

--
-- Name: sp_new_socio(integer, integer, date, date, character varying, character varying, character varying, integer, character varying, integer, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_new_socio(IN p_id_empresa integer, IN p_id_socio integer, IN p_fecha_incorporacion date, IN p_fecha_retiro date, IN p_rut character varying, IN p_nombre character varying, IN p_vigente character varying, IN p_participacion integer, IN p_email character varying, IN p_id_canalisis integer, IN p_id_cuenta integer, IN p_db character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    INSERT INTO "SOCIOS" (
        "ID_EMPRESA", "DB", "ID_SOCIO",
        "FECHA_INCORPORACION", "FECHA_DESVINCULACION",
        "RUT", "NOMBRE", "VIGENTE", "PARTICIPACION",
        "EMAIL", "ID_CANALISIS", "ID_CUENTA", "FECHA_LAST_UPD"
    ) VALUES (
        p_id_empresa, p_db, p_id_socio,
        p_fecha_incorporacion, p_fecha_retiro,
        p_rut, p_nombre, p_vigente, p_participacion,
        p_email, p_id_canalisis, p_id_cuenta, now()
    );
END;
$$;


ALTER PROCEDURE public.sp_new_socio(IN p_id_empresa integer, IN p_id_socio integer, IN p_fecha_incorporacion date, IN p_fecha_retiro date, IN p_rut character varying, IN p_nombre character varying, IN p_vigente character varying, IN p_participacion integer, IN p_email character varying, IN p_id_canalisis integer, IN p_id_cuenta integer, IN p_db character varying) OWNER TO my_postgres;

--
-- Name: sp_new_socio_movimientos(integer, integer, integer, integer, integer, integer, integer, character varying, numeric, character varying, integer, character varying, date, timestamp without time zone, character varying, integer, character varying, character varying, integer, integer, integer); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_new_socio_movimientos(IN p_id_socio integer, IN p_id_periodo integer, IN p_id_voucher integer, IN p_id_vpartidas integer, IN p_id_pccuenta integer, IN p_id_ccosto integer, IN p_id_canalisis integer, IN p_debe_haber character varying, IN p_monto numeric, IN p_descripcion character varying, IN p_comprobante integer, IN p_tv_nombre character varying, IN p_fecha_voucher date, IN p_fecha timestamp without time zone, IN p_n_documento character varying, IN p_id_tipodm integer, IN p_cc_nombre character varying, IN p_descripcion_analisis character varying, IN p_tvoucher_ifrstrib integer, IN p_tvoucher integer, IN p_trvoucher integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
    INSERT INTO "SOCIOS_MOVIMIENTOS" (
        "IDFK_SOCIO",
        "ID_PERIODO",
        "ID_VOUCHER",
        "ID_VPARTIDAS",
        "ID_PCCUENTA",
        "ID_CCOSTO",
        "ID_CANALISIS",
        "DEBE_HABER",
        "MONTO",
        "DESCRIPCION",
        "COMPROBANTE",
        "TV_NOMBRE",
        "FECHA_VOUCHER",
        "M_FECHA",
        "N_DOCTO",
        "ID_TIPODM",
        "CC_NOMBRE",
        "DESCRIPCION_ANALISIS",
        "ID_TVOUCHER_IFRSTRIB",
        "ID_TVOUCHER",
        "ID_TRVOUCHER"
    ) VALUES (
        p_id_socio,
        p_id_periodo,
        p_id_voucher,
        p_id_vpartidas,
        p_id_pccuenta,
        p_id_ccosto,
        p_id_canalisis,
        p_debe_haber,
        p_monto,
        p_descripcion,
        p_comprobante,
        p_tv_nombre,
        p_fecha_voucher,
        p_fecha,
        p_n_documento,
        p_id_tipodm,
        p_cc_nombre,
        p_descripcion_analisis,
        p_tvoucher_ifrstrib,
        p_tvoucher,
        p_trvoucher
    );
END;
$$;


ALTER PROCEDURE public.sp_new_socio_movimientos(IN p_id_socio integer, IN p_id_periodo integer, IN p_id_voucher integer, IN p_id_vpartidas integer, IN p_id_pccuenta integer, IN p_id_ccosto integer, IN p_id_canalisis integer, IN p_debe_haber character varying, IN p_monto numeric, IN p_descripcion character varying, IN p_comprobante integer, IN p_tv_nombre character varying, IN p_fecha_voucher date, IN p_fecha timestamp without time zone, IN p_n_documento character varying, IN p_id_tipodm integer, IN p_cc_nombre character varying, IN p_descripcion_analisis character varying, IN p_tvoucher_ifrstrib integer, IN p_tvoucher integer, IN p_trvoucher integer) OWNER TO my_postgres;

--
-- Name: sp_obt_balance(integer, integer, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_balance(IN p_id_empresa integer, IN p_periodo integer, INOUT cursor_balance refcursor DEFAULT 'cursor_balance'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cursor_balance FOR
    SELECT
        "ID",
        "ID_EMPRESA",
        "DB",
        "PERIODO",
        "ID_PERIODO",
        "TOTALES_GENERALES",
        "TOTALES_IGUALES",
        "TOTALES_UTILIDAD",
        "CHANGELOG"
    FROM "REP_BALANCE"
    WHERE "ID_EMPRESA" = p_id_empresa
    AND "PERIODO" = p_periodo;
END;
$$;


ALTER PROCEDURE public.sp_obt_balance(IN p_id_empresa integer, IN p_periodo integer, INOUT cursor_balance refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_balance(integer, integer, character varying, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_balance(IN p_id_empresa integer, IN p_periodo integer, IN p_db character varying, INOUT cursor_balance refcursor DEFAULT 'cursor_balance'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cursor_balance FOR
    SELECT
        "ID",
        "ID_EMPRESA",
        "DB",
        "PERIODO",
        "ID_PERIODO",
        "TOTALES_GENERALES",
        "TOTALES_IGUALES",
        "TOTALES_UTILIDAD",
        "CHANGELOG"
    FROM "REP_BALANCE"
    WHERE "ID_EMPRESA" = p_id_empresa
    AND "PERIODO" = p_periodo
    AND "DB" = p_db;
END;
$$;


ALTER PROCEDURE public.sp_obt_balance(IN p_id_empresa integer, IN p_periodo integer, IN p_db character varying, INOUT cursor_balance refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_balance_cuentas(integer, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_balance_cuentas(IN p_id_balance integer, INOUT cur refcursor DEFAULT 'cursor_balance_cuentas'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cur FOR
    SELECT *
    FROM "REP_BALANCE_CUENTAS"
    WHERE "ID_BALANCE" = p_id_balance;
END;
$$;


ALTER PROCEDURE public.sp_obt_balance_cuentas(IN p_id_balance integer, INOUT cur refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_movimientos_socio(integer, integer, integer, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_movimientos_socio(IN p_id_socio integer, IN p_periodo integer, IN p_id_empresa integer, INOUT cursor_socios_movimientos refcursor DEFAULT 'cursor_socios_movimientos'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cursor_socios_movimientos FOR
    SELECT *
    FROM "SOCIOS_MOVIMIENTOS" SM
    INNER JOIN "SOCIOS" S
        ON S."ID" = SM."IDFK_SOCIO"
        AND S."ID_SOCIO" = p_id_socio
    INNER JOIN "PERIODO_EMPRESA" P
        ON P."ID" = SM."ID_PERIODO"
        AND P."PERIODO" = p_periodo
        AND P."ID_EMPRESA" = p_id_empresa
    -- Escenario 1: sin código de análisis → sin filtro adicional.
    -- Escenario 2: cuenta compartida con código de análisis → filtra solo las partidas del socio.
    WHERE (S."ID_CANALISIS" IS NULL OR SM."ID_CANALISIS" = S."ID_CANALISIS");
END;
$$;


ALTER PROCEDURE public.sp_obt_movimientos_socio(IN p_id_socio integer, IN p_periodo integer, IN p_id_empresa integer, INOUT cursor_socios_movimientos refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_periodo_empresa(integer, integer, character varying, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_periodo_empresa(IN p_id_empresa integer, IN p_periodo integer, IN p_origen character varying, INOUT cursor_periodo refcursor DEFAULT 'cursor_periodo'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cursor_periodo FOR 
    SELECT * FROM "PERIODO_EMPRESA" 
    WHERE "ID_EMPRESA" = p_id_empresa 
    AND "PERIODO" = p_periodo 
    AND "DB" = p_origen 
    ORDER BY "PERIODO" ASC;
END;
$$;


ALTER PROCEDURE public.sp_obt_periodo_empresa(IN p_id_empresa integer, IN p_periodo integer, IN p_origen character varying, INOUT cursor_periodo refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_periodos_empresa(integer, character varying, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_periodos_empresa(IN p_id_empresa integer, IN p_origen character varying, INOUT cur refcursor DEFAULT 'cursor_periodo'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cur FOR 
    SELECT * 
    FROM "PERIODO_EMPRESA" 
    WHERE "ID_EMPRESA" = p_id_empresa
    AND "DB" = p_origen
    ORDER BY "PERIODO" ASC;
END;
$$;


ALTER PROCEDURE public.sp_obt_periodos_empresa(IN p_id_empresa integer, IN p_origen character varying, INOUT cur refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_socio(integer, integer, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_socio(IN p_id_socio integer, IN p_id_empresa integer, INOUT cursor_socio refcursor DEFAULT 'cursor_socio'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cursor_socio FOR SELECT * FROM "SOCIOS" WHERE "ID_SOCIO" = p_id_socio AND "ID_EMPRESA" = p_id_empresa;
END;
$$;


ALTER PROCEDURE public.sp_obt_socio(IN p_id_socio integer, IN p_id_empresa integer, INOUT cursor_socio refcursor) OWNER TO my_postgres;

--
-- Name: sp_obt_socios(integer, refcursor); Type: PROCEDURE; Schema: public; Owner: my_postgres
--

CREATE PROCEDURE public.sp_obt_socios(IN p_id_empresa integer, IN p_db character varying, INOUT cur refcursor DEFAULT 'cursor_socios'::refcursor)
    LANGUAGE plpgsql
    AS $$
BEGIN
    OPEN cur FOR
        SELECT * FROM "SOCIOS"
        WHERE "ID_EMPRESA" = p_id_empresa
          AND "DB"         = p_db;
END;
$$;


ALTER PROCEDURE public.sp_obt_socios(IN p_id_empresa integer, IN p_db character varying, INOUT cur refcursor) OWNER TO my_postgres;



--
-- Name: PERIODO_EMPRESA; Type: TABLE; Schema: public; Owner: my_postgres
--

CREATE TABLE public."PERIODO_EMPRESA" (
    "ID" integer NOT NULL,
    "ID_EMPRESA" integer,
    "DB" character varying(7) NOT NULL,
    "PERIODO" integer,
    "ACTIVO" character varying(1) DEFAULT 'S'::character varying,
    "FECHA_APERTURA" timestamp without time zone
);


ALTER TABLE public."PERIODO_EMPRESA" OWNER TO my_postgres;

--
-- Name: PERIODO_EMPRESA_ID_seq; Type: SEQUENCE; Schema: public; Owner: my_postgres
--

CREATE SEQUENCE public."PERIODO_EMPRESA_ID_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public."PERIODO_EMPRESA_ID_seq" OWNER TO my_postgres;

--
-- Name: PERIODO_EMPRESA_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: my_postgres
--

ALTER SEQUENCE public."PERIODO_EMPRESA_ID_seq" OWNED BY public."PERIODO_EMPRESA"."ID";


--
-- Name: REP_BALANCE; Type: TABLE; Schema: public; Owner: my_postgres
--

CREATE TABLE public."REP_BALANCE" (
    "ID" integer NOT NULL,
    "ID_EMPRESA" integer NOT NULL,
    "DB" character varying(7) NOT NULL,
    "PERIODO" integer NOT NULL,
    "ID_PERIODO" integer NOT NULL,
    "TOTALES_GENERALES" jsonb,
    "TOTALES_IGUALES" jsonb,
    "TOTALES_UTILIDAD" jsonb,
    "CHANGELOG" jsonb
);


ALTER TABLE public."REP_BALANCE" OWNER TO my_postgres;

--
-- Name: REP_BALANCE_CUENTAS; Type: TABLE; Schema: public; Owner: my_postgres
--

CREATE TABLE public."REP_BALANCE_CUENTAS" (
    "ID" integer NOT NULL,
    "ID_BALANCE" integer NOT NULL,
    "ID_CUENTA" integer NOT NULL,
    "ID_TIPO_CUENTA" integer NOT NULL,
    "CODIGO" character varying(20) NOT NULL,
    "NOMBRE_CUENTA" character varying(100) NOT NULL,
    "TIPO_CUENTA" character varying(25) NOT NULL,
    "DEBITOS" numeric(15,2) NOT NULL,
    "CREDITOS" numeric(15,2) NOT NULL,
    "DEUDOR" numeric(15,2) NOT NULL,
    "ACREEDOR" numeric(15,2) NOT NULL,
    "ACTIVO" numeric(15,2) NOT NULL,
    "PASIVO" numeric(15,2) NOT NULL,
    "PERDIDA" numeric(15,2) NOT NULL,
    "GANANCIA" numeric(15,2) NOT NULL,
    "ID_PCCUENTA" integer
);


ALTER TABLE public."REP_BALANCE_CUENTAS" OWNER TO my_postgres;

--
-- Name: REP_BALANCE_CUENTAS_ID_seq; Type: SEQUENCE; Schema: public; Owner: my_postgres
--

CREATE SEQUENCE public."REP_BALANCE_CUENTAS_ID_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public."REP_BALANCE_CUENTAS_ID_seq" OWNER TO my_postgres;

--
-- Name: REP_BALANCE_CUENTAS_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: my_postgres
--

ALTER SEQUENCE public."REP_BALANCE_CUENTAS_ID_seq" OWNED BY public."REP_BALANCE_CUENTAS"."ID";


--
-- Name: REP_BALANCE_ID_seq; Type: SEQUENCE; Schema: public; Owner: my_postgres
--

CREATE SEQUENCE public."REP_BALANCE_ID_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public."REP_BALANCE_ID_seq" OWNER TO my_postgres;

--
-- Name: REP_BALANCE_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: my_postgres
--

ALTER SEQUENCE public."REP_BALANCE_ID_seq" OWNED BY public."REP_BALANCE"."ID";


--
-- Name: SOCIOS; Type: TABLE; Schema: public; Owner: my_postgres
--

CREATE TABLE public."SOCIOS" (
    "ID" integer NOT NULL,
    "ID_EMPRESA" integer NOT NULL,
    "DB" character varying(7) NOT NULL,
    "ID_SOCIO" integer NOT NULL,
    "FECHA_INCORPORACION" date NOT NULL,
    "FECHA_DESVINCULACION" date,
    "RUT" character varying(13) NOT NULL,
    "NOMBRE" character varying(100) NOT NULL,
    "VIGENTE" character varying(1) DEFAULT 'S'::character varying NOT NULL,
    "PARTICIPACION" integer DEFAULT 100 NOT NULL,
    "EMAIL" character varying(45) NOT NULL,
    "ID_CANALISIS" integer,
    "ID_CUENTA" integer,
    "CANT_ACCIONES" integer,
    "FECHA_LAST_UPD" timestamp without time zone
);


ALTER TABLE public."SOCIOS" OWNER TO my_postgres;

--
-- Name: SOCIOS_ID_seq; Type: SEQUENCE; Schema: public; Owner: my_postgres
--

CREATE SEQUENCE public."SOCIOS_ID_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public."SOCIOS_ID_seq" OWNER TO my_postgres;

--
-- Name: SOCIOS_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: my_postgres
--

ALTER SEQUENCE public."SOCIOS_ID_seq" OWNED BY public."SOCIOS"."ID";


--
-- Name: SOCIOS_MOVIMIENTOS; Type: TABLE; Schema: public; Owner: my_postgres
--

CREATE TABLE public."SOCIOS_MOVIMIENTOS" (
    "ID" integer NOT NULL,
    "IDFK_SOCIO" integer NOT NULL,
    "ID_PERIODO" integer NOT NULL,
    "ID_VOUCHER" integer NOT NULL,
    "ID_VPARTIDAS" integer NOT NULL,
    "ID_PCCUENTA" integer NOT NULL,
    "ID_CCOSTO" integer,
    "ID_CANALISIS" integer,
    "DEBE_HABER" character varying(1),
    "MONTO" numeric(15,2),
    "DESCRIPCION" character varying(100),
    "COMPROBANTE" character varying(10),
    "TV_NOMBRE" character varying(20),
    "FECHA_VOUCHER" date,
    "M_FECHA" timestamp without time zone,
    "N_DOCTO" character varying(20),
    "ID_TIPODM" integer,
    "CC_NOMBRE" character varying(100),
    "DESCRIPCION_ANALISIS" character varying(100),
    "ID_TVOUCHER_IFRSTRIB" integer,
    "ID_TVOUCHER" integer,
    "ID_TRVOUCHER" integer
);


ALTER TABLE public."SOCIOS_MOVIMIENTOS" OWNER TO my_postgres;

--
-- Name: SOCIOS_MOVIMIENTOS_ID_seq; Type: SEQUENCE; Schema: public; Owner: my_postgres
--

CREATE SEQUENCE public."SOCIOS_MOVIMIENTOS_ID_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public."SOCIOS_MOVIMIENTOS_ID_seq" OWNER TO my_postgres;

--
-- Name: SOCIOS_MOVIMIENTOS_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: my_postgres
--

ALTER SEQUENCE public."SOCIOS_MOVIMIENTOS_ID_seq" OWNED BY public."SOCIOS_MOVIMIENTOS"."ID";


--
-- Name: PERIODO_EMPRESA ID; Type: DEFAULT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."PERIODO_EMPRESA" ALTER COLUMN "ID" SET DEFAULT nextval('public."PERIODO_EMPRESA_ID_seq"'::regclass);


--
-- Name: REP_BALANCE ID; Type: DEFAULT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE" ALTER COLUMN "ID" SET DEFAULT nextval('public."REP_BALANCE_ID_seq"'::regclass);


--
-- Name: REP_BALANCE_CUENTAS ID; Type: DEFAULT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE_CUENTAS" ALTER COLUMN "ID" SET DEFAULT nextval('public."REP_BALANCE_CUENTAS_ID_seq"'::regclass);


--
-- Name: SOCIOS ID; Type: DEFAULT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS" ALTER COLUMN "ID" SET DEFAULT nextval('public."SOCIOS_ID_seq"'::regclass);


--
-- Name: SOCIOS_MOVIMIENTOS ID; Type: DEFAULT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS_MOVIMIENTOS" ALTER COLUMN "ID" SET DEFAULT nextval('public."SOCIOS_MOVIMIENTOS_ID_seq"'::regclass);


--
-- Name: PERIODO_EMPRESA PERIODO_EMPRESA_pkey; Type: CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."PERIODO_EMPRESA"
    ADD CONSTRAINT "PERIODO_EMPRESA_pkey" PRIMARY KEY ("ID");


--
-- Name: REP_BALANCE_CUENTAS REP_BALANCE_CUENTAS_pkey; Type: CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE_CUENTAS"
    ADD CONSTRAINT "REP_BALANCE_CUENTAS_pkey" PRIMARY KEY ("ID");


--
-- Name: REP_BALANCE REP_BALANCE_pkey; Type: CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE"
    ADD CONSTRAINT "REP_BALANCE_pkey" PRIMARY KEY ("ID");


--
-- Name: SOCIOS_MOVIMIENTOS SOCIOS_MOVIMIENTOS_pkey; Type: CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS_MOVIMIENTOS"
    ADD CONSTRAINT "SOCIOS_MOVIMIENTOS_pkey" PRIMARY KEY ("ID");


--
-- Name: SOCIOS SOCIOS_pkey; Type: CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS"
    ADD CONSTRAINT "SOCIOS_pkey" PRIMARY KEY ("ID");


--
-- Name: socios_id_empresa; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_id_empresa ON public."SOCIOS" USING btree ("ID_EMPRESA");


--
-- Name: socios_movimientos_id_canalisis; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_movimientos_id_canalisis ON public."SOCIOS_MOVIMIENTOS" USING btree ("ID_CANALISIS");


--
-- Name: socios_movimientos_id_ccosto; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_movimientos_id_ccosto ON public."SOCIOS_MOVIMIENTOS" USING btree ("ID_CCOSTO");


--
-- Name: socios_movimientos_id_pccuenta; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_movimientos_id_pccuenta ON public."SOCIOS_MOVIMIENTOS" USING btree ("ID_PCCUENTA");


--
-- Name: socios_movimientos_id_voucher; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_movimientos_id_voucher ON public."SOCIOS_MOVIMIENTOS" USING btree ("ID_VOUCHER");


--
-- Name: socios_movimientos_id_vpartidas; Type: INDEX; Schema: public; Owner: my_postgres
--

CREATE INDEX socios_movimientos_id_vpartidas ON public."SOCIOS_MOVIMIENTOS" USING btree ("ID_VPARTIDAS");


--
-- Name: REP_BALANCE_CUENTAS fk_balance_cuentas_id_balance; Type: FK CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE_CUENTAS"
    ADD CONSTRAINT fk_balance_cuentas_id_balance FOREIGN KEY ("ID_BALANCE") REFERENCES public."REP_BALANCE"("ID");


--
-- Name: REP_BALANCE_CUENTAS fk_rbc_plan_cuenta; Type: FK CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE_CUENTAS"
    ADD CONSTRAINT fk_rbc_plan_cuenta FOREIGN KEY ("ID_PCCUENTA") REFERENCES public."PLAN_CUENTA_TRIBUTARIO"("ID") ON DELETE SET NULL;


--
-- Name: REP_BALANCE fk_balance_id_periodo; Type: FK CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."REP_BALANCE"
    ADD CONSTRAINT fk_balance_id_periodo FOREIGN KEY ("ID_PERIODO") REFERENCES public."PERIODO_EMPRESA"("ID");


--
-- Name: SOCIOS_MOVIMIENTOS fk_socios_movimientos_id_periodo; Type: FK CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS_MOVIMIENTOS"
    ADD CONSTRAINT fk_socios_movimientos_id_periodo FOREIGN KEY ("ID_PERIODO") REFERENCES public."PERIODO_EMPRESA"("ID");


--
-- Name: SOCIOS_MOVIMIENTOS fk_socios_movimientos_idfk_socio; Type: FK CONSTRAINT; Schema: public; Owner: my_postgres
--

ALTER TABLE ONLY public."SOCIOS_MOVIMIENTOS"
    ADD CONSTRAINT fk_socios_movimientos_idfk_socio FOREIGN KEY ("IDFK_SOCIO") REFERENCES public."SOCIOS"("ID");


--
--

\unrestrict Kh26NNyXt6nMzxmdbja9aPNpARS1DMSXGwm0SRPpM4fW995uDB5aVMZ6At52xSr

