/* 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) ctx.Abort() return } var invoice Invoice err = getInvoice(&invoice, uint(id)) if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": invoice, }) } func handleGetInvoices(getDrafts bool) func(*gin.Context) { return func(ctx *gin.Context) { var invoices []Invoice err := getInvoices(&invoices, getDrafts) if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": invoices, }) } } func handleSaveInvoice (ctx *gin.Context) { var invoice Invoice ctx.Bind(&invoice) // if invoice number is 0, generate one! // (maybe the client didn't give us one) if invoice.InvoiceNumber == 0 { n, err := getNewInvoiceNumber() if err != nil { ctx.Error(err) ctx.Abort() return } invoice.InvoiceNumber = n } err := invoice.upsert() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": invoice, }) } func handleDelInvoice (ctx *gin.Context) { // TODO: only drafts can be deleted, non-drafts should be "cancelled" id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var invoice Invoice invoice.ID = uint(id) err = invoice.del() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, nil) } // get items belonging to a certain invoice func handleGetInvoiceItems (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var items []InvoiceItem err = getInvoiceItems(&items, uint(id)) if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": items, }) } func addItem (ctx *gin.Context) { // TODO: only drafts can be edited id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var item InvoiceItem ctx.Bind(&item) item.InvoiceID = uint(id) err = item.upsert() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, nil) } func removeItem (ctx *gin.Context) { // TODO: only drafts can be edited id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var item InvoiceItem item.ID = uint(id) err = item.del() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, nil) } // get custom fields belonging to a certain invoice func handleGetInvoiceCustomFields (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var cf []CustomField err = getInvoiceCustomFields(&cf, uint(id)) if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": cf, }) } func addCustomField (ctx *gin.Context) { // TODO: only drafts can be edited id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var cf CustomField ctx.Bind(&cf) cf.InvoiceID = uint(id) err = cf.upsert() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": cf, }) } func removeCustomField (ctx *gin.Context) { // TODO: only drafts can be edited id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { ctx.Error(e.ErrInvalidID) ctx.Abort() return } var cf CustomField cf.ID = uint(id) err = cf.del() if err != nil { ctx.Error(err) ctx.Abort() return } ctx.JSON(http.StatusOK, gin.H{ "data": cf, }) }