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

;*******************************************************************************
;* Rotina p/ Impressao                                                         *
;*******************************************************************************
Proc RM_1511A_Imprimir()
private qtddiferdescto
 
AGUARDE("Aguarde, processando dados")

Print File ARQIMP ASP1+CI_15CPP+ASP2+"\n"
Print File ARQIMP ASP1+CI_6LPP+ASP2+"\n"
If Not IsAssigned(NOMEMPRESA) Then NOMEMPRESA="" EndIf

Sort Tabela2 On "Codigo da Empresa", "Codigo", "Descricao do Item" To "Answer"
ClearAll

If VSDGOPACVR="S"
   then {Ask}{Answer} typein "Fast"
        moveto [Preco Unitario       ] typein "changeto blank"
        moveto [Saida_VrTotal        ] typein "changeto blank"
        moveto [Devolucao_VrTotal    ] typein "changeto blank"
        moveto [Retorno_VrTotal      ] typein "changeto blank"
        Do_it!
        ClearAll
endif   
    
View Tabela1
View "Answer"

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

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

          EMPANT =[Codigo da Empresa]
          CODCLI =[Codigo           ]
          
          Moveto Tabela1
          Moveto [Codigo da Empresa]
          Locate EMPANT, CODCLI
          
          RM_1511A_Cabecalho()
  EndIf
 
  QTDDIFER=[Saida_Qtd]-([Retorno_Qtd]+[Devolucao_Qtd])
  Switch
    Case QTDDIFER=0 : QTDDIFER=BlankNum() 
    OtherWise       : QTDDIFER=QTDDIFER*-1 
  EndSwitch
  
  If (VSDGOPITE="T") Or
     (VSDGOPITE="D" And not isblank(QTDDIFER))
     Then 
          RM_1511A_Detalhe()
          
          TIT_QTD  =TIT_QTD  +QTDDIFER
          TIT_SAIDA=TIT_SAIDA+[Saida_VrTotal        ]
          TIT_DEVOL=TIT_DEVOL+[Devolucao_VrTotal    ]
          TIT_RETOR=TIT_RETOR+[Retorno_VrTotal      ]
          
  EndIf

  TOT_SAIDA       = TOT_SAIDA      +[Saida_VrTotal          ]    TOT_QSAIDA      = TOT_QSAIDA     +[Saida_Qtd        ]
  
  TOT_DEVOL       = TOT_DEVOL      +[Devolucao_VrTotal      ]    TOT_QDEVOL      = TOT_QDEVOL     +[Devolucao_Qtd    ]
  
  TOT_RETOR       = TOT_RETOR      +[Retorno_VrTotal        ]    TOT_QRETOR      = TOT_QRETOR     +[Retorno_Qtd      ]

  Moveto "Answer"
  
EndScan
RM_1511A_TotalItem()
RM_1511A_TotalFinanc()

Do_It!
ClearAll

return true

EndProc   ;  (Imprimir)

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

If lin>=MaxLin
   Then While True
          If OPCAOPAG<>"Todas"
             Then If pag<OPCAOPAGINI Or
                     pag>OPCAOPAGFIM
                     Then QuitLoop
                  EndIf
          EndIf
          Print File ARQIMP ASP1+"\f"+ASP2+"\n"
          QuitLoop
        EndWhile
        RM_1511A_Cabecalho()

EndIf
EndProc

;*******************************************************************************
;* Rotina p/ Cabecalho                                                         *
;*******************************************************************************
Proc RM_1511A_Cabecalho()
While True
  pag=pag+1
  lin=11
  If OPCAOPAG<>"Todas"
     Then If pag<OPCAOPAGINI Or
             pag>OPCAOPAGFIM
             Then QuitLoop
          EndIf
  EndIf

  Moveto Tabela1
  Print File ARQIMP ASP1+Fill("-",137)+ASP2+"\n"

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

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

  Print File ARQIMP ASP1+Format("W137,AC","Diferenca de Industrializacao X Remessas X Devolucoes X Retornos")+ASP2+"\n\n"

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

  Print File ARQIMP ASP1+COMPLETA("Empresa: "+[Codigo da Empresa]+" "+[Nome Fantasia Empresa],57," ")      ;40 -> 57
  Print File ARQIMP      "Emissao: "+Format("D13",[Data de Emissao])+"          Itens: "+VSDGDESCOPITE+ASP2+"\n"

  If Not IMPRIME_APURACAO Then 
  Print File ARQIMP ASP1+"                                                                                            _____________ Valores __________"+ASP2+"\n"                                                                                                      
  Print File ARQIMP ASP1+"Produto                                   UN  Vr.Unit Remessa Devoluc   Retorno %     Difer    Remessa  Devolucao    Retorno"+ASP2+"\n"
  Print File ARQIMP ASP1+"----------------------------------------- -- -------- ------- ------- ------------- ------- ---------- ---------- ----------"+ASP2+"\n"
                         ;1234567890 123456789012345678901234567890 XX 9.999,99 1234567 1234567 1234567 123,1 1234567 999.999,99 999.999,99 999.999,99
  lin = 8 
  EndIf                       
                                                                                                            
  Moveto "Answer"

  QuitLoop
EndWhile


EndProc   ; (Cabecalho)

;*******************************************************************************
;* Rotina p/ Detalhe                                                           *
;*******************************************************************************
Proc RM_1511A_Detalhe()
RM_1511A_ChecaLinhaPagina(58)
While True
  If OPCAOPAG<>"Todas"
     Then If pag<OPCAOPAGINI Or
             pag>OPCAOPAGFIM
             Then QuitLoop
          EndIf
  EndIf

  Print File ARQIMP ASP1+COMPLETA([Codigo do Item],11," ")
  Print File ARQIMP COMPLETA([Descricao do Item  ],30," ")+Spaces(1)
  Print File ARQIMP COMPLETA([Unidade Fiscal     ],02," ")+Spaces(1)
  Print File ARQIMP Format("W8.2,EIC",[Preco Unitario ])+Spaces(01)
  Print File ARQIMP Format("W7,AR",[Saida_Qtd    ])+Spaces(01)
  Print File ARQIMP Format("W7,AR",[Devolucao_Qtd])+Spaces(01)
  Print File ARQIMP Format("W7,AR",[Retorno_Qtd  ])
  Print File ARQIMP Format("W6.1,EIC",([Retorno_Qtd]/([Saida_Qtd]-[Devolucao_Qtd]))*100)+Spaces(01)
  Print File ARQIMP Format("W7,AR,S+",QTDDIFER)
  Print File ARQIMP Format("W11.2,EIC",[Saida_VrTotal    ])
  Print File ARQIMP Format("W11.2,EIC",[Devolucao_VrTotal])
  Print File ARQIMP Format("W11.2,EIC",[Retorno_VrTotal  ])  
  Print File ARQIMP ASP2+"\n"
  QuitLoop
EndWhile
lin=lin+1                                 
EndProc    ;  (Detalhe)

;*******************************************************************************
;* Rotina p/ SubTotal                                                          *
;*******************************************************************************
Proc RM_1511A_TotalItem()
While pag>0
  RM_1511A_ChecaLinhaPagina(58)
  lin=lin+2
  
  If OPCAOPAG<>"Todas"
     Then If pag<OPCAOPAGINI Or
             pag>OPCAOPAGFIM
             Then QuitLoop
          EndIf
  EndIf
                                            
  Print File ARQIMP ASP1+Format("W124,AR","----- ---------- ---------- ----------")+ASP2+"\n"
  Print File ARQIMP ASP1+CI_INEG+Format("W84,AR","Total...")
  Print File ARQIMP      Format("W7,AR",TIT_QTD)
  Print File ARQIMP      Format("W11.2,EIC",TIT_SAIDA)
  Print File ARQIMP      Format("W11.2,EIC",TIT_DEVOL)
  Print File ARQIMP      Format("W11.2,EIC",TIT_RETOR)+CI_FNEG+ASP2+"\n"
  QuitLoop
EndWhile 

EndProc    ;  (Total)                

;*******************************************************************************
;* Rotina de Impressao da rela‡Æo de doctos                                    *
;*******************************************************************************
proc RM_1511A_RelacaoDoctos(s)
private TamObs, nlobs, i, s, s2

TamObs=137

nlobs=Len(s)/tamobs
If nlobs-Int(nlobs)>0
   Then nlobs=Int(nlobs)+1
   Else nlobs=Int(nlobs)
EndIf

posobs=1
For i from 1 to nlobs
  Print File ARQIMP ASP1+SubStr(s,posobs,tamobs)+ASP2+"\n"
  posobs=posobs+tamobs
EndFor

endproc

;*******************************************************************************
;* Rotina p/ Total                                                             *
;*******************************************************************************
Proc RM_1511A_TotalFinanc()  
Private CONT, TIPO, TOTAL, CONTROLA_TOTAL
  
While pag>0
  RM_1511A_ChecaLinhaPagina(40)

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

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

  If Not IsBlank(RELDOCTO_SAIDA) Then RM_1511A_RelacaoDoctos("Notas de Remessa  : "+RELDOCTO_SAIDA) EndIf
  If Not IsBlank(RELDOCTO_DEVOL) Then RM_1511A_RelacaoDoctos("Notas de Devolucao: "+RELDOCTO_DEVOL) EndIf
  If Not IsBlank(RELDOCTO_RETOR) Then RM_1511A_RelacaoDoctos("Notas de Retorno  : "+RELDOCTO_RETOR) EndIf
  
  Print File ARQIMP ASP1+Fill("-",137)+ASP2+"\n"

  Print File ARQIMP ASP1+CI_INEG+"Diferen‡a "+CI_FNEG+"(Remessas-Devolucoes-Retornos):"+
                         CI_INEG+Format("W13,EIC",TOT_QSAIDA-TOT_QDEVOL-TOT_QRETOR)+
                         Format("W12.2,EIC",TOT_SAIDA-TOT_DEVOL-TOT_RETOR)+CI_FNEG+ASP2+"\n"
                          
  Moveto Tabela1
  Print File ARQIMP ASP1+ASP2+"\n"
  Print File ARQIMP ASP1+"Recebemos de "+[Razao Social]+" no dia ___/___/___"+ASP2+"\n"
  Moveto "Answer"

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

  Print File ARQIMP ASP1+"\f"+ASP2+"\n"

  QuitLoop
EndWhile

TOT_SAIDA       = 0    

TOT_RETOR       = 0 

TOT_DEVOL       = 0

TOT_QSAIDA      = 0   
TOT_QRETOR      = 0   
TOT_QDEVOL      = 0

TIT_QTD         = 0    
TIT_SAIDA       = 0    
TIT_RETOR       = 0    
TIT_DEVOL       = 0

EndProc    ;  (TotalFinanc)


;*******************************************************************************
;* Monta Relat¢rio p/ ImpressÆo do Consignado                                  * 
;*******************************************************************************
Proc RM_1511A_Monta_Relatorio()
private result             

DISPOSITIVO()
If OK=""
    Then Return False
EndIf

ASP1=""
ASP2=""
ARQIMP = DIREPORT+"ACERTO.TXT"
Switch
  Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
  Case OK="V": ASP1="\""
               ASP2="\""
EndSwitch
If IsFile(ARQIMP)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo")

result=Gera_Dados()
if result
   Then
        RM_1511A_Imprimir()
        
        If OK="V"
          Then If IsTable("Answer") Then Delete "Answer" EndIf
          
               {Tools} {Copy} {Table} {\\Base\\Sistemas\\Estoque\\Rm_tx250} {Answer} ClearAll
               {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
               {Answer} ClearAll
        
               OPCAOPAG="Todas"
               IMPRIREL("Answer","1")
        
               Deleta_Arquivo(ARQIMP)
               FastMsg("Impress„o Concluida !")
                 
          Else If OK="I"
                  Then 
                       ImpriArq(ARQIMP)
                       FastMsg("Impress„o Concluida !")
               EndIf
        EndIf
EndIf

DeletaTabelas()
        
Return result

EndProc ;Monta_Relatorio()


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

Retorno_ok=true

Retorno_ok=RM_1511A_Monta_Relatorio()

Release Vars  ASP1,ASP2,ARQIMP,OPCAOPAG,OPCAOPAGINI,OPCAOPAGFIM,
              EMPANT,CODCLI,pag,lin,LINHA_AP

Release Procs RM_1511A_Imprimir, RM_1511A_ChecaLinhaPagina, RM_1511A_Cabecalho, RM_1511A_Detalhe,
              RM_1511A_TotalItem, RM_1511A_TotalFinanc, RM_1511A_RelacaoDoctos  
