aboutsummaryrefslogtreecommitdiff
path: root/customer
diff options
context:
space:
mode:
Diffstat (limited to 'customer')
-rw-r--r--customer/controller.go102
-rw-r--r--customer/customer.go66
-rw-r--r--customer/hooks.go48
-rw-r--r--customer/router.go32
-rw-r--r--customer/service.go73
-rw-r--r--customer/validators.go107
6 files changed, 428 insertions, 0 deletions
diff --git a/customer/controller.go b/customer/controller.go
new file mode 100644
index 0000000..9381c45
--- /dev/null
+++ b/customer/controller.go
@@ -0,0 +1,102 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ e "vidhukant.com/openbills/errors"
+ "github.com/gin-gonic/gin"
+ "net/http"
+ "strconv"
+)
+
+func handleGetSingleCustomer (ctx *gin.Context) {
+ id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
+ if err != nil {
+ ctx.Error(e.ErrInvalidID)
+ return
+ }
+
+ var customer Customer
+
+ err = getCustomer(&customer, uint(id))
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+
+ ctx.JSON(http.StatusOK, gin.H{
+ "message": "success",
+ "data": customer,
+ })
+}
+
+func handleGetCustomers (ctx *gin.Context) {
+ var customers []Customer
+
+ err := getCustomers(&customers)
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+
+ ctx.JSON(http.StatusOK, gin.H{
+ "message": "success",
+ "data": customers,
+ })
+}
+
+func handleSaveCustomer (ctx *gin.Context) {
+ var customer Customer
+ ctx.Bind(&customer)
+
+ err := customer.upsert()
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+
+ ctx.JSON(http.StatusOK, gin.H{
+ "message": "success",
+ "data": customer,
+ })
+}
+
+func handleDelCustomer (ctx *gin.Context) {
+ id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
+ if err != nil {
+ ctx.Error(e.ErrInvalidID)
+ return
+ }
+
+ var customer Customer
+ customer.ID = uint(id)
+
+ err = customer.del()
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+
+ ctx.JSON(http.StatusOK, gin.H{
+ "message": "success",
+ })
+}
diff --git a/customer/customer.go b/customer/customer.go
new file mode 100644
index 0000000..5f25e2d
--- /dev/null
+++ b/customer/customer.go
@@ -0,0 +1,66 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ "gorm.io/gorm"
+ d "vidhukant.com/openbills/db"
+)
+
+var db *gorm.DB
+func init() {
+ db = d.DB
+
+ db.AutoMigrate(&Customer{}, &CustomerContact{}, &CustomerBillingAddress{}, &CustomerShippingAddress{})
+}
+
+type CustomerContact struct {
+ gorm.Model
+ CustomerID uint
+ Name string
+ Phone string
+ Email string
+ Website string
+}
+
+type Address struct {
+ AddressText string
+ City string
+ State string
+ PostalCode string
+ Country string
+}
+
+type CustomerBillingAddress struct {
+ Address
+ CustomerID uint
+}
+
+type CustomerShippingAddress struct {
+ Address
+ CustomerID uint
+}
+
+type Customer struct {
+ gorm.Model
+ Name string
+ Gstin string
+ Contact CustomerContact
+ BillingAddress CustomerBillingAddress
+ ShippingAddresses []CustomerShippingAddress
+}
diff --git a/customer/hooks.go b/customer/hooks.go
new file mode 100644
index 0000000..020e1c5
--- /dev/null
+++ b/customer/hooks.go
@@ -0,0 +1,48 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ "gorm.io/gorm"
+ "vidhukant.com/openbills/errors"
+)
+
+func (c *Customer) BeforeSave(tx *gorm.DB) error {
+ var err error
+
+ err = c.validate()
+ if err != nil {
+ return err
+ }
+
+ err = c.Contact.validate()
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (c *Customer) BeforeDelete(tx *gorm.DB) error {
+ // if ID is 0, customer won't be deleted
+ if c.ID == 0 {
+ return errors.ErrNoWhereCondition
+ }
+
+ return nil
+}
diff --git a/customer/router.go b/customer/router.go
new file mode 100644
index 0000000..9db92d3
--- /dev/null
+++ b/customer/router.go
@@ -0,0 +1,32 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ "github.com/gin-gonic/gin"
+)
+
+func Routes(route *gin.RouterGroup) {
+ g := route.Group("/customer")
+ {
+ g.GET("/", handleGetCustomers)
+ g.GET("/:id", handleGetSingleCustomer)
+ g.POST("/", handleSaveCustomer)
+ g.DELETE("/:id", handleDelCustomer)
+ }
+}
diff --git a/customer/service.go b/customer/service.go
new file mode 100644
index 0000000..c5d7cb8
--- /dev/null
+++ b/customer/service.go
@@ -0,0 +1,73 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ e "vidhukant.com/openbills/errors"
+)
+
+func getCustomer(customer *Customer, id uint) error {
+ res := db.Preload("Contact").Preload("BillingAddress").Preload("ShippingAddresses").Find(&customer, id)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return e.ErrNotFound
+ }
+
+ return nil
+}
+
+func getCustomers(customers *[]Customer) error {
+ res := db.Find(&customers)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return e.ErrEmptyResponse
+ }
+
+ return nil
+}
+
+func (c *Customer) upsert() error {
+ res := db.Save(c)
+ // TODO: handle potential errors
+ return res.Error
+}
+
+func (c *Customer) del() error {
+ res := db.Delete(c)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return e.ErrNotFound
+ }
+
+ return nil
+}
diff --git a/customer/validators.go b/customer/validators.go
new file mode 100644
index 0000000..6c51ad9
--- /dev/null
+++ b/customer/validators.go
@@ -0,0 +1,107 @@
+/* openbills - Server for web based Libre Billing Software
+ * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package customer
+
+import (
+ "regexp"
+ "strings"
+ "vidhukant.com/openbills/errors"
+ e "errors"
+)
+
+// NOTE: very inefficient and really really really dumb but it works
+// TODO: find a better (or even a remotely good) way
+func validateContactField(field, value string) error {
+ if value != "" {
+ var count int64
+ err := db.Model(&CustomerContact{}).
+ Where(field + " = ?", value).
+ Count(&count).
+ Error
+
+ if err != nil {
+ return err
+ }
+
+ if count > 0 {
+ switch(field) {
+ case "phone":
+ return errors.ErrNonUniquePhone
+ case "email":
+ return errors.ErrNonUniqueEmail
+ case "website":
+ return errors.ErrNonUniqueWebsite
+ default:
+ return e.New(field + " is not unique")
+ }
+ }
+ }
+
+ return nil
+}
+
+func (c *CustomerContact) validate() error {
+ // trim whitespaces
+ c.Name = strings.TrimSpace(c.Name)
+ c.Phone = strings.TrimSpace(c.Phone)
+ c.Email = strings.TrimSpace(c.Email)
+ c.Website = strings.TrimSpace(c.Website)
+
+ var err error
+ for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}} {
+ err = validateContactField(i[0], i[1])
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func (c *Customer) validate() error {
+ // trim whitespaces
+ c.Name = strings.TrimSpace(c.Name)
+ c.Gstin = strings.TrimSpace(c.Gstin)
+
+ // don't validate if GSTIN is empty
+ if c.Gstin != "" {
+ // GSTIN regex validation
+ match, _ := regexp.MatchString("^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$", c.Gstin)
+ if (!match) {
+ return errors.ErrInvalidGSTIN
+ }
+
+ // make sure GSTIN is unique
+ var count int64
+ err := db.Model(&Customer{}).
+ Select("gstin").
+ Where("gstin = ?", c.Gstin).
+ Count(&count).
+ Error
+
+ if err != nil {
+ return err
+ }
+
+ if count > 0 {
+ return errors.ErrNonUniqueGSTIN
+ }
+ }
+
+ return nil
+}