//Faza 1: hMARFA 8.9A1 - Inceput an 2017 - declaratii si noutati tehnice

// ----------------------------------------------------------------
// ----------------------------------------------------------------
// in loc de F_.TXT

// titlu meniu de operatii
MenuTitle   := "Operatii upgrade hMARFA/hMARFT"

// Algoritmul de calcul al numarului de regim special si                    
// al numarului de document urmator:                                        
// -Algoritmul nou, bazat pe Numere de interval si                         
//  modul lor de validare la completarea documentelor de iesire             
//   -.F. -atentionare, daca nu satisface criteriile din Numerotare documente
//         este valoarea implicita                                          
//   -.T. -eroare                                                           
// -Algoritmul vechi                                                        
//   -NIL -algoritmul vechi bazat pe comenzile generale NRD_MV              
VAL_NRDOC := NIL                                                            

// Procedura upgrade se executa in regim normal, cu afisarea textelor
// si a ferestrelor ajutatoare.
lModSilentios := .F.

// pt. vizibilitate intre faze:
// - variabile
x_aplrecno  := 0
x_arrecno   := 0
x_versiune  := {}
lEroare     := .F.
sMesaj      := ""
x_path      := h_FilePathes[fil_file]
DataMarfa   := CtoD("")

x_open      := {}
slogf       := h_AddPath("UMV89A"+h_StatId()+".TXT",h_FilePathes[fil_tmp])

sDBName   := h_FileName(h_CutSl(h_FilePathes[fil_file]))

fil_cnts  := IIF(Type("fil_cnts") ="N", fil_cnts, nil)
fil_tjur  := IIF(Type("fil_tjur") ="N", fil_tjur, nil)

fil_p1    := IIF(Type("fil_p1")   ="N", fil_p1,   nil)
fil_n_    := IIF(Type("fil_n_")   ="N", fil_n_,   nil)
fil_n1    := IIF(Type("fil_n1")   ="N", fil_n1,   nil)
fil_n2    := IIF(Type("fil_n2")   ="N", fil_n2,   nil)
fil_s3    := IIF(Type("fil_s3")   ="N", fil_s3,   nil)
fil_x2    := IIF(Type("fil_x2")   ="N", fil_x2,   nil)
fil_t1    := IIF(Type("fil_t2")   ="N", fil_t2,   nil)
fil_t2    := IIF(Type("fil_t2")   ="N", fil_t2,   nil)
fil_t4    := IIF(Type("fil_t4")   ="N", fil_t4,   nil)
fil_t6    := IIF(Type("fil_t6")   ="N", fil_t6,   nil)
fil_t7    := IIF(Type("fil_t7")   ="N", fil_t7,   nil)
fil_e7    := IIF(Type("fil_e7")   ="N", fil_e7,   nil)
fil_e7p   := IIF(Type("fil_e7p")  ="N", fil_e7p,  nil)
fil_e3p   := IIF(Type("fil_e3p")  ="N", fil_e3p,  nil)
fil_e4p   := IIF(Type("fil_e4p")  ="N", fil_e4p,  nil)

// ----------------------------------------------------------------
// ----------------------------------------------------------------
h_ApplC0 := h_ApplCode
sVM      := ""
sVt      := ""
lEroare  := .F.

x_open := {}

// verificare versiune hMARFA
h_ApplCode := "M"
x_versiune := {"V 8.8C1","V 8.8C3","V 8.8C4","V 8.8C5","V 8.8C6","V 8.8D1"}
#DO F_VERS.TXT
#IF lEroare
    h_MesErr(IIF(apl->V="V 8.9A1","Operatia de upgrade a fost deja executata!",  ;
             IIF(apl->V="V 8.8Cx","Operatia de upgrade a fost intrerupta!",sMesaj)), 0)
    Abandon("C")
#ENDIF
// returneaza lEroare
aref_MOPT   := aref->OPT
aref_MV     := aref->V
aref_MEXE   := aref->EXE_FILE
aref_MNRORD := aref->NRORD
sVM         := apl->V
DbCloseAll()

// verificare versiune hMARFT
h_ApplCode := "t"
x_versiune := {"V 4.4C1","V 4.4C3","V 4.4C4","V 4.4C5","V 4.4C6","V 4.4D1","V 4.5A1"}
#DO F_VERS.TXT
#IF lEroare
    h_MesErr(IIF(apl->V="V 4.5D1","Operatia de upgrade a fost deja executata!",  ;
             IIF(apl->V="V 4.4Cx","Operatia de upgrade a fost intrerupta!",sMesaj)), 0)
    Abandon("C")
#ENDIF
// returneaza lEroare
aref_tOPT   := aref->OPT
aref_tV     := aref->V
aref_tEXE   := aref->EXE_FILE
aref_tNRORD := aref->NRORD
sVt         := apl->V
DbCloseAll()
h_ApplCode := h_ApplC0

#IF sVM[7]$"x" .OR. sVt[7]$"x"
    h_MesErr("Faza 1 a fost deja executata/lansata !", 0)
    Abandon("C")
#ENDIF
DbCloseAll()

// -------------------------------------------------------------------------
xx_scr := SaveScreen(0, 0, MaxRow(), MaxCol())

// -------------------------------------------------------------------------
// Test BD accesata de pe alta statie de lucru?
// HMARFA
a_open := {"nomm","tran","lotm","fac","facr","inc","icm"}
#IF !h_UseAlias(a_open)
    h_MesErr("BD accesata de pe alta statie de lucru. Operatia de upgrade nu poate fi executata!",0)
    Abandon("C")
#ENDIF
h_CloseAlias(a_open)
// HMARFT
fobj      := h_ExtUse():new(h_FilePathes[fil_file], "t", "R" )
fobj:afis := {{"stat","statt"}}
#IF !fobj:open()
    h_MesErr("Eroare deschidere fisiere jurnale TVA !",0)
    Abandon("C")
#ENDIF
fobj:close()

// -------------------------------------------------------------------------
// Test jurnale TVA si D394T vide?

fobj      := h_ExtUse():new(h_FilePathes[fil_file], "t", "R" )
fobj:afis := {"t1","t2","e7",{"stat","statt"},"l_"}
#IF !fobj:open()
    h_MesErr("Eroare deschidere fisiere jurnale TVA !",0)
    Abandon("C")
#ENDIF
#IF !(Empty(statt->DATA) .OR. h_DtoC(statt->DATA,"aaaa") >= "2017")
    h_MesErr("Data curenta din baza de date hMARFT < 2017/01/01 !",0)
    Abandon("C")
#ENDIF
 
#IF sVM = "V 8.8C1" .OR. sVM = "V 8.8C3" .OR. sVM = "V 8.8C4"
    aMes   := {}
    IIf(t1->(!Eof()),(AAdd(aMes," -Jurnalul de vanzari")),   nil)
    IIf(t2->(!Eof()),(AAdd(aMes," -Jurnalul de cumparari")), nil)
    IIf(e7->(!Eof()),(AAdd(aMes," -Declaratia D394T")),             nil)
    
    #IF !Empty(aMes)
    
        sPerioada := "???"
        DbSelectArea("l_")
        __dbLocate( {|| l_->SL_08=statt->DATA},,,, .F. )
        IIf(Found(), (sPerioada := l_->SL_00), NIL)
    
        AIns (aMes,1,"Exista date extrase in:",.T.)
        AIns (aMes,1,"",.T.)
        AIns (aMes,1,"Perioada curenta hMARFT: "+sPerioada,.T.)
        AIns (aMes,1,"Baza de date: "+sDBName,.T.)
        AIns (aMes,1,"",.T.)
        AAdd (aMes, "")
        AAdd (aMes, "Prin operatia de update aceste date vor fi sterse.")
        AAdd (aMes, "")
        AAdd (aMes, "Continuati ?")
        
        aMesOpt :=  {aMes, {" Continuare ", " Abandon "}}
        
        wMes :=  h_NewOptions("WO",                                           ;
                ,,,,                                                          ;
                NIL,"WARNING", aMesOpt,{1})
        
        #IF !h_PopUpWindow (wMes) .OR. wMes[13,1] != 1
            h_MesErr("Operatia de update a fost abandonata!",0)
            Abandon("C")
        #ENDIF
    
    #ENDIF
#ENDIF
fobj:close()

// -------------------------------------------------------------------------
h_OpenOut(slogf)
h_PutLine("Upgrade hMARFA V 8.9A1 -Inceput an 2017 - declaratii si noutati tehnice")
h_PutLine("Societatea:   " + x_SocData ())
h_PutLine("Baza de date: " + sDBName)
h_PutLine("Versiunea:    HMARFA: " +sVM+ "  HMARFT: "+sVt)
h_PutLine("---------------------------------------------------------------------------------")
h_PutLine("")
    
// -------------------------------------------------------------------------
// marcheaza inceputl op.de update

#IF ! h_UseAlias("apl")
    h_PutLine("*** Deschidere fisier Aplicatii nereusita !")
    #DO FUABANDN.TXT
#ENDIF
DBEval( {|| apl->V        := Left(apl->V,6)+"x"},    ;
        {|| apl->CODE ="M"},,,, .F. )
DBEval( {|| apl->V        := Left(apl->V,6)+"x"},    ;
        {|| apl->CODE ="t"},,,, .F. )
h_CloseAlias()

// -------------------------------------------------------------------------
// Update la hMARFA V 8.8C

// acceseaza ca hMARFA
h_ApplCode := "M"
h_ApplIdnt := {"HAMOR Soft SRL",;
               "hMARFA",        ;
               Left(sVM,5),     ;
               "1992-2017"}

// initializeaza variabilele fil_xxxx cu NIL
#DO F_FIL.TXT
h_Begin (x_path, { || ""} )
RestScreen(0, 0, MaxRow(), MaxCol(), xx_scr)

#IF ! x_cM (@DataMarfa)
    h_PutLine("*** Acces nereusit !")
    Abandon("C")
#ENDIF
h_CloseOut("")

// algoritmul update hMARFA
// ...
#DO F1M.TXT
DbCloseAll()

// Update la hMARFT V 4.4C
h_ApplCode := "t"
h_ApplIdnt := {"HAMOR Soft SRL",;
               "hMARFT",        ;
               Left(sVt,5),     ;
               ""}

// initializeaza variabilele fil_xxxx cu NIL
#DO F_FIL.TXT
h_Begin (x_path, { || ""} )
RestScreen(0, 0, MaxRow(), MaxCol(), xx_scr)

#IF ! x_ct (@DataMarfa)
    h_PutLine("*** Acces nereusit !")
    Abandon("C")
#ENDIF
// algoritmul update pt.hMARFT
// ...
#DO F1T.TXT

DbCloseAll()

// #IF sVM = "V 8.8C1" .OR. sVM = "V 8.8C3" .OR. sVM = "V 8.8C4"
//     h_OpenOut(slogf,.T.)
//     h_PutLine("")
//     h_PutLine(" Atentie!")
//     h_PutLine(" Lansati hMARFA si verificati numerele de serii extrase in Cataloage/ Numerotare documente")
//     h_PutLine(' Daca este cazul, completati numerele "pana la" pentru campurile "Interval NrRegSp" si "Interval NrFac",')
//     h_PutLine(' daca nu au fost extrase serii si sunt operatii efectuate, adaugati o serie de facturi manual,')
//     h_PutLine(' dupa care executati Recalcul')
//     h_CloseOut("")
// #ENDIF

h_OpenOut(slogf,.T.)
h_PutLine("")
h_PutLine("*   Executati Recalcul in Numerotare documente")
h_PutLine("*   Revizuiti plajele 2017 introduse in Numerotare documente")
h_PutLine("*   Executati Recalcul in Corespondente TVA (hMARFT)")
h_PutLine("")
h_PutLine("                               - o -")
h_CloseOut("")

// -------------------------------------------------------------------------
// Aplicatii
#IF ! h_UseAlias("apl")
    h_PutLine("*** Deschidere fisier Aplicatii nereusita !")
    #DO FUABANDN.TXT
#ENDIF
DBEval( {|| apl->V        := aref_MV,                                        ;
            apl->EXE_FILE := Left(apl->EXE_FILE,6)+SubStr(aref_MEXE,7),      ;
            apl->NRORD    := aref_MNRORD,                                    ;
            apl->OPT      := RTrim(aref_MOPT)+IIf("$"$aref_MOPT,"","$")},    ;
        {|| apl->CODE ="M"},,,, .F. )
DBEval( {|| apl->V        := aref_tV,                                        ;
            apl->EXE_FILE := Left(apl->EXE_FILE,6)+SubStr(aref_tEXE,7),      ;
            apl->NRORD    := aref_tNRORD,                                    ;
            apl->OPT      := RTrim(aref_tOPT)+IIf("$"$aref_tOPT,"","$")},    ;
        {|| apl->CODE ="t"},,,, .F. )
h_CloseAlias()

// -------------------------------------------------------------------------
// NewMsg()

// -------------------------------------------------------------------------
// Adaugarea fisierului .log in Notite 

#IF ! h_UseAlias("j_")
    h_PutLine("*** Eroare deschidere Notite !")
    Abandon("C")
#ENDIF
DbGoBottom()
// DataAppl := IIf(!Empty(DataMarfa),DataMarfa,Date())
// J_02A    := h_DtoC(DataAppl,"aa")+"0001"
J_02A    := Date()
gcmd_exec("J_INCR")
#IF h_AddRec()
    j_->SJ_02A := J_02A
    j_->SJ_02B := Date()
    j_->SJ_02C := "UPG"
    j_->SJ_A1  := "hMARFA 8.9A1 - Upgrade evidenta TVA in 2017"
    j_->SJ_O   := h_MemoRead(slogf)
#ENDIF
h_CloseAlias()

// -------------------------------------------------------------------------

DbCloseAll()
h_ViewFile(slogf,"Declaratia 394",150,60,"P")

h_MesHlp("Terminat",0)
// EndMsg()

RestScreen(0, 0, MaxRow(), MaxCol(), xx_scr)
