From 64b47d83073893b1442fd250005775f25ba4403e Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Tue, 30 Apr 2019 01:54:21 +0200 Subject: EditPayment --- sqlite.go | 242 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 162 insertions(+), 80 deletions(-) (limited to 'sqlite.go') diff --git a/sqlite.go b/sqlite.go index adaba17..b9234b1 100644 --- a/sqlite.go +++ b/sqlite.go @@ -1559,6 +1559,7 @@ func AddPayment(dat string) { 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) + sum += bsum } if amtfl != sum { fmt.Println(sli,boldRed(" The sum of the bills (",sum,"€) does not match Payment amount!")) @@ -1964,11 +1965,13 @@ func DeletePayment(id int) { bsum := 0.0 hsum := 0.0 for _,bi := range mybills{ + bisum := 0.0 for _,it := range bi.items { - bsum += it.Money + bisum += 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) + bsum += bisum } if amount != bsum { fmt.Println(sli,boldRed(" The sum of the bills (",bsum,"€ for",hsum,"h) does not match Payment amount!")) @@ -2064,6 +2067,149 @@ func deleteTask(id int) { fmt.Println(frame("",false)) } +// Edit a Payment of given id +func EditPayment(id int) { + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + fmt.Println(frame(boldGreen("Editing 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) + } else { + fmt.Println(nli+boldRed("There Is No Payment"), id) + return + } + //comp = getNewInterInput("New Company Name: ", comp, nli) + for { + newcu := getNewInterInput("New Customer id: ", fmt.Sprint(cuid), nli) + cuid, err = strconv.Atoi(newcu) + if err != nil { + fmt.Println(nli,newcu, boldRed("is Not a Valid id."), "Try an Integer instead") + } + if !isCustomer(cuid) { + fmt.Println(nli, boldRed("There is no Customer"), cuid) + } else { + break + } + } + for { + amtstr := getNewInterInput("New Amount: ", fmt.Sprintf("%.2f", amount), nli) + amount, err = strconv.ParseFloat(amtstr, 64) + if err != nil { + fmt.Println(nli,amtstr, boldRed("can not be Parsed as a Float."), "Try a shape of X.X") + } else { + break + } + } + datstr := date.Local().Format("2006-01-02") + for { + newdatstr := getNewInterInput("New Date: ", datstr, nli) + if !isDate(newdatstr) { + fmt.Println(nli, newdatstr, boldRed("is Not a Valid Datestring!"), "use: 'YYYY-MM-DD'") + } else { + datstr = newdatstr+" 12:01" + break + } + } + fmt.Println(sub("Bills")) + biids := String2IntArray(billstr,";") + mybills := loadBills(biids) + + bsum := 0.0 + hsum := 0.0 + for _,bi := range mybills{ + bisum := 0.0 + for _,it := range bi.items { + bisum += 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,bisum) + bsum += bisum + } + if amount != bsum { + fmt.Println(sub("")) + fmt.Println(nli,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+"Do You want to renew Selection?") { + for _,i := range biids { + uncheckBill(i) + } + billids := GetBillIds(true) + listids,billlist := GetBillList(billids) + var selids []int + for{ + res := Checklist("What Bills are includes in this Payment",billlist) + out := func() (c []int) { + for _,v := range res { + if v > -1 { + c = append(c, listids[v]) + } + } + return + } + selids = out() + if len(selids)>0{ + billstr = IntArray2String(selids,";") + break + }else{ + fmt.Println(boldRed("At least one Bill should Correspont to a payment!")) + getInterInput("") + } + } + } + stmt, err := db.Prepare("UPDATE payments SET customerid = ?, amount = ?, bills = ?, date = datetime(?,'utc') WHERE id = ?") + checkErr(err) + _, err = stmt.Exec(cuid, amount, billstr, datstr, id) + checkErr(err) + custr := getCustomerName(cuid) + + fmt.Println(frame(boldGreen("Editing Payment ", id),true)) + 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")) + fmt.Printf("%s %vx Sums: %.2f[€] %.2f[h]\n",nli,len(mybills),bsum,hsum) + + hsum = 0.0 + bsum = 0.0 + for _,bi := range mybills{ + bisum := 0.0 + for _,it := range bi.items { + bisum += 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,bisum) + bsum += bisum + } + if amount != bsum { + fmt.Println(sub("")) + fmt.Println(nli,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) ) + } + } + + fmt.Println(nli,"...Payment", id, "Updated") + fmt.Println(frame("",false)) +} + func editCustomer(id int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() @@ -2082,43 +2228,17 @@ func editCustomer(id int) { } rows.Close() //good habit to close - //fmt.Println(boldGreen("Edit Customer",id)) - /*fmt.Println("Old Company Name:",comp) - in := getInterInput("Enter New:") - if in!=""{ - comp=in - }*/ comp = getNewInterInput("New Company Name: ", comp, nli) - /*fmt.Println("Old Name:",name) - in = getInterInput("Enter New:") - if in!=""{ - name=in - }*/ name = getNewInterInput("New Customer Name: ", name, nli) - /*fmt.Println("Old Adress:",addr) - in = getInterInput("Enter New:") - if in!=""{ - addr=in - }*/ addr = getNewInterInput("New Adress: ", addr, nli) - //fmt.Println("Old Hourly Rate:",satz) for { satzstr := getNewInterInput("New Hourly Rate: ", fmt.Sprintf("%.2f", satz), nli) satz, err = strconv.ParseFloat(satzstr, 64) if err != nil { fmt.Println(nli,satzstr, boldRed("can not be Parsed as a Float."), "Try a shape of X.X") - //os.Exit(0) } else { break } - /*satzstr := getInterInput("Enter New:") - if satzstr!=""{ - satz,err = strconv.ParseFloat(satzstr,64) - if err != nil { - fmt.Println(satzstr,boldRed("can not be Parsed as a Float."), "Try a shape of X.X") - //os.Exit(0) - }else{break} - }else{break}*/ } stmt, err := db.Prepare("UPDATE customers SET company = ?, name = ?, address = ?, satz = ? WHERE id = ?") checkErr(err) @@ -2148,7 +2268,7 @@ func editBill(id int) { ident = getNewInterInput("New Bill Identifier: ", ident, nli) timran = getNewInterInput("New Timerange:", timran, nli) - paiyr := pai.Local().Format("2006-01-02") + //paiyr := pai.Local().Format("2006-01-02") paistr = pai.Local().Format("2006-01-02 15:04") datstr = dat.Local().Format("2006-01-02 15:04") for { @@ -2160,7 +2280,7 @@ func editBill(id int) { break } } - if paiyr == "1791-09-30" { + /*if paiyr == "1791-09-30" { fmt.Println(nli,boldRed("Open")) }else{ for { @@ -2172,7 +2292,7 @@ func editBill(id int) { break } } - } + }*/ for { fmt.Printf("%s !!Setting a different project id will not change the corresponding Tasks.\n%s Delete Bill to renew Task selection!!\n",nli,nli) newprj := getNewInterInput("New Project id: ", fmt.Sprint(prj), nli) @@ -2401,6 +2521,18 @@ func isTask(id int) bool { } } +func isPayment(id int) bool { + + rows, err := db.Query("SELECT * FROM payments WHERE id = $1", id) + checkErr(err) + defer rows.Close() + if rows.Next() { + return true + } else { + return false + } +} + func isProject(id int) bool { rows, err := db.Query("SELECT * FROM projects WHERE id = $1", id) @@ -2478,53 +2610,3 @@ func isTime(in string) bool { return false } -/* -func getInput(quest string) string { - fmt.Print(quest) - in := bufio.NewReader(os.Stdin) - line, err := in.ReadString('\n') - 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) - line, err := in.ReadString('\n') - line = strings.TrimSuffix(line, "\n") - checkErr(err) - - if line == "yes" { - return true - } else { - return false - } -}*/ -/* -func isElement(some int, group []int) bool { - for _, e := range group { - if e == some { - return true - } - } - return false -} - -func removeItems(master, rem []int) []int { - var out []int - for _, v := range master { - if !isElement(v, rem) { - out = append(out, v) - } - } - return out -}*/ - -/* -func checkErr(err error) { - if err != nil { - panic(err) - } -}*/ -- cgit v1.2.3