function dbInit()
{
    var db = LocalStorage.openDatabaseSync("VehiclesDB", "", "Vehicles database", 5000000)
    try {
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS vehicles (typ text,vyrobce text,model text,rok integer,palivo text,nadrz integer,tachometr integer,nakup text,cena integer,spz text,vin text,pojistka text,poznamka text)')
        })
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS refuel (datum teext,tachometr integer,palivo text,cena integer,objem integer,misto text,doplna boolean,poznamka text,fid integer)')
        })
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS servis (datum text,tachometr integer,typ text,cena integer,misto text,poznamka text,fid integer,druh text)')
        })
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS finance (datum text,ucel text,cena integer,misto text,poznamka text,fid integer)')
        })
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS dily (oznaceni text,druh text,stav text,mnozstvi integer,poznamka text)')
        })
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS photo (album text,cesta text,fid numeric)')
        })
        /*db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS notifications (oznaceni text,zprava text,datum text)')
        })*/
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS events (datum text,nazev text,misto text,poznamka text)')
        })
    } catch (err) {
        console.log("Error creating table in database: " + err)
    };
}

function dbGetHandle()
{
    try {
        var db = LocalStorage.openDatabaseSync("VehiclesDB", "", "Vehicles database", 5000000)
    } catch (err) {
        console.log("Error opening database: " + err)
    }
    return db
}

function dropTables()
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DROP TABLE vehicles')
        tx.executeSql('DROP TABLE refuel')
        tx.executeSql('DROP TABLE servis')
        tx.executeSql('DROP TABLE finance')
        tx.executeSql('DROP TABLE dily')
        tx.executeSql('DROP TABLE photo')
        tx.executeSql('DROP TABLE events')
    })
}

//DATABAZOVE OPERACE PRO TABULKU VEHICLES
//DATABAZOVE OPERACE PRO TABULKU VEHICLES
//DATABAZOVE OPERACE PRO TABULKU VEHICLES
//DATABAZOVE OPERACE PRO TABULKU VEHICLES
//DATABAZOVE OPERACE PRO TABULKU VEHICLES

function dbInsert(Ptyp,Pvyrobce,Pmodel,Prok,Ppalivo,Pnadrz,Ptachometr,Pnakup,Pcena,Pspz,Pvin,Ppojistka,Ppoznamka)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO vehicles VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
                      [Ptyp,Pvyrobce,Pmodel,Prok,Ppalivo,Pnadrz,Ptachometr,Pnakup,Pcena,Pspz,Pvin,Ppojistka,Ppoznamka])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllVehiclesJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,typ,vyrobce,model,rok,palivo,nadrz,tachometr,nakup,cena,spz,vin,pojistka,poznamka FROM vehicles')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "typ": results.rows.item(i).typ,
                "vyrobce": results.rows.item(i).vyrobce,
                "model": results.rows.item(i).model,
                "rok": results.rows.item(i).rok,
                "palivo": results.rows.item(i).palivo,
                "nadrz": results.rows.item(i).nadrz,
                "tachometr": results.rows.item(i).tachometr,
                "nakup": Date.fromLocaleString(Qt.locale(),results.rows.item(i).nakup,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                "cena": results.rows.item(i).cena,
                "spz": results.rows.item(i).spz,
                "vin": results.rows.item(i).vin,
                "pojistka": results.rows.item(i).pojistka,
                "poznamka": results.rows.item(i).poznamka}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadAll(PlistModel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,typ,vyrobce,model,rok,palivo,nadrz,tachometr,nakup,cena,spz,vin,pojistka,poznamka FROM vehicles')
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                         rowid: results.rows.item(i).rowid,
                                         typ: results.rows.item(i).typ,
                                         vyrobce: results.rows.item(i).vyrobce,
                                         model: results.rows.item(i).model,
                                         rok: results.rows.item(i).rok,
                                         palivo: results.rows.item(i).palivo,
                                         nadrz: results.rows.item(i).nadrz,
                                         tachometr: results.rows.item(i).tachometr,
                                         nakup: Date.fromLocaleString(Qt.locale(),results.rows.item(i).nakup,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                         cena: results.rows.item(i).cena,
                                         spz: results.rows.item(i).spz,
                                         vin: results.rows.item(i).vin,
                                         pojistka: results.rows.item(i).pojistka,
                                         poznamka: results.rows.item(i).poznamka})
        }
    })
}

function dbReadById(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,typ,vyrobce,model,rok,palivo,nadrz,tachometr,nakup,cena,spz,vin,pojistka,poznamka FROM vehicles WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                     rowid: results.rows.item(0).rowid,
                                     typ: results.rows.item(0).typ,
                                     vyrobce: results.rows.item(0).vyrobce,
                                     model: results.rows.item(0).model,
                                     rok: results.rows.item(0).rok,
                                     palivo: results.rows.item(0).palivo,
                                     nadrz: results.rows.item(0).nadrz,
                                     tachometr: results.rows.item(0).tachometr,
                                     nakup: Date.fromLocaleString(Qt.locale(),results.rows.item(0).nakup,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                     cena: results.rows.item(0).cena,
                                     spz: results.rows.item(0).spz,
                                     vin: results.rows.item(0).vin,
                                     pojistka: results.rows.item(0).pojistka,
                                     poznamka: results.rows.item(0).poznamka})
    })
}

function dbUpdate(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE vehicles SET typ=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE vehicles SET vyrobce=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE vehicles SET model=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE vehicles SET rok=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 5: tx.executeSql('UPDATE vehicles SET palivo=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 6: tx.executeSql('UPDATE vehicles SET nadrz=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 7: tx.executeSql('UPDATE vehicles SET tachometr=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 8: tx.executeSql('UPDATE vehicles SET nakup=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 9: tx.executeSql('UPDATE vehicles SET cena=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 10: tx.executeSql('UPDATE vehicles SET spz=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 11: tx.executeSql('UPDATE vehicles SET vin=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 12: tx.executeSql('UPDATE vehicles SET pojistka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 13: tx.executeSql('UPDATE vehicles SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDelete(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM vehicles WHERE rowid = ?', [Prowid])
    })
}


//DATABAZOVE OPERACE PRO TABULKU TANKOVANI
//DATABAZOVE OPERACE PRO TABULKU TANKOVANI
//DATABAZOVE OPERACE PRO TABULKU TANKOVANI
//DATABAZOVE OPERACE PRO TABULKU TANKOVANI
//DATABAZOVE OPERACE PRO TABULKU TANKOVANI

function dbInsertRefuel(Pdatum,Ptachometr,Ppalivo,Pcena,Pobjem,Pmisto,Pdoplna,Ppoznamka,Pfid)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO refuel VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)',
                      [Pdatum,Ptachometr,Ppalivo,Pcena,Pobjem,Pmisto,Pdoplna,Ppoznamka,Pfid])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllByFidRefuel(PlistModel,Pfid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,palivo,cena,objem,misto,doplna,poznamka,fid FROM refuel WHERE fid= ?',[Pfid])
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                  rowid: results.rows.item(i).rowid,
                                  datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                  tachometr: results.rows.item(i).tachometr,
                                  palivo: results.rows.item(i).palivo,
                                  cena: results.rows.item(i).cena,
                                  objem: results.rows.item(i).objem,
                                  misto: results.rows.item(i).misto,
                                  doplna: results.rows.item(i).doplna,
                                  poznamka: results.rows.item(i).poznamka,
                                  fid: results.rows.item(i).fid})
        }
    })
}

function dbReadAllRefuelJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,palivo,cena,objem,misto,doplna,poznamka,fid FROM refuel')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "datum": Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                "tachometr": results.rows.item(i).tachometr,
                "palivo": results.rows.item(i).palivo,
                "cena": results.rows.item(i).cena,
                "objem": results.rows.item(i).objem,
                "misto": results.rows.item(i).misto,
                "doplna": results.rows.item(i).doplna,
                "poznamka": results.rows.item(i).poznamka,
                "fid": results.rows.item(i).fid}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadByIdRefuel(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,palivo,cena,objem,misto,doplna,poznamka,fid FROM refuel WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                   rowid: results.rows.item(0).rowid,
                                   datum: Date.fromLocaleString(Qt.locale(),results.rows.item(0).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                   tachometr: results.rows.item(0).tachometr,
                                   palivo: results.rows.item(0).palivo,
                                   cena: results.rows.item(0).cena,
                                   objem: results.rows.item(0).objem,
                                   misto: results.rows.item(0).misto,
                                   doplna: results.rows.item(0).doplna,
                                   poznamka: results.rows.item(0).poznamka,
                                   fid: results.rows.item(0).fid})

    })
}

function dbUpdateRefuel(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE refuel SET datum=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE refuel SET tachometr=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE refuel SET palivo=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE refuel SET cena=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 5: tx.executeSql('UPDATE refuel SET objem=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 6: tx.executeSql('UPDATE refuel SET misto=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 7: tx.executeSql('UPDATE refuel SET doplna=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 8: tx.executeSql('UPDATE refuel SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteRefuel(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM refuel WHERE rowid = ?', [Prowid])
    })
}

function dbDeleteAllRefuelByFid(Pfid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results= tx.executeSql('DELETE FROM refuel WHERE fid = ?', [Pfid])
    })
}

//DATABAZOVE OPERACE PRO TABULKU SERVIS
//DATABAZOVE OPERACE PRO TABULKU SERVIS
//DATABAZOVE OPERACE PRO TABULKU SERVIS
//DATABAZOVE OPERACE PRO TABULKU SERVIS
//DATABAZOVE OPERACE PRO TABULKU SERVIS

function dbInsertServis(Pdatum,Ptachometr,Ptyp,Pcena,Pmisto,Ppoznamka,Pfid,Pdruh)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO servis VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
                      [Pdatum,Ptachometr,Ptyp,Pcena,Pmisto,Ppoznamka,Pfid,Pdruh])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllByFidServis(PlistModel,Pfid,Pdruh)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,typ,cena,misto,poznamka,fid,druh FROM servis WHERE fid= ? AND druh= ? ORDER BY datum ASC',[Pfid,Pdruh])
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                       rowid: results.rows.item(i).rowid,
                                       datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                       tachometr: results.rows.item(i).tachometr,
                                       typ: results.rows.item(i).typ,
                                       cena: results.rows.item(i).cena,
                                       misto: results.rows.item(i).misto,
                                       poznamka: results.rows.item(i).poznamka,
                                       fid: results.rows.item(i).fid,
                                       druh: results.rows.item(i).druh})
        }
    })
}

function dbReadAllServisJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,typ,cena,misto,poznamka,fid,druh FROM servis ORDER BY datum ASC')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "datum": Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                "tachometr": results.rows.item(i).tachometr,
                "typ": results.rows.item(i).typ,
                "cena": results.rows.item(i).cena,
                "misto": results.rows.item(i).misto,
                "poznamka": results.rows.item(i).poznamka,
                "fid": results.rows.item(i).fid,
                "druh": results.rows.item(i).druh}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadByIdServis(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,tachometr,typ,cena,misto,poznamka,fid,druh FROM servis WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                   rowid: results.rows.item(0).rowid,
                                   datum: Date.fromLocaleString(Qt.locale(),results.rows.item(0).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                   tachometr: results.rows.item(0).tachometr,
                                   typ: results.rows.item(0).typ,
                                   cena: results.rows.item(0).cena,
                                   misto: results.rows.item(0).misto,
                                   poznamka: results.rows.item(0).poznamka,
                                   fid: results.rows.item(0).fid,
                                   druh: results.rows.item(0).druh})

    })
}

function dbUpdateServis(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE servis SET datum=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE servis SET tachometr=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE servis SET typ=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE servis SET cena=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 5: tx.executeSql('UPDATE servis SET misto=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 6: tx.executeSql('UPDATE servis SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 7: tx.executeSql('UPDATE servis SET druh=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteServis(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM servis WHERE rowid = ?', [Prowid])
    })
}

function dbDeleteAllServisByFid(Pfid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results= tx.executeSql('DELETE FROM servis WHERE fid = ?', [Pfid])
    })
}

function dbSumServis(Pdruh,Pfid)
{
    var db = dbGetHandle()
    var results
    db.transaction(function (tx) {
        results= tx.executeSql('SELECT sum(cena) AS value FROM servis WHERE fid= ? AND druh= ?', [Pfid,Pdruh])
    })
    if (results.rows.item(0).value === null)
        return 0
    return results.rows.item(0).value
}

//DATABAZOVE OPERACE PRO TABULKU PHOTO
//DATABAZOVE OPERACE PRO TABULKU PHOTO
//DATABAZOVE OPERACE PRO TABULKU PHOTO
//DATABAZOVE OPERACE PRO TABULKU PHOTO
//DATABAZOVE OPERACE PRO TABULKU PHOTO

function dbInsertPhoto(Palbum, Pcesta, Pfid)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO photo VALUES(?, ?, ?)',
                      [Palbum, Pcesta, Pfid])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllByFidPhoto(PlistModel,Pfid,Palbum)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,album,cesta,fid FROM photo WHERE fid= ? AND album= ?',[Pfid,Palbum])
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                      rowid: results.rows.item(i).rowid,
                                      album: results.rows.item(i).album,
                                      cesta: results.rows.item(i).cesta,
                                      fid: results.rows.item(i).fid})
        }
    })
}

function dbReadByIdPhoto(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,album,cesta,fid FROM photo WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                  rowid: results.rows.item(0).rowid,
                                  album: results.rows.item(0).album,
                                  cesta: results.rows.item(0).cesta,
                                  fid: results.rows.item(0).fid})

    })
}

function dbUpdatePhoto(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE photo SET album=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE photo SET cesta=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeletePhoto(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM photo WHERE rowid = ?', [Prowid])
    })
}

function dbDeleteAllPhotoByFid(Pfid,Palbum)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results= tx.executeSql('DELETE FROM photo WHERE fid = ? AND album= ?', [Pfid,Palbum])
        //console.log("Smazáno: "+results.rowsAffected)
    })
}

//DATABAZOVE OPERACE PRO TABULKU DILY
//DATABAZOVE OPERACE PRO TABULKU DILY
//DATABAZOVE OPERACE PRO TABULKU DILY
//DATABAZOVE OPERACE PRO TABULKU DILY
//DATABAZOVE OPERACE PRO TABULKU DILY

function dbInsertParts(Poznaceni,Pdruh,Pstav,Pmnozstvi,Ppoznamka)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO dily VALUES(?, ?, ?, ?, ?)',
                      [Poznaceni,Pdruh,Pstav,Pmnozstvi,Ppoznamka])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllParts(PlistModel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,oznaceni,druh,stav,mnozstvi,poznamka FROM dily')
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                         rowid: results.rows.item(i).rowid,
                                         oznaceni: results.rows.item(i).oznaceni,
                                         druh: results.rows.item(i).druh,
                                         stav: results.rows.item(i).stav,
                                         mnozstvi: results.rows.item(i).mnozstvi,
                                         poznamka: results.rows.item(i).poznamka})
        }
    })
}

function dbReadAllPartsJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,oznaceni,druh,stav,mnozstvi,poznamka FROM dily')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "oznaceni": results.rows.item(i).oznaceni,
                "druh": results.rows.item(i).druh,
                "stav": results.rows.item(i).stav,
                "mnozstvi": results.rows.item(i).mnozstvi,
                "poznamka": results.rows.item(i).poznamka}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadByIdParts(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql('SELECT rowid,oznaceni,druh,stav,mnozstvi,poznamka FROM dily WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                              rowid: results.rows.item(0).rowid,
                              oznaceni: results.rows.item(0).oznaceni,
                              druh: results.rows.item(0).druh,
                              stav: results.rows.item(0).stav,
                              mnozstvi: results.rows.item(0).mnozstvi,
                              poznamka: results.rows.item(0).poznamka})
    })
}

function dbUpdateParts(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE dily SET oznaceni=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE dily SET druh=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE dily SET stav=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE dily SET mnozstvi=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 5: tx.executeSql('UPDATE dily SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteParts(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM dily WHERE rowid = ?', [Prowid])
    })
}

//DATABAZOVE OPERACE PRO TABULKU FINANCE
//DATABAZOVE OPERACE PRO TABULKU FINANCE
//DATABAZOVE OPERACE PRO TABULKU FINANCE
//DATABAZOVE OPERACE PRO TABULKU FINANCE
//DATABAZOVE OPERACE PRO TABULKU FINANCE

function dbInsertFinance(Pdatum,Pucel,Pcena,Pmisto,Ppoznamka,Pfid)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO finance VALUES(?, ?, ?, ?, ?, ?)',
                      [Pdatum,Pucel,Pcena,Pmisto,Ppoznamka,Pfid])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllFinance(PlistModel,Pfid,Pexpense)
{
    var db = dbGetHandle()
    var results
    db.transaction(function (tx) {
        if (Pexpense)
            results = tx.executeSql(
                    'SELECT rowid,datum,ucel,cena,misto,poznamka,fid FROM finance WHERE fid= ? AND ucel!= "0ND9" AND cena<0 ORDER BY datum ASC',[Pfid])
        else
            results = tx.executeSql(
                    'SELECT rowid,datum,ucel,cena,misto,poznamka,fid FROM finance WHERE fid= ? AND ucel!= "0ND9" AND cena>0 ORDER BY datum ASC',[Pfid])
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                       rowid: results.rows.item(i).rowid,
                                       datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                       ucel: results.rows.item(i).ucel,
                                       cena: results.rows.item(i).cena,
                                       misto: results.rows.item(i).misto,
                                       poznamka: results.rows.item(i).poznamka,
                                       fid: results.rows.item(i).fid})
        }
    })
}


function dbReadAllFinanceJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql(
                'SELECT rowid,datum,ucel,cena,misto,poznamka,fid FROM finance ORDER BY datum ASC')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "datum": Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                "ucel": results.rows.item(i).ucel,
                "cena": results.rows.item(i).cena,
                "misto": results.rows.item(i).misto,
                "poznamka": results.rows.item(i).poznamka,
                "fid": results.rows.item(i).fid}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadAllByUcelFinance(PlistModel,Pucel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,ucel,cena,misto,poznamka,fid FROM finance WHERE ucel= ? ORDER BY datum ASC',[Pucel])
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                       rowid: results.rows.item(i).rowid,
                                       datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                       ucel: results.rows.item(i).ucel,
                                       cena: results.rows.item(i).cena,
                                       misto: results.rows.item(i).misto,
                                       poznamka: results.rows.item(i).poznamka,
                                       fid: results.rows.item(i).fid})
        }
    })
}

function dbReadByIdFinance(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,ucel,cena,misto,poznamka,fid FROM finance WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                   rowid: results.rows.item(0).rowid,
                                   datum: Date.fromLocaleString(Qt.locale(),results.rows.item(0).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                   ucel: results.rows.item(0).ucel,
                                   cena: results.rows.item(0).cena,
                                   misto: results.rows.item(0).misto,
                                   poznamka: results.rows.item(0).poznamka,
                                   fid: results.rows.item(0).fid})

    })
}

function dbUpdateFinance(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE finance SET datum=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE finance SET ucel=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE finance SET cena=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE finance SET misto=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 5: tx.executeSql('UPDATE finance SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteFinance(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM finance WHERE rowid = ?', [Prowid])
    })
}

function dbDeleteAllFinanceByFidUcel(Pfid,Pucel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results= tx.executeSql('DELETE FROM finance WHERE fid = ? AND ucel= ?', [Pfid,Pucel])
    })
}

function dbSumFinance(Pucel,Pfid)
{
    var db = dbGetHandle()
    var results
    db.transaction(function (tx) {
        if(Pucel=== "0ND9")
            results= tx.executeSql('SELECT sum(cena) AS value FROM finance WHERE ucel= ? AND cena>0', [Pucel])
        else
            results= tx.executeSql('SELECT sum(cena) AS value FROM finance WHERE fid= ? AND ucel!= ? AND cena>0', [Pfid,"0ND9"])
    })
    if (results.rows.item(0).value === null)
        return 0
    return results.rows.item(0).value
}

function dbNegFinance(Pucel,Pfid)
{
    var db = dbGetHandle()
    var results
    db.transaction(function (tx) {
        if(Pucel=== "0ND9")
            results= tx.executeSql('SELECT sum(cena) AS value FROM finance WHERE ucel= ? AND cena<0', [Pucel])
        else
            results= tx.executeSql('SELECT sum(cena) AS value FROM finance WHERE fid= ? AND ucel!= ? AND cena<0', [Pfid,"0ND9"])
    })
    if (results.rows.item(0).value === null)
        return 0
    return results.rows.item(0).value
}

//DATABAZOVE OPERACE PRO TABULKU NOTIFICACIONS
//DATABAZOVE OPERACE PRO TABULKU NOTIFICACIONS
//DATABAZOVE OPERACE PRO TABULKU NOTIFICACIONS
//DATABAZOVE OPERACE PRO TABULKU NOTIFICACIONS
//DATABAZOVE OPERACE PRO TABULKU NOTIFICACIONS

function dbInsertNotif(Poznaceni,Pzprava,Pdatum)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO notifications VALUES(?, ?, ?)',
                      [Poznaceni,Pzprava,Pdatum])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllNotif(PlistModel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,oznaceni,zprava,datum FROM notifications ORDER BY datum ASC')
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                       rowid: results.rows.item(i).rowid,
                                       oznaceni: results.rows.item(i).oznaceni,
                                       zprava: results.rows.item(i).zprava,
                                       datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy")})
        }
    })
}

function dbReadByIdNotif(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,oznaceni,zprava,datum FROM notifications WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                   rowid: results.rows.item(0).rowid,
                                   oznaceni: results.rows.item(0).oznaceni,
                                   zprava: results.rows.item(0).zprava,
                                   datum: Date.fromLocaleString(Qt.locale(),results.rows.item(0).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy")})

    })
}

function dbUpdateNotif(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE notifications SET oznaceni=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE notifications SET zprava=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE notifications SET datum=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteNotif(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM notifications WHERE rowid = ?', [Prowid])
    })
}

//DATABAZOVE OPERACE PRO TABULKU EVENTS
//DATABAZOVE OPERACE PRO TABULKU EVENTS
//DATABAZOVE OPERACE PRO TABULKU EVENTS
//DATABAZOVE OPERACE PRO TABULKU EVENTS
//DATABAZOVE OPERACE PRO TABULKU EVENTS

function dbInsertEvent(Pdatum,Pnazev,Pmisto,Ppoznamka)
{
    var db = dbGetHandle()
    var rowid = 0;
    db.transaction(function (tx) {
        var result= tx.executeSql('INSERT INTO events VALUES(?, ?, ?, ?)',
                      [Pdatum,Pnazev,Pmisto,Ppoznamka])
        rowid = result.insertId
    })
    return rowid;
}

function dbReadAllEvent(PlistModel)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,nazev,misto,poznamka FROM events ORDER BY datum ASC')
        for (var i = 0; i < results.rows.length; i++) {
            PlistModel.insert(0,{
                                       rowid: results.rows.item(i).rowid,
                                       datum: Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                       nazev: results.rows.item(i).nazev,
                                       misto: results.rows.item(i).misto,
                                       poznamka: results.rows.item(i).poznamka})
        }
    })
}

function dbReadAllEventsJson(PlistModel)
{
    var db = dbGetHandle()
    var data= []
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,nazev,misto,poznamka FROM events ORDER BY datum ASC')
        for (var i = 0; i < results.rows.length; i++) {
            var newItem = {
                "rowid": results.rows.item(i).rowid,
                "datum": Date.fromLocaleString(Qt.locale(),results.rows.item(i).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                "nazev": results.rows.item(i).nazev,
                "misto": results.rows.item(i).misto,
                "poznamka": results.rows.item(i).poznamka}

            data.push(newItem)
        }
    })
    PlistModel.source= data
}

function dbReadByIdEvent(PlistModel,Pid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT rowid,datum,nazev,misto,poznamka FROM events WHERE rowid= ?',[Pid])
        PlistModel.insert(0,{
                                   rowid: results.rows.item(0).rowid,
                                   datum: Date.fromLocaleString(Qt.locale(),results.rows.item(0).datum,"yyyy-MM-dd").toLocaleString(Qt.locale(),"dd.MM.yyyy"),
                                   nazev: results.rows.item(0).nazev,
                                   misto: results.rows.item(0).misto,
                                   poznamka: results.rows.item(0).poznamka})

    })
}

function dbUpdateEvent(Prowid,Pcolumn,Pvalue)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        switch (Pcolumn) {
        case 1: tx.executeSql('UPDATE events SET datum=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 2: tx.executeSql('UPDATE events SET nazev=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 3: tx.executeSql('UPDATE events SET misto=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        case 4: tx.executeSql('UPDATE events SET poznamka=? WHERE rowid = ?', [Pvalue, Prowid])
            break;
        default: console.log("Error updating database")
            break;
        }
    })
}

function dbDeleteEvent(Prowid)
{
    var db = dbGetHandle()
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM events WHERE rowid = ?', [Prowid])
    })
}
