;** RC_0171 ********************************************************************
;** Imprime relacao p/ Corte                               ** Venus    **      *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 09/04/2007 por Anivel Augusto Rodrigues
;Ultima Alteracao: 13/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 13/03/2015 por Luis Gustavo
;Motivo Alteracao: Nao imprimia na impressora, so em Video. Ajustado na rotina Principal

;*******************************************************************************
;* Rotina p/ Impressao                                                         *
;*******************************************************************************
Proc Imprimir()
AGUARDE("Aguarde, processando dados")

If Not IsAssigned(NOMEMPRESA) Then NOMEMPRESA="TESTE" EndIf


Sort "Answer" On "Numero_Corte","Descricao do Item","Codigo do Item"

View "CFACC"
View "CFACP"
View "Answer"

pag =0
c   =0
sqit=0
qtdetot=0
Scan
  If sqit=0
     Then obscort="Observacao: "
          Moveto "CFACC"
          Moveto [Numero_Corte]
          Locate NCORT
          If RetVal
             Then obscort=obscort+[Observacoes]
          EndIf

          NPEDs  ="Pedidos   : "
          Moveto "CFACP"
          Moveto [Numero_Corte]
          Locate NCORT
          If RetVal
             Then While [Numero_Corte]=NCORT
                    If NPEDs="Pedidos   : "
                       Then NPEDs=NPEDs+[Numero do Pedido]
                       Else NPEDs=NPEDs+", "+[Numero do Pedido]
                    EndIf
                    Skip
                    If EOT() Then QuitLoop EndIf
                  EndWhile
          EndIf

          Moveto "Answer"

          Cabecalho()
  EndIf

  sqit=sqit+1
  Item=[Codigo do Item   ]
  Ref =[Referencia       ]
  Unid=[Unidade Fiscal   ]
  Desc=[Descricao do Item]
  Qtde=[Quantidade Programada]
  Qtdetot=Qtdetot+[Quantidade Programada]

  Detalhe()

EndScan
Rodape("Fim")

ClearAll
EndProc   ;  (Imprimir)

;*******************************************************************************
;* Rotina p/ Cabecalho                                                         *
;*******************************************************************************
Proc ChecaLinhaPagina(nl)
private nl

If (C+nl)>=62
   Then Rodape("")
        Cabecalho()
EndIf
EndProc

;*******************************************************************************
;* Rotina p/ Cabecalho                                                         *
;*******************************************************************************
Proc Cabecalho()
While True
  pag=pag+1

  If OPCAOPAG<>"Todas"
     Then If pag<OPCAOPAGINI Or
             pag>OPCAOPAGFIM
             Then QuitLoop
          EndIf
  EndIf

  Print File ARQIMP ASP1+CI_10CPP+CI_6LPP+Fill("-",80)+ASP2+"\n"

  Print File ARQIMP ASP1+COMPLETA(NOMEMPRESA,41," ")+"Emissao.: "+
             AlinhM(LTrim(Format("D13",Today())),10)+"-"+
             AlinhM(SubStr(Time(),1,5),5)+"  Pag.:"+Format("W4,EIC",pag)+ASP2+"\n"

  Print File ARQIMP ASP1+Fill("-",80)+ASP2+"\n\n"

  Print File ARQIMP ASP1+Spaces(02)+"Programacao de Corte: "+CI_INEG+CI_IEXP+[Numero_Corte]+CI_FNEG+CI_FEXP
  Print File ARQIMP Spaces(11)+"Responsavel: ____________________"
  Print File ARQIMP ASP2+"\n\n"

  Print File ARQIMP ASP1+"Sq |Ref   |Item      |Descricao                     |    Qtde|Un                "+ASP2+"\n"
  Print File ARQIMP ASP1+"---|------|----------|------------------------------|--------|------------------"+ASP2+"\n"


  QuitLoop
EndWhile
C=8

EndProc   ; (Cabecalho)

;*******************************************************************************
;* Rotina p/ Detalhe                                                           *
;*******************************************************************************
Proc Detalhe()
ChecaLinhaPagina(2)

C=C+1
While True
  If OPCAOPAG<>"Todas"
     Then If pag<OPCAOPAGINI Or
             pag>OPCAOPAGFIM
             Then QuitLoop
          EndIf
  EndIf

  Print File ARQIMP ASP1+Format("W03,AR",StrVal(SqIt))
  Print File ARQIMP "|"+Format("W06,AL",ref)
  Print File ARQIMP "|"+Format("W10,AR",item)
  Print File ARQIMP "|"+Format("W30,AL",Desc)
  Print File ARQIMP "|"+Format("W6,EIC",Qtde)+Spaces(2)
  Print File ARQIMP "|"+Format("W02,AR",Unid)
  Print File ARQIMP "|"
  Print File ARQIMP ASP2+"\n"

  QuitLoop
EndWhile
EndProc    ;  (Detalhe)

;*******************************************************************************
;* Rotina p/ Total/Rodape                                                      *
;*******************************************************************************
Proc Rodape(op)
private op,i,p,wi,s,nl

If OPCAOPAG<>"Todas"
   Then If pag<OPCAOPAGINI Or
           pag>OPCAOPAGFIM
           Then Return
        EndIf
EndIf

If op=""
   Then Print File ARQIMP ASP1+"--------------------------------------------------------------------------------"+ASP2+"\n"
        Print File ARQIMP ASP1+Spaces(67)+"Continua =>  "+ASP2+"\n"
   Else
        ChecaLinhaPagina(3)
        Print File ARQIMP ASP1+"--------------------------------------------------------------------------------"+ASP2+"\n"
        Print File ARQIMP ASP1+CI_INEG+Spaces(41)+"Total: "+Format("W11,EIC",qtdetot)
        Print File ARQIMP Spaces(05)+Format("W04,EIC",sqit)+" Itens "+CI_FNEG+ASP2+"\n"
        Print File ARQIMP ASP1+"--------------------------------------------------------------------------------"+ASP2+"\n"

        For wi From 1 To 2
          Switch
            Case wi=1: s=obscort
            Case wi=2: s=NPEDs
          EndSwitch

          nl=Int(Len(s)/76)+1
          ChecaLinhaPagina(nl)
          p=1
          For i From 1 To nl
            Print File ARQIMP ASP1+CI_INEG+SubStr(s,p,76)+CI_FNEG+ASP2+"\n"
            p=p+76
          EndFor
        EndFor


EndIf
Print File ARQIMP ASP1+CI_10CPP+CI_6LPP+"\f"+ASP2+"\n"

EndProc    ;  (Total)

;*******************************************************************************
;* Rotina p/ Selecionar registros                                              *
;*******************************************************************************
Proc SelecionaB()
{Ask} {CFACRI} Check
Moveto [Numero_Corte] TypeIn "~NCORT"
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************

ASP1=""
ASP2=""
ARQIMP = DIREPORT+"RC_0171.TXT"
Switch
  Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
  Case OK="V": ASP1="\""
               ASP2="\""
EndSwitch
Deleta_Arquivo(ARQIMP)

AGUARDE("Imprimindo")
SelecionaB()
Imprimir()

If OK="V"
   Then If IsTable("Answer") Then Delete "Answer" EndIf
        {Tools} {Copy} {Table} {\\Base\\Sistemas\\Estoque\\Rm_tx160} {Answer} ClearAll
        {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
        {Answer} ClearAll
        OPCAOPAG="Todas"
        IMPRIREL("Answer","R")
        Deleta_Arquivo(ARQIMP)
   Else If OK="I"
           Then ;---- Imprime Arquivo Impressao ------------------------------------------------
                ImpriArq(ARQIMP)
        EndIf
        Mensagem("Impress„o Concluida !")
EndIf

Release Vars SqIt, Item, Qtde, Unid, Desc, Ref, qtdetot, NPEDs,obscort
Release Procs Imprimir, Cabecalho, Detalhe, Rodape, SelecaoB


