summaryrefslogtreecommitdiff
path: root/sqlite.go
diff options
context:
space:
mode:
authorNikolaus Gotsche <n@softwarefools.com>2018-09-05 04:10:54 +0200
committerNikolaus Gotsche <n@softwarefools.com>2018-09-05 04:10:54 +0200
commit4a4f372605d56f2241699da6a5bf8dae7eda2b86 (patch)
tree80025d8ee00a1ea37ff94b351ca720b7e2fc110e /sqlite.go
parentd6dd3088da980467909a5a43fb393260c3ff7c93 (diff)
TOML Configuration added. texification working. outsourced some functions to my utilitys. billed tasks not yet checked
Diffstat (limited to 'sqlite.go')
-rw-r--r--sqlite.go96
1 files changed, 88 insertions, 8 deletions
diff --git a/sqlite.go b/sqlite.go
index 79ef4a7..f51a459 100644
--- a/sqlite.go
+++ b/sqlite.go
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
"os"
- "bufio"
+ //"bufio"
"strings"
"regexp"
"strconv"
@@ -42,6 +42,24 @@ type customer struct{
lastbill time.Time
}
+type billitem struct{
+ Task string
+ Time string
+ Hours float64
+ Money float64
+}
+
+type bill struct{
+ id int
+ identity string //invoice number
+ timerange string
+ project int
+ projectname string
+ date time.Time
+ paid time.Time
+ items []billitem
+}
+
var db *sql.DB
var err error
var currproject project
@@ -160,7 +178,7 @@ func newTask(proj int) {
}
}
-func newBill(proj int) (int) {
+func newBill(proj int) (int,string) {
boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc()
//boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
fmt.Println(boldGreen("Creating New Bill"))
@@ -173,7 +191,65 @@ func newBill(proj int) (int) {
checkErr(err)
lid,_ := answ.LastInsertId()
fmt.Println("Bill",invno,"Created with ID",lid)
- return int(lid)
+ return int(lid), invno
+}
+
+func saveBill(in bill) {
+ boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc()
+ tasks,times,hours,moneys := items2strings(in.items)
+ fmt.Println(boldGreen("Saving Bill"),in.id)
+ stmt, err := db.Prepare("UPDATE bills SET identity = ?, timerange = ?, tasks = ?, times = ?, hours = ?, moneys = ? WHERE id = ?")
+ checkErr(err)
+ _, err = stmt.Exec(in.identity,in.timerange,tasks,times,hours,moneys,in.id)
+ checkErr(err)
+}
+
+func loadBills(in []int) (out []bill){
+ ins := strings.Trim(strings.Replace(fmt.Sprint(in)," "," , ",-1),"[]")
+ que := fmt.Sprintf("SELECT * FROM bills WHERE id IN (%s) ORDER BY project DESC",ins)
+ rows,err := db.Query(que)
+
+ var id,proj int
+ var ident,timerange string
+ var date,paid time.Time
+ var taskstr,timestr,hourstr,moneystr string
+
+ defer rows.Close()
+ for rows.Next() {
+ err = rows.Scan(&id,&ident,&timerange,&proj,&taskstr,&timestr,&hourstr,&moneystr,&paid,&date)
+ checkErr(err)
+ itms := strings2items(taskstr,timestr,hourstr,moneystr)
+ prname,_ := getProjectName(proj)
+ bi := bill{id,ident,timerange,proj,prname,date,paid,itms}
+ out = append(out,bi)
+ }
+ return
+}
+func strings2items(tasks, times, hours, moneys string) (out []billitem) {
+ ta := string2StringArray(tasks,";")
+ ti := string2StringArray(times,";")
+ ho := string2FloatArray(hours,";")
+ mo := string2FloatArray(moneys,";")
+ for i,_ := range ta{
+ out = append(out,billitem{ta[i],ti[i],ho[i],mo[i]})
+ }
+ return
+}
+
+func items2strings(in []billitem) (tasks, times, hours, moneys string) {
+ var tsk,tim []string
+ var hrs,mny []float64
+ for _,item := range in {
+ tsk = append(tsk,item.Task)
+ tim = append(tim,item.Time)
+ hrs = append(hrs,item.Hours)
+ mny = append(mny,item.Money)
+ }
+ tasks = stringArray2String(tsk,";")
+ times = stringArray2String(tim,";")
+ hours = strings.Trim(strings.Replace(fmt.Sprint(hrs)," ",";",-1),"[]")
+ moneys = strings.Trim(strings.Replace(fmt.Sprint(mny)," ",";",-1),"[]")
+ return
}
func closeTaskTime(tim string) {
@@ -385,7 +461,8 @@ func analyzeTasks(in []task) (count int, hours float64, duration string) {
}
//txt := fmt.Sprintf("%s - (%v) - %.2f h",task, durstr, dur)
}
- duration = fmt.Sprintf("%v - %v",lstart.Local().Format("01.02.2006"),hstop.Local().Format("01.02.2006"))
+ duration = fmt.Sprintf("%v - %v",lstart.Local().Format("01.02."),hstop.Local().Format("01.02.2006"))
+ //duration = fmt.Sprintf("%v - %v",lstart.Local().Format("01.02.2006"),hstop.Local().Format("01.02.2006"))
return
}
@@ -728,7 +805,7 @@ func deleteTask(id int) {
fmt.Println(boldGreen("Delete Task", id))
dur := float64(stop.Sub(start))/(1000000000*60*60)
fmt.Printf("%v: %v (%v-%v) - %.2f h\n",prj,task,start.Local().Format("2006 Mon Jan _2 15:04"),stop.Local().Format("15:04"),dur)
- if isSure() {
+ if isSure("Are You Sure?") {
stmt, err := db.Prepare("DELETE FROM timetable WHERE id = ?")
checkErr(err)
_, err = stmt.Exec(id)
@@ -991,6 +1068,7 @@ func isTime(in string) bool {
return match
}
+/*
func getInput(quest string) string {
fmt.Print(quest)
in := bufio.NewReader(os.Stdin)
@@ -998,8 +1076,9 @@ func getInput(quest string) string {
line = strings.TrimSuffix(line, "\n")
checkErr(err)
return line
-}
+}*/
+/*
func isSure() bool {
fmt.Print("Are You Sure ? (type 'yes' to confirm) : ")
in := bufio.NewReader(os.Stdin)
@@ -1012,7 +1091,7 @@ func isSure() bool {
} else {
return false
}
-}
+}*/
func isElement(some int, group []int) (bool) {
for _,e := range group {
@@ -1033,8 +1112,9 @@ func removeItems(master, rem []int) ([]int) {
return out
}
+/*
func checkErr(err error) {
if err != nil {
panic(err)
}
-}
+}*/