/*

	REQUERIDO: FTR 4.4.0.32
	FORÇA DE VENDA: 6.0.22

*/


SET TERM ^ ;

CREATE OR ALTER PROCEDURE SP_INS_PEDIDO (
    ID_PED INTEGER,
    ID_PES INTEGER,
    ID_REP INTEGER,
    ID_COP SMALLINT,
    EMISSAO DATE,
    FATURA DATE,
    STATUS SMALLINT,
    TOTAL_NEG NUMERIC(10,2),
    TOTAL_SUG NUMERIC(10,2),
    TOTAL_MKT NUMERIC(10,2),
    OBSERV_1 VARCHAR(80),
    OBSERV_2 VARCHAR(80),
    OBSERV_3 VARCHAR(80),
    NUM_PED VARCHAR(20),
    HASHING VARCHAR(500),
    IN_STORE SMALLINT)
AS
DECLARE ID_EMP DOM_ID_EMPRESA;
DECLARE EMISSOR INTEGER = 2;
BEGIN

  STATUS = 99;
  SELECT FIRST 1 EMPRESA.ID_EMP FROM EMPRESA ORDER BY EMPRESA.EMP_DEF DESC INTO :ID_EMP;

  INSERT INTO PEDIDO (
  ID_PED,
  ID_PES,
  ID_REP,
  ID_EMP,
  ID_COP,
  EMISSAO,
  FATURA,
  STATUS,
  TOTAL_NEG,
  TOTAL_SUG,
  TOTAL_MKT,
  TOTAL_FID,
  TOTAL_MIN,
  PESO_LIQ,
  PESO_BRT,
  EMISSOR,
  OBSERV_1,
  OBSERV_2,
  OBSERV_3,
  TRANS_EMISSOR,
  PICKING_STATUS,
  COD_COMPRA,
  HASHING,
  IN_STORE)
  VALUES (:ID_PED,
          :ID_PES,
          :ID_REP,
          :ID_EMP,
          :ID_COP,
          :EMISSAO,
          :FATURA,
          :STATUS,
          :TOTAL_NEG,
          :TOTAL_SUG,
          :TOTAL_MKT,
          0,
          0,
          0,
          0,
          :EMISSOR,
          :OBSERV_1,
          :OBSERV_2,
          :OBSERV_3,
          CURRENT_TIMESTAMP,
          0,
          :NUM_PED,
          :HASHING,
          :IN_STORE);

END^

CREATE OR ALTER TRIGGER AIUD_PEDIDO_MKT FOR PEDIDO
ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 99
AS
DECLARE ID_PED       INTEGER;
DECLARE ID_REP       INTEGER;
DECLARE ID_PES       INTEGER;
DECLARE EMISSAO      DATE;
DECLARE TOTAL_MKT    NUMERIC(10,2);
DECLARE TOTAL_NEG    NUMERIC(10,2);
DECLARE VALOR_MKT    NUMERIC(10,2);
DECLARE RESERVA      NUMERIC(10,2);
DECLARE RESER_MKT    NUMERIC(10,2);
DECLARE PERC_AJU_MKT NUMERIC(10,2);
DECLARE VLR_AJU_MKT  NUMERIC(10,2);
DECLARE OBSERVACAO   VARCHAR(80);
DECLARE MGR_MKT      SMALLINT;
DECLARE FALSE        SMALLINT = 0;
DECLARE OP_VENDA     SMALLINT = 1;
BEGIN

  SELECT AJUSTE_MKT, RESER_MKT, MGR_PRC_MIN_MKT FROM CONFIG_SYS INTO :PERC_AJU_MKT, :RESER_MKT, :MGR_MKT;

  IF (USER = 'REPLICADOR' OR :MGR_MKT = :FALSE ) THEN
    EXIT;

  IF ((INSERTING AND NEW.ID_COP =:OP_VENDA) OR
      (UPDATING  AND NEW.ID_COP =:OP_VENDA AND (OLD.ID_REP    <> NEW.ID_REP    OR
                                                OLD.ID_PES    <> NEW.ID_PES    OR
                                                OLD.EMISSAO   <> NEW.EMISSAO   OR
                                                OLD.TOTAL_MKT <> NEW.TOTAL_MKT OR
                                                OLD.TOTAL_NEG <> NEW.TOTAL_NEG))) THEN BEGIN
    ID_PED     = NEW.ID_PED;
    ID_REP     = NEW.ID_REP;
    ID_PES     = NEW.ID_PES;
    EMISSAO    = NEW.EMISSAO;
    TOTAL_MKT  = NEW.TOTAL_MKT;
    TOTAL_NEG  = NEW.TOTAL_NEG;
    OBSERVACAO = NEW.OBSERV_1;

  END ELSE IF (DELETING) THEN BEGIN
    DELETE FROM MKT WHERE ID_PED = OLD.ID_PED;
    EXIT;
  END

  RESERVA     = 0;
  VLR_AJU_MKT = 0;

  /* Valida */
  IF (TOTAL_MKT > 0) THEN BEGIN
    VALOR_MKT   = (:TOTAL_NEG - :TOTAL_MKT);

    IF (VALOR_MKT > 0) THEN BEGIN
      RESERVA   = (:VALOR_MKT * :RESER_MKT) / 100;
      VALOR_MKT = :VALOR_MKT - :RESERVA;
    END
    IF (VALOR_MKT >= 0) THEN BEGIN
      VLR_AJU_MKT = (:TOTAL_MKT * :PERC_AJU_MKT) / 100;
      VALOR_MKT = :VALOR_MKT + VLR_AJU_MKT;
    END

    /* Se for um updating no pedido, delete o pedido no MKT */
    IF (UPDATING) THEN
      DELETE FROM MKT WHERE ID_PED = :ID_PED;

    INSERT INTO MKT(ID_PED,  ID_REP,  ID_PES,  DATA,     SUGERIDO,   NEGOCIADO,  RESERVA, VLR_AJU_MKT, VALOR_MKT, CREDITO, OBSERVACAO)VALUES
                  (:ID_PED, :ID_REP, :ID_PES, :EMISSAO, :TOTAL_MKT, :TOTAL_NEG, :RESERVA, :VLR_AJU_MKT, :VALOR_MKT, 0, :OBSERVACAO);
  END
END^

SET TERM ; ^

EXECUTE PROCEDURE MAIN_TRIGGER_DROP('AIUD_VENDA_MKT');


