diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2019-03-19 22:06:05 +0100 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2019-03-19 22:06:05 +0100 |
| commit | c532f7e46109bc18143c49a976db1efb34493c6e (patch) | |
| tree | d61fef7d9ef9262baafa355e7fd90bf2e0f4fcfa | |
| parent | ef18e31975eb998bdc4ed922c7b77281721d08d4 (diff) | |
Bills export und import0.3.8
| -rw-r--r-- | importexport.go | 70 | ||||
| -rw-r--r-- | interact.go | 7 | ||||
| -rw-r--r-- | sqlite.go | 56 |
3 files changed, 127 insertions, 6 deletions
diff --git a/importexport.go b/importexport.go index 92d028e..5dfb9f3 100644 --- a/importexport.go +++ b/importexport.go @@ -16,11 +16,29 @@ const ( taskfile string = "/tmp/laboravi.export.tasks" custfile string = "/tmp/laboravi.export.customers" projfile string = "/tmp/laboravi.export.projects" + billfile string = "/tmp/laboravi.export.bills" //taskfile string = "laboravi.export.tasks" //custfile string = "laboravi.export.customers" //projfile string = "laboravi.export.projects" ) +func ExportBills(in []bill) { + filename := billfile + file ,err := os.Create(filename) + checkErr(err) + defer file.Close() + + w := csv.NewWriter(file) + defer w.Flush() + for _,i := range in { + tsks, tms, hours, mnys := items2strings(i.items) + //str := []string{Sint(i.Id),Sint(i.Projectid),i.Start.Format(time.RFC3339),i.Stop.Format(time.RFC3339),i.Taskname,i.Comment,Sint(i.Checkout)} + str := []string{Sint(i.id),i.identity,i.timerange,Sint(i.project),i.projectname,i.date.Format(timeform),i.paid.Format(timeform),tsks,tms,hours,mnys} + w.Write(str) + //w.WriteStruct(i) + } +} + func ExportTasks(in []Task) { filename := taskfile file ,err := os.Create(filename) @@ -70,6 +88,54 @@ func ExportCustomers(in []Customer) { } } +// Import an array of Bills from a csv of given filename +func ImportBills() ([]bill) { + filename := billfile + //file ,err := os.Create(filename) + var bll []bill + //w := NewReader(strings.NewReader()) + f, _ := os.Open(filename) + + // Create a new reader. + r := csv.NewReader(bufio.NewReader(f)) + //err := r.ReadStructAll(&cust) + //checkErr(err) + lines, err := r.ReadAll() + checkErr(err) + + for _, line := range lines { + i, err := strconv.Atoi(line[0]) + checkErr(err) + ide := line[1] + timr := line[2] + pr, err := strconv.Atoi(line[3]) + checkErr(err) + prn := line[4] + dat,err := time.Parse(timeform,line[6]) + checkErr(err) + pai,err := time.Parse(timeform,line[5]) + checkErr(err) + tsks := line[7] + dats := line[8] + hrs := line[9] + mos := line[10] + its := strings2items(tsks,dats,hrs,mos) + data := bill{ + id: i, + identity: ide, + timerange: timr, + project: pr, + projectname: prn, + date: dat, + paid: pai, + items: its, + } + bll=append(bll,data) + + } + return bll +} + // Import an array of Tasks from a csv of given filename func ImportTasks() ([]Task) { filename := taskfile @@ -193,7 +259,7 @@ func TarExports(filename string) { cmd := "tar" fln := fmt.Sprintf("/tmp/%s",filename) //args := []string{"-cf",filename,"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"} - args := []string{"-cf",fln,taskfile,projfile,custfile} + args := []string{"-cf",fln,taskfile,projfile,custfile,billfile} if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil { //panic(err) } @@ -233,7 +299,7 @@ func PurgeTemps() { ) cmd := "rm" //args := []string{"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"} - args := []string{taskfile,projfile,custfile} + args := []string{taskfile,projfile,custfile,billfile} //args := []string{"/tmp/laboravi.export.*"} if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil { //panic(err) diff --git a/interact.go b/interact.go index d5bf1dc..143df51 100644 --- a/interact.go +++ b/interact.go @@ -567,19 +567,23 @@ func interact(fulldb bool) { prid := GetProjectIds() cuid := GetCustomerIds() taid := GetTaskIds() + //biid := GetBillIds() prs := GetSelectedProjects(prid) cus := GetSelectedCustomers(cuid) tas := GetSelectedTasks(taid) + bls := GetAllBills() c.Println(frame(boldGreen("Export DB Data"),true)) c.Println(nli+"Customers:",len(cus)) c.Println(nli+" Projects:",len(prs)) c.Println(nli+" Tasks:",len(tas)) + c.Println(nli+" Bills:",len(bls)) //c.Println("Customers:",cus) if isInterSure(sli+"Export this data?"){ filen := getNewInterInput("Filename: ","export.tar",nli) ExportCustomers(cus) ExportProjects(prs) ExportTasks(tas) + ExportBills(bls) TarExports(filen) PurgeTemps() c.Println(nli+"Data exported to",filen) @@ -611,13 +615,14 @@ func interact(fulldb bool) { icus := ImportCustomers() itas := ImportTasks() iprs := ImportProjects() + ibls := ImportBills() PurgeTemps() //c.Println(sli,"Loaded Data") //c.Println(nli,"Customers:",len(icus)) //c.Println(nli," Projects:",len(iprs)) //c.Println(nli," Tasks:",len(itas)) //if isInterSure(sli+"Import this into DB?"){ - SaveImportsToDB(icus,iprs,itas) + SaveImportsToDB(icus,iprs,itas,ibls) //c.Println(boldGreen("Import Successful")) //c.Println(frame(posR(),false)) @@ -412,8 +412,10 @@ func showLastBills(count int) { hsum := sumFloatArray(string2FloatArray(hourstr, ";")) msum := sumFloatArray(string2FloatArray(moneystr, ";")) fmt.Printf("%s %v:%s - %s (%v) %.1f[h]: %.2f[€] - ",nli, id, ident, prn, date.Local().Format("2006.01.02"), hsum, msum) - p := fmt.Sprintf("%v", paid) - if p == "1791-09-30 19:07:00 +0000 UTC" { + p := fmt.Sprintf("%v", paid.Local().Format("2006.01.02")) + //fmt.Println(p) + //if p == "1791-09-30 19:07:00 +0000 UTC" { + if p == "1791.09.30" { fmt.Print(boldRed("OPEN\n")) } else { fmt.Printf(boldGreen("%v\n"), paid.Local().Format("2006.01.02")) @@ -484,8 +486,19 @@ func SaveTask(in Task) { checkErr(err) } +// Save Bill in DB +func SaveBill(in bill) { + tidat := in.date.Local().Format("2006-01-02 15:04") + tipai := in.paid.Local().Format("2006-01-02 15:04") + tsks,dats,tims,mos := items2strings(in.items) + stmt, err := db.Prepare("INSERT INTO bills (identity, timerange, project, tasks, times, hours, moneys, paid, date) values(?, ?, ?,?, ?, ?, ?, datetime(?,'utc'), datetime(?,'utc'))") + checkErr(err) + _, err = stmt.Exec(in.identity, in.timerange, in.project, tsks, dats, tims,mos, tidat, tipai) + checkErr(err) +} + // Loop through Array of []Customers, []Projects and []Tasks, update new db ids and call their respected SaveFunctions -func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { +func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task, bls []bill) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() var cusids, prids []IdMap //var acc,rej []Project @@ -493,6 +506,7 @@ func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { fmt.Println(nli," Customers:",len(cus)) fmt.Println(nli," Projects:",len(prs)) fmt.Println(nli," Tasks:",len(tsk)) + fmt.Println(nli," Bills:",len(bls)) fmt.Println(sub("")) if isInterSure(nli+" Are You sure to Import them all?") { i := 0 @@ -514,6 +528,11 @@ func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task) { SaveTask(t) i++ } + bls = ChangeBillIds(prids,bls) + for _,t := range bls { + SaveBill(t) + i++ + } fmt.Println(nli,i,"items imported") fmt.Println(frame(posR(),false)) }else{ @@ -541,6 +560,16 @@ func ChangeProjectIds(idm []IdMap, tsk []Task) ([]Task) { return out } +// Swap the old Ids in []bill array from imported +// projects to the new ones +func ChangeBillIds(idm []IdMap, bll []bill) ([]bill) { + out := bll + for it,t := range bll { + out[it].project = GetMapId(t.project,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 { @@ -1118,6 +1147,27 @@ func GetTaskIds() []int { } return ids } +// Returns Slice of All Bills +func GetAllBills() (out []bill) { + //var ids []int + rows, err := db.Query("SELECT * FROM bills") // ORDER BY id DESC") + checkErr(err) + var id,prj int + var ide,trg,prn string + var dat,pai time.Time + var itms []billitem + var tsks,tims,hrs,mos string + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&id,&ide,&trg,&prj,&tsks,&tims,&hrs,&mos,&pai,&dat) + checkErr(err) + prn,_ = getProjectName(id) + itms = strings2items(tsks,tims,hrs,mos) + out = append(out, bill{id,ide,trg,prj,prn,dat,pai,itms}) + } + return +} // Get All Tasks of Project with prid as a slice of Tasknames and // a curresponding slice of strings to display |
