fix: pricelist selection preserved when opening configurations
- Remove 'auto (latest active)' option from pricelist dropdowns; new configs pre-select the first active pricelist instead - Stop resetting stored pricelist_id to null when it is not in the active list (deactivated pricelists are shown as inactive options) - RefreshPricesNoAuth now accepts an optional pricelist_id; uses the UI-selected pricelist, then the config's stored pricelist, then latest as a last-resort fallback — no longer silently overwrites the stored pricelist on every price refresh - Same fix applied to RefreshPrices (with auth) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -399,17 +399,29 @@ func (s *LocalConfigurationService) RefreshPrices(uuid string, ownerUsername str
|
||||
return nil, ErrConfigForbidden
|
||||
}
|
||||
|
||||
// Refresh local pricelists when online and use latest active/local pricelist for recalculation.
|
||||
// Refresh local pricelists when online.
|
||||
if s.isOnline() {
|
||||
_ = s.syncService.SyncPricelistsIfNeeded()
|
||||
}
|
||||
latestPricelist, latestErr := s.localDB.GetLatestLocalPricelist()
|
||||
|
||||
// Use the pricelist stored in the config; fall back to latest if unavailable.
|
||||
var pricelist *localdb.LocalPricelist
|
||||
if localCfg.PricelistID != nil && *localCfg.PricelistID > 0 {
|
||||
if pl, err := s.localDB.GetLocalPricelistByServerID(*localCfg.PricelistID); err == nil {
|
||||
pricelist = pl
|
||||
}
|
||||
}
|
||||
if pricelist == nil {
|
||||
if pl, err := s.localDB.GetLatestLocalPricelist(); err == nil {
|
||||
pricelist = pl
|
||||
}
|
||||
}
|
||||
|
||||
// Update prices for all items from pricelist
|
||||
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
||||
for i, item := range localCfg.Items {
|
||||
if latestErr == nil && latestPricelist != nil {
|
||||
price, err := s.localDB.GetLocalPriceForLot(latestPricelist.ID, item.LotName)
|
||||
if pricelist != nil {
|
||||
price, err := s.localDB.GetLocalPriceForLot(pricelist.ID, item.LotName)
|
||||
if err == nil && price > 0 {
|
||||
updatedItems[i] = localdb.LocalConfigItem{
|
||||
LotName: item.LotName,
|
||||
@@ -434,8 +446,8 @@ func (s *LocalConfigurationService) RefreshPrices(uuid string, ownerUsername str
|
||||
}
|
||||
|
||||
localCfg.TotalPrice = &total
|
||||
if latestErr == nil && latestPricelist != nil {
|
||||
localCfg.PricelistID = &latestPricelist.ServerID
|
||||
if pricelist != nil {
|
||||
localCfg.PricelistID = &pricelist.ServerID
|
||||
}
|
||||
|
||||
// Set price update timestamp and mark for sync
|
||||
@@ -762,8 +774,10 @@ func (s *LocalConfigurationService) ListTemplates(page, perPage int) ([]models.C
|
||||
return templates[start:end], total, nil
|
||||
}
|
||||
|
||||
// RefreshPricesNoAuth updates all component prices in the configuration without ownership check
|
||||
func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string) (*models.Configuration, error) {
|
||||
// RefreshPricesNoAuth updates all component prices in the configuration without ownership check.
|
||||
// pricelistServerID optionally specifies which pricelist to use; if nil, the config's stored
|
||||
// pricelist is used; if that is also absent, the latest local pricelist is used as a fallback.
|
||||
func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string, pricelistServerID *uint) (*models.Configuration, error) {
|
||||
// Get configuration from local SQLite
|
||||
localCfg, err := s.localDB.GetConfigurationByUUID(uuid)
|
||||
if err != nil {
|
||||
@@ -773,13 +787,36 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string) (*models.Co
|
||||
if s.isOnline() {
|
||||
_ = s.syncService.SyncPricelistsIfNeeded()
|
||||
}
|
||||
latestPricelist, latestErr := s.localDB.GetLatestLocalPricelist()
|
||||
|
||||
// Resolve which pricelist to use:
|
||||
// 1. Explicitly requested pricelist (from UI selection)
|
||||
// 2. Pricelist stored in the configuration
|
||||
// 3. Latest local pricelist as last-resort fallback
|
||||
var targetServerID *uint
|
||||
if pricelistServerID != nil && *pricelistServerID > 0 {
|
||||
targetServerID = pricelistServerID
|
||||
} else if localCfg.PricelistID != nil && *localCfg.PricelistID > 0 {
|
||||
targetServerID = localCfg.PricelistID
|
||||
}
|
||||
|
||||
var pricelist *localdb.LocalPricelist
|
||||
if targetServerID != nil {
|
||||
if pl, err := s.localDB.GetLocalPricelistByServerID(*targetServerID); err == nil {
|
||||
pricelist = pl
|
||||
}
|
||||
}
|
||||
if pricelist == nil {
|
||||
// Fallback: use latest local pricelist
|
||||
if pl, err := s.localDB.GetLatestLocalPricelist(); err == nil {
|
||||
pricelist = pl
|
||||
}
|
||||
}
|
||||
|
||||
// Update prices for all items from pricelist
|
||||
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
||||
for i, item := range localCfg.Items {
|
||||
if latestErr == nil && latestPricelist != nil {
|
||||
price, err := s.localDB.GetLocalPriceForLot(latestPricelist.ID, item.LotName)
|
||||
if pricelist != nil {
|
||||
price, err := s.localDB.GetLocalPriceForLot(pricelist.ID, item.LotName)
|
||||
if err == nil && price > 0 {
|
||||
updatedItems[i] = localdb.LocalConfigItem{
|
||||
LotName: item.LotName,
|
||||
@@ -804,8 +841,8 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string) (*models.Co
|
||||
}
|
||||
|
||||
localCfg.TotalPrice = &total
|
||||
if latestErr == nil && latestPricelist != nil {
|
||||
localCfg.PricelistID = &latestPricelist.ServerID
|
||||
if pricelist != nil {
|
||||
localCfg.PricelistID = &pricelist.ServerID
|
||||
}
|
||||
|
||||
// Set price update timestamp and mark for sync
|
||||
|
||||
Reference in New Issue
Block a user