;** Consulta Notas Fiscais******************************************************
;*  Consulta de Nota Fiscal                                                    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 14/06/2006 por Jose Luis Ameno da Silva
;Ultima Alteracao: 26/03/2011 por Bruno Carvalho
;Motivo Alteracao: Ajustes para corre‡Æo de estrutura
;Ultima Alteracao: 05/08/2015 por Andrey Fillipe
;Motivo Alteracao: Mudan‡a na visualizacao da Nota Fiscal de Entrada
;                  Feito igual o consulta de Notas do Vendas
;Ultima Alteracao: 15/03/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes exclusÆo rotinas controle de pecas
;Ultima Alteracao: 27/01/2018 por Bruno Carvalho
;Motivo Alteracao: Removido processos que referencia ao campo [Edita Descricao do Item]
;Ultima Alteracao: 24/12/2019 por Bruno Carvalho
;Motivo Alteracao: Adicionado Bib_Proc

;*******************************************************************************
;* Rotina para Controlar Chave da Ordem de Faturamento                         *
;*******************************************************************************
Proc CHAVEC()
INC_COMPRA   =False
FACA_DIGITAC=True
INICIO_PAGINA=True

VCITENS =BlankNum()
VSQTDE  =BlankNum()
VSVRMERC=BlankNum()

While True
  
  Moveto "NotaEC"
  End
  Moveto [Numero do Lancamento]
  LANCTO=[Numero do Lancamento]

  If OPCAO_FORM="F"
     Then Down
     Else PgDn PgDn PgDn
  EndIf
  Moveto [Numero do Lancamento]

  SetaCorRegistro("Editar")
  While True

  TECLA=""
  AguardaTeclasPadrao() 

     Switch
        Case RetVal=-33 : SemFuncoes()
        Case RetVal=-94 : MsgTeclasPadrao()
        Case RetVal="F3":
             If OPCAO_FORM="F"
                Then OPCAO_FORM="1"
                Else OPCAO_FORM="F"
             EndIf
             Del                    
             CHAVEC=5
             Return
        Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
             Del
             While True
               INC_COMPRA   =False
               FACA_DIGITAC=True
               INICIO_PAGINA=True

               VCITENS     =BlankNum()
               VSQTDE      =BlankNum()
               VSVRMERC    =BlankNum()

               If OPCAO_FORM="F"
                  Then PAGINA("NotaEC","MR")
                  Else PAGINA("NotaEC","MT")
               EndIf
               LANCTO=[Numero do Lancamento]
               Switch
                 Case EDITAR="S":
                      PickForm "1"
                      Echo Normal
                      Echo Off
                      If Checa_Controle([Numero_Controle_Dia])
                         Then CHAVEC=0
                              Controla_EdicaoC()
                         Else UnLockRecord
                      EndIf
                 Case EXCLUIR="S":
                      UnLockRecord
                      FastMsg("Fun‡Æo nÆo permitida!")
                 OtherWise: QuitLoop
               EndSwitch
               If OPCAO_FORM="1"
                  Then PickForm "1"
                  Else PickForm "F"
               EndIf
               SetaPosForm()
             EndWhile
             QuitLoop
        Case Retval="ESC":
             CHAVEC=9
             Del
             Return             
        Case Retval="ENTER": 
             If IsBlank([])
                Then Loop
             EndIf
             []=ALINH()
             LANCTO=[]   
             Del
             PickForm "1"
             Locate LANCTO
             If Retval
                Then CHAVEC=0
                     Return
                Else CHAVEC=1
                     End
                     If Not IsBlank([Numero do Lancamento])
                        Then PgDn PgDn PgDn
                     EndIf
                     FastMsg("Documento nÆo localizado, inser‡Æo nÆo permitida !")
             EndIf
             If Not IsLockRecord()
                Then Echo Normal
                     Echo Off
             EndIf
             If CHAVEC=1
                Then Del
             EndIf
             If OPCAO_FORM="F"
                Then PickForm "F"
             EndIf
             QuitLoop
        EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveC)

;*******************************************************************************
;* Rotina para Digitacao da Cabeca                                             *
;*******************************************************************************
Proc DIGITAC()

Moveto "NotaEC"
Moveto "NotaECP" SetaCorRegistro("Padrao")
Moveto "NotaEC"

If INICIO_PAGINA
   Then If Not Carrega_Parametros()
           Then CHAVEC=9
                Return
        EndIf
EndIf

Moveto [Numero do Lancamento]

FACA_CHAVEI=True
INICIO_PAGINA=False

While True

      TECLA=" Esc=Retornar  PgDn  "
      Prompt TECLA

      MSGCAMPO=""
      FORMMSGCAMPO=""
      MSGCAMPO=Field()

      Wait Field
        Message MSGCAMPO
         Until "ESC","PGDN"
 
      Switch
        Case RetVal="PGDN":
             QuitLoop
        Case Retval="ESC":
             CHAVEC=9
             QuitLoop
      EndSwitch
EndWhile

EndProc  ;(DigitaC)

;*******************************************************************************
;* Rotina para Localizar Item                                                  *
;*******************************************************************************
Proc CHAVEI()
FACA_DIGITAF=True

While True
  Moveto "NotaEI"

  ;nÆo tem no form
  ;Moveto [Preco Total            ] VSVRMERC=ImageCSum()
  ;Moveto [Quantidade             ] VSQTDE  =ImageCSum()
  ;Moveto [Sequencia do Lancamento] VCITENS =ImageCCount()

  Moveto [Sequencia do Lancamento]
  End
  NITEM = [Sequencia do Lancamento]
  Down
  Moveto [Sequencia do Lancamento]

  Seta_Sequencia(NITEM,False)
  If IsLockRecord()
     Then Del
          Loop
  EndIf

  Sw_Item_Pesquisado=""

  Opcao_Ver    =False
  SetaCorRegistro("Editar")
  While True
                                                              
    TECLA= " Esc=Retornar  F3=Ver Ficha  F7=Paginar  PgDn/PgUp "

    Prompt TECLA

    Wait Field
         Until "ESC","F3","F7","HOME","UP","PGUP","PGDN"

		Switch
    Case RetVal="F3":
         Del
         Moveto [Sequencia do Lancamento]
         If RecordStatus("New")
            Then MENSAGEM("Sem Item p/ ver !")
                 QuitLoop
         EndIf
         Opcao_Ver=True
         CHAVEI=0
         Home
         SEQIT=[Sequencia do Lancamento]
         Inicia_Registro()
         DigitaI()
         QuitLoop
    Case Retval="PGDN":
         Del
         Moveto [Sequencia do Lancamento]
         CHAVEI=9
         Return
    Case Retval="PGUP" Or RetVal="ESC":
         Del
         CHAVEI=9
         FACA_CHAVEI =False
         FACA_DIGITAF=False
         Return
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         Del
         While True
           PAGINA("NotaEI","MR")
           SEQIT=[Sequencia do Lancamento]
           If EDITAR="S"
              Then UnLockRecord
                   FastMsg("Fun‡Æo nÆo permitida!")
              Else If EXCLUIR="S"
                      Then UnLockRecord
                           FastMsg("Fun‡Æo nÆo permitida!")
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveI)

;*******************************************************************************
;* Rotina de Digitacao Dados do Item                                           *
;*******************************************************************************
Proc Item_Dialog(Disparo,Campo,Evento,Elemento)
Private JANELA, WinArray5

TECLA=" Esc=Retornar  "
MSG_CAMPO=""
Temp=True

If CHAVEI=0
   Then Moveto [Codigo do Item]
        Ativa_Lookup()
        Locate [Notaei->Codigo do Item]
        VSD_I_Referencia=[Referencia]
        KeyPress "ESC"
EndIf

If INICIO_DIALOG
   Then If Sw_Dialog="Custo"
           Then Campo="I_Flag2"
           Else If CHAVEI=0
                   Then Campo="I_Quantidade"
                   Else If IsBlank(Sw_Item_Pesquisado)
                           Then Campo="I_OC"
                           Else 
        									      VSD_I_OC     =""
                                VSD_I_OC_Item=0
        									 
        												VSD_I_Codigo_Fornecedor=""
        												CODFORN_ITEM=""
        												CODFORN_OK  =False									 
        									 
        									      Campo="I_Codigo_Fornecedor"
                        EndIf   
                EndIf
        EndIf
        INICIO_DIALOG=False
EndIf
If Opcao_Ver=True Then Campo="I_Seq" EndIf

If Disparo="EVENT" And DynArraySize(Evento)>0 And Evento["Type"]="KEY" Then
   
   Switch
      Case Opcao_Ver=True:
           Temp=False
           Switch
             Case Evento["KeyCode"] =  27:
                  CancelDialog
             OtherWise:
               Mover_Reg=True
               Switch
                 Case Evento["KeyCode"] = -64 Or Evento["KeyCode"] = "F6" :
                      Temp=False
                      Ver_CalcCusto()
                 Case Evento["KeyCode"] = -71:
                      If AtFirst()
                         Then Mover_Reg=False Beep
                         Else Home
                      EndIf
                 Case Evento["KeyCode"] = -79:
                      If AtLast()
                         Then Mover_Reg=False Beep
                         Else End
                      EndIf
                 Case Evento["KeyCode"] = -73:
                      If AtFirst()
                         Then Mover_Reg=False Beep
                         Else Up
                      EndIf
                      If CHAVEI=0
                         Then Moveto [Codigo do Item]
                              Ativa_Lookup()
                              Locate [Notaei->Codigo do Item]
                              VSD_I_Referencia=[Referencia]
                              KeyPress "ESC"
                      EndIf
                 Case Evento["KeyCode"] = -81:
                      If AtLast()
                         Then Mover_Reg=False Beep
                         Else Down
                      EndIf
                      If CHAVEI=0
                         Then Moveto [Codigo do Item]
                              Ativa_Lookup()
                              Locate [Notaei->Codigo do Item]
                              VSD_I_Referencia=[Referencia]
                              KeyPress "ESC"
                      EndIf
               EndSwitch
               If Mover_Reg=True
                  Then SEQIT=[Sequencia do Lancamento]
                       Inicia_Registro()
               EndIf
           EndSwitch
   EndSwitch
EndIf

MSGCAMPO=""

If Opcao_Ver=True           
   Then TECLA=" Esc=Retornar  F6=Ver Custo  Home/End/PgDn/PgUp=Paginar  "
EndIf

@ 24,0 ?? TECLA+Spaces(80-Len(TECLA))
PaintCanvas Attribute 31 24,0,24,79
Message(MSGCAMPO)
Mouse Hide
RefreshDialog
Return TEMP
EndProc  ; (Item_Dialog)

;*******************************************************************************
;* Rotina para Iniciar Registro                                                *
;*******************************************************************************
Proc Inicia_Registro()

INICIO_DIALOG=True
Moveto [Sequencia do Lancamento]
Locate [NotaEC->Numero do Lancamento],SEQIT
CopyToArray RegItem

QTDCONV =[Quantidade Convertida]
QUANT   =[Quantidade ]
CTTOTAL =NDecimal(([Quantidade ]*[Custo Medio Unitario]),2)
VSD_I_Flag=""

VSD_I_TX_ICMS_SAIDA=0
VSD_I_RD_ICMS_SAIDA=0
If Not IsBlank([Codigo do Item])
   Then VSD_I_Codigo_Item=[Codigo do Item]
        Moveto [Codigo do Item]
        Ativa_Lookup()
        Moveto [Codigo do Item]
        Locate VSD_I_Codigo_Item
        If RetVal
           Then VSD_I_TX_ICMS_SAIDA  =[Aliquota de ICMS     ]
                VSD_I_RD_ICMS_SAIDA  =[Aliquota Reduzida BCICMS  ]
        EndIf
        KeyPress "ESC"
EndIf

Registro_Item("Ler")

EndProc ; (Inicia_Registro)

;*******************************************************************************
;* Rotina para Digitacao Item                                                  *
;*******************************************************************************
Proc DigitaI()
Private TIT_COFINS1, TIT_CONFIS2

Sw_Dialog="Item"
Echo Normal
Echo Off
Showdialog "Dados do Item [     ]" Proc "Item_Dialog"
   Trigger "OPEN","ARRIVE"
    Key "F1","TAB","ReverseTab","ENTER","ESC","F4","ClearArea","HOME","END","PGDN","PGUP",-33,"F6","F7","F8"
    @ 02,0 height 20 width 80

   Accept @ -01,43 Width 5 "A2" Tag "I_Seq" To VSD_I_SEQ

   Label  @ 00,08 "Compra" For "I_OC"
   Accept @ 01,08 Width 09 "A06" picture "#*#" Tag "I_OC" to VSD_I_OC
   Label  @ 00,16 "/Item" For "I_OC_Item"
   Accept @ 01,18 Width 05 "N" picture "!*!" Tag "I_OC_Item" to VSD_I_OC_Item
   Label  @ 00,32 "Codigo Item/Fornecedor" For "I_Codigo_Fornecedor"
   Accept @ 01,33 Width 23 "A20" picture "!*!" Tag "I_Codigo_Fornecedor" to VSD_I_Codigo_Fornecedor
   Label  @ 00,56 "Referˆncia" For "I_Referencia"
   Accept @ 01,57 Width 14 "A12" picture "!*!" Tag "I_Referencia" to VSD_I_Referencia

   Label  @ 02,01 "Codigo Item" For "I_Codigo_Item"
   Accept @ 03,01 Width 16 "A25" picture "!*!" Tag "I_Codigo_Item" to VSD_I_Codigo_Item
   Label  @ 02,18 "Descricao Item" For "I_Descricao_Item"
   Accept @ 03,18 Width 33 "A30" picture "!*!" Tag "I_Descricao_Item" to VSD_I_Descricao_Item
   Label  @ 02,51 "Tipo" For "I_Tipo_Item"
   Accept @ 03,52 Width 04 "A1" picture "&" Tag "I_Tipo_Item" to VSD_I_Tipo_Item

   Label  @ 04,01 "Quantidade" For "I_Quantidade"
   Accept @ 05,01 Width 13 "N" picture "#*#.[#####]" Tag "I_Quantidade" to VSD_I_Quantidade
   Label  @ 04,15 "UnC" For "I_UN_Item"
   Accept @ 05,15 Width 05 "A2" picture "!*!" Tag "I_UN_Item" to VSD_I_UN_Item
   Label  @ 04,21 "Pr.Unitario" For "I_Preco_Unitario"
   Accept @ 05,21 Width 12 "N" picture "#*#.[#####]" Tag "I_Preco_Unitario" to VSD_I_Preco_Unitario

   Label  @ 04,34 "%/" For "I_Taxa_Desconto_Unitario"
   Accept @ 05,34 Width 11 "N" picture "#*#.[####]" Tag "I_Taxa_Desconto_Unitario" to VSD_I_Taxa_Desconto_Unitario
   Label  @ 04,37 "Vr.Desconto" For "I_Valor_Desconto_Unitario"
   Accept @ 05,46 Width 12 "N" picture "#*#.[#####]" Tag "I_Valor_Desconto_Unitario" to VSD_I_Valor_Desconto_Unitario
   Label  @ 04,59 "Pr.Total" For "I_Preco_Total"
   Accept @ 05,59 Width 12 "$" picture "#*#.[##]" Tag "I_Preco_Total" to VSD_I_Preco_Total
   
   @ 06,00 ?? "Impostos CST  %Red     Aliq.  BC         Valor" 
   Label  @ 07,00 "  ICMS" For "I_CST_Item"
   Accept @ 07,80 Width 03 "A1" picture "!" Tag "I_TICMS" to VSD_I_TICMS ; Ficar invisivel
   Accept @ 07,08 Width 06 "A3" picture "###" Tag "I_CST_Item" to VSD_I_CST_Item
   Accept @ 07,13 Width 10 "N" picture "#*#.[####]" Tag "I_RBC_ICMS" to VSD_I_RBC_ICMS
   Accept @ 07,22 Width 08 "N" picture "#*#.[##]" Tag "I_AICMS" to VSD_I_AICMS
   Accept @ 07,29 Width 12 "$" picture "#*#.[##]" Tag "I_BC_ICMS" to VSD_I_BC_ICMS
   Accept @ 07,40 Width 12 "$" picture "#*#.[##]" Tag "I_Valor_ICMS" to VSD_I_Valor_ICMS

   Label  @ 08,00 " CSOSN" For "I_CSOSN"
   Accept @ 08,08 Width 07 "A4" picture "####" Tag "I_CSOSN" to VSD_I_CSOSN
   PaintCanvas Attribute 112 08, 10, 08, 51

   Label  @ 09,00 "    ST" For "I_BC_ST"
   Accept @ 09,14 Width 09 "N" picture "#*#.[##]" Tag "I_MVA" to VSD_I_MVA
   @ 09,10 ?? "%MVA"
   PaintCanvas Attribute 112 09, 08, 09, 21
   Accept @ 09,22 Width 08 "N" picture "#*#.[##]" Tag "I_AST" to VSD_I_AST
   Accept @ 09,29 Width 12 "$" picture "#*#.[##]" Tag "I_BC_ST" to VSD_I_BC_ST
   Accept @ 09,40 Width 12 "$" picture "#*#.[##]" Tag "I_Valor_ST" to VSD_I_Valor_ST

   Label  @ 10,00 "   IPI" For "I_CST_IPI"
   Accept @ 10,80 Width 03 "A1" picture "!" Tag "I_TIPI" to VSD_I_TIPI ; Ficar invisivel
   Accept @ 10,08 Width 16 "A2" picture "##" Tag "I_CST_IPI" to VSD_I_CST_IPI  
   Accept @ 10,22 Width 08 "N" picture "#*#.[##]" Tag "I_AIPI" to VSD_I_AIPI
   Accept @ 10,29 Width 12 "$" picture "#*#.[##]" Tag "I_BC_IPI" to VSD_I_BC_IPI
   Accept @ 10,40 Width 12 "$" picture "#*#.[##]" Tag "I_Valor_IPI" to VSD_I_Valor_IPI

   Label  @ 11,00 " ISSQN" For "I_TISSQN"
   Accept @ 11,08 Width 21 "A1" picture "D,N,I,S,R" Tag "I_TISSQN" to VSD_I_TISSQN
   Accept @ 11,22 Width 08 "N" picture "#*#.[##]"   Tag "I_AISSQN" to VSD_I_AISSQN
   Accept @ 11,29 Width 12 "$" picture "#*#.[##]"   Tag "I_BC_ISSQN" to VSD_I_BC_ISSQN
   Accept @ 11,40 Width 12 "$" picture "#*#.[##]"   Tag "I_Valor_ISSQN" to VSD_I_Valor_ISSQN

   Label  @ 12,00 "   PIS" For "I_CST_PIS"
   Accept @ 12,08 Width 16 "A2" picture "##" Tag "I_CST_PIS" to VSD_I_CST_PIS  
   Accept @ 12,22 Width 08 "N" picture "#*#.[###]" Tag "I_APIS" to VSD_I_APIS
   Accept @ 12,29 Width 12 "$" picture "#*#.[##]" Tag "I_BC_PIS" to VSD_I_BC_PIS
   Accept @ 12,40 Width 12 "$" picture "#*#.[##]" Tag "I_Valor_PIS" to VSD_I_Valor_PIS

   Label  @ 13,00 "COFINS" For "I_CST_COFINS"
   Accept @ 13,08 Width 16 "A2" picture "##" Tag "I_CST_COFINS" to VSD_I_CST_COFINS
   Accept @ 13,22 Width 08 "N" picture "#*#.[###]" Tag "I_ACOFINS" to VSD_I_ACOFINS
   Accept @ 13,29 Width 12 "$" picture "#*#.[##]" Tag "I_BC_COFINS" to VSD_I_BC_COFINS
   Accept @ 13,40 Width 12 "$" picture "#*#.[##]" Tag "I_Valor_COFINS" to VSD_I_Valor_COFINS

   Label  @ 06,56 "CFOP" For "I_CFOP"
   Accept @ 07,57 Width 07 "A4" picture "####" Tag "I_CFOP" to VSD_I_CFOP
   Label  @ 06,64 "NCM" For "I_NCM"
   Accept @ 07,65 Width 11 "A8" picture "#*#" Tag "I_NCM" to VSD_I_NCM
 
   Label  @ 08,52 "Frete"    For "I_RAT_FRETE"
   Accept @ 09,53 Width 11 "$" picture "#*#.[##]" Tag "I_RAT_FRETE" to VSD_I_RAT_FRETE
   Label  @ 08,64 "Seguro"   For "I_RAT_SEGURO"
   Accept @ 09,65 Width 11 "$" picture "#*#.[##]" Tag "I_RAT_SEGURO" to VSD_I_RAT_SEGURO

   Label  @ 10,52 "Desconto" For "I_RAT_DESCONTO"
   Accept @ 11,53 Width 11 "$" picture "#*#.[##]" Tag "I_RAT_DESCONTO" to VSD_I_RAT_DESCONTO
   Label  @ 10,64 "OutDesp"  For "I_RAT_OUTDESP"
   Accept @ 11,65 Width 11 "$" picture "#*#.[##]" Tag "I_RAT_OUTDESP" to VSD_I_RAT_OUTDESP
   
   Label  @ 12,53 "Servi‡o" For "I_CDSERV"
   Accept @ 13,53 Width 10 "N" picture "#*#" Tag "I_CDSERV" to VSD_I_CDSERV
   Label  @ 12,63 "Munic¡pio" For "I_MUNSERV"
   Accept @ 13,63 Width 10 "N" picture "#*#" Tag "I_MUNSERV" to VSD_I_MUNSERV
   Label  @ 12,73 "UF" For "I_UFSERV"
   Accept @ 13,73 Width 5  "A2" picture "!!" Tag "I_UFSERV" to VSD_I_UFSERV

   Label  @ 14,01 "Ajustes +/-" For "I_Acrescimos_Desctos"
   Accept @ 15,01 Width 13 "N" picture "[-]#*#.[#####]" Tag "I_Acrescimos_Desctos" to VSD_I_Acrescimos_Desctos
   @ 14,14 ?? "/"
   Accept @ 15,15 Width 05 "A2" picture "!*!" to VSD_I_UN_Item

   Label  @ 14,21 "Ft.Conversao=>" For "I_Fator_Conversao"
   Accept @ 15,22 Width 13 "N" picture "#*#.[#####]" Tag "I_Fator_Conversao" to VSD_I_Fator_Conversao
   Label  @ 14,36 "Quantidade" For "I_Quantidade_Convertida"
   Accept @ 15,36 Width 13 "N" picture "#*#.[###]" Tag "I_Quantidade_Convertida" to VSD_I_Quantidade_Convertida
   Label  @ 14,49 "UnI" For "I_Unidade_Interna"
   Accept @ 15,49 Width 05 "A2" picture "!!" Tag "I_Unidade_Interna" to VSD_I_Unidade_Interna
   Label  @ 14,55 "Custo" For "I_Ct_Unitario"
   Accept @ 15,55 Width 12 "N" picture "#*#.[#####]" Tag "I_Ct_Unitario" to VSD_I_Ct_Unitario
   @ 15,35 ?? "="
   @ 15,54 ?? "/"

   Label  @ 16,01 "Centro Custo" For "I_CCusto"
   Accept @ 17,01 Width 10 "A7" picture "###.###" Tag "I_CCusto" to VSD_I_CCusto
   Accept @ 17,10 Width 25 "A25" picture "!*!" Tag "I_Descricao_CCusto" to VSD_I_Descricao_CCusto

   Label  @ 16,35 "Tabela=" For "I_Tabela_Preco"
   Accept @ 17,36 Width 08 "A5" picture "!*!" Tag "I_Tabela_Preco" to VSD_I_Tabela_Preco
   Label  @ 16,42 "Pre‡o/" For "I_Preco_Tabela"
   Accept @ 17,44 Width 12 "N" picture "#*#.[#####]" Tag "I_Preco_Tabela" to VSD_I_Preco_Tabela
   @ 16,49 ?? VSD_I_Unidade_Interna
   Label  @ 16,54 "%Venda" For "I_Indice_PrVenda"
   Accept @ 17,54 Width 09 "$" picture "#*#.[##]" Tag "I_Indice_PrVenda" to VSD_I_Indice_PrVenda
   Label  @ 16,63 "PrVenda/" For "I_Preco_VendaOk"
   Accept @ 17,63 Width 12 "N" picture "#*#.[#####]" Tag "I_Preco_VendaOk" to VSD_I_Preco_VendaOk
   @ 16,72 ?? VSD_I_Unidade_Interna

   Accept @ 17,75 Width 1 "A1" picture "!" Tag "I_Flag" to VSD_I_Flag

EndDialog
Mouse Show

EndProc  ;  (DigitaI)

;*******************************************************************************
;* Rotina para Ver Calculo do Custo                                            *
;*******************************************************************************
Proc Ver_CalcCusto()
Private Tit

Sw_Dialog="Custo"
Tit=" C lculo custo unit rio por ["+VSD_I_UN_Item+"] "
INICIO_DIALOG=True
Showdialog Tit Proc "Item_Dialog"
   Trigger "OPEN","ARRIVE"
    Key "ESC"
     @ 12,0 height 11 width 80

   @ 00,00 ?? "(=)Pre‡o..."+Format("W10.2,EIC",VCU_Preco_Unitario)+Spaces(3)
   @ 01,00 ?? "(-)Descto.."+Format("W13.5,EIC",VCU_Desconto)
   @ 02,00 ?? "(=)Liquido."+Format("W13.5,EIC",VCU_Preco_Unitario-VCU_Desconto)
   @ 03,00 ?? "("+VCU_SINAL_FRETE+   ")"+TIT_FRETE   +Format("W13.5,EIC",VCU_FRETE)
   @ 04,00 ?? "("+VCU_SINAL_SEGURO+  ")"+TIT_SEGURO  +Format("W13.5,EIC",VCU_SEGURO)
   @ 05,00 ?? "("+VCU_SINAL_OUTDESP+ ")"+TIT_OUTDESP +Format("W13.5,EIC",VCU_OUTDESP)
   @ 06,00 ?? "("+VCU_SINAL_ACR_DESC+")"+TIT_ACR_DESC+Format("W13.5,EIC",VCU_ACRE_DESC)
   @ 07,00 ?? "("+VCU_SINAL_SERVICO+ ")"+TIT_SERVICO +Format("W13.5,EIC",VCU_SERVICO)
   @ 08,00 ?? "("+VCU_SINAL_AJUSTES +")Ajustes."     +Format("W13.5,EIC",VCU_AJUSTES)
  
   @ 00,25 ?? "("+VCU_SINAL_DIF_ICMS+")"+TIT_DIF_ICMS+Format("W14.5,EIC",VCU_DIF_ICMS)
   @ 01,25 ?? "("+VCU_SINAL_IPI+      ")IPI....."+Format("W14.5,EIC",VCU_IPI)
   @ 02,25 ?? "(+)ST......"+Format("W14.5,EIC",VCU_VR_Subst)
   @ 03,25 ?? "("+VCU_SINAL_COFINSCR+")"+TIT_COFINSCR+Format("W13.5,EIC",VCU_COFINSCR)
   @ 04,25 ?? "("+VCU_SINAL_COFINSDB+")"+TIT_COFINSDB+Format("W13.5,EIC",VCU_COFINSDB)
   @ 05,25 ?? "("+VCU_SINAL_ICMSCR  +")"+TIT_ICMSCR  +Format("W13.5,EIC",VCU_ICMSCR)
   @ 06,25 ?? "("+VCU_SINAL_ICMSDB  +")"+TIT_ICMSDB  +Format("W13.5,EIC",VCU_ICMSDB)
   @ 07,25 ?? "("+VCU_SINAL_PIS     +")"+TIT_PIS     +Format("W13.5,EIC",VCU_PIS)
              
   
   @ 00,52 ?? "(=)Custo."+Format("W14.5,EIC",VSD_I_Custo_Unitario)
   @ 01,52 ?? "(*)BC.PV."+Format("W14.5,EIC",VCU_BC_PrVenda)
   @ 02,52 ?? "(+)IC.PV."+Format("W14.5,EIC",VCU_VrIndice_PrVenda)
   @ 03,52 ?? "(=)Venda."+Format("W11.2,EIC",VSD_I_Preco_Venda)+"/"+VSD_I_UN_Item
   @ 04,52 ?? "         "+Format("W11.2,EIC",VSD_I_Preco_VendaI)+"/"+VSD_I_Unidade_Interna

   Accept @ 05,75 Width 1 "A1" picture "!" Tag "I_Flag2" to VSD_I_Flag2

   PaintCanvas Attribute 32  00, 00, 08, 77

   PaintCanvas Attribute 47  00, 12, 08, 23
   PaintCanvas Attribute 47  00, 37, 08, 50
   PaintCanvas Attribute 47  00, 61, 00, 77
   PaintCanvas Attribute 47  03, 61, 03, 77

EndDialog
Sw_Dialog="Item"
EndProc  ;  (DigitaI)

;*******************************************************************************
;* Rotina para Gravacao/Leitura do Item                                        *
;*******************************************************************************
Proc Registro_Item(Funcao_Registro)
Private Funcao_Registro
Switch
  Case Funcao_Registro="Ler":
       VSD_I_Seq                    =[Sequencia do Lancamento   ]
       VSD_I_OC                     =[Numero da OC              ]
       VSD_I_OC_Item                =[Item da OC                ]
       VSD_I_Tipo_Item              =[Tipo do Item              ]
       VSD_I_Codigo_Item            =[Codigo do Item            ]
       VSD_I_Descricao_Item         =[Descricao do Item         ]
       VSD_I_Tabela_Preco           =[Lookup_Tabela_Preco       ]
       VSD_I_Indice_PrVenda         =[Indice Preco de Venda     ]
       VSD_I_Quantidade             =[Quantidade                ]
       VSD_I_UN_Item                =[Unidade Entrada           ]
       VSD_I_Preco_Unitario         =[Preco Unitario            ]
       VSD_I_Taxa_Desconto_Unitario =[Taxa Desconto Unitario    ]
       VSD_I_Valor_Desconto_Unitario=[Valor Desconto Unitario   ]
       VSD_I_Preco_Total            =Round([Preco Total         ],2)
       VSD_I_Preco_Tabela           =[Preco Unitario Tabela     ]
       VSD_I_Custo_Unitario         =[Custo Medio Unitario      ]

       VSD_I_CST_Item               =[Tipo Tributacao           ]
       VSD_I_TICMS                  =[Tributacao de ICMS        ]
       VSD_I_RBC_ICMS               =[Aliquota Reduzida BCICMS  ]
       VSD_I_BC_ICMS                =[ICMS_BC                   ]
       VSD_I_AICMS                  =[Aliquota de ICMS          ]
       VSD_I_Valor_ICMS             =[Valor do ICMS             ]

       VSD_I_BC_ST                  =[ICMS_ST_BC                ]   
       VSD_I_AST                    =BlankNum()                       
       VSD_I_Valor_ST               =[ICMS_ST_Valor             ]   

       VSD_I_TIPI                   =[Tributacao de IPI         ]
       VSD_I_CST_IPI                =[IPI_CST                   ]  
       VSD_I_BC_IPI                 =[IPI_BC                    ]
       VSD_I_AIPI                   =[Aliquota de IPI           ]
       VSD_I_Valor_IPI              =[Valor do IPI              ]

       VSD_I_TISSQN                 =[Tributacao de ISSQN       ]
       VSD_I_BC_ISSQN               =[ISSQN_BC                  ]
       VSD_I_AISSQN                 =[Aliquota de ISSQN         ]
       VSD_I_Valor_ISSQN            =[Valor do ISSQN            ]
       VSD_I_CDSERV                 =[Codigo_Servico            ]   
       VSD_I_UFSERV                 =[ISSQN_UF                  ]   
       VSD_I_MUNSERV                =[ISSQN_Municipio           ]   

       VSD_I_CST_PIS                =[PIS_CST                   ]
       VSD_I_BC_PIS                 =[PIS_BC                    ]
       VSD_I_APIS                   =[PIS_Aliquota              ]
       VSD_I_Valor_PIS              =[PIS_Valor                 ]

       VSD_I_CST_COFINS             =[COFINS_CST                ]
       VSD_I_BC_COFINS              =[COFINS_BC                 ]
       VSD_I_ACOFINS                =[COFINS_Aliquota           ]
       VSD_I_Valor_COFINS           =[COFINS_Valor              ]

       VSD_I_CCusto                 =[Codigo CCusto             ]
       VSD_I_Descricao_CCusto       =[Descricao CCusto          ]
       VSD_I_Fator_Conversao        =[Fator de Conversao        ]
       VSD_I_Acrescimos_Desctos     =[Valor Acrescimos_Desctos  ]
       VSD_I_Preco_Venda            =[Preco_Venda               ]
       VSD_I_Preco_VendaOk          =[Preco_Venda               ]
       VSD_I_Unidade_Interna        =[Unidade Interna           ]
       VSD_I_Quantidade_Convertida  =[Quantidade Convertida     ]
       VSD_I_Codigo_Fornecedor      =[Codigo_Fornecedor         ]

       VSD_I_Flag                   =""
       VSD_I_Ct_Unitario            =BlankNum()
       VSD_I_Preco_VendaI           =BlankNum()

       VSD_I_NCM                    =[NCM                       ]
       VSD_I_CFOP                   =[CFO                       ]
       VSD_I_MVA                    =[MVA                       ]    
       VSD_I_CSOSN                  =SubStr([Tipo Tributacao],1,1)+[CSOSN]
       
       VSD_I_RAT_FRETE              = [Valor Frete Total        ]
       VSD_I_RAT_SEGURO             = [Valor Seguro Total       ]
       VSD_I_RAT_DESCONTO           = [Valor Desconto Total     ]
       VSD_I_RAT_OUTDESP            = [Valor OutDesp Total      ]
  
       Play "\\SISTEMAS\\ESTOQUE\\FT_CUSTO"
EndSwitch
EndProc  ; (Registro_Item)

;*******************************************************************************
;* Rotina para Confirmacao de Valores                                          *
;*******************************************************************************
Proc DIGITAF()
CHAVEF=0

Moveto "NotaEC"

Moveto "NotaECV"
SetaCorRegistro("Padrao")

Moveto "NotaEC"

Moveto [Tipo de Pagamento]
TPPAGTO=[Tipo de Pagamento]

While True

      TECLA=" Esc=Retornar  PgUp  PgDn=Finalizar  "
      MSGCAMPO=Field()
      Prompt TECLA

      Wait Field
           Message MSGCAMPO
           Until "ESC","PGDN","PGUP"
           
    Switch
      Case RetVal="PGUP":
           CHAVEF=9
           QuitLoop
      Case RetVal="PGDN":
           QuitLoop
      Case RetVal="ESC":
           CHAVEF=9
           QuitLoop
    EndSwitch
EndWhile
EndProc   ; (DigitaF)

;*******************************************************************************
;* Rotina para zerar variaveis usadas no Form                                  *
;*******************************************************************************
Proc Limpa_VarsForm()

VCITENS    =BlankNum()
VSQTDE     =BlankNum()
VSVRMERC   =BlankNum()

DIF_VRMERC =BlankNum()
DIF_VRSERV =BlankNum()
DIF_BCICMS =BlankNum()
DIF_ICMS   =BlankNum()
DIF_BCST   =BlankNum()
DIF_ST     =BlankNum()
DIF_IPI    =BlankNum()
DIF_NOTA   =BlankNum()

EndProc

;*******************************************************************************
;* Rotina para Carregar Parametros                                             *
;*******************************************************************************
Proc Carrega_Parametros()
Private Result,REG_P_ENTRA,REG_CONTA

Result=False
GetRecord "\\Sistemas\\Estoque\\P_Entra" [NotaEC->Codigo da Empresa] To REG_P_ENTRA
If Not RetVal
   Then Mensagem("Parametros n„o Cadastrado para esta Empresa !")
   Else PARAM_CONTA_CAIXA         = REG_P_ENTRA["Codigo_Conta_Caixa"       ]
        GetRecord "\\Sistemas\\Ctacor\\Conta" [NotaEC->Codigo da Empresa] To REG_CONTA
        If RetVal
           Then PARAM_DESC_CONTA_CAIXA    = REG_CONTA["Descricao da Conta"]
           Else PARAM_DESC_CONTA_CAIXA    = ""
        EndIf   
        
        PARAM_TABELA_PRECO        = REG_P_ENTRA["Tabela_Preco"             ]
        PARAM_ATTAB_PRECO         = REG_P_ENTRA["Atualiza_Tabela_Preco"    ]
        PARAM_CCE_SUBICMS         = REG_P_ENTRA["SubTrai_ICMS_CustoEntrada"]
        PARAM_BCPV_SUBICMS        = REG_P_ENTRA["SubTrai_ICMS_BC_PrVenda"  ]
        PARAM_RELATORIO           = REG_P_ENTRA["Script_Impressao"         ]
        PARAM_ATUA_CXBC           = REG_P_ENTRA["Atualiza_Caixa_Banco"     ]
        PARAM_ATUA_CAPG           = REG_P_ENTRA["Atualiza_Contas_Pagar"    ]
        PARAM_CALC_COFLUCR        = REG_P_ENTRA["Calcular_COFINS_Lucrativ" ]
        PARAM_TAXA_COFINS         = REG_P_ENTRA["Taxa_COFINS"              ]
        PARAM_TAXA_LUCRATIV       = REG_P_ENTRA["Taxa_Lucratividade"       ]
        PARAM_USA_COFINS_DBCR     = REG_P_ENTRA["Utiliza_COFINS_DBCR"      ]
        ;;PARAM_PECAS_ITEM          = REG_P_ENTRA["Controla_Pecas_Item"      ] nÆo usa mais
        PARAM_PECAS_ITEM          = "N"
        PARAM_CCE_SUBCOFINS       = REG_P_ENTRA["SubTrai_COFINS_CustoEntra"]
        PARAM_CCE_SUBPIS          = REG_P_ENTRA["SubTrai_PIS_CustoEntrada" ]
        PARAM_TAXA_PIS            = REG_P_ENTRA["Taxa_PIS"                 ]
        PARAM_ACRES_DIF_ALIQ_CUST = REG_P_ENTRA["Acresce_DifAliq_Custo"    ]
        
        Result=True
EndIf

Return Result

EndProc ; (Carrega_Parametros)

;*******************************************************************************
;* Rotina para Carregar Tabelas                                                *
;*******************************************************************************
Proc Monta_Tabelas()

View NOWINDOW "NotaEI"
View NOWINDOW "NotaECV"
View NOWINDOW "NotaEcp"

CoEdit "NotaEC"

;************** Rotina para tratar o erro de tabela locada ********************
If ErrorCode()=4
   Then Return False
EndIf

PickForm "1"
Moveto "NotaECV"                  SetHighLight Current Record 119 Field 119

Moveto "NotaEC"
PickForm "F"
Return True
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()

ImageRights ReadOnly
SetaCorRegistro("Paginar")

While FACA_DIGITAC
  DIGITAC()
  If CHAVEC=9 Then QuitLoop EndIf
  
  While FACA_CHAVEI
    While True
      CHAVEI()
      If CHAVEI=9 Then QuitLoop EndIf
    EndWhile
    While FACA_DIGITAF
      DIGITAF()
      If CHAVEF=0
         Then FACA_DIGITAC=False
              FACA_CHAVEI =False
      EndIf
      QuitLoop
    EndWhile
  EndWhile
EndWhile
If RecordStatus("Locked") Then UnLockRecord EndIf         
If OPCAO_FORM="F"
   Then PickForm "F"
EndIf
EndProc

;*******************************************************************************
;* Rotina Principal Notas de Entrada                                           *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bib_Proc"

If Not IsAssigNed(SYS_EMPRESA_DEFAULT) Then SYS_EMPRESA_DEFAULT="" EndIf
If Not IsAssigned(SYS_CONTROLE_DIA)    Then SYS_CONTROLE_DIA=""    EndIf
If Not IsAssigned(SYS_CODIGO_CLIENTE)  Then SYS_CODIGO_CLIENTE=""  EndIf

Limpa_VarsForm()

PARAM_FTCUSTO     = "NOTAEC"
PARAM_EXIBECUSTO  = "S"

If Not Monta_Tabelas()
   Then Do_It!
        ClearAll
        Release Vars All
        Release Procs All
        return
EndIf

If NRecords("NotaEC")>1
   Then Home
        If IsBlank([Numero do Lancamento])
           Then Echo Normal
                Echo Off
                MSG="Existe registro com Erro !// //"
                MSG=MSG+"Verificar numero lan‡amento em branco !// //"
                MSG=MSG+"Excluir o lan‡amento e verificar a origem do mesmo !"
                Mensagem(MSG)
        EndIf
EndIf

If Verifica_NRegistros("Notaei")
   Then COR_MENSAGEM="1"
        Mensagem(MSG_TABELA)
        Release Vars COR_MENSAGEM,MSG_TABELA
   Else Release Vars MSG_TABELA     
EndIf  

OPCAO_FORM="F"
While True
  CHAVEC()
  If CHAVEC=9 Then QuitLoop EndIf
  If CHAVEC=5
     Then If OPCAO_FORM="F"
             Then PickForm "F"
             Else PickForm "1"
          EndIf
          Loop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll

Release Procs CHAVEC,DIGITAC,CHAVEI,Item_Dialog,Inicia_Registro,DigitaI,Ver_CalcCusto,
              Registro_Item,DIGITAF,Limpa_VarsForm,Carrega_Parametros,Monta_Tabelas,
              Controla_EdicaoC
