diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2019-05-16 19:00:12 +0200 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2019-05-16 19:00:12 +0200 |
| commit | 19d611f02ebf83f751c0f21e4bcaf56377711c8a (patch) | |
| tree | 10a7f103616c040b0ecfbd488adea2d02ca295c2 | |
| parent | 0c4c5ac1ba534425716c80d27dc72817718f0367 (diff) | |
Payments Import/Export0.4.5
| -rw-r--r-- | analyze.go | 2 | ||||
| -rw-r--r-- | importexport.go | 60 | ||||
| -rw-r--r-- | interact.go | 6 | ||||
| -rw-r--r-- | sqlite.go | 88 |
4 files changed, 127 insertions, 29 deletions
@@ -42,6 +42,8 @@ var yearly []Year //Analyze all tasks and PAyments func MakeAnalysis() { fmt.Println("Making Analysis") + analysis = nil + yearly = nil tids := GetTaskIds() alltasks := GetSelectedTasks(tids) diff --git a/importexport.go b/importexport.go index 5dfb9f3..dde6026 100644 --- a/importexport.go +++ b/importexport.go @@ -17,11 +17,27 @@ const ( custfile string = "/tmp/laboravi.export.customers" projfile string = "/tmp/laboravi.export.projects" billfile string = "/tmp/laboravi.export.bills" + payfile string = "/tmp/laboravi.export.payments" //taskfile string = "laboravi.export.tasks" //custfile string = "laboravi.export.customers" //projfile string = "laboravi.export.projects" ) +func ExportPayments(in []Payment) { + filename := payfile + file ,err := os.Create(filename) + checkErr(err) + defer file.Close() + + w := csv.NewWriter(file) + defer w.Flush() + for _,i := range in { + str := []string{Sint(i.Id),Sint(i.Customerid),Sflo(i.Amount),i.Date.Format(timeform),IntArray2String(i.Billids,";")} + w.Write(str) + //w.WriteStruct(i) + } +} + func ExportBills(in []bill) { filename := billfile file ,err := os.Create(filename) @@ -33,7 +49,7 @@ func ExportBills(in []bill) { 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} + 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,Sint(i.paymentid)} w.Write(str) //w.WriteStruct(i) } @@ -88,6 +104,40 @@ func ExportCustomers(in []Customer) { } } +func ImportPayments() (pay []Payment) { + filename := payfile + + f, _ := os.Open(filename) + + // Create a new reader. + r := csv.NewReader(bufio.NewReader(f)) + lines, err := r.ReadAll() + checkErr(err) + + for _, line := range lines { + i, err := strconv.Atoi(line[0]) + checkErr(err) + icu, err := strconv.Atoi(line[1]) + checkErr(err) + amt,err := strconv.ParseFloat(line[2],64) + checkErr(err) + dat,err := time.Parse(timeform,line[3]) + checkErr(err) + billi := String2IntArray(line[4],";") + + data := Payment{ + Id: i, + Customerid: icu, + Amount: amt, + Date: dat, + Billids: billi, + } + pay=append(pay,data) + } + return +} + + // Import an array of Bills from a csv of given filename func ImportBills() ([]bill) { filename := billfile @@ -119,6 +169,9 @@ func ImportBills() ([]bill) { dats := line[8] hrs := line[9] mos := line[10] + pid, err := strconv.Atoi(line[11]) + checkErr(err) + its := strings2items(tsks,dats,hrs,mos) data := bill{ id: i, @@ -129,6 +182,7 @@ func ImportBills() ([]bill) { date: dat, paid: pai, items: its, + paymentid: pid, } bll=append(bll,data) @@ -259,7 +313,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,billfile} + args := []string{"-cf",fln,taskfile,projfile,custfile,billfile,payfile} if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil { //panic(err) } @@ -299,7 +353,7 @@ func PurgeTemps() { ) cmd := "rm" //args := []string{"/tmp/laboravi.export.tasks","/tmp/laboravi.export.customers","/tmp/laboravi.export.projects"} - args := []string{taskfile,projfile,custfile,billfile} + args := []string{taskfile,projfile,custfile,billfile,payfile} //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 ab5a950..71e40a3 100644 --- a/interact.go +++ b/interact.go @@ -817,11 +817,13 @@ func interact(fulldb bool) { cus := GetSelectedCustomers(cuid) tas := GetSelectedTasks(taid) bls := GetAllBills() + pys := GetAllPayments() 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(nli+" Payments:",len(pys)) //c.Println("Customers:",cus) if isInterSure(sli+"Export this data?"){ filen := getNewInterInput("Filename: ","export.tar",nli) @@ -829,6 +831,7 @@ func interact(fulldb bool) { ExportProjects(prs) ExportTasks(tas) ExportBills(bls) + ExportPayments(pys) TarExports(filen) PurgeTemps() c.Println(nli+"Data exported to",filen) @@ -861,13 +864,14 @@ func interact(fulldb bool) { itas := ImportTasks() iprs := ImportProjects() ibls := ImportBills() + ipys := ImportPayments() 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,ibls) + SaveImportsToDB(icus,iprs,itas,ibls,ipys) //c.Println(boldGreen("Import Successful")) //c.Println(frame(posR(),false)) @@ -541,7 +541,7 @@ func SavePayment(in Payment) (int) { tila := in.Date.Local().Format("2006-01-02 15:04") billids := IntArray2String(in.Billids,";") if in.Id == 0 { - stmt, err := db.Prepare("INSERT INTO payments (customerid, amount ,bills ,date) values(?, ?, ?, ?)") + stmt, err := db.Prepare("INSERT INTO payments (customerid, amount ,bills ,date) values(?, ?, ?, datetime(?,'utc'))") checkErr(err) res, err := stmt.Exec(in.Customerid,in.Amount,billids,tila) checkErr(err) @@ -549,7 +549,7 @@ func SavePayment(in Payment) (int) { checkErr(err) return int(newid) } else { - stmt, err := db.Prepare("UPDATE payments SET customerid = ? , amount = ? , bills = ? , date = ? ) WHERE id = ?") + stmt, err := db.Prepare("UPDATE payments SET customerid = ? , amount = ? , bills = ? , date = datetime(?,'utc') WHERE id = ?") checkErr(err) _, err = stmt.Exec(in.Customerid,in.Amount,billids,tila,in.Id) checkErr(err) @@ -581,26 +581,30 @@ func SaveTask(in Task) { } // Save Bill in DB -func SaveBill(in bill) { +func SaveBill(in bill) (int) { 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'))") + stmt, err := db.Prepare("INSERT INTO bills (identity, timerange, project, tasks, times, hours, moneys, paid, date, paymentid) values(?, ?, ?,?, ?, ?, ?, datetime(?,'utc'), datetime(?,'utc'), ?)") checkErr(err) - _, err = stmt.Exec(in.identity, in.timerange, in.project, tsks, dats, tims,mos, tidat, tipai) + res, err := stmt.Exec(in.identity, in.timerange, in.project, tsks, dats, tims,mos, tidat, tipai,in.paymentid) checkErr(err) + newid, err := res.LastInsertId() + checkErr(err) + return int(newid) } // 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, bls []bill) { +func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task, bls []bill, pays []Payment) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() - var cusids, prids []IdMap + var cusids, prids, bilids, payids []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(nli," Bills:",len(bls)) + fmt.Println(nli," Payments:",len(pays)) fmt.Println(sub("")) if isInterSure(nli+" Are You sure to Import them all?") { i := 0 @@ -610,22 +614,35 @@ func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task, bls []bill) { i++ } //fmt.Println(cusids) - prs = ChangeCustomerIds(cusids,prs) + prs,pays = ChangeCustomerIds(cusids,prs,pays) + for _,py := range pays { + p := py + p.Id = 0 // Special behaviour aof SavePayments! id ==0 -> INSERT; id!=0 UPDATE + id := SavePayment(p) + payids=append(payids,IdMap{py.Id,id}) + fmt.Println("DEBUG PayId Old:",py.Id,"New:", id) + i++ + } for _,p := range prs { id := SaveProject(p) prids=append(prids,IdMap{p.Id,id}) i++ } + bls = ChangeBillIds(prids,payids,bls) + for _,t := range bls { + id := SaveBill(t) + bilids=append(bilids,IdMap{t.id,id}) + i++ + } //fmt.Println(prids) - tsk = ChangeProjectIds(prids,tsk) + tsk = ChangeTaskIds(prids,bilids,tsk) for _,t := range tsk { SaveTask(t) i++ } - bls = ChangeBillIds(prids,bls) - for _,t := range bls { - SaveBill(t) - i++ + pays = ChangePaymentIds(bilids,payids,pays) + for _,py := range pays { + SavePayment(py) } fmt.Println(nli,i,"items imported") fmt.Println(frame(posR(),false)) @@ -634,32 +651,53 @@ func SaveImportsToDB(cus []Customer, prs []Project, tsk []Task, bls []bill) { } } -// Swap the old Ids in []Project array from imported +// Swap the old Ids in []Projecti and []Payment array from imported // cutsomers to the new ones -func ChangeCustomerIds(idm []IdMap, prs []Project) ([]Project) { - out := prs +func ChangeCustomerIds(idm []IdMap, prs []Project, pays []Payment) ([]Project, []Payment) { + outpr := prs + outpy := pays for ip,p := range prs { - out[ip].Customer = GetMapId(p.Customer,idm) + outpr[ip].Customer = GetMapId(p.Customer,idm) } - return out + for ipy,py := range pays { + outpy[ipy].Customerid = GetMapId(py.Customerid,idm) + } + return outpr, outpy } // Swap the old Ids in []Task array from imported -// projects to the new ones -func ChangeProjectIds(idm []IdMap, tsk []Task) ([]Task) { +// projects and bills to the new ones +func ChangeTaskIds(idpr,idbi []IdMap, tsk []Task) ([]Task) { out := tsk for it,t := range tsk { - out[it].Projectid = GetMapId(t.Projectid,idm) + out[it].Projectid = GetMapId(t.Projectid,idpr) + out[it].Checkout = GetMapId(t.Checkout,idbi) } return out } // Swap the old Ids in []bill array from imported -// projects to the new ones -func ChangeBillIds(idm []IdMap, bll []bill) ([]bill) { +// projects and payments to the new ones +func ChangeBillIds(idpr,idpay []IdMap, bll []bill) ([]bill) { out := bll for it,t := range bll { - out[it].project = GetMapId(t.project,idm) + out[it].project = GetMapId(t.project,idpr) + out[it].paymentid = GetMapId(t.paymentid,idpay) + } + return out +} + +// Swap the old Bill Ids in []Payment array from imported +// bills to the new ones +func ChangePaymentIds(idbi,idpy []IdMap, pay []Payment) ([]Payment) { + out := pay + for i,p := range pay { + var nids []int + for _,id := range p.Billids { + nids = append(nids,GetMapId(id,idbi)) + } + out[i].Id = GetMapId(p.Id,idpy) + out[i].Billids = nids } return out } @@ -1339,7 +1377,7 @@ func GetAllBills() (out []bill) { defer rows.Close() for rows.Next() { - err = rows.Scan(&id,&ide,&trg,&prj,&tsks,&tims,&hrs,&mos,&pai,&dat) + err = rows.Scan(&id,&ide,&trg,&prj,&tsks,&tims,&hrs,&mos,&pai,&dat,&payid) checkErr(err) prn,_ = getProjectName(id) itms = strings2items(tsks,tims,hrs,mos) |
