diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-13 00:37:30 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-13 00:37:30 +0530 |
commit | fb9ba155438100f295fdb563ad955151ee038ad3 (patch) | |
tree | 558c27ce3910a2aa2e7d18803b7548f6e7f6a032 /invoice | |
parent | 327a32f563394f92313e4a751515d69d90f4e7f5 (diff) |
flattened out the invoice schema, was overengineered
Diffstat (limited to 'invoice')
-rw-r--r-- | invoice/hooks.go | 16 | ||||
-rw-r--r-- | invoice/invoice.go | 79 | ||||
-rw-r--r-- | invoice/service.go | 6 |
3 files changed, 50 insertions, 51 deletions
diff --git a/invoice/hooks.go b/invoice/hooks.go index 68d730b..07284ed 100644 --- a/invoice/hooks.go +++ b/invoice/hooks.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2025 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 @@ -39,20 +39,6 @@ func (i *Invoice) BeforeDelete(tx *gorm.DB) error { 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 index 97ba254..9cbc4e7 100644 --- a/invoice/invoice.go +++ b/invoice/invoice.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023-2024 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2025 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 @@ -21,7 +21,6 @@ import ( "gorm.io/gorm" "time" d "vidhukant.com/openbills/db" - i "vidhukant.com/openbills/item" u "vidhukant.com/openbills/util" ) @@ -30,27 +29,26 @@ var db *gorm.DB func init() { db = d.DB - db.AutoMigrate(&Invoice{}, &InvoiceItem{}, &InvoiceBillingAddress{}, &InvoiceShippingAddress{}, &CustomField{}) -} - -type InvoiceBillingAddress struct { - gorm.Model - u.Address - InvoiceID uint -} - -type InvoiceShippingAddress struct { - gorm.Model - u.Address - InvoiceID uint + db.AutoMigrate(&Invoice{}, &InvoiceItem{}, &CustomField{}) } type InvoiceItem struct { - i.Item ID uint InvoiceID uint - BrandName string Quantity string // float + // fields below these must be kept in sync with + // item.SavedItem. I'm not extending that struct because + // there are some things that I don't want / want in a + // different format. In short, it's *not* worth the headache + Name string + BrandName string + Category string + Description string + HSN string + UnitOfMeasure string + VariantName string + UnitPrice string // float + GSTPercentage string // float } // user can add as many custom fields as they like @@ -63,24 +61,37 @@ type CustomField struct { type Invoice struct { gorm.Model - InvoiceDate time.Time - InvoiceNumber uint - BillingAddress InvoiceBillingAddress - ShippingAddress InvoiceShippingAddress - IsDraft bool - Note string - Items []InvoiceItem - CustomFields []CustomField + InvoiceDate time.Time + InvoiceNumber uint + IsDraft bool + Note string + Items []InvoiceItem + CustomFields []CustomField + + BillingAddressText string + BillingAddressCity string + BillingAddressState string + BillingAddressPostalCode string + BillingAddressCountry string + + ShippingAddressText string + ShippingAddressCity string + ShippingAddressState string + ShippingAddressPostalCode string + ShippingAddressCountry string + + IssuerFirmName string + IssuerFirmGstin string + IssuerFirmPhone string + IssuerFirmEmail string + IssuerFirmWebsite string + + IssuerFirmAddressText string + IssuerFirmAddressCity string + IssuerFirmAddressState string + IssuerFirmAddressPostalCode string + IssuerFirmAddressCountry string - // issuer and customer details are stored here - // because they are NOT intended to ever change - IssuerFirmName string - IssuerFirmAddress string - IssuerFirmGstin string - IssuerFirmPhone string - IssuerFirmEmail string - IssuerFirmWebsite string - IssuerFirmDetails string CustomerName string CustomerGstin string CustomerContactName string diff --git a/invoice/service.go b/invoice/service.go index 55b1319..20cf767 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2025 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 @@ -47,7 +47,7 @@ func getNewInvoiceNumber() (uint, error) { } func getInvoice(invoice *Invoice, id uint) error { - res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Preload("CustomFields").Find(&invoice, id) + res := db.Preload("Items").Preload("CustomFields").Find(&invoice, id) // TODO: handle potential errors if res.Error != nil { @@ -61,6 +61,8 @@ func getInvoice(invoice *Invoice, id uint) error { return nil } +// TODO: rather than returning all invoices, always require a filter, maybe +// so we're actually searching invoices func getInvoices(invoices *[]Invoice, isDraft bool) error { res := db.Where("is_draft = ?", isDraft).Find(&invoices) |