diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-12-03 20:53:42 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-12-03 20:53:42 +0530 |
commit | f169571cffeb3ac0404d56ac01ece90c33338113 (patch) | |
tree | 6d868c4a61a80f12a48350c23dc7bc4c803ccb85 | |
parent | d51f58b8fea22c9dfd64f3a1665d1994697dfa94 (diff) |
setting invoice number as 1 if no invoices existv0.2.1
-rw-r--r-- | invoice/service.go | 22 | ||||
-rw-r--r-- | main.go | 2 |
2 files changed, 22 insertions, 2 deletions
diff --git a/invoice/service.go b/invoice/service.go index 3dcc5e2..9408a0d 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -25,8 +25,28 @@ import ( func getNewInvoiceNumber(userId uint) (uint, error) { var i uint + // check if number of invoices is 0 + var count int64 + err := db.Model(&Invoice{}). + Where("user_id = ?", userId). + Count(&count). + Error + + if err != nil { + return i, err + } + + // if no records exist, then invoice number should be 1 + if count == 0 { + return 1, nil + } + + // if records exist, get max invoice number + // NOTE: if there are gaps in invoice numbers, + // they won't be filled and the series would continue + // from the greatest invoice number. row := db.Model(&Invoice{}).Where("user_id = ?", userId).Select("max(invoice_number)").Row() - err := row.Scan(&i) + err = row.Scan(&i) return i + 1, err } @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.2.0" +const OPENBILLS_VERSION = "v0.2.1" func init() { if !viper.GetBool("debug_mode") { |