diff options
| -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") {  |