From fd863aadf34b9837bd77587c1d36eb49000a8de7 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 12 Oct 2025 13:38:07 +0530 Subject: moved shipping address and billing address to the customers table itself --- customer/customer.go | 41 +++++++++++++++++++---------------------- customer/hooks.go | 16 +--------------- customer/service.go | 8 +++----- 3 files changed, 23 insertions(+), 42 deletions(-) (limited to 'customer') diff --git a/customer/customer.go b/customer/customer.go index 521c531..0d107d7 100644 --- a/customer/customer.go +++ b/customer/customer.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023-2024 Vidhu Kant Sharma + * Copyright (C) 2023-2025 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 @@ -20,36 +20,33 @@ package customer import ( "gorm.io/gorm" d "vidhukant.com/openbills/db" - u "vidhukant.com/openbills/util" ) var db *gorm.DB func init() { db = d.DB - db.AutoMigrate(&Customer{}, &CustomerBillingAddress{}, &CustomerShippingAddress{}) + db.AutoMigrate(&Customer{}) } -type CustomerBillingAddress struct { +type Customer struct { gorm.Model - u.Address - CustomerID uint -} + FirmName string + Gstin string + ContactName string + Phone string + Email string + Website string -type CustomerShippingAddress struct { - gorm.Model - u.Address - CustomerID uint -} + BillingAddressText string + BillingAddressCity string + BillingAddressState string + BillingAddressPostalCode string + BillingAddressCountry string -type Customer struct { - gorm.Model - FirmName string - Gstin string - ContactName string - Phone string - Email string - Website string - BillingAddress CustomerBillingAddress - ShippingAddress CustomerShippingAddress + ShippingAddressText string + ShippingAddressCity string + ShippingAddressState string + ShippingAddressPostalCode string + ShippingAddressCountry string } diff --git a/customer/hooks.go b/customer/hooks.go index 148004f..7f715c3 100644 --- a/customer/hooks.go +++ b/customer/hooks.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2025 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 @@ -39,19 +39,5 @@ func (c *Customer) BeforeDelete(tx *gorm.DB) error { return errors.ErrNoWhereCondition } - var err error - - // delete billing address - err = db.Where("customer_id = ?", c.ID).Delete(&CustomerBillingAddress{}).Error - if err != nil { - return err - } - - // delete shipping address - err = db.Where("customer_id = ?", c.ID).Delete(&CustomerShippingAddress{}).Error - if err != nil { - return err - } - return nil } diff --git a/customer/service.go b/customer/service.go index ca401f1..0286c3e 100644 --- a/customer/service.go +++ b/customer/service.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2025 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 @@ -19,11 +19,10 @@ package customer import ( e "vidhukant.com/openbills/errors" - "gorm.io/gorm" ) func getCustomer(customer *Customer, id uint) error { - res := db.Preload("BillingAddress").Preload("ShippingAddress").Find(&customer, id) + res := db.Find(&customer, id) // TODO: handle potential errors if res.Error != nil { @@ -53,8 +52,7 @@ func getCustomers(customers *[]Customer) error { } func (c *Customer) upsert() error { - db.Model(&c).Association("ShippingAddress").Replace(c.ShippingAddress) - res := db.Session(&gorm.Session{FullSaveAssociations: true}).Save(&c) + res := db.Save(&c) // TODO: handle potential errors return res.Error } -- cgit v1.2.3