CREATE TABLE COBROS (
    NRO_COBRO      INTEGER NOT NULL,
    FECHA         DATE NOT NULL,
    FECHA_INGRESO TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    IMPORTE       NUMERIC(12,2) NOT NULL,
    DESCUENTO       NUMERIC(12,2) NOT NULL,
    TOTAL       NUMERIC(12,2) NOT NULL,
    NRO_PUNTO_COBRO INTEGER NOT NULL,
    NRO_RECIBO INTEGER NOT NULL,
    NUMERO        VARCHAR(16) CHARACTER SET ISO8859_1 NOT NULL COLLATE ES_ES,
    NRO_ALUMNO   INTEGER NOT NULL,
    NRO_USUARIO   INTEGER NOT NULL,
    OBSERVACION   VARCHAR(500) CHARACTER SET ISO8859_1 COLLATE ES_ES,
    INFO_COBRO         VARCHAR(5000) CHARACTER SET ISO8859_1 COLLATE ES_ES
);

-----
ALTER TABLE COBROS ADD CONSTRAINT PK_COBROS PRIMARY KEY (NRO_COBRO);
-----
ALTER TABLE COBROS ADD CONSTRAINT FK_ALUMNOS_COBROS FOREIGN KEY (NRO_ALUMNO) REFERENCES ALUMNOS (NRO_ALUMNO);
-----
CREATE INDEX IDX_COBROS ON COBROS (FECHA);
-----
CREATE INDEX IDX_COBROS1 ON COBROS (NUMERO);
-----
CREATE TRIGGER COBROS_BI FOR COBROS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.NRO_COBRO IS NULL) THEN BEGIN
    NEW.NRO_COBRO = GEN_ID(ORDEN_GEN, 1);
  END
  NEW.NUMERO = 'REC-' || LPAD(NEW.NRO_PUNTO_COBRO, 3, '0') || '-' || LPAD(NEW.NRO_RECIBO, 8, '0');
END;
-----
CREATE OR ALTER TRIGGER COBROS_BU FOR COBROS
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  NEW.NUMERO = 'REC-' || LPAD(NEW.NRO_PUNTO_COBRO, 3, '0') || '-' || LPAD(NEW.NRO_RECIBO, 8, '0');
END;

