#ldxscripta
#DESC FLX040101
#COD_PRODUTO 80
#COD_BANCO 3
#COD_CLIENTE 0
#DATA 28/11/2012 09:53
#LINGUAGEM A
#MARCO 0
#DISTRIBUICAO 04.01.01
#TITULO CREATE TABLE FIREBIRD
#BEGIN
CREATE TABLE TAFCOMBPROM (
CDCBPR VARCHAR (25) DEFAULT '0' NOT NULL,
DSCBPR VARCHAR (30) DEFAULT '-' NOT NULL,
QTINIC DECIMAL (15,4) DEFAULT 0 NOT NULL,
QTFINA DECIMAL (15,4) DEFAULT 0 NOT NULL,
PRIMARY KEY (CDCBPR)
)
#@#

CREATE TABLE TAFITCOMBPROM (
CDCBPR VARCHAR (25) DEFAULT '0' NOT NULL,
CDPROD  VARCHAR (25) DEFAULT '0' NOT NULL,
CDDRV   VARCHAR (25) DEFAULT '0' NOT NULL,
VLPROM DECIMAL (15,4) DEFAULT 0 NOT NULL,
PRIMARY KEY (CDCBPR, CDPROD, CDDRV),
FOREIGN KEY (CDPROD, CDDRV) REFERENCES TAFPROD (CDPROD, CDDRV),
FOREIGN KEY (CDCBPR) REFERENCES TAFCOMBPROM (CDCBPR)
)
#@#

CREATE TABLE TAFALICOMB
	(
	CDCBPR VARCHAR (25) DEFAULT '0' NOT NULL,
	CDALIA VARCHAR (25) DEFAULT '0' NOT NULL,
	QTINIC DECIMAL (15,4) DEFAULT 0 NOT NULL,
	QTFINA DECIMAL (15,4) DEFAULT 0 NOT NULL,
	PRIMARY KEY (CDCBPR, CDALIA),
	FOREIGN KEY (CDCBPR) REFERENCES TAFCOMBPROM (CDCBPR)
	)
#@#

CREATE TABLE TAFITEALICOMB
	(
	CDCBPR VARCHAR (25) DEFAULT '0' NOT NULL,
	CDALIA VARCHAR (25) DEFAULT '0' NOT NULL,
	CDPROD VARCHAR (25) DEFAULT '0' NOT NULL,
	CDDRV  VARCHAR (25) DEFAULT '0' NOT NULL,
	VLPROM DECIMAL (15,4) DEFAULT 0 NOT NULL,
	PRIMARY KEY (CDCBPR, CDALIA, CDPROD, CDDRV),
	FOREIGN KEY (CDPROD, CDDRV) REFERENCES TAFPROD (CDPROD, CDDRV),
	FOREIGN KEY (CDCBPR, CDALIA) REFERENCES TAFALICOMB (CDCBPR, CDALIA)
	)
#@#

#END
#LINGUAGEM A
#MARCO 0
#DISTRIBUICAO 04.01.01
#TITULO ALTER TABLE FIREBIRD
#BEGIN
ALTER TABLE TMVSPARAMGL ADD IDCBPR CHAR(1) DEFAULT '0' NOT NULL
#@#

ALTER TABLE TMVSCAB ADD CDCBPR VARCHAR(25) DEFAULT '0'
#@#

ALTER TABLE TAFCAB ADD COD_COMBPROM VARCHAR(25) DEFAULT '0'
#@#

update RDB$RELATION_FIELDS set RDB$NULL_FLAG = NULL where (RDB$FIELD_NAME = 'CDFLES') and (RDB$RELATION_NAME = 'TMVSPARAMGL')
#@#
#END
#LINGUAGEM A
#MARCO 0
#DISTRIBUICAO 04.01.01
#TITULO INSERT COMUNS
#BEGIN
INSERT INTO TMVSRESPMW (RESP_TMVSRESPMW, COD_TMVSRESPMW, DESC_TMVSRESPMW, IDFIX_TMVSRESPMW, SELECT_TMVSRESPMW, KEYVALUE_TMVSRESPMW, KEYLIST_TMVSRESPMW)
VALUES ('PB', '0', 'PROMOÇÃO I (TAFPROM) E BRINDE (TAFBRINDE)', 1, NULL, NULL, NULL)
#@#

INSERT INTO TMVSRESPMW (RESP_TMVSRESPMW, COD_TMVSRESPMW, DESC_TMVSRESPMW, IDFIX_TMVSRESPMW, SELECT_TMVSRESPMW, KEYVALUE_TMVSRESPMW, KEYLIST_TMVSRESPMW)
VALUES ('PB', '1', 'COMBO PROMOCIONAL E PROMOÇÃO I (TAFPROM)', 1, NULL, NULL, NULL)
#@#

INSERT INTO TMVSTABMW (TABLE_TMVSTABMW, CAMPO_TMVSTABMW, TYPE_TMVSTABMW, NULL_TMVSTABMW, PRK_TMVSTABMW, PREC_TMVSTABMW, SCALE_TMVSTABMW, SIZE_TMVSTABMW, VLRDFT_TMVSTABMW, DESC_TMVSTABMW, RESP_TMVSRESPMW, NRCS_TMVSTABMW, MXLG_TMVSTABMW, ORDER_TMVSTABMW, VLRGRID_TMVSTABMW, TABGRID_TMVSTABMW, TABMASK_TMVSTABMW, TABCRYPT_TMVSTABMW, IDFEXIBE_TMVSTABMW, DESC_HINT, READONLY_TMVSTABMW)
VALUES ('TMVSPARAMGL', 'IDCBPR', 'CHAR', '0', '0', NULL, NULL, '1', '0', 'Sistema utiliza regra de Combo / Promoção / Brinde:', 'PB', '0', '1', 72, 'PROMOÇÃO I (TAFPROM) E BRINDE (TAFBRINDE)', '03 - SSM Módulo Pedido 1', NULL, NULL, NULL, 'Este parâmetro identifica o tipo de Promoção/Combo/Brinde que será utilizado pelo sistema de vendas', '0')
#@#

#END
#LINGUAGEM A
#MARCO 0
#DISTRIBUICAO 04.01.01
#TITULO ALTER PROCEDURE LDX_EXP_TMVS_TAF_FIREBIRD
#BEGIN
ALTER PROCEDURE LDX_EXP_TMVS_TAF ( NUM_PROCESSAMENTO VARCHAR (25) CHARACTER SET NONE)
AS
 DECLARE CDPED VARCHAR(25);
 DECLARE CDFRM VARCHAR(25);
 DECLARE CDCL VARCHAR(25);
 DECLARE CDCNPG VARCHAR(25);
 DECLARE CDEQP VARCHAR(25);
 DECLARE DTPED TIMESTAMP;
 DECLARE DTFAT TIMESTAMP;
 DECLARE VLPED DECIMAL(15,4);
 DECLARE DSOBS VARCHAR(100);
 DECLARE DTPROC TIMESTAMP;
 DECLARE NUPROC VARCHAR(25);
 DECLARE IDEXP CHAR(1);
 DECLARE DSMSEX VARCHAR(4000);
 DECLARE CDMW VARCHAR(39);
 DECLARE DSOBSN VARCHAR(100);
 DECLARE CDPRIO VARCHAR(25);
 DECLARE IDSTS CHAR(1);
 DECLARE CDEMP VARCHAR(25);
 DECLARE CDFILI VARCHAR(25);
 DECLARE NUM_PEDIDO VARCHAR(25);
 DECLARE CDPROD VARCHAR(25);
 DECLARE QTDVND DECIMAL(15,4);
 DECLARE VLTAB DECIMAL(15,4);
 DECLARE VLVEND DECIMAL(15,4);
 DECLARE PCDSAC DECIMAL(15,4);
 DECLARE VLTOT DECIMAL (15,4);
 DECLARE DSUNID CHAR(3);
 DECLARE PCDSMX DECIMAL (15,4);
 DECLARE VLTBDC DECIMAL (15,4);
 DECLARE IDPROM CHAR (1);
 DECLARE CDDRPR VARCHAR(25);
 DECLARE DTFABR TIMESTAMP;
 DECLARE CDMTTR VARCHAR(25);
 DECLARE VLST DECIMAL (15,4);
 DECLARE MNV_CDINC INT;
 DECLARE MNV_CDCL VARCHAR(25);
 DECLARE MNV_CDMOT VARCHAR(25);
 DECLARE MNV_CDEQP VARCHAR(25);
 DECLARE MNV_DTMNV TIMESTAMP;
 DECLARE MNV_DTPROC TIMESTAMP;
 DECLARE MNV_NUPROC VARCHAR(25);
 DECLARE MNV_DSMSEX VARCHAR(4000);
 DECLARE IDEXP2 VARCHAR(10);
 DECLARE MNV_MTV_NV VARCHAR(25);
 DECLARE CDATIV VARCHAR(25);
 DECLARE CDTAB  VARCHAR(25);
 DECLARE DSRAZ  VARCHAR(40);
 DECLARE DSFAN  VARCHAR(40);
 DECLARE DSCGCP VARCHAR(20);
 DECLARE DSIERG VARCHAR(20);
 DECLARE VLCRED DECIMAL(15,4);
 DECLARE IDPFPJ CHAR(1);
 DECLARE IDBLQ  CHAR(1);
 DECLARE DSMTBL VARCHAR(40);
 DECLARE IDLMCR CHAR(1);
 DECLARE IDTITV CHAR(1);
 DECLARE DSNCTT VARCHAR(40);
 DECLARE DSACTT VARCHAR(20);
 DECLARE DSCRCT VARCHAR(20);
 DECLARE DSTCTT VARCHAR(15);
 DECLARE DSRML  VARCHAR(10);
 DECLARE IDFREQ CHAR(1);
 DECLARE IDVIP  CHAR(1);
 DECLARE PCVIP  DECIMAL(15,4);
 DECLARE CDREG  VARCHAR(25);
 DECLARE CDPRAC VARCHAR(25);
 DECLARE DSCP01 VARCHAR(50);
 DECLARE DSCP02 VARCHAR(50);
 DECLARE DSCP03 VARCHAR(50);
 DECLARE DSCP04 VARCHAR(50);
 DECLARE DSENDP VARCHAR(40);
 DECLARE DSBAIP VARCHAR(40);
 DECLARE DSCEPP VARCHAR(10);
 DECLARE DSCIDP VARCHAR(40);
 DECLARE DSUFP  CHAR(2);
 DECLARE DSTLPR VARCHAR(30);
 DECLARE DSFAXP VARCHAR(30);
 DECLARE DSENDC VARCHAR(40);
 DECLARE DSBAIC VARCHAR(40);
 DECLARE DSCEPC VARCHAR(10);
 DECLARE DSCIDC varchar(40);
 DECLARE DSUFC  CHAR(2);
 DECLARE DSTLCB VARCHAR(30);
 DECLARE DSFAXC VARCHAR(30);
 DECLARE IDIAE  CHAR(1);
 declare res  integer;
 DECLARE IDDIA  integer;
 DECLARE SEMANA CHAR(1);
 DECLARE SEQDIA integer;
 DECLARE CDINC INTEGER;
 DECLARE CDMOT VARCHAR (25);
 DECLARE DTMEXC TIMESTAMP;
 DECLARE VLLAT  DECIMAL(15,4);
 DECLARE VLLONG  DECIMAL(15,4);
 DECLARE NRORCP VARCHAR (12);
 DECLARE CDPDOR VARCHAR (25);
 DECLARE IDFRET CHAR(1);
 DECLARE QTITPD DECIMAL(15,4);
 DECLARE DTDESE TIMESTAMP;
 DECLARE CDORVE VARCHAR(25);
 DECLARE CDCANA VARCHAR(25);
 DECLARE CDSET VARCHAR(25);
 DECLARE CDINCO VARCHAR(25);
 DECLARE CDLIST VARCHAR(25);
 DECLARE CDESVE VARCHAR(25);
 DECLARE CDGRPC VARCHAR(25);
 DECLARE CDTPDO VARCHAR(25);
 DECLARE CDCEN VARCHAR(25);
 DECLARE CDCLRE VARCHAR(25);
 DECLARE IDTPPE CHAR(1);
 DECLARE IDFTDE CHAR(1);
 DECLARE CDGRPR varchar(25);
 DECLARE CDGRMA varchar(25);
 DECLARE CDBLOQ VARCHAR(25);
 DECLARE CDGRCL VARCHAR(25);
 DECLARE UFDOFI char(3);
 DECLARE CDZONI VARCHAR(25);
 DECLARE CDCIDP VARCHAR(25);
 DECLARE CDCIDC VARCHAR(25);
 DECLARE IDTPCL CHAR(1);
 DECLARE DSOBOB VARCHAR(120);
 DECLARE DSMAIL VARCHAR(50);
 DECLARE DTLEV TIMESTAMP;
 DECLARE QTDEST INTEGER;
 DECLARE CDDRV VARCHAR(25);
 DECLARE DTVIS TIMESTAMP;
 DECLARE QTDCL	Integer;
 DECLARE QTDPED Integer;
 DECLARE QTDCNV Integer;
 DECLARE INATEN Decimal (15,4);
 DECLARE DTNCO TIMESTAMP;
 DECLARE CDTPPD VARCHAR(25);
 DECLARE PCDCI1 Decimal (15,4);
 DECLARE PCDCI2 Decimal (15,4);
 DECLARE PCDCI3 Decimal (15,4);
 DECLARE PCDCI4 Decimal (15,4);
 DECLARE PCDCI5 Decimal (15,4);
 DECLARE VLTABF Decimal (15,4);
 DECLARE VLDSPP Decimal (15,4);
 DECLARE PCDCP1 Decimal (15,4);
 DECLARE PCDCP2 Decimal (15,4);
 DECLARE PCDCP3 Decimal (15,4);
 DECLARE PCDCP4 Decimal (15,4);
 DECLARE PCDCP5 Decimal (15,4); 
 DECLARE VLFRPD Decimal (15,4);
 DECLARE VLIPI  Decimal (15,4);
 DECLARE VLFINA Decimal (15,4);
 DECLARE IDDCQT CHAR(1);
 DECLARE CDITE  VARCHAR (25);
 DECLARE DTPESQ TIMESTAMP;
 DECLARE VLITE  DECIMAL(15,4);
 DECLARE CDDRIT VARCHAR(25);
 DECLARE VLCOMP DECIMAL(15,4);
 DECLARE CDMARC VARCHAR(25);
 DECLARE DTATUA TIMESTAMP;
 DECLARE VLSLIN Decimal(15,4);
 DECLARE VLSLAT Decimal(15,4);
 DECLARE VLSLVE Decimal(15,4);
 DECLARE NUPRPC Integer;
 DECLARE CDCLPR Integer;
 DECLARE DSOBS2 VARCHAR(100);
 DECLARE IDCOIN CHAR(1);
 DECLARE CDTRAN VARCHAR(25);
 DECLARE CDCBPR VARCHAR(25);
 
 BEGIN
    FOR   SELECT    CAST(CDPED AS INTEGER),
                    CDCL,
                    CDFRM,
                    CDCNPG,
                    CDEQP,
                    CDPRIO,
                    DTPED,
                    DTFAT,
                    VLPED,
                    DSOBSI,
                    DSOBSN,
                    DTPROC,
                    NUPROC,
                    IDEXP,
                    DSMSEX,
                    IDSTS,
                    CDMW ,
                    cdemp,
                    cdfili,
					NRORCP,
					CDPDOR,
					IDFRET,
					QTITPD,
                    DTDESE,
                    CDORVE,
                    CDCANA,
                    CDSET,
                    CDINCO,
                    CDLIST,
                    CDESVE,
                    CDGRPC,
                    CDTPDO,
                    CDCEN,
                    CDCLRE,
                    IDTPPE,
                    IDFTDE,			
					CDTPPD, 
					CDTAB,	
					VLDSPP,
					PCDCP1,
					PCDCP2,
					PCDCP3,
					PCDCP4,
					PCDCP5,
					VLFRPD,
				    VLFINA,
					CDCLPR,
					IDCOIN,
					CDTRAN,
					DSOBS2,
					CDCBPR
          FROM     TMVSCAB
          WHERE     NUPROC = :NUM_PROCESSAMENTO
            AND IDEXP = 0
          INTO     :CDPED,
                   :CDCL,
                   :CDFRM,
                   :CDCNPG,
                   :CDEQP,
                   :CDPRIO,
                   :DTPED,
                   :DTFAT,
                   :VLPED,
                   :DSOBS,
                   :DSOBSN,
                   :DTPROC,
                   :NUPROC,
                   :IDEXP,
                   :DSMSEX,
                   :IDSTS,
                   :CDMW ,
                   :cdemp,
                   :cdfili,
				   :NRORCP,
				   :CDPDOR,
				   :IDFRET,
				   :QTITPD,
				   :DTDESE,
				   :CDORVE,
				   :CDCANA,
				   :CDSET ,
				   :CDINCO,
				   :CDLIST,
				   :CDESVE,
				   :CDGRPC,
				   :CDTPDO,
				   :CDCEN ,
				   :CDCLRE,
				   :IDTPPE,
				   :IDFTDE,			   
				   :CDTPPD, 
				   :CDTAB,
				   :VLDSPP,
				   :PCDCP1,
				   :PCDCP2,
				   :PCDCP3,
				   :PCDCP4,
				   :PCDCP5,
				   :VLFRPD,
				   :VLFINA,
				   :CDCLPR,
				   :IDCOIN,
				   :CDTRAN,
				   :DSOBS2,
				   :CDCBPR

 DO BEGIN
          INSERT INTO TAFCAB (NUM_PEDIDO,
          					  COD_VENDEDOR,
          					  COD_CLIENTE,
          					  COD_FORMAPAGTO,
          					  COD_CONDPGTO,
          					  TIPO_VENDA,
          					  DAT_PEDIDO,
          					  DAT_EXTRA,
          					  VLR_TOTALPEDIDO,
          					  DES_OBSINT,
          					  DES_OBSNF,
          					  IMPORTADO,
          					  DTPROC,
          					  DAT_RETATUAL,
          					  NUPROC,
          					  MSG_IMPORTACAO,
          					  CDMW,
          					  COD_EMPRESA,
          					  COD_FILIAL,
          					  SITUACAO_PEDIDO,
							  NUM_ORDCOMPRA,
							  COD_PEDORIG,
							  IDF_TIPFRETE,
							  QTD_ITEPED,
							  DATA_DESEJADA,
							  COD_ORGVEND,
							  COD_CANAL,
							  COD_SETOR,
							  COD_INCOTERM,
							  COD_LISTA,
							  COD_ESCRVENDAS,
							  COD_GRUPOPRE,
							  COD_TIPODOCTO,
							  COD_CENTRO,
							  COD_RECEBEDOR,
							  IDF_TIPOPEDIDO,
							  IDF_FATDATADESE,
							  COD_TIPPED,
							  COD_TABPRE,						  
							  VLR_DESCPEPED,
							  PER_DESCASPED1,
							  PER_DESCASPED2,
							  PER_DESCASPED3,
							  PER_DESCASPED4,
							  PER_DESCASPED5,
							  VLR_FRETEPED,
							  VLR_FINAL,
							  NUM_COLPRECO,
							  IDF_CONSINTERNO,
							  COD_TRANSPORTADORA,
							  DES_OBSINT2,
							  COD_COMBPROM
							  )
          VALUES (:cdped,
                  :CDEQP,
                  :CDCL,
                  :CDFRM,
                  :CDCNPG,
                  :CDPRIO,
                  :DTPED,
                  :DTFAT,
                  :VLPED,
                  :DSOBS,
                  :DSOBSN,
                  '1',
                  :DTPROC,
                  :DTPROC,
                  :NUPROC,
                  'Pedido incluído na base de dados de mobilidade!',
                  :CDMW,
                  :cdemp,
                  :cdfili,
                  'G',
				  :NRORCP,
				  :CDPDOR,
				  :IDFRET,
				  :QTITPD,
				  :DTDESE,
                  :CDORVE,
                  :CDCANA,
                  :CDSET ,
                  :CDINCO,
                  :CDLIST,
                  :CDESVE,
                  :CDGRPC,
                  :CDTPDO,
                  :CDCEN ,
                  :CDCLRE,
                  :IDTPPE,
                  :IDFTDE,
				  :CDTPPD, 
				  :CDTAB,
				  :VLDSPP,
				  :PCDCP1,
				  :PCDCP2,
				  :PCDCP3,
				  :PCDCP4,
				  :PCDCP5,
				  :VLFRPD,
				  :VLFINA,
				  :CDCLPR,
				  :IDCOIN,
				  :CDTRAN,
				  :DSOBS2,
				  :CDCBPR
				  );
				  
				  
          UPDATE TMVSCAB 		SET IDEXP = '1'
          WHERE CDPED = :cdped;

          FOR 	  SELECT CAST(ITE.CDPED AS INTEGER),
                         ITE.CDPROD,
                         ITE.QTDVND,
                         ITE.VLTAB,
                         ITE.VLVEND,
                         ITE.PCDSAC,
                         ITE.VLTOT,
                         ITE.DSUNID,
                         ITE.PCDSMX,
                         ITE.VLTBDC,
                         ITE.IDPROM,
                         ITE.CDEMP,
                         ITE.CDFILI,
                         ITE.CDDRPR,
                         ITE.DTFABR,
                         ITE.CDMTTR,
                         ITE.VLST,
						 ITE.CDGRPR,
						 ITE.CDGRMA,
						 ITE.CDDRV,
						 ITE.PCDCI1,
						 ITE.PCDCI2,
						 ITE.PCDCI3,
						 ITE.PCDCI4,
						 ITE.PCDCI5,
						 ITE.VLTABF,
						 ITE.VLIPI,
						 ITE.CDTAB,
						 ITE.IDDCQT
                  FROM TMVSITE ITE , TMVSCAB CAB
                  WHERE ITE.CDPED = CAB.CDPED
                    AND CAB.CDPED =  :CDPED
                  INTO  :CDPED,
                        :CDPROD,
                        :QTDVND,
                        :VLTAB,
                        :VLVEND,
                        :PCDSAC,
                        :VLTOT,
                        :DSUNID,
                        :PCDSMX,
                        :VLTBDC,
                        :IDPROM,
                        :CDEMP,
                        :CDFILI,
                        :CDDRPR,
						:DTFABR,
						:CDMTTR,
						:VLST,
						:CDGRPR,
						:CDGRMA,
						:CDDRV,
						:PCDCI1,
						:PCDCI2,
						:PCDCI3,
						:PCDCI4,
						:PCDCI5,
						:VLTABF,
						:VLIPI,
						:CDTAB,
						:IDDCQT
						
	DO BEGIN
          INSERT INTO TAFITE (NUM_PEDIDO,
                              COD_PRODUTO,
                              QTDE_VENDA,
                              VLR_TABELA,
                              VLR_VENDA,
                              PER_DESCACRES,
                              VLR_TOTAL_ITEM,
                              DES_UNIDADE,
                              MSG_IMPORTACAO,
                              COD_EMPRESA,
                              COD_FILIAL,
                              COD_DERPRODUTO,
							  DAT_FABRICACAO,
							  COD_MOTTROCA,
							  VLR_SUBST,
					          COD_GRUPOPROD,
							  COD_GRUPOMAT,
							  COD_DERIVACAO,
							  PER_DESCASITE1,
							  PER_DESCASITE2,
							  PER_DESCASITE3,
							  PER_DESCASITE4,
							  PER_DESCASITE5,
							  VLR_TABELA_FOB,
							  VLR_IPI,
							  COD_TABELA,
							  IDDCQT
						    )
          VALUES (:CDPED,
                  :CDPROD,
                  :QTDVND,
                  :VLTAB,
                  :VLVEND,
                  :PCDSAC,
                  :VLTOT,
                  :DSUNID,
                  'Itens incluídos na base de dados de mobilidade!',
                  :CDEMP,
                  :CDFILI,
                  :CDDRPR,
				  :DTFABR,
				  :CDMTTR,
				  :VLST,
				  :CDGRPR,
				  :CDGRMA,
				  :CDDRV,
				  :PCDCI1,
				  :PCDCI2,
				  :PCDCI3,
				  :PCDCI4,
				  :PCDCI5,
				  :VLTABF,
				  :VLIPI,
				  :CDTAB,
				  :IDDCQT
				  );
				  
     END
	 
	FOR
		SELECT 
			CDPED,
			NUPRPC
		FROM 
			TMVSPRAZOS 
		WHERE 
			CDPED = :CDPED
	    INTO 
			:CDPED,
			:NUPRPC
	DO 
	BEGIN
		INSERT INTO TAFPRAZOS
		(
			NUM_PEDIDO,
			NUM_PRZPARC
		)
		VALUES 
		(
			:CDPED,
			:NUPRPC
		); 	  
	END
 END
FOR   SELECT CDINC,
			 CDCL,
			 CDMOT,
			 CDEQP,
			 DTMNV,
			 DTPROC,
			 NUPROC,
			 IDEXP,
			 DSMSEX
	  FROM TMVSMNVCL
	  WHERE     NUPROC = :NUM_PROCESSAMENTO
	    AND IDEXP = 0
	  INTO :MNV_CDINC,
	       :MNV_CDCL,
	       :MNV_CDMOT,
	       :MNV_CDEQP,
	       :MNV_DTMNV,
	       :MNV_DTPROC,
	       :MNV_NUPROC,
	       :IDEXP2,
	       :MNV_DSMSEX
 DO BEGIN
    EXECUTE PROCEDURE LDX_GEN_INCREMENTA 'INC_TAFMNV' RETURNING_VALUES :MNV_MTV_NV;

    INSERT INTO TAFMNV (NUM_MOTNVENDA,
                        COD_VENDEDOR,
                        COD_CLIENTE,
                        DAT_MOTNVENDA,
                        COD_OCORRENCIA,
                        MSG_IMPORTACAO,
                        IMPORTADO,
                        DTPROC,
                        NUPROC)
    VALUES     (:MNV_MTV_NV,
                :MNV_CDEQP,
                :MNV_CDCL,
                :MNV_DTMNV,
                :MNV_CDMOT,
                :MNV_DSMSEX,
                '1',
                :MNV_DTPROC,
                :MNV_NUPROC);
   UPDATE TMVSMNVCL 	SET IDEXP = '1'
   WHERE CDCL = :MNV_CDCL;
 END
 
 for 	SELECT 		CDCL,
  				    CDFRM,
  				    CDCNPG,
  				    CDATIV,
  				    CDTAB,
  				    DSRAZ,
  				    DSFAN,
  				    DSCGCP,
  				    DSIERG,
  				    VLCRED,
  				    IDPFPJ,
  				    IDBLQ,
  				    DSMTBL,
  				    IDLMCR,
  				    IDTITV,
  				    DSNCTT,
  				    DSACTT,
  				    DSCRCT,
  				    DSTCTT,
  				    DSRML,
  				    DSOBS,
  				    IDFREQ,
  				    IDVIP,
  				    PCVIP,
  				    CDREG,
  				    CDPRAC,
  				    DSCP01,
  				    DSCP02,
  				    DSCP03,
  				    DSCP04,
  				    DSENDP,
  				    DSBAIP,
  				    DSCEPP,
  				    DSCIDP,
  				    DSUFP,
  				    DSTLPR,
  				    DSFAXP,
  				    DSENDC,
  				    DSBAIC,
  				    DSCEPC,
  				    DSCIDC,
  				    DSUFC,
  				    DSTLCB,
  				    DSFAXC,
  				    IDIAE,
  				    DTPROC,
  				    NUPROC,
  				    DSMSEX,
  				    IDEXP,
  				    CDEQP,
					VLLAT,
					VLLONG,
					CDBLOQ,
					CDGRCL,
					UFDOFI,
					CDZONI,
					CDCIDP,
					CDCIDC,
					IDTPCL,
					DSOBOB,
					DSMAIL
  		FROM TMVSCLIALT
  		where idexp = '0'
  		into 		:CDCL,		    :CDFRM,		:CDCNPG,
  		            :CDATIV,		:CDTAB,		:DSRAZ,
  		            :DSFAN, 		:DSCGCP,	:DSIERG,
  		            :VLCRED,		:IDPFPJ,	:IDBLQ,
  		            :DSMTBL,	    :IDLMCR,	:IDTITV,
  		            :DSNCTT,		:DSACTT,	:DSCRCT,
  		            :DSTCTT,		:DSRML,		:DSOBS,
  		            :IDFREQ,		:IDVIP,		:PCVIP,
  		            :CDREG,		    :CDPRAC,	:DSCP01,
  		            :DSCP02,		:DSCP03, 	:DSCP04,
  		            :DSENDP,		:DSBAIP,	:DSCEPP,
  		            :DSCIDP,		:DSUFP,		:DSTLPR,
  		            :DSFAXP, 		:DSENDC,	:DSBAIC,
  		            :DSCEPC,		:DSCIDC,	:DSUFC,
  		            :DSTLCB,		:DSFAXC,	:IDIAE,
  		            :DTPROC,		:NUPROC,	:DSMSEX,
  		            :IDEXP, 		:CDEQP,     :VLLAT,
					:VLLONG,		:CDBLOQ,    :CDGRCL,
					:UFDOFI,        :CDZONI,    :CDCIDP,
					:CDCIDC,        :IDTPCL,    :DSOBOB,
					:DSMAIL
DO BEGIN
   select count(cdcl) from tafclialt where cdcl = :cdcl into :res;
   if (:res = 0)  then
      begin
         insert into tafclialt 	(CDEQP,    CDCL,	 CDFRM,
         						 CDCNPG,   CDATIV,   CDTAB,
         						 DSRAZ,	   DSFAN,	 DSCGCP,
         						 DSIERG,   VLCRED,	 IDPFPJ,
         						 IDBLQ,	   DSMTBL,	 IDLMCR,
         						 IDTITV,   DSNCTT, 	 DSACTT,
         						 DSCRCT,   DSTCTT,	 DSRML,
         						 DSOBS,	   IDFREQ,	 IDVIP,
         						 PCVIP,    CDREG,	 CDPRAC,
         						 DSCP01,   DSCP02,	 DSCP03,
         						 DSCP04,   DSENDP,	 DSBAIP,
         						 DSCEPP,   DSCIDP,	 DSUFP,
         						 DSTLPR,   DSFAXP,	 DSENDC,
         						 DSBAIC,   DSCEPC, 	 DSCIDC,
         						 DSUFC,	   DSTLCB,	 DSFAXC,
         						 IDIAE,	   DTPROC,	 NUPROC,
         						 IMPORTADO, VLLAT,   VLLONG,		 
							     CDBLOQ, CDGRCL,    UFDOFI,
					             CDZONI, CDCIDP,    CDCIDC,
					             IDTPCL, DSOBOB,    DSMAIL					 
								 )
         values ( :CDEQP,   :CDCL,	    :CDFRM,
                  :CDCNPG,	:CDATIV,	:CDTAB,
                  :DSRAZ,	:DSFAN,	    :DSCGCP,
                  :DSIERG, 	:VLCRED,	:IDPFPJ,
                  :IDBLQ, 	:DSMTBL,	:IDLMCR,
                  :IDTITV,	:DSNCTT,	:DSACTT,
                  :DSCRCT,	:DSTCTT,	:DSRML,
                  :DSOBS, 	:IDFREQ,	:IDVIP,
                  :PCVIP, 	:CDREG, 	:CDPRAC,
                  :DSCP01,	:DSCP02,	:DSCP03,
                  :DSCP04,	:DSENDP,	:DSBAIP,
                  :DSCEPP, 	:DSCIDP,	:DSUFP,
                  :DSTLPR,	:DSFAXP,	:DSENDC,
                  :DSBAIC,	:DSCEPC,	:DSCIDC,
                  :DSUFC, 	:DSTLCB,	:DSFAXC,
                  :IDIAE, 	:DTPROC,	:NUPROC,
                  '1',		:VLLAT,     :VLLONG,			  
				  :CDBLOQ,  :CDGRCL,    :UFDOFI,
				  :CDZONI,  :CDCIDP,    :CDCIDC,
				  :IDTPCL,  :DSOBOB,    :DSMAIL);
         UPDATE tmvsclialt 		SET IDEXP = '1'
         WHERE CDCL = :CDCL ;
      end
   else
      begin
         update tafclialt set CDEQP  = :CDEQP ,       CDCL   = :CDCL,	    CDFRM  = :CDFRM,
                              CDCNPG = :CDCNPG,			CDATIV = :CDATIV,	 		CDTAB  = :CDTAB,
                              DSRAZ  = :DSRAZ,			DSFAN  = :DSFAN,			DSCGCP = :DSCGCP,
                              DSIERG = :DSIERG,		VLCRED = :VLCRED,			IDPFPJ = :IDPFPJ,
                              IDBLQ  = :IDBLQ, 	 		DSMTBL = :DSMTBL,			IDLMCR = :IDLMCR,
                              IDTITV = :IDTITV,			DSNCTT = :DSNCTT,	 		DSACTT = :DSACTT,
                              DSCRCT = :DSCRCT,			DSTCTT = :DSTCTT,			DSRML  = :DSRML,
                              DSOBS  = :DSOBS, 			IDFREQ = :IDFREQ,			IDVIP  = :IDVIP,
                              PCVIP  = :PCVIP, 	 		CDREG  = :CDREG,		CDPRAC = :CDPRAC,
                              DSCP01 = :DSCP01,			DSCP02 = :DSCP02,	 		DSCP03 = :DSCP03,
                              DSCP04 = :DSCP04,			DSENDP = :DSENDP,			DSBAIP = :DSBAIP,
                              DSCEPP = :DSCEPP,		DSCIDP = :DSCIDP,			DSUFP  = :DSUFP,
                              DSTLPR = :DSTLPR,	 		DSFAXP = :DSFAXP,			DSENDC = :DSENDC,
                              DSBAIC = :DSBAIC,			DSCEPC = :DSCEPC,	 		DSCIDC = :DSCIDC,
                              DSUFC  = :DSUFC, 			DSTLCB = :DSTLCB,			DSFAXC = :DSFAXC,
                              IDIAE  = :IDIAE, 			DTPROC = :DTPROC,			NUPROC = :NUPROC,
                              IMPORTADO  = '1',         VLLAT  = :VLLAT,            VLLONG = :VLLONG,
							  CDBLOQ = :CDBLOQ,       CDGRCL = :CDGRCL,         UFDOFI = :UFDOFI,
							  CDZONI = :CDZONI,       CDCIDP = :CDCIDP,         CDCIDC = :CDCIDC,
							  IDTPCL = :IDTPCL,       DSOBOB = :DSOBOB,         DSMAIL = :DSMAIL

         where cdcl = :cdcl;

         UPDATE tmvsclialt 		SET IDEXP = '1'
         WHERE CDCL = :CDCL;
      end
 END
  
for    select 	CDCL, 	CDEQP,	IDDIA,
                SEMANA,	SEQDIA, 	IDIAE,
                DTPROC,	NUPROC,	IDEXP,	DSMSEX, CDINC
            from Tmvsvis
            where idexp = '0'
            into 	:CDCL,	  :CDEQP,	:IDDIA,
            		:SEMANA,	:SEQDIA, 	:IDIAE,
            		:DTPROC,	:NUPROC,  :IDEXP,
            		:DSMSEX,    :CDINC
 do begin 	select count(cdcl) from Tafvisalt where cdcl = :cdcl and cdeqp = :cdeqp and iddia = :iddia into :res;
 	if (:res = 0) then
 	   begin
 	      insert Into tafvisalt ( CDCL, CDEQP,	IDDIA,
 	      						  SEMANA,	SEQDIA, IDIAE,
 	      						  DTPROC,	NUPROC,	IMPORTADO, NUM_VISALT )
 	      values ( :CDCL,	  :CDEQP,	:IDDIA,
 	               :SEMANA,	:SEQDIA, 		:IDIAE,
 	               :DTPROC,	:NUPROC,  '1', 	:CDINC	);
 	      UPDATE Tmvsvis 		SET IDEXP = '1'
 	      WHERE cdcl = :cdcl
			    and cdeqp = :cdeqp
			    and iddia = :iddia;
 	   end
 	else
 	   begin
 	        update tafvisalt set CDCL   = :CDCL,		CDEQP  = :CDEQP,		IDDIA  = :IDDIA,
 	        					 SEMANA = :SEMANA,		SEQDIA = :SEQDIA,		IDIAE  = :IDIAE,
 	        					 DTPROC = :DTPROC,		NUPROC = :NUPROC,		IMPORTADO  = '1',  
								 NUM_VISALT = :CDINC
 	        where cdcl = :cdcl
 	        and cdeqp = :cdeqp
 	        and iddia = :iddia;

 	        UPDATE Tmvsvis 		SET IDEXP = '1'
 	        WHERE cdcl = :cdcl
			  and cdeqp = :cdeqp
			  and iddia = :iddia;
 	   end
	end

FOR 	SELECT CDINC,  CDCL,   CDMOT,
			   CDEQP,  DTMEXC, DTPROC,
			   NUPROC, IDSTS,  IDEXP
	    FROM TMVSMECL
	    where idexp = '0'
	    INTO :CDINC,  :CDCL,   :CDMOT,
	         :CDEQP,  :DTMEXC, :DTPROC,
	         :NUPROC, :IDSTS,  :IDEXP
DO    BEGIN
           select count(*) from TAFME where COD_CLIENTE = :cdcl into :res;
           if (:res = 0) then
              begin
              INSERT INTO TAFME ( NUM_MOTEXC, COD_VENDEDOR, COD_CLIENTE,
                                  DAT_MOTEXC, COD_MOTEXC, 	MSG_IMPORTACAO,
                                  IMPORTADO,  DTPROC, 		NUPROC 		)
              VALUES (:CDINC, :CDEQP, :CDCL,
                      :DTMEXC,:CDMOT, 'Exportado com sucesso!',
                      '1', 	:DTPROC, :NUPROC );

              UPDATE TMVSMECL 		SET IDEXP = '1'
              WHERE CDCL = :CDCL;
			end
END

FOR SELECT CDINC,
		   CDITE,
		   CDCL,
		   DTPESQ,
		   CDEQP,
		   VLITE,
		   DTPROC,
		   NUPROC,
		   IDSTS,
		   CDDRIT,
		   CDMARC,
		   VLCOMP
      FROM TMVSCLPESQ	
	 WHERE idexp = '0' 	
	  INTO :CDINC,
	       :CDITE,
		   :CDCL,
		   :DTPESQ,
		   :CDEQP,
		   :VLITE,
		   :DTPROC,
		   :NUPROC,
		   :IDSTS,
		   :CDDRIT,
		   :CDMARC,
		   :VLCOMP
DO
BEGIN
    SElECT COUNT(*) 
	  FROM TAFCLPESQ 
     WHERE NUM_CLPESQ = :CDINC 
      INTO :res;
    
	if(:res = 0) then 	
        INSERT INTO TAFCLPESQ 
		( 
		    NUM_CLPESQ 
            		,CDITE      
			,CDCL       
			,DTPESQ     
			,CDEQP      
			,VLITE      
			,IMPORTADO
			,MSG_IMPORTACAO
			,DTPROC
			,NUPROC
			,CDDRIT
			,CDMARC
		   	,VLCOMP
        )
        VALUES
        (
            :CDINC
            ,:CDITE
            ,:CDCL
            ,:DTPESQ
            ,:CDEQP
            ,:VLITE
            ,'1'
            ,'Exportado com sucesso!'
            ,:DTPROC
            ,:NUPROC
	    ,:CDDRIT
	    ,:CDMARC
	    ,:VLCOMP		
        );

    UPDATE TMVSCLPESQ
	SET
	    IDEXP = '1' 		
    WHERE
	    CDINC = :CDINC ; 	
END

FOR SELECT 
	 		CDINC,  CDCL,   CDPR, 
	 		CDEQP,  DTLEV,  QTDEST, 
	 		DTPROC, NUPROC, CDDRV,
	 		DSMSEX
	 FROM TMVSLVCL	 
     WHERE IDEXP = '0'

	    INTO :CDINC,  :CDCL,   :CDPROD,
			 :CDEQP,  :DTLEV,  :QTDEST,
			 :DTPROC, :NUPROC, :CDDRV,
			 :DSMSEX		
DO    BEGIN
		INSERT INTO TAFLVCL
		(
			NUM_LEVANT,  COD_VENDEDOR,   COD_CLIENTE,
			COD_PRODUTO, DAT_LEVANT,     QTD_ESTOQUE,
			DTPROC, 	  NUPROC,  		  IMPORTADO,   
			MSG_IMPORTACAO, COD_DERIVACAO
		)
		VALUES 
		(
			:CDINC,  :CDEQP, :CDCL,
			:CDPROD, :DTLEV,   :QTDEST,
			:dtproc, :nuproc,  '1',   
			'Exportado com sucesso!', :CDDRV
		);
			
        UPDATE TMVSLVCL	SET IDEXP = '1' WHERE CDINC = :CDINC;
end


FOR 	SELECT 
		 CDINC,  CDEQP,	 DTVIS,
		 QTDCL,	 QTDPED, QTDCNV,
		 INATEN, DTPROC, NUPROC,
	     DSMSEX
     FROM TMVSESTATISTICA 
     WHERE IDEXP = '0'
	    INTO :CDINC,  :CDEQP,  :DTVIS,
		 :QTDCL,  :QTDPED, :QTDCNV,
		 :INATEN, :DTPROC, :NUPROC,
	     :DSMSEX		
DO    BEGIN
		INSERT INTO TAFESTATISTICA
			(
			NUM_ESTATISTICA, COD_VENDEDOR,  DAT_VISITA,
			QTD_CLIENTE,     QTD_PEDIDO,    QTD_CLINCOMPROU,
			IND_ATEND,       DTPROC,        NUPROC,
			IMPORTADO,       MSG_IMPORTACAO
			)
		VALUES 
			(
			:CDINC,  :CDEQP,  :DTVIS,
			:QTDCL,  :QTDPED, :QTDCNV,
			:INATEN, :DTPROC, :NUPROC,
			'1',     'Exportado com sucesso!'
			); 	  
	   UPDATE TMVSESTATISTICA SET IDEXP = '1' WHERE CDINC = :CDINC;
end

FOR 	SELECT	
		 CDINC,  CDCL,	CDEQP,
		 DTNCO,	 IDDIA,	DTPROC,
		 NUPROC, DSMSEX 
	 FROM TMVSCLNCP
     WHERE IDEXP = '0'
	    INTO :CDINC,  :CDCL,  :CDEQP,
		 :DTNCO,  :IDDIA, :DTPROC,
		 :NUPROC, :DSMSEX 
DO    BEGIN
		INSERT INTO TAFCLNCP
			(
			NUM_CLINCOMP, COD_VENDEDOR, COD_CLIENTE,  
			DAT_NCOMPROU, IDF_DIA,   	DTPROC,
			NUPROC,	      IMPORTADO,	MSG_IMPORTACAO
			)
		VALUES 
			(
			:CDINC,	 :CDEQP,  :CDCL,
			:DTNCO,	 :IDDIA,  :DTPROC,
			:NUPROC, '1',     'Exportado com sucesso!'
			);	
	   UPDATE TMVSCLNCP SET IDEXP = '1' WHERE CDINC = :CDINC;
 end
 
 
FOR SELECT	
		CDEQP,  DTATUA, VLSLIN,
		VLSLAT, VLSLVE, DTPROC,
		NUPROC
	FROM
		TMVSSALDOSPOT
	WHERE 
		IDEXP = '0'
	INTO 
		:CDEQP,  :DTATUA, :VLSLIN,
		:VLSLAT, :VLSLVE, :DTPROC,
		:NUPROC
DO
BEGIN
	DELETE FROM TAFSALDOSPOT WHERE CDEQP = :CDEQP;
	
	INSERT INTO TAFSALDOSPOT
	(
		CDEQP,
		VLSLIN,
		VLSLVE,
		DTPROC,
		NUPROC,
		IMPORTADO,
		MSG_IMPORTACAO
	)
	VALUES 
	(
		:CDEQP,  
		:VLSLIN,
		:VLSLVE,
		:DTPROC,
		:NUPROC,
		'1',
		'Exportado com sucesso.'
	);	
	
	UPDATE TMVSSALDOSPOT SET IDEXP = '1' WHERE CDEQP = :CDEQP;
end
 
END;

#@#
#END
