diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2019-04-29 01:51:36 +0200 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2019-04-29 01:51:36 +0200 |
| commit | b5bc66a01b24b5a10c04de584c1681898308124b (patch) | |
| tree | 5be00d024b9f08ea8f09a7b7983e4533619db686 | |
| parent | 60210ae11b7777b4cd41e25c5e3db39be38e4cb5 (diff) | |
Uncheck Bills0.4.1
| -rw-r--r-- | interact.go | 41 | ||||
| -rw-r--r-- | sqlite.go | 73 |
2 files changed, 111 insertions, 3 deletions
diff --git a/interact.go b/interact.go index 4aae3ac..e3135d3 100644 --- a/interact.go +++ b/interact.go @@ -183,7 +183,7 @@ func interact(fulldb bool) { }) */ - // Delete Commands: Bill / Task + // Delete Commands: Bill / Task / Payment { delcmd := &ishell.Cmd{ Name: "delete", @@ -214,6 +214,37 @@ func interact(fulldb bool) { }, }) delcmd.AddCmd(&ishell.Cmd{ + Name: "payment", + Help: "<id> Delete the Payment with the following id", + LongHelp: ` Usage: delete payment <id> + Delete the payment of the set <id>`, + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + argi, err := strconv.Atoi(arg) + if err == nil { + DeletePayment(argi) + stdOut() + } else { + c.Println(boldRed(arg, " is not a valid id!")) + } + } else { + /* + tids := GetTaskIds() + selids, lids := GetPaymentList(tids, false,false) + choice := c.MultiChoice(lids, "Select a Task to Edit") + if choice > -1 { + deleteTask(selids[choice]) + } + */ + //c.Println(boldRed("deletetask <id> - Please enter an id")) + //allProjects() + } + c.Println(promptcol("______________________")) + }, + }) + delcmd.AddCmd(&ishell.Cmd{ Name: "task", Help: "<id> Delete a Task with the following id", LongHelp: ` Usage: delete task <id> @@ -227,12 +258,12 @@ func interact(fulldb bool) { deleteTask(argi) stdOut() } else { - c.Println(boldRed(arg, "is not a valid id!")) + c.Println(boldRed(arg, " is not a valid id!")) } } else { tids := GetTaskIds() selids, lids := GetTaskList(tids, false,false) - choice := c.MultiChoice(lids, "Select a Task to Edit") + choice := c.MultiChoice(lids, "Select a Task to Delete") if choice > -1 { deleteTask(selids[choice]) } @@ -245,6 +276,8 @@ func interact(fulldb bool) { shell.AddCmd(delcmd) } + // OPEN PROJECT COMMAND + shell.AddCmd(&ishell.Cmd{ Name: "project", Help: "<id> Open a Project of the following id", @@ -277,6 +310,8 @@ func interact(fulldb bool) { }, }) + // EDIT COMMAND + { editcmd := &ishell.Cmd{ Name: "edit", @@ -890,6 +890,21 @@ func checkBill(billid, payid, projectid int, datestr string) { //fmt.Println(frame(posR(),false)) } +func uncheckBill(billid int) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + if !isBill(billid) { + fmt.Println(nli,billid, boldRed("is not a known bill ID")) + //fmt.Println(frame(negR(),false)) + return + } + payid := 0 + datestr := "1791-09-30 19:07" + stmt, err := db.Prepare("UPDATE bills SET paid = datetime(?,'utc'), paymentid = ? WHERE id = ?") + checkErr(err) + _, err = stmt.Exec(datestr, payid, billid) + checkErr(err) +} + func checkTasks(in []int, billid int) { ins := strings.Trim(strings.Replace(fmt.Sprint(in), " ", " , ", -1), "[]") que := fmt.Sprintf("UPDATE timetable SET checkout = ? WHERE id IN (%s)", ins) @@ -1923,6 +1938,64 @@ func allProjects() { //fmt.Println("_______________________________\n") } +// Delete a Payment of given id +func DeletePayment(id int) { + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + fmt.Println(frame(boldGreen("Deleting Payment ", id),true)) + + var cuid int + var amount float64 + var date time.Time + var billstr string + rows, err := db.Query("SELECT customerid, amount, bills, date FROM payments WHERE id = $1", id) + checkErr(err) + if rows.Next() { + err = rows.Scan(&cuid, &amount, &billstr, &date) + checkErr(err) + rows.Close() //good habit to close + custr := getCustomerName(cuid) + fmt.Printf("%s From %s Amount %.2f [€]\n",nli,custr,amount) + fmt.Printf("%s Date: %s\n",nli,date.Local().Format("2006 Mon Jan _2")) + fmt.Println(sub("Bills")) + biids := String2IntArray(billstr,";") + mybills := loadBills(biids) + + bsum := 0.0 + hsum := 0.0 + for _,bi := range mybills{ + for _,it := range bi.items { + bsum += it.Money + hsum += it.Hours + } + fmt.Printf("%s %s : %s (%s) %.1f[h] %.2f[€]\n",nli,bi.identity,bi.projectname,bi.date.Format("2006-01-02 15:04"),hsum,bsum) + } + if amount != bsum { + fmt.Println(sli,boldRed(" The sum of the bills (",bsum,"€ for",hsum,"h) does not match Payment amount!")) + if amount > bsum { + fmt.Printf("%s %.2f € Overpaid\n",nli,(amount-bsum) ) + } + if amount < bsum { + fmt.Printf("%s %.2f € Underpaid\n",nli,(amount-bsum) ) + } + } + if isInterSure(sli+"Are You Sure?") { + for _,i := range biids { + uncheckBill(i) + } + stmt, err := db.Prepare("DELETE FROM payments WHERE id = ?") + checkErr(err) + _, err = stmt.Exec(id) + checkErr(err) + fmt.Println(nli,boldGreen("Payment ", id, " deleted successfully!")) + } + }else{ + fmt.Println(nli,boldRed("There is no Payment "),id) + rows.Close() //good habit to close + } + fmt.Println(frame("",false)) +} + func deleteBill(id int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() |
