From b5bc66a01b24b5a10c04de584c1681898308124b Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Mon, 29 Apr 2019 01:51:36 +0200 Subject: Uncheck Bills --- sqlite.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'sqlite.go') diff --git a/sqlite.go b/sqlite.go index c304d77..adaba17 100644 --- a/sqlite.go +++ b/sqlite.go @@ -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() -- cgit v1.2.3