From b643f7852f93f73843aa5f52f9b4545321713e10 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 5 Oct 2023 15:29:50 +0530 Subject: added invoice route (barebones) --- invoice/controller.go | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ invoice/hooks.go | 57 +++++++++++++++++++ invoice/invoice.go | 61 ++++++++++++++++++++ invoice/router.go | 32 +++++++++++ invoice/service.go | 74 ++++++++++++++++++++++++ invoice/validators.go | 71 +++++++++++++++++++++++ 6 files changed, 448 insertions(+) create mode 100644 invoice/controller.go create mode 100644 invoice/hooks.go create mode 100644 invoice/invoice.go create mode 100644 invoice/router.go create mode 100644 invoice/service.go create mode 100644 invoice/validators.go (limited to 'invoice') diff --git a/invoice/controller.go b/invoice/controller.go new file mode 100644 index 0000000..efcaa40 --- /dev/null +++ b/invoice/controller.go @@ -0,0 +1,153 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + e "vidhukant.com/openbills/errors" + "github.com/gin-gonic/gin" + "net/http" + "strconv" +) + +func handleGetSingleInvoice (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + var invoice Invoice + + err = getInvoice(&invoice, uint(id)) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + if invoice.UserID != userId { + ctx.Error(e.ErrForbidden) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": invoice, + }) +} + +func handleGetInvoices (ctx *gin.Context) { + var invoices []Invoice + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + err := getInvoices(&invoices, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": invoices, + }) +} + +func handleSaveInvoice (ctx *gin.Context) { + var invoice Invoice + ctx.Bind(&invoice) + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + invoice.UserID = userId + + err := invoice.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": invoice, + }) +} + +func handleDelInvoice (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var invoice Invoice + invoice.ID = uint(id) + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + invoice.UserID = userId + + err = checkInvoiceOwnership(invoice.ID, invoice.UserID) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + err = invoice.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + }) +} diff --git a/invoice/hooks.go b/invoice/hooks.go new file mode 100644 index 0000000..3d933fb --- /dev/null +++ b/invoice/hooks.go @@ -0,0 +1,57 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + "gorm.io/gorm" + "vidhukant.com/openbills/errors" +) + +func (i *Invoice) BeforeSave(tx *gorm.DB) error { + var err error + + err = i.validate() + if err != nil { + return err + } + + return nil +} + +func (i *Invoice) BeforeDelete(tx *gorm.DB) error { + // if ID is 0, invoice won't be deleted + if i.ID == 0 { + return errors.ErrNoWhereCondition + } + + var err error + + // delete billing address + err = db.Where("invoice_id = ?", i.ID).Delete(&InvoiceBillingAddress{}).Error + if err != nil { + return err + } + + // delete shipping address + err = db.Where("invoice_id = ?", i.ID).Delete(&InvoiceShippingAddress{}).Error + if err != nil { + return err + } + + return nil +} diff --git a/invoice/invoice.go b/invoice/invoice.go new file mode 100644 index 0000000..0f4a601 --- /dev/null +++ b/invoice/invoice.go @@ -0,0 +1,61 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + "gorm.io/gorm" + d "vidhukant.com/openbills/db" + "vidhukant.com/openbills/user" + c "vidhukant.com/openbills/customer" + "time" +) + +var db *gorm.DB +func init() { + db = d.DB + + db.AutoMigrate(&Invoice{}, &InvoiceBillingAddress{}, &InvoiceShippingAddress{}) +} + +type InvoiceBillingAddress struct { + gorm.Model + c.Address + InvoiceID uint +} + +type InvoiceShippingAddress struct { + gorm.Model + c.Address + InvoiceID uint +} + +type Invoice struct { + gorm.Model + UserID uint `json:"-"` + User user.User `json:"-"` + InvoiceDate time.Time + InvoiceNumber uint + BillingAddress InvoiceBillingAddress + ShippingAddress InvoiceShippingAddress + Draft bool + + // Transporter Transporter + // DueDate string + // TransactionID string + // Client idk +} diff --git a/invoice/router.go b/invoice/router.go new file mode 100644 index 0000000..5536466 --- /dev/null +++ b/invoice/router.go @@ -0,0 +1,32 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + "github.com/gin-gonic/gin" +) + +func Routes(route *gin.RouterGroup) { + g := route.Group("/invoice") + { + g.GET("/", handleGetInvoices) + g.GET("/:id", handleGetSingleInvoice) + g.POST("/", handleSaveInvoice) + g.DELETE("/:id", handleDelInvoice) + } +} diff --git a/invoice/service.go b/invoice/service.go new file mode 100644 index 0000000..6b59949 --- /dev/null +++ b/invoice/service.go @@ -0,0 +1,74 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + e "vidhukant.com/openbills/errors" +) + +func getInvoice(invoice *Invoice, id uint) error { + res := db.Preload("BillingAddress").Preload("ShippingAddress").Find(&invoice, id) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return e.ErrNotFound + } + + return nil +} + +func getInvoices(invoices *[]Invoice, userId uint) error { + res := db.Where("user_id = ?", userId).Find(&invoices) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return e.ErrEmptyResponse + } + + return nil +} + +func (i *Invoice) upsert() error { + res := db.Save(i) + // TODO: handle potential errors + return res.Error +} + +func (i *Invoice) del() error { + res := db.Where("id = ? and user_id = ?", i.ID, i.UserID).Delete(i) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + // returns 404 if either row doesn't exist or if the user doesn't own it + if res.RowsAffected == 0 { + return e.ErrNotFound + } + + return nil +} diff --git a/invoice/validators.go b/invoice/validators.go new file mode 100644 index 0000000..645bdff --- /dev/null +++ b/invoice/validators.go @@ -0,0 +1,71 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package invoice + +import ( + //"regexp" + //"strings" + //"net/mail" + //"net/url" + "vidhukant.com/openbills/errors" + //e "errors" +) + +func (i *Invoice) validate() error { + var count int64 + err := db.Model(&Invoice{}). + Where("user_id = ? and invoice_number = ?", i.UserID, i.InvoiceNumber). + Count(&count). + Error + + if err != nil { + return err + } + + if count > 0 { + return errors.ErrNonUniqueInvoiceNumber + } + + return nil +} + +func checkInvoiceOwnership(invoiceId, userId uint) error { + var invoice Invoice + err := db. + Select("id", "user_id"). + Where("id = ?", invoiceId). + Find(&invoice). + Error + + // TODO: handle potential errors + if err != nil { + return err + } + + // invoice doesn't exist + if invoice.ID == 0 { + return errors.ErrNotFound + } + + // user doesn't own this invoice + if invoice.UserID != userId { + return errors.ErrForbidden + } + + return nil +} -- cgit v1.2.3