close databases

if file(m.gcdat_path + "d_inv.dbf") .and. file(m.gcdat_path + "o_inv.dbf")
    if messagebox( "Opravdu chces přidat inventury? ", 3+32+256, "Pridani inventur po odpoctech") = 6

       SELECT MIN(idriadok) as id_dokl FROM (m.gcdat_path + "doklady") INTO CURSOR __pol
       SCATTER memvar
       SELECT MIN(idriadok) as id_obra FROM (m.gcdat_path + "obraty") INTO CURSOR __pol
       SCATTER memvar

       SELECT MIN(c_dokl) as min_c_inv FROM (m.gcdat_path + "d_inv") INTO CURSOR __pol
       SCATTER memvar
       SELECT MAX(c_dokl) as max_c_dok FROM (m.gcdat_path + "doklady") INTO CURSOR __pol
       SCATTER memvar
       rozdil = INT(abs(m.max_c_dok - m.min_c_inv) * 1.3)
       ? rozdil
       
       wait "updating c_dokl" wind nowa at 22,22
       update (m.gcdat_path + "d_inv") SET c_dokl = c_dokl + m.rozdil
       update (m.gcdat_path + "o_inv") SET c_dokl = c_dokl + m.rozdil

       wait "updating c_dokl, inserting into doklady" wind nowa at 22,22
       SELECT doklady
       APPEND FROM (m.gcdat_path + "d_inv")
       REPLACE ALL idriadok WITH RECNO() + m.id_dokl + 99
       
       wait "updating c_dokl, inserting into doklady, inserting into obraty" wind nowa at 22,22
       SELECT obraty
       APPEND FROM (m.gcdat_path + "o_inv")
       wait "updating c_dokl, inserting into doklady, inserting into obraty, idriadok" wind nowa at 22,22
       REPLACE ALL idriadok WITH RECNO() + m.id_obra + 99

       close databases
       delete file (gcdat_path + "d_inv.dbf")
       delete file (gcdat_path + "o_inv.dbf")

       wait clear
    endif
else
   = messagebox( "Inventury už byly pridany, nebo tu ještě nejsou pripraveny", 1+16, "Pridani inventur po odpoctech", 5555)
endif
