;** Rm_1511P *******************************************************************
;* Acerto Remessa/Industrializacao - Retornos Pendentes                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/07/2020 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao: 

;*******************************************************************************
;* Rotina p/ Impressao                                                         *
;*******************************************************************************
Proc RM_1511P_Imprimir()
 
Sort Tabela2 On "Codigo da Empresa", "Codigo", "Descricao do Item" To "Answer"
ClearAll

View Tabela1
View "Answer"

EMPANT  = "****"
CODCLI  = "****"

pag=0                 
lin=0
Scan
  
  If [Codigo da Empresa]<>EMPANT  Or
     [Codigo           ]<>CODCLI
     Then
          RM_1511P_TotalItem()
          RM_1511P_TotalFinanc()

          EMPANT =[Codigo da Empresa]
          CODCLI =[Codigo           ]
          
          Moveto Tabela1
          Moveto [Codigo da Empresa]
          Locate EMPANT, CODCLI
          
          RM_1511P_Cabecalho()
  EndIf
 
  QTDDIFER=[Saida_Qtd]-([Retorno_Qtd]+[Devolucao_Qtd])
  if QTDDIFER>0
     then
          RM_1511P_Detalhe()
          
          TIT_QTD  =TIT_QTD  +QTDDIFER
          TIT_VRT  =TIT_VRT  +(QTDDIFER*[Preco Unitario])
  EndIf

EndScan
RM_1511P_TotalItem()
RM_1511P_TotalFinanc()

Do_It!
ClearAll

return true

EndProc   ;  (Imprimir)

;*******************************************************************************
;* Rotina checagem n. de linhas                                                *
;*******************************************************************************
Proc RM_1511P_ChecaLinhaPagina(MaxLin)
Private MaxLin

If lin>=MaxLin
   Then 
        Print File ARQIMP "\f"
        RM_1511P_Cabecalho()

EndIf
EndProc

;*******************************************************************************
;* Rotina p/ Cabecalho                                                         *
;*******************************************************************************
Proc RM_1511P_Cabecalho()

pag=pag+1

Moveto Tabela1

If Not IsAssigned(NOMEMPRESA) Then NOMEMPRESA="" EndIf
  
Print File ARQIMP CI_10CPP+"\n"
Print File ARQIMP CI_6LPP+"\n"
  
Print File ARQIMP Fill("-",80)+"\n"

Print File ARQIMP COMPLETA(NOMEMPRESA,40," ")+"Emissao.: "+
           AlinhM(LTrim(Format("D13",Today())),10)+"-"+
           AlinhM(LTrim(Time()),8)+"  Pag.:"+Format("W4,EIC",pag)+"\n"

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

Print File ARQIMP Format("W80,AC","Retorno Pendente")+"\n\n"

Print File ARQIMP "Pessoa : "+[Codigo]+" "+[Razao Social]+"\n"

Print File ARQIMP COMPLETA("Empresa: "+[Codigo da Empresa]+" "+[Nome Fantasia Empresa],61," ")      
Print File ARQIMP "Emissao: "+Format("D13",[Data de Emissao])+"\n\n"

Print File ARQIMP "Produto                                            UN  Vr.Unit   Qtde    VrTotal"+"\n"
Print File ARQIMP "----------------------------------------- -------- -- -------- ------ ----------"+"\n"
                       ;1234567890 123456789012345678901234567890 XX 9.999,99 1234567 123,1 999.999,99
lin = 11 
                                                                                                            
Moveto "Answer"

EndProc   ; (Cabecalho)

;*******************************************************************************
;* Rotina p/ Detalhe                                                           *
;*******************************************************************************
Proc RM_1511P_Detalhe()

RM_1511P_ChecaLinhaPagina(58)

Print File ARQIMP COMPLETA([Codigo do Item     ],11," ")
Print File ARQIMP COMPLETA([Descricao do Item  ],31," ")
Print File ARQIMP COMPLETA([NCM                ],09," ")
Print File ARQIMP COMPLETA([Unidade Fiscal     ],03," ")
Print File ARQIMP Format("W8.2,EIC",[Preco Unitario ])
Print File ARQIMP Format("W7,AR",QTDDIFER)
Print File ARQIMP Format("W11.2,EIC",(QTDDIFER*[Preco Unitario]))  
Print File ARQIMP "\n"

lin=lin+1                                 
EndProc    ;  (Detalhe)

;*******************************************************************************
;* Rotina p/ SubTotal                                                          *
;*******************************************************************************
Proc RM_1511P_TotalItem()

if pag>0
   then
        RM_1511P_ChecaLinhaPagina(58)
        lin=lin+2
                                                  
        Print File ARQIMP Format("W80,AR","------ ---------")+"\n"
        Print File ARQIMP CI_INEG+Format("W80,AR","Total..."+
                                  Format("W7,AR",TIT_QTD)+
                                  Format("W11.2,EIC",TIT_VRT))+CI_FNEG+"\n"
endif
EndProc    ;  (Total)                

;*******************************************************************************
;* Rotina de Impressao da rela‡Æo de doctos                                    *
;*******************************************************************************
proc RM_1511P_RelacaoDoctos()
private s

Print File ARQIMP Fill("-",80)+"\n"
Print File ARQIMP CI_INEG+"Documentos da Remessa para Referenciar:"+CI_FNEG+"\n"
lin=lin+2                                 

Moveto Tabela1
scan

  s=[Tipo do Documento  ]+"/"+[Numero do Documento]+"-"+[Serie do Documento]+", "+format("D13",[Data de Emissao])
  if not isblank([Chave_Acesso_Nfe])
     then s=s+", "+[Chave_Acesso_Nfe] 
  endif

  RM_1511P_ChecaLinhaPagina(58)

  Print File ARQIMP s+"\n"
  lin=lin+1                                 

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

Moveto "Answer"

endproc

;*******************************************************************************
;* Rotina p/ Total                                                             *
;*******************************************************************************
Proc RM_1511P_TotalFinanc()  

if pag>0
   then
        RM_1511P_RelacaoDoctos()
        Print File ARQIMP "\f"
endif

TIT_QTD         = 0    
TIT_VRT         = 0    

EndProc    ;  (TotalFinanc)


;*******************************************************************************
;* Monta Relat¢rio p/ ImpressÆo do Consignado                                  * 
;*******************************************************************************
Proc RM_1511P_Monta_Relatorio()
private result             
 
DISPOSITIVO()
If OK=""
    Then Return False
EndIf


ArqImp=PrivDir()+"Rm_1511P.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo")

result=Gera_Dados()
if result
   Then
        RM_1511P_Imprimir()
        
        ImpriArq(ARQIMP)
EndIf

DeletaTabelas()
        
Return result

EndProc ;Monta_Relatorio()


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

Retorno_ok=true

Retorno_ok=RM_1511P_Monta_Relatorio()

Release Vars  ARQIMP,EMPANT,CODCLI,pag,lin

Release Procs RM_1511P_Imprimir, RM_1511P_ChecaLinhaPagina, RM_1511P_Cabecalho, RM_1511P_Detalhe,
              RM_1511P_TotalItem, RM_1511P_TotalFinanc, RM_1511P_RelacaoDoctos  
