diff options
Diffstat (limited to 'sqlite.go')
| -rw-r--r-- | sqlite.go | 88 |
1 files changed, 63 insertions, 25 deletions
@@ -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) |
