//Faza 1: Actualizare structura si situatii
// --------------------------------------------------
NewMsg()
x_open := {}

x_versiune := {"V 3.6D","V 3.6E","V 3.6F"}
#DO F_VERS.TXT
#IF lEroare
    #DO FUABANDN.TXT
#ENDIF

// ------------------------------------------------------------
// Creare fisier log

sDbName  := h_FileName(h_AbsPath(h_CutSl(h_FilePathes[fil_file])))
xf_log   := h_AddPath("UBV36D_"+h_StatId()+".TXT",h_FilePathes[fil_tmp])

h_OpenOut(xf_log)
h_PutLine("Upgrade "+x_tit_vers)
h_PutLine("Societatea:   " + x_SocData ())
h_PutLine("Baza de date: " + sDBName)
h_PutLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

#IF "windows" $ Lower(h_Build())
    h_PutLine("Fisier jurnal upgrade: "+h_TruePath(h_AddPath(h_AddPath(xf_log,CurDir()),CurDrive()+":\")))
#ELSE
    h_PutLine("Fisier jurnal upgrade: "+h_TruePath(h_AddPath(xf_log,CurDir())))
#ENDIF
h_PutLine("Pentru datele de contact ale partenerilor (distribuitorilor) ")
h_PutLine("consultati pagina www.hamorsoft.ro")
h_PutLine("")
// Versiune Linux ?
lLinux := ("linux"$lower(h_build()))

// --------------------------------------------------------------
PutMsg("Faza 1: actualizare structura")

// ------------------------------------------------------------            
// Dictionar

PutMsg("ADD:dict")
#IF !h_UseAlias("dict")
    #DO FUABANDN.TXT
#ENDIF
sscript  := ""

// se inlocuiesc
sscript += "AUDIT-OBLIG ,"
sscript += "AUDIT-VAR   ,"
sscript += "AUDIT-OPT   ,"
sscript += "TIP-BILANT  ,"

__dbLocate({|| dict->RUB="BIFA-ED"},,,,.F.)
#IF !FOUND()
    sscript  += "BIFA-ED     ,"
#ENDIF
__dbLocate({|| dict->RUB="BIFA-IP"},,,,.F.)
#IF !FOUND()
    sscript  += "BIFA-IP     ,"
#ENDIF
DBEval( {|| DbDelete()}, {|| ((dict->RUB+",") $ sscript)},,,, .F. )
__dbApp( (homepath+"DICTIONA"), { }, ;
         {|| ((field->RUB+",") $ sscript)},,,, .F. )
h_PutLine("    Adaugare rubrici noi in Dictionar: "+StrTran(Left(sscript,Len(sscript)-1)," ",""))
h_CloseAlias()

// ------------------------------------------------------------            
// Constante si functii

PutMsg("ADD:cf")
#IF !h_UseAlias("cf")
    #DO FUABANDN.TXT
#ENDIF
sscript  := ""

DBEVAL({|| cf->VALCOD := "N"},{|| cf->COD="BIFA-MICRO"},,,,.F.)
h_PutLine("    Constanta BIFA-MICRO:=N")

sscript  += "AUDIT-VAR   ,"
sscript  += "AUDIT-OPT   ,"
sscript  += "AUDIT-OBLIG ,"

__dbLocate({|| cf->COD="INTOCM-CIF"},,,,.F.)
#IF !FOUND()
    sscript  += "INTOCM-CIF  ,"
#ENDIF

// BIFA-ED      Exercitiu diferit de anul calendaristic
__dbLocate({|| cf->COD="BIFA-ED"},,,,.F.)
#IF !FOUND()
    sscript  += "BIFA-ED     ,"
#ENDIF

// BIFA-IP      Entitate de interes public
__dbLocate({|| cf->COD="BIFA-IP"},,,,.F.)
#IF !FOUND()
    sscript  += "BIFA-IP     ,"
#ENDIF

DBEval( {|| DbDelete()}, {|| ((cf->COD+",") $ sscript)},,,, .F. )
__dbApp( (homepath+"CNSTFUNC"), { }, ;
         {|| ((field->COD+",") $ sscript)},,,, .F. )
h_PutLine("    Adaugare constante noi: "+StrTran(Left(sscript,Len(sscript)-1)," ",""))
h_CloseAlias()
    
// ------------------------------------------------------------            
// Comenzi generale

PutMsg("MOD:gcmd")
#IF !h_UseAlias("gcmd")
    #DO FUABANDN.TXT
#ENDIF

sscript  :="BD_XML,BS_XML,DU_IVD,DU_XML,VEXP_1,VEXP_2,VEXP_7,RUND  ,DOCUM1,1_    ,9_    ,1_GRUT,DU_PDF,X_CAMP,DOCUM2,"
DBEval( {|| DbDelete()}, {|| ((gcmd->COD+",") $ sscript)},,,, .F. )
__dbApp( (homepath+"GENCMD"), { }, {|| ((field->COD+",") $ sscript)},,,, .F. )
h_PutLine("    Inlocuit comenzile generale: "+StrTran(Left(sscript,Len(sscript)-1)," ",""))
h_CloseAlias()
    
// -------------------------------------------------------------------------
// Comenzi generale hUTIL

PutMsg("MOD:gcmd-u")
fobj      := h_ExtUse():new(h_FilePathes[fil_file], "u" )
fobj:afis := {{"gcmd","gcmdu"}}
#IF !fobj:open()
    #DO FUABANDN.TXT
#ENDIF
// inlocuire RUND
xx_sxmd := "RUND  ,"
DbEval({|| DbDelete()}, {|| (gcmdu->COD+",") $ xx_sxmd})
__dbApp( (homepath+"GENCMDU"), { }, {|| (field->COD+",") $ xx_sxmd},,,, .F. )
h_PutLine("    Adaugat/Inlocuit comenzile generale (hUTIL): "+StrTran(xx_sxmd," ",""))
h_CloseAlias()

// -----------------------------------------------------------------------------
// Corecteaza Dxxx_DEP.HTM in cataloagele de definitii pentru declaratii

PutMsg("MOD:Dxxx_DEP.HTM")
xx_sD1 := h_FilePathes[fil_bsp]
xx_aD1 := Directory(h_AddPath(h_AllF(),xx_sD1),"D")
xx_i   := 1
#WHILE xx_i <= Len(xx_aD1)
    #IF xx_aD1[xx_i,5]="D" .AND. !(xx_aD1[xx_i,1]==".") .AND. !(xx_aD1[xx_i,1]=="..")
        xx_sD2 := h_AddPath(xx_aD1[xx_i,1],xx_sD1)
        xx_aD2 := Directory(h_AddPath(h_AllF(),xx_sD2))
        xx_k   := 1
        #WHILE xx_k <= Len(xx_aD2)
            #IF h_FileEx(h_AddPath(xx_aD2[xx_k,1],xx_sD2)) .AND.                ;
                Right(xx_aD2[xx_k,1],8)="_DEP.HTM"    
                xx_shtm := h_MemoRead(h_AddPath(xx_aD2[xx_k,1],xx_sD2))
                #IF "http://www.anaf.mfinante.gov.ro/StareD112" $ xx_shtm
                    xx_shtm := StrTran(xx_shtm,                                     ;
                                       "http://www.anaf.mfinante.gov.ro/StareD112", ;
                                       "https://www.anaf.ro/StareD112/")
                    h_MemoWrit(h_AddPath(xx_aD2[xx_k,1],xx_sD2),xx_shtm)
                    h_PutLine("    Actualizat: "+h_AddPath(xx_aD2[xx_k,1],xx_sD2))
                #ENDIF
            #ENDIF
            xx_k++
        #ENDDO
    #ENDIF
    xx_i++
#ENDDO

// -----------------------------------------------------------------------------
//- Schimbare S100X, D101

PutMsg("MOD:*PDF")
xx_decs:={{"D101", h_FilePathes[fil_bdp],{"D101_XML.PDF","D101_ERR.PDF"}},;
          {"S100X",h_FilePathes[fil_bsp],{"S1005_ERR.PDF","S1005_XML.PDF","S1003_ERR.PDF","S1003_XML.PDF"}}}

xx_i:=1
#WHILE xx_i<=LEN(xx_decs)
    xx_decl_path:=h_AddPath(xx_decs[xx_i,1],xx_decs[xx_i,2])
    #IF (h_FDExist(xx_decl_path) = "D")
        xx_j:=1
        #WHILE xx_j<=LEN(xx_decs[xx_i,3])
            xx_sFisDep := h_AddPath(xx_decs[xx_i,3,xx_j],xx_decl_path)    
            xx_sLocFile:= homepath+xx_decs[xx_i,3,xx_j]
            //Verificare daca trebuie copiat
            lCopy:=.f.
            xx_oper    :="modificat"
            aDs   := h_Directory(xx_sLocFile)
            aDd   := h_Directory(xx_sFisDep)
            #IF Len(aDd)=0
                //Fisierul destinatie nu exista 
                xx_oper  :="adaugat"
                lCopy := .t.
            #ELSEIF (Len(aDd)=1) .AND. (DtoS(aDd[1,3])+aDd[1,4]) < (DtoS(aDs[1,3])+aDs[1,4])
                //Fisierul destinatie este mai veche decat cel de referinta 
                lCopy := .t.
            #ENDIF
            #IF (lCopy == .t.)
                #IF h_SafeErase(xx_sFisDep) .AND. h_SafeErase(StrTran(xx_sFisDep,".PDF",".pdf")) .AND. ;
                    h_CopyFile(xx_sLocFile,xx_sFisDep)
                    h_PutLine("    Fisier: "+xx_sFisDep+" "+xx_oper)
                #ELSE
                    h_PutLine("    Fisier: "+xx_sFisDep+" actualizare nereusita!")
                #ENDIF
            #ENDIF
            xx_j:=xx_j+1    
        #ENDDO 
    #ENDIF
    xx_i:=xx_i+1
#ENDDO 

// --------------------------------------------------------------------
// Actualizare Aplicatii

DbSelectArea("apl")
// Rezolva HBILANT si HBILANTU
DBEval( {|| apl->OPT := RTrim(aref->OPT)+IIf("$"$aref->OPT,"","$"), ;
            apl->V   := aref->V, apl->NRORD := aref->NRORD,         ;
            apl->EXE_FILE := Left(apl->EXE_FILE, 6)+"36"},          ;
        {|| HB_RegExMatch("^[WH]BILA.36",apl->EXE_FILE)},,,, .F. )

h_CloseAlias()
DbSelectArea("aref")
h_CloseAlias()

// --------------------------------------------------------------
PutMsg("Faza 2: actualizare situatii")

DbCloseAll()
F2_APELATA := .T.
#DO F2.TXT

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

h_PutLine("")
h_PutLine("-------------------------------------------------------------------------------")
h_CloseOut("")
#IF "linux"$lower(h_build())
    h_Chmod(xf_log,CtoN("111111101",2))        
#ENDIF 

#IF ! h_UseAlias("j_")
    #DO FUABANDN.TXT
#ENDIF
DbGoBottom()
J_02A    := "200001"
DataAppl := Date()
gcmd_exec("J_INCR")
// Adaugarea jurnalului de upgrade 
#IF h_AddRec()
    j_->SJ_02A := J_02A
    j_->SJ_02B := Date()
    j_->SJ_02C := "UPG"
    j_->SJ_A1  := x_tit_vers
    j_->SJ_O   := h_MemoRead(h_AddPath(xf_log,h_FilePathes[fil_file]))
#ENDIF
index_file("j_")
h_CloseAlias()

// ------------------------------------------------------------            
DbCloseAll()
EndMsg()
h_ViewFile(xf_log,x_tit_vers,150,60,"P")

#IF lModSilentios
    h_End(0,"Terminat")
#ENDIF    
//
