;** ConsPedG *******************************************************************
;*  Consulta de Pedido Grade                                                   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 11/01/2012 por Bruno Carvalho
;Ultima Alteracao: 19/01/2012 por Bruno Carvalho
;Motivo Altera‡Æo: Ajustes impressÆo e tela de informa‡äes do transportador 
;Ultima Alteracao: 20/07/2012 por Bruno Carvalho
;Motivo Altera‡Æo: Ajustado rotina para ser usado no consulta MES_FECHADO 
;Ultima Alteracao: 19/03/2013 por Bruno Carvalho
;Motivo Alteracao: Comentado a linha onde utilizava o campo/variavel "NBM"
;Ultima Alteracao: 06/04/2017 por Bruno Carvalho
;Motivo Alteracao: Ajustes Carrega_Parametros()
;Ultima Alteracao: 27/07/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes nÆo usar condi‡Æo pagamento por item
;Ultima Alteracao: 28/07/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes nÆo usar modelo no item 0003            
;Ultima Alteracao: 18/10/2017 por Bruno Carvalho
;Motivo Alteracao: Removido VSD_I_RBC_IPI=[Aliquota Reduzida BCIPI] -> Registro_Item()
;Ultima Alteracao: 23/10/2017 por Bruno Carvalho
;Motivo Alteracao: Ajustado na visualiza‡Æo do showdialog dos itens processo de desconto no item (Unit rio ou Total)
;Ultima Alteracao: 01/11/2017 por Bruno Carvalho
;Motivo Alteracao: Ajustes na impressÆo ao realiz -la pelo consulta (fechames) 
;Ultima Alteracao: 14/05/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Registro_Item("Ler")+CalcularPrecoTotal_Item()

;*******************************************************************************
;* Rotina para Controlar Chave do Pedido                                       *
;*******************************************************************************
Proc CHAVEC()
INICIO_PAGINA=True
FACA_DIGITAC =True

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

While True
  Moveto "PedidoC"
  End
  Moveto [Codigo da Empresa]
               
  If OPCAO_FORM="F"
     Then Down
     Else PgDn PgDn PgDn
  EndIf
  Moveto [Codigo da Empresa]

  SetaCorRegistro("Editar")
  While True

  MSGCAMPO=Field()
  
  If Field()="Codigo da Empresa" And Not IsBlank([Codigo da Empresa])
     Then MSGCAMPO="Empresa: "+[Nome Fantasia Empresa]
  EndIf
  
  TECLA="" 

  AguardaTeclasPadrao()

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

               FACA_DIGITAC =True
               VCITENS =BlankNum()
               VSQTDE  =BlankNum()
               VSVRMERC=BlankNum()

               If OPCAO_FORM="F"
                  Then PAGINA("PedidoC","MR")
                  Else PAGINA("PedidoC","MT")
               EndIf
               Switch
                 Case EDITAR="S":
                      PickForm "4"
                      Echo Normal
                      Echo Off
                      CHAVEC=0
                      Controla_EdicaoC()
                 Case EXCLUIR="S":
                      UnLockRecord
                      FastMsg("Fun‡Æo nÆo permitida!")                       
                 OtherWise: QuitLoop
               EndSwitch
               If OPCAO_FORM="3"
                  Then PickForm "4"
                  Else PickForm "F"
               EndIf
               SetaPosForm()
             EndWhile
             QuitLoop
        Case Retval="ESC":
             If Field()="Codigo da Empresa"
                Then CHAVEC=9
                     Del
                     Return
                Else Left
             EndIf
        Case Retval="ENTER":
             If IsBlank([])
                Then Loop
             EndIf
             If OPCAO_FORM="F"
                 Then PickForm "F"
             EndIf
             QuitLoop
        EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveC)

;*******************************************************************************
;* Rotina para Digitacao da Cabeca                                             *
;*******************************************************************************
Proc DigitaC()
FACA_CHAVEI   = True

Moveto "PedidoC"
Moveto Field ncampop1
While True

      MSGCAMPO=""
      TECLA=" Esc=Retornar  F6=Imprimir  PgDn "
      Prompt TECLA

      Wait Field
           Message MSGCAMPO
           Until "ESC","PGDN","F6","F8"

      Switch
      Case RetVal="PGDN":
           ncampop1=Field()
           QuitLoop
      Case Retval="ESC":
           CHAVEC=9
           QuitLoop
      Case Retval="F6":
           ncampo=Field()
           If IMPRIME()
              Then CHAVEC=9
                   QuitLoop
              Else Moveto Field ncampo
           EndIf           
      EndSwitch
EndWhile
EndProc  ;(DigitaC)

;*******************************************************************************
;* Rotina para Localizar Item                                                  *
;*******************************************************************************
Proc CHAVEI()

Moveto "PedidoG"
FACA_DIGITAF  = True
While True    
  Moveto [Preco Total   ]   VSVRMERC=ImageCSum()
  Moveto [Quantidade    ]   VSQTDE  =ImageCSum()
  Moveto [Item do Pedido]   VCITENS =ImageCCount()

  End
  NITEM = [Item do Pedido]
  Down Down
  Moveto [Item do Pedido]

  Opcao_Ver     = False
  SetaCorRegistro("Editar")
  While True                                      
                                  
    TECLA= " Esc=Retornar  F7=Paginar  F3=Ver ficha  PgDn/PgUp "
    Prompt TECLA
    Wait Field
         Until "ESC","F3","F7","HOME","UP","PGDN","PGUP"

    Switch
    Case RetVal="F3":
         Del
         Moveto [Item do Pedido]
         If RecordStatus("New")
            Then MENSAGEM("Sem Item p/ ver !")
                 QuitLoop
         EndIf      
         Opcao_Ver=True
         CHAVEI=0
         Home
         SEQIT=[Item do Pedido]
         Inicia_Registro()
         DigitaI()
         QuitLoop
    Case Retval="PGDN":         
         Del
         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":
         While True
           FACA_DIGITAF  = True

           Moveto [Preco Total   ]   VSVRMERC=ImageCSum()
           Moveto [Quantidade    ]   VSQTDE  =ImageCSum()
           Moveto [Item do Pedido]   VCITENS =ImageCCount()

           PAGINA("PedidoG","MR")
           SEQIT=[Item do Pedido]
           If EDITAR="S"
              Then CHAVEI=0
                   Controla_EdicaoI()
              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)

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

If INICIO_DIALOG
   Then Campo="I_Seq"
        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"] = -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 Up
                     EndIf
                Case Evento["KeyCode"] = -81:
                     If AtLast()
                        Then Mover_Reg=False Beep
                        Else Down Down
                     EndIf
              EndSwitch
              If Mover_Reg=True
                 Then SEQIT=[Item do Pedido]
                      Inicia_Registro()
              EndIf
          EndSwitch
     Case Evento["KeyCode"] = 9 Or
          Evento["KeyCode"] = -15:
          Temp=False
     Case Evento["KeyCode"] = 27:
          CHAVEI=9
          CancelDialog
   EndSwitch
EndIf

SelectControl Campo
If Opcao_Ver=True
   Then TECLA=" Esc=Retornar  Home/End/PgDn/PgUp=Paginar  "
   Else TECLA=" Esc=Retornar  "
EndIf   

MSGCAMPO=""

@ 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 Monta Aray Grade                                                     *
;*******************************************************************************
Proc Monta_Array_Grade(Tipo)
Private i,tm,ItemPesq,Itok,ContIt,Tipo

grdant="***"
For i From 1 to 12
  Array_TM[i]="F"
EndFor

ItemPesq=[Codigo do Item]

If [Codigo da Grade]<>grdant
   Then grdant=[Codigo da Grade]
        Moveto [Codigo da Grade]
        If Not Busca_Lookup("Codigo da Grade",[Codigo da Grade],"Item "+ItemPesq+" com defini‡„o de grade incorreta !//Verificar !")
           Then Return False
        EndIf
        
        Array_TM[1] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade1"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item1"]))
        Array_TM[2] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade2"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item2"]))
        Array_TM[3] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade3"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item3"]))
        Array_TM[4] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade4"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item4"]))
        Array_TM[5] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade5"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item5"]))
        Array_TM[6] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade6"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item6"]))
        Array_TM[7] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade7"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item7"]))
        Array_TM[8] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade8"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item8"]))
        Array_TM[9] ="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade9"] ,2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item9"]))
        Array_TM[10]="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade10"],2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item10"]))
        Array_TM[11]="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade11"],2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item11"]))
        Array_TM[12]="F"+Completa(REG_BUSCA_LOOKUP["Titulo Grade12"],2," ") +LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item12"]))
Endif

For i From 1 to 12
  Tm=LTrim(RTrim(SuBstr(Array_TM[i],4,2)))
  Switch
    Case Len(Tm)=1 : ItemPesq=SubStr([Codigo do Item],1,9) QuitLoop
    Case Len(Tm)=2 : ItemPesq=SubStr([Codigo do Item],1,8) QuitLoop
  EndSwitch
EndFor

Moveto [Codigo do Item]
Ativa_Lookup()
Moveto [Codigo do Item]
Locate Pattern ItemPesq+".."
If Not RetVal
   Then ItOk=False
   Else ContIt=0
        While True
          Switch
            Case Len(Tm)=1 : vr=SubStr([Codigo do Item],10,1)
            Case Len(Tm)=2 : vr=SubStr([Codigo do Item],09,2)
          EndSwitch
          If vr<>"0" And vr<>"00"
             Then If [Inativo]<>"S"
                     Then For i From 1 to 12
                            If LTrim(RTrim(SuBstr(Array_TM[i],4,2)))=vr
                               Then Array_TM[i]="T"+RTrim(SubStr(Array_TM[i],2,4))
                                    ContIt=ContIt+1
                                    QuitLoop
                            EndIf
                          EndFor
                  EndIf
          EndIf
          Skip
          If EOT()
             Then QuitLoop
          EndIf
          Locate Next Pattern ItemPesq+".."
          If Not Retval
             Then QuitLoop
          EndIf
        EndWhile
EndIf
KeyPress "ESC"
;Return ItOk
EndProc

;*******************************************************************************
;* Rotina para Digitacao Item                                                  *
;*******************************************************************************
Proc DigitaI()

ImageRights ReadOnly

  Showdialog "Dados do Item [      ]" Proc "Item_Dialog"
     Trigger "OPEN","ARRIVE"
      Key "F1","TAB","ReverseTab","ENTER","ESC","F4","F5","F6","ClearArea","HOME","END","PGDN","PGUP"
     @ 02,0 height 20 width 80

     Accept @ -01,43 Width 6 "A3" Tag "I_Seq" To VSD_I_SEQ
  
     Label  @ 00,01 "Codigo Item" For "I_Codigo_Item"
     Accept @ 01,01 Width 13 "A10" picture "!*!" Tag "I_Codigo_Item" to VSD_I_Codigo_Item
     Label  @ 00,15 "Descricao Item" For "I_Descricao_Item"
     Accept @ 01,15 Width 42 "A45" picture "!*!" Tag "I_Descricao_Item" to VSD_I_Descricao_Item
     Label  @ 00,58 "TI" For "I_Tipo_Item"
     Accept @ 01,58 Width 04 "A1" picture "&" Tag "I_Tipo_Item" to VSD_I_Tipo_Item
     ;Label  @ 00,63 "CF" For "I_CF_Item"
     ;Accept @ 01,63 Width 05 "A10" picture "!*!" Tag "I_CF_Item" to VSD_I_CF_Item
  
     Label  @ 02,02 "Cor/Tecido" For "I_Codigo_Cor_Tecido"
     Accept @ 03,07 Width 07 "A4" picture "!*!" Tag "I_Codigo_Cor_Tecido" to VSD_I_Codigo_Cor_Tecido
     Label  @ 02,15 "Descricao Cor/Tecido" For "I_Descricao_Cor_Tecido"
     Accept @ 03,15 Width 22 "A20" picture "!*!" Tag "I_Descricao_Cor_Tecido" to VSD_I_Descricao_Cor_Tecido
     Label  @ 02,37 "Tamanho" For "I_Tamanho"
     Accept @ 03,38 Width 06 "A2" picture "!*!" Tag "I_Tamanho" to VSD_I_Tamanho
  
     Label  @ 02,80 "Tabela" For "I_Tabela"
     Accept @ 03,86 Width 08 "A5" picture "!*!" Tag "I_Tabela" to VSD_I_Tabela
  
     @ 04,11 ?? SubStr(Array_TM[01],2,2)
     @ 04,16 ?? SubStr(Array_TM[02],2,2)
     @ 04,21 ?? SubStr(Array_TM[03],2,2)
     @ 04,26 ?? SubStr(Array_TM[04],2,2)
     @ 04,31 ?? SubStr(Array_TM[05],2,2)
     @ 04,36 ?? SubStr(Array_TM[06],2,2)
     @ 04,41 ?? SubStr(Array_TM[07],2,2)
     @ 04,46 ?? SubStr(Array_TM[08],2,2)
     @ 04,51 ?? SubStr(Array_TM[09],2,2)
     @ 04,56 ?? SubStr(Array_TM[10],2,2)
     @ 04,61 ?? SubStr(Array_TM[11],2,2)
     @ 04,66 ?? SubStr(Array_TM[12],2,2)
  
     @ 05,01 ?? "  Pedida"
     Accept @ 05,10 Width 05 "S" Tag "I_Qtd1"  to VSD_I_Qtd1
     Accept @ 05,15 Width 05 "S" Tag "I_Qtd2"  to VSD_I_Qtd2
     Accept @ 05,20 Width 05 "S" Tag "I_Qtd3"  to VSD_I_Qtd3
     Accept @ 05,25 Width 05 "S" Tag "I_Qtd4"  to VSD_I_Qtd4
     Accept @ 05,30 Width 05 "S" Tag "I_Qtd5"  to VSD_I_Qtd5
     Accept @ 05,35 Width 05 "S" Tag "I_Qtd6"  to VSD_I_Qtd6
     Accept @ 05,40 Width 05 "S" Tag "I_Qtd7"  to VSD_I_Qtd7
     Accept @ 05,45 Width 05 "S" Tag "I_Qtd8"  to VSD_I_Qtd8
     Accept @ 05,50 Width 05 "S" Tag "I_Qtd9"  to VSD_I_Qtd9
     Accept @ 05,55 Width 05 "S" Tag "I_Qtd10" to VSD_I_Qtd10
     Accept @ 05,60 Width 05 "S" Tag "I_Qtd11" to VSD_I_Qtd11
     Accept @ 05,65 Width 05 "S" Tag "I_Qtd12" to VSD_I_Qtd12
  
     Label  @ 04,71 "Total" For "I_Quantidade"
     Accept @ 05,71 Width 06 "S" Tag "I_Quantidade" to VSD_I_Quantidade
  
     @ 06,01 ?? "Faturada"
     Accept @ 06,10 Width 05 "S" Tag "I_FQtd1"  to VSD_I_FQtd1
     Accept @ 06,15 Width 05 "S" Tag "I_FQtd2"  to VSD_I_FQtd2
     Accept @ 06,20 Width 05 "S" Tag "I_FQtd3"  to VSD_I_FQtd3
     Accept @ 06,25 Width 05 "S" Tag "I_FQtd4"  to VSD_I_FQtd4
     Accept @ 06,30 Width 05 "S" Tag "I_FQtd5"  to VSD_I_FQtd5
     Accept @ 06,35 Width 05 "S" Tag "I_FQtd6"  to VSD_I_FQtd6
     Accept @ 06,40 Width 05 "S" Tag "I_FQtd7"  to VSD_I_FQtd7
     Accept @ 06,45 Width 05 "S" Tag "I_FQtd8"  to VSD_I_FQtd8
     Accept @ 06,50 Width 05 "S" Tag "I_FQtd9"  to VSD_I_FQtd9
     Accept @ 06,55 Width 05 "S" Tag "I_FQtd10" to VSD_I_FQtd10
     Accept @ 06,60 Width 05 "S" Tag "I_FQtd11" to VSD_I_FQtd11
     Accept @ 06,65 Width 05 "S" Tag "I_FQtd12" to VSD_I_FQtd12
  
     Accept @ 06,71 Width 06 "S" Tag "I_Quantidade_Faturada" to VSD_I_Quantidade_Faturada
  
     Label  @ 08,01 "Quantidade" For "I_Quantidade"
     Accept @ 09,01 Width 13 "N" picture "#*#.[###]" Tag "I_Quantidade" to VSD_I_Quantidade
     Label  @ 08,15 "UN" For "I_UN_Item"
     Accept @ 09,15 Width 05 "A2" picture "!*!" Tag "I_UN_Item" to VSD_I_UN_Item
     Label  @ 08,21 "Pr.Unitario" For "I_Preco_Unitario"
     Accept @ 09,21 Width 12 "N" picture "#*#.[####]" Tag "I_Preco_Unitario" to VSD_I_Preco_Unitario
  
     Label  @ 08,34 "?Desconto" For "I_Tipo_Desconto_Item"
     Accept @ 09,34 Width  4 "A1"  picture "U,T" Tag "I_Tipo_Desconto_Item" to VSD_I_Tipo_Desconto_Item
     Accept @ 09,37 Width 11 "A08" picture "!*!" Tag "I_Desconto_Descricao" to VSD_I_Desconto_Descricao
     Label  @ 08,45 "Vr.Desconto" For "I_Valor_Desconto_Item"
     Accept @ 09,47 Width 10 "$" picture "#*#.[##]" Tag "I_Valor_Desconto_Item" to VSD_I_Valor_Desconto_Item
     Label  @ 08,57 "Pr.Total" For "I_Preco_Total"
     Accept @ 09,58 Width 12 "$" picture "#*#.[##]" Tag "I_Preco_Total" to VSD_I_Preco_Total  
     
     Label  @ 10,01 "Qtd.Faturada" For "I_Quantidade_Faturada"
     Accept @ 11,01 Width 13 "N" picture "#*#.[###]" Tag "I_Quantidade_Faturada" to VSD_I_Quantidade_Faturada
     Label  @ 10,21 "Pr.Tabela" For "I_Preco_Tabela"
     Accept @ 11,21 Width 12 "$" picture "#*#.[##]" Tag "I_Preco_Tabela" to VSD_I_Preco_Tabela
     Label  @ 10,34 "% Comiss„o" For "I_Comissao_Item"
     Accept @ 11,34 Width 10 "N" picture "#*#.[###]" Tag "I_Comissao_Item" to VSD_I_Comissao_Item

     @ 12,01 ?? "Impostos CST   %Red.BC    Aliq    Valor"
  
     Label  @ 13,00 "    ICMS" For "I_CST_Item"
     Accept @ 13,10 Width 06 "A3" picture "!*!" Tag "I_CST_Item" to VSD_I_CST_Item   
     Accept @ 13,15 Width 11 "N" picture "#*#.[####]" Tag "I_RBC_ICMS" to VSD_I_RBC_ICMS
     Accept @ 13,26 Width 08 "N" picture "#*#.[##]" Tag "I_AICMS" to VSD_I_AICMS
     Accept @ 13,34 Width 11 "$" picture "#*#.[##]" Tag "I_Valor_ICMS" to VSD_I_Valor_ICMS
  
     Label  @ 14,00 "     IPI" For "I_CST_IPI"
     Accept @ 14,10 Width 06 "A2" picture "!*!" Tag "I_CST_IPI" to VSD_I_CST_IPI  
     Accept @ 14,15 Width 11 "A2" picture "#*#.[####]" Tag "I_RBC_IPI" to VSD_I_RBC_IPI
     Accept @ 14,26 Width 08 "N" picture "#*#.[##]" Tag "I_AIPI" to VSD_I_AIPI
     Accept @ 14,34 Width 11 "$" picture "#*#.[##]" Tag "I_Valor_IPI" to VSD_I_Valor_IPI
  
     Label  @ 12,47 "CFOP" For "I_CFOP"
     Accept @ 13,47 Width 08 "A4" picture "####" Tag "I_CFOP" to VSD_I_CFOP
     Label  @ 12,55 "CSOSN" For "I_CSOSN"
     Accept @ 13,55 Width 07 "A3" picture "###" Tag "I_CSOSN" to VSD_I_CSOSN
     Label  @ 12,62 "%MVA" For "I_MVA"
     Accept @ 13,62 Width 08 "N" picture "#*#.[##]" Tag "I_MVA" to VSD_I_MVA
     
     @ 14,47 ?? "Volumes/Unidade"
     Label  @ 15,46 "Quantidade...." For "I_Numero_Volumes"
     Accept @ 15,62 Width 12 "N" picture "#*#.[###]" Tag "I_Numero_Volumes" to VSD_I_Numero_Volumes
     Label  @ 16,46 "Peso Liquido.." For "I_Peso_Liquido"
     Accept @ 16,62 Width 12 "N" picture "#*#.[###]" Tag "I_Peso_Liquido" to VSD_I_Peso_Liquido
     Label  @ 17,46 "Peso Embalagem" For "I_Peso_Embalagem"
     Accept @ 17,62 Width 12 "N" picture "#*#.[###]" Tag "I_Peso_Embalagem" to VSD_I_Peso_Embalagem

Enddialog
Mouse Show
ImageRights
EndProc    ;  (DigitaI)

;*******************************************************************************
;* Rotina para Gravacao/Leitura do Item                                        *
;*******************************************************************************
Proc Registro_Item(Funcao_Registro)
Private Funcao_Registro
Switch
  Case Funcao_Registro="Ler":
       VSD_I_Seq                    =[Item do Pedido            ]

       VSD_I_Tipo_Item              =[Tipo do Item              ]
       VSD_I_Codigo_Item            =[Codigo do Item            ]
       VSD_I_Descricao_Item         =[Descricao do Item ]
       If CHAVEI=1
          Then VSD_I_Descricao_Item=TiraTamanhoDescricaoItem(VSD_I_Descricao_Item)
       EndIf
       ;VSD_I_CF_Item                =[NBM                       ]
       VSD_I_CST_Item               =[Tipo Tributacao           ]

       VSD_I_Codigo_Cor_Tecido      =[Codigo da Cor_Tecido      ]
       VSD_I_Descricao_Cor_Tecido   =[Descricao da Cor_Tecido   ]
       VSD_I_Tamanho                =[Tamanho                   ]

       VSD_I_Qtd1                   =[Qtd1                      ]
       VSD_I_Qtd2                   =[Qtd2                      ]
       VSD_I_Qtd3                   =[Qtd3                      ]
       VSD_I_Qtd4                   =[Qtd4                      ]
       VSD_I_Qtd5                   =[Qtd5                      ]
       VSD_I_Qtd6                   =[Qtd6                      ]
       VSD_I_Qtd7                   =[Qtd7                      ]
       VSD_I_Qtd8                   =[Qtd8                      ]
       VSD_I_Qtd9                   =[Qtd9                      ]
       VSD_I_Qtd10                  =[Qtd10                     ]
       VSD_I_Qtd11                  =[Qtd11                     ]
       VSD_I_Qtd12                  =[Qtd12                     ]
       VSD_I_Quantidade             =[Quantidade                ]

       VSD_I_FQtd1                  =[QtdF1                     ]
       VSD_I_FQtd2                  =[QtdF2                     ]
       VSD_I_FQtd3                  =[QtdF3                     ]
       VSD_I_FQtd4                  =[QtdF4                     ]
       VSD_I_FQtd5                  =[QtdF5                     ]
       VSD_I_FQtd6                  =[QtdF6                     ]
       VSD_I_FQtd7                  =[QtdF7                     ]
       VSD_I_FQtd8                  =[QtdF8                     ]
       VSD_I_FQtd9                  =[QtdF9                     ]
       VSD_I_FQtd10                 =[QtdF10                    ]
       VSD_I_FQtd11                 =[QtdF11                    ]
       VSD_I_FQtd12                 =[QtdF12                    ]
       VSD_I_Quantidade_Faturada    =[Quantidade Faturada       ]

       VSD_I_UN_Item                =[Unidade Fiscal            ]
       VSD_I_Preco_Unitario         =[Preco Unitario            ]
       VSD_I_Tipo_Desconto_Item     =[Tipo Desconto Item        ]
       VSD_I_Taxa_Desconto_Unitario =[Taxa Desconto Unitario    ]
       VSD_I_Valor_Desconto_Unitario=[Valor Desconto Unitario   ]
       VSD_I_Valor_Desconto_Item    =[Valor Desconto Item       ]
       VSD_I_Preco_Total            =[Preco Total               ]
       
       CalcularPrecoTotal_Item()

       VSD_I_Preco_Tabela           =[Preco Unitario Tabela     ]

       VSD_I_TICMS                  =[Tributacao de ICMS        ]
       VSD_I_RBC_ICMS               =[Aliquota Reduzida BCICMS  ]
       VSD_I_AICMS                  =[Aliquota de ICMS          ]
       VSD_I_Valor_ICMS             =[Valor do ICMS             ]

       VSD_I_TIPI                   =[Tributacao de IPI         ]
       VSD_I_AIPI                   =[Aliquota de IPI           ]
       VSD_I_Valor_IPI              =[Valor do IPI              ]

       VSD_I_Numero_Volumes         =[Volumes                   ]
       VSD_I_Peso_Liquido           =[Peso Liquido              ]
       VSD_I_Peso_Embalagem         =[Peso Embalagem            ]

       VSD_I_Comissao_Item          =[Comissao do Item          ]
       VSD_I_Tabela                 =[Codigo da Tabela          ]

       VSD_I_Referencia             =[Referencia                ]

       VSD_I_MVA                    =[MVA                       ]
       VSD_I_CFOP                   =[CFO                       ]

       VSD_I_CST_IPI                =[IPI_CST                   ]
       VSD_I_CSOSN                  =[CSOSN                     ]
       
EndSwitch
EndProc   ; (Registro_Item)

;*******************************************************************************
;* Rotina para Iniciar Registro                                                *
;*******************************************************************************
Proc Inicia_Registro()
INICIO_DIALOG=True
Moveto [Item do Pedido]
Locate [PedidoC->Numero do Pedido],SEQIT
Registro_Item("Ler")
Monta_Array_Grade("")
EndProc ; (Inicia_Registro)

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

FACA_DIGITAT=False

Moveto "PedidoC"
Moveto Field ncampop2
While True

      MSGCAMPO=""
      TECLA=" Esc=Retornar  F8=Vencimentos  PgDn/PgUp "
      Prompt TECLA

      Wait Field
           Message MSGCAMPO
           Until "ESC","PGDN","PGUP","F8"

    Switch
      Case RetVal="F8":
           Moveto "PedidoV"
           While True
               CHAVEV()
               If CHAVEV=9 Then QuitLoop EndIf
           EndWhile
           Moveto "PedidoC"
           Moveto Field ncampop2
      Case RetVal="PGUP":  
           QuitLoop
      Case RetVal="PGDN": 
           FACA_DIGITAT=True
           QuitLoop
      Case RetVal="ESC":
           QuitLoop
    EndSwitch
EndWhile
EndProc   ; (DigitaF)

;*******************************************************************************
;* Rotina para Confirmacao de Valores                                          *
;*******************************************************************************
Proc TRANSPORTADORA()
Private ncampo
CHAVET=0

Moveto "PedidoC"
Moveto [Codigo da Transportadora]
While True

      MSGCAMPO=""
      TECLA=" Esc=Retornar  F6=Imprimir  PgUp  PgDn=Finalizar"
      Prompt TECLA

      Wait Field
           Message MSGCAMPO
           Until "ESC","PGDN","PGUP","F6","F8"

    Switch
      Case RetVal="PGUP":
           CHAVET=9
           CHAVEF=3
           QuitLoop
      Case RetVal="PGDN":
           QuitLoop
      Case RetVal="ESC":
           CHAVET=9
           CHAVEF=3
           QuitLoop
      Case Retval="F6":
           ncampo=Field()
           If IMPRIME()
              Then CHAVEC=9
                   QuitLoop
              Else Moveto Field ncampo
           EndIf           
      EndSwitch
EndWhile
EndProc   ; (TRANSPORTADORA)

;*******************************************************************************
;* Rotina para Digitacao de Vencimentos                                        *
;*******************************************************************************
Proc ChaveV()
While True
  Moveto [Sequencia]
  End
  Down
  Moveto [Sequencia]

  SetaCorRegistro("Editar")
  While True

        TECLA= " Esc=Retornar  F7=Paginar  "

        Prompt TECLA

        Wait Field
             Until "ESC","ENTER","F7","HOME","UP","PGUP"

        Switch
        Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
             While True
               PAGINA("PedidoV","MR")
               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
        Case Retval="ESC":
             Del
             CHAVEV=9
             Return
        EndSwitch
  EndWhile
EndWhile
EndProc   ;  (ChaveV)

;*******************************************************************************
;* Rotina de Impressao                                                         *
;*******************************************************************************
Proc IMPRIME()
Private OPCAOC,OPCAON

Carrega_Parametros()          

Echo Normal
Echo Off
ShowPopup " Relatorio " CENTERED
  "~G~erencial" : " Imprime Modelo Gerencial"   :"Gerencial",
  "~E~xpedi‡„o" : " Imprime Modelo p/ Expedi‡„o":"Expedi‡„o",
  "~A~mbos"     : " Imprime Ambos os Modelos"   :"Ambos"
EndMenu
To Opcaoc
Echo off

If Not RetVal or Opcaoc="Esc"
   Then Return False
EndIf

CODPED=[PedidoC->Numero do Pedido]
UnLockRecord
Do_It!
SaveTables

Dispositivo()
If OK=""
   Then Moveto "PedidoC"
	      CoEditKey
        Return False
EndIf

For L From 1 To NUMCOPIA
  Switch
    Case Opcaoc="Gerencial" : Play "\\Sistemas\\Vendas\\"+PARAM_RELATORIO
    Case Opcaoc="Expedi‡„o" : Play "\\Sistemas\\Vendas\\"+PARAM_RELATORIO_S
    Case Opcaoc="Ambos"     : Play "\\Sistemas\\Vendas\\"+PARAM_RELATORIO
                              Play "\\Sistemas\\Vendas\\"+PARAM_RELATORIO_S  
  EndSwitch
EndFor   

Do_it!
ClearAll
View NoWindow "\\Sistemas\\Cadastro\\Destinat"
View NoWindow "P_Pedido"
Coedit "PedidoC"
PickForm "4"
Return True
EndProc

;*******************************************************************************
;* Rotina para Carregar Tabelas                                                *
;*******************************************************************************
Proc Closed Carrega_Tabelas()
UseVars AutoLib, ErrorProc, PARAM_CONSIGNACAO, PARAM_RELAT_NF

View NOWINDOW "PedidoI"
View NOWINDOW "PedidoV"

EndProc  ; (Carrega_Tabelas)

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoI()
INICIA_REGISTRO()
DIGITAI()
Moveto "PedidoG"
If RecordStatus("Locked") Then UnLockRecord EndIf         
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
ncampop1="Numero do Pedido"
ncampop2="Forma de pagamento"
ncampop3="Codigo da Transportadora"
FLAG4=False

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
      If CHAVEI=7 Then     Loop EndIf
      Controla_EdicaoI()
    EndWhile

    While FACA_DIGITAF
      DIGITAF()   
      ncampop2=Field()       

      While FACA_DIGITAT
        TRANSPORTADORA()  
        If CHAVET=9 Then QuitLoop EndIf
        If CHAVET=7 Then     Loop EndIf
        
        If CHAVET=0
           Then FACA_DIGITAC=False
                FACA_CHAVEI =False
        EndIf 
        QuitLoop
      EndWhile
      
      If CHAVEF=3
         Then Loop
         Else QuitLoop
      EndIf   
    EndWhile
  EndWhile
EndWhile
Moveto "PedidoC"
If RecordStatus("Locked") Then UnLockRecord EndIf         
If OPCAO_FORM="F"
   Then PickForm "F"
EndIf

SaveTables
EndProc

;*******************************************************************************
;* Rotina para Carregar Parametros                                             *
;*******************************************************************************
Proc Carrega_Parametros()

IMPRIME=True

If IsEmpty("P_Pedido")
   Then IMPRIME=False
        Return False
EndIf

Moveto "PedidoC"
PEDIDO=[PedidoC->Codigo da Empresa]
GetRecord "\\Sistemas\\Vendas\\P_Pedido" [PedidoC->Codigo da Empresa] To REG_PEDIDO
If RetVal
   Then PARAM_RELATORIO   = REG_PEDIDO["Script_ImpressaoG"]                        
        PARAM_RELATORIO_S = REG_PEDIDO["Script_ImpressaoG_S"]
   Else Moveto "PedidoC"
        Return False
EndIf
Return True

EndProc ; Carrega_Parametros()

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,"+
        "\\Sistemas\\Cadastro\\Bib_Show,"+
        "\\Sistemas\\Vendas\\Bib_Vend"

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

Carrega_Tabelas()

Array Array_TM[12]

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

CoEdit "PedidoC"
;************** Rotina para tratar o erro de tabela locada ********************
If ErrorCode()=4
   Then Do_It!
        ClearAll
        Release Vars All
        Release Procs All
        return
EndIf

PickForm "4"
Moveto "PedidoC"
PickForm "F"
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 "4"
          EndIf
          Loop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll

If MODO="FECHA_MES"
   Then Release Procs CHAVEC,CHAVEI,ChaveV,DigitaC,DigitaI,DIGITAF,Item_Dialog,Registro_Item,
                      Inicia_Registro,TRANSPORTADORA,IMPRIME,Carrega_Tabelas,Controla_EdicaoI,Controla_EdicaoC,
                      Carrega_Parametros 
   Else Release Vars All
        Release Procs All
EndIf  
