aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--invoice/controller.go13
-rw-r--r--invoice/service.go10
-rw-r--r--main.go2
3 files changed, 24 insertions, 1 deletions
diff --git a/invoice/controller.go b/invoice/controller.go
index 02bbaf5..7260834 100644
--- a/invoice/controller.go
+++ b/invoice/controller.go
@@ -101,6 +101,19 @@ func handleSaveInvoice (ctx *gin.Context) {
userId := uId.(uint)
invoice.UserID = userId
+ // if invoice number is 0, generate one!
+ if invoice.InvoiceNumber == 0 {
+ n, err := getNewInvoiceNumber(invoice.UserID)
+
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+
+ invoice.InvoiceNumber = n
+ }
+
err := invoice.upsert()
if err != nil {
ctx.Error(err)
diff --git a/invoice/service.go b/invoice/service.go
index 099f6a0..3dcc5e2 100644
--- a/invoice/service.go
+++ b/invoice/service.go
@@ -21,6 +21,16 @@ import (
e "vidhukant.com/openbills/errors"
)
+// returns greatest invoice number + 1
+func getNewInvoiceNumber(userId uint) (uint, error) {
+ var i uint
+
+ row := db.Model(&Invoice{}).Where("user_id = ?", userId).Select("max(invoice_number)").Row()
+ err := row.Scan(&i)
+
+ return i + 1, err
+}
+
func getInvoice(invoice *Invoice, id uint) error {
res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Find(&invoice, id)
diff --git a/main.go b/main.go
index 6595c7c..71abb27 100644
--- a/main.go
+++ b/main.go
@@ -38,7 +38,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.1.0"
+const OPENBILLS_VERSION = "v0.2.0"
func init() {
if !viper.GetBool("debug_mode") {