diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-31 03:57:19 +0100 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-31 03:57:19 +0100 |
| commit | 2a50cbb5ffd3f4fbc088bd18598980e0f142d7b7 (patch) | |
| tree | ec5793e69441665019b0feffa5d19a38e2c0ef41 | |
| parent | 0e7ea506b76660c91fa9f479f7de9c79304ae0dc (diff) | |
Import now updates all ids
| -rw-r--r-- | interact.go | 12 | ||||
| -rw-r--r-- | sqlite.go | 123 |
2 files changed, 130 insertions, 5 deletions
diff --git a/interact.go b/interact.go index d0b3d71..c9b96e6 100644 --- a/interact.go +++ b/interact.go @@ -514,10 +514,11 @@ func interact(fulldb bool) { c.Println("Projects:",len(prs)) c.Println("Customers:",len(cus)) //c.Println("Customers:",cus) - - ExportCustomers(cus,"export.customers") - ExportProjects(prs,"export.projects") - ExportTasks(tas,"export.tasks") + if isInterSure("Export ?"){ + ExportCustomers(cus,"export.customers") + ExportProjects(prs,"export.projects") + ExportTasks(tas,"export.tasks") + } icus := ImportCustomers("export.customers") c.Println("Imported Customers:",len(icus)) @@ -529,7 +530,8 @@ func interact(fulldb bool) { iprs := ImportProjects("export.projects") c.Println("Imported Projects:",len(iprs)) - c.Println("Projects:",iprs) + //c.Println("Projects:",iprs) + SaveImportsToDB(icus,iprs,itas) }, }) @@ -65,6 +65,12 @@ type bill struct { items []billitem } +// Useful to Map some old ids to New ones +type IdMap struct { + Old int + New int +} + var db *sql.DB var err error var currproject Project @@ -430,6 +436,123 @@ func loadBills(in []int) (out []bill) { return } +// Save Customer in DB and return its new id +func SaveCustomer(in Customer) (int) { + //tila := in.Lastbill.Local().Format("2006-01-02 15:04 MST") + tila := in.Lastbill.Local().Format("2006-01-02 15:04") + stmt, err := db.Prepare("INSERT INTO customers (company, name ,address ,satz ,lastbill) values(?, ?, ?, ?, ?)") + checkErr(err) + res, err := stmt.Exec(in.Company,in.Name,in.Address,in.Satz,tila) + checkErr(err) + newid, err := res.LastInsertId() + checkErr(err) + return int(newid) +} + +// Save Project in DB and return its new id +func SaveProject(in Project) (int) { + tifi := in.First.Local().Format("2006-01-02 15:04") + tila := in.Last.Local().Format("2006-01-02 15:04") + stmt, err := db.Prepare("INSERT INTO projects (name ,comment ,first ,last ,finished ,customer) values(?, ?, datetime(?,'utc'), datetime(?,'utc'), ?, ?)") + checkErr(err) + res, err := stmt.Exec(in.Name,in.Comment,tifi,tila,in.Finished,in.Customer) + checkErr(err) + newid, err := res.LastInsertId() + checkErr(err) + return int(newid) +} + +// Save Task in DB +func SaveTask(in Task) { + tista := in.Start.Local().Format("2006-01-02 15:04") + tisto := in.Stop.Local().Format("2006-01-02 15:04") + stmt, err := db.Prepare("INSERT INTO timetable (project, start ,stop ,task ,comment ,checkout) values(?, datetime(?,'utc'), datetime(?,'utc'), ?, ?, ?)") + checkErr(err) + _, err = stmt.Exec(in.Projectid, tista, tisto, in.Taskname, in.Comment, in.Checkout) + checkErr(err) +} + +// Loop through Array of []Projects and call SaveProject +func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + var cusids, prids []IdMap + //var acc,rej []Project + fmt.Println(frame(boldGreen("Saving Imported Data to DB"),true)) + fmt.Println(nli," Customers:",len(cus)) + fmt.Println(nli," Projects:",len(prs)) + fmt.Println(nli," Tasks:",len(tsk)) + fmt.Println(sub("")) + for _,c := range cus { + id := SaveCustomer(c) + cusids=append(cusids,IdMap{c.Id,id}) + } + fmt.Println(cusids) + prs = ChangeCustomerIds(cusids,prs) + for _,p := range prs { + id := SaveProject(p) + prids=append(prids,IdMap{p.Id,id}) + } + fmt.Println(prids) + tsk = ChangeProjectIds(prids,tsk) + for _,t := range tsk { + SaveTask(t) + } +} + +// Swap the old Ids in []Project array from imported +// cutsomers to the new ones +func ChangeCustomerIds(idm []IdMap, prs []Project) ([]Project) { + out := prs + for ip,p := range prs { + out[ip].Customer = GetMapId(p.Customer,idm) + } + return out +} + +// Swap the old Ids in []Task array from imported +// projects to the new ones +func ChangeProjectIds(idm []IdMap, tsk []Task) ([]Task) { + out := tsk + for it,t := range tsk { + out[it].Projectid = GetMapId(t.Projectid,idm) + } + return out +} + +// Check which entry holds the old id and return the new one +func GetMapId(id int, idm []IdMap) (int) { + if id == 0 { + return 0 + } + some := 0 + for _,i := range idm { + if id == i.Old { + some = i.New + } + } + return some +} + +/* + fmt.Println(nli,"Rejected Entries:",len(rej)," Accepted Entries:",len(acc)) + if len(rej) > 0 { + fmt.Println(sub("Rejections")) + for _,r := range rej { + fmt.Printf("%s %v: %s (%s - %s) - Fin: %v, Cutomer: %v\n",nli ,r.Id,r.Name,r.First.Local().Format("2006-01-02 15:04 MST"),r.Last.Local().Format("2006-01-02 15:04 MST"),r.Finished,r.Customer) + } + fmt.Println(nli) + } + if len(acc) > 0 { + fmt.Println(sub("Accepted and Imported")) + for _,r := range acc { + fmt.Printf("%s %v: %s (%s - %s) - Fin: %v, Cutomer: %v\n",nli ,r.Id,r.Name,r.First.Local().Format("2006-01-02 15:04 MST"),r.Last.Local().Format("2006-01-02 15:04 MST"),r.Finished,r.Customer) + } + fmt.Println(nli) + } + fmt.Println(frame("",false)) + */ + + func strings2items(tasks, times, hours, moneys string) (out []billitem) { ta := String2StringArray(tasks, ";") ti := String2StringArray(times, ";") |
