SET TERM ^;


CREATE OR ALTER TRIGGER INSDOK FOR "ANTRO" ACTIVE AFTER INSERT POSITION 0
AS
DECLARE VARIABLE EXP_IMP SMALLINT;
DECLARE VARIABLE VRSTA SMALLINT;
DECLARE VARIABLE MAGACINID SMALLINT;
DECLARE VARIABLE MAGID SMALLINT;
BEGIN
  /* Import podataka radi korisnik NO_TRIGG -> trigeri ne smeju da funkcionisu */
  IF (USER = 'NO_TRIGG') THEN
    EXIT;

  SELECT VRSTA FROM ROBA
  WHERE ROBAID = NEW.ROBAID
  INTO :VRSTA;  

  IF ((NEW.IZNOS <> 0) AND (:VRSTA <> 5)) THEN
    /* PDV placen kod uvoza ne ulazi u troskove */
    UPDATE DOKUMENT SET
    TROSKOVI = TROSKOVI + NEW.IZNOS
    WHERE VRDOK = NEW.VRDOK AND BRDOK = NEW.BRDOK;

  IF (USER <> 'REPL') THEN
    UPDATE POSLEDNJI SET ID = NEW.ANTROID
    WHERE NAZIV = 'antroid';

  /* EI_STATUS */
  SELECT EXP_IMP FROM VRSTADOK WHERE VRDOK = NEW.VRDOK
  INTO :EXP_IMP;

  IF (EXP_IMP = 1) THEN BEGIN
    EXECUTE PROCEDURE UPISI_EI_STATUS(NEW.VRDOK, NEW.BRDOK, 1, 0);
  END

  /* SINHRO    */
  SELECT MAGACINID, MAGID
  FROM DOKUMENT WHERE VRDOK = NEW.VRDOK AND BRDOK = NEW.BRDOK
  INTO :MAGACINID, :MAGID;
  EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', '', NEW.ANTROID, NEW.VRDOK, :MAGACINID);
  IF (NEW.VRDOK IN (18, 26)) THEN
    EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', '', NEW.ANTROID, NEW.VRDOK, :MAGID);

END
 ^

CREATE OR ALTER TRIGGER UPDDOK FOR "ANTRO" ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE EXP_IMP SMALLINT;
DECLARE VARIABLE VRSTA SMALLINT;
DECLARE VARIABLE MAGACINID SMALLINT;
DECLARE VARIABLE MAGID SMALLINT;
BEGIN
  /* Import podataka radi korisnik NO_TRIGG -> trigeri ne smeju da funkcionisu */
  IF (USER = 'NO_TRIGG') THEN
    EXIT;

  SELECT VRSTA FROM ROBA
  WHERE ROBAID = NEW.ROBAID
  INTO :VRSTA;  

  IF ((NEW.IZNOS <> OLD.IZNOS) AND (:VRSTA <> 5)) THEN
    /* PDV placen kod uvoza ne ulazi u troskove */
    UPDATE DOKUMENT SET
    TROSKOVI = TROSKOVI - OLD.IZNOS + NEW.IZNOS
    WHERE VRDOK = NEW.VRDOK AND BRDOK = NEW.BRDOK;

  /* EI_STATUS */
  SELECT EXP_IMP FROM VRSTADOK WHERE VRDOK = NEW.VRDOK
  INTO :EXP_IMP;

  IF (EXP_IMP = 1) THEN BEGIN
    EXECUTE PROCEDURE UPISI_EI_STATUS(NEW.VRDOK, NEW.BRDOK, 1, 1);
  END

  /* SINHRO    */
  SELECT MAGACINID, MAGID
  FROM DOKUMENT WHERE VRDOK = NEW.VRDOK AND BRDOK = NEW.BRDOK
  INTO :MAGACINID, :MAGID;
  EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', OLD.ANTROID, NEW.ANTROID, NEW.VRDOK, :MAGACINID);
  IF (NEW.VRDOK IN (18, 26)) THEN
    EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', OLD.ANTROID, NEW.ANTROID, NEW.VRDOK, :MAGID);

END
 ^

CREATE OR ALTER TRIGGER DELDOK FOR "ANTRO" ACTIVE AFTER DELETE POSITION 0
AS
DECLARE VARIABLE EXP_IMP SMALLINT;
DECLARE VARIABLE ID VARCHAR(30) CHARACTER SET NONE;
DECLARE VARIABLE VRSTA SMALLINT;
DECLARE VARIABLE MAGACINID SMALLINT;
DECLARE VARIABLE MAGID SMALLINT;
BEGIN
  /* Import podataka radi korisnik NO_TRIGG -> trigeri ne smeju da funkcionisu */
  IF (USER = 'NO_TRIGG') THEN
    EXIT;

  SELECT VRSTA FROM ROBA
  WHERE ROBAID = OLD.ROBAID
  INTO :VRSTA;  

  IF ((OLD.IZNOS <> 0) AND (:VRSTA <> 5)) THEN
    /* PDV placen kod uvoza ne ulazi u troskove */
    UPDATE DOKUMENT SET
    TROSKOVI = TROSKOVI - OLD.IZNOS
    WHERE VRDOK = OLD.VRDOK AND BRDOK = OLD.BRDOK;

  /* EI_STATUS */
  SELECT EXP_IMP FROM VRSTADOK WHERE VRDOK = OLD.VRDOK
  INTO :EXP_IMP;

  IF (EXP_IMP = 1) THEN BEGIN
    ID = RTRIM(CAST(OLD.VRDOK AS VARCHAR(3))) || ';' ||
         RTRIM(CAST(OLD.BRDOK AS VARCHAR(10))) || ';' ||
         RTRIM(CAST(OLD.ANTROID AS VARCHAR(10)));
    EXECUTE PROCEDURE UPISI_EI_STATUS(-110, :ID, 2, 0);
  END

  /* SINHRO    */
  SELECT MAGACINID, MAGID
  FROM DOKUMENT WHERE VRDOK = OLD.VRDOK AND BRDOK = OLD.BRDOK
  INTO :MAGACINID, :MAGID;
  EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', OLD.ANTROID, '', OLD.VRDOK, :MAGACINID);
  IF (OLD.VRDOK IN (18, 26)) THEN
    EXECUTE PROCEDURE UPISI_SINHRO('ANTRO', 'ANTROID', OLD.ANTROID, '', OLD.VRDOK, :MAGID);

END
^
