Update pricelist repository, service, and tests

This commit is contained in:
Mikhail Chusavitin
2026-02-06 10:14:24 +03:00
parent c0beed021c
commit 38d7332a38
3 changed files with 125 additions and 19 deletions

View File

@@ -1,7 +1,9 @@
package repository
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
@@ -210,14 +212,31 @@ func (r *PricelistRepository) GetItems(pricelistID uint, offset, limit int, sear
func (r *PricelistRepository) GenerateVersion() (string, error) {
today := time.Now().Format("2006-01-02")
var count int64
if err := r.db.Model(&models.Pricelist{}).
Where("version LIKE ?", today+"%").
Count(&count).Error; err != nil {
return "", fmt.Errorf("counting today's pricelists: %w", err)
var last models.Pricelist
err := r.db.Model(&models.Pricelist{}).
Select("version").
Where("version LIKE ?", today+"-%").
Order("version DESC").
Limit(1).
Take(&last).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Sprintf("%s-001", today), nil
}
return "", fmt.Errorf("loading latest today's pricelist version: %w", err)
}
return fmt.Sprintf("%s-%03d", today, count+1), nil
parts := strings.Split(last.Version, "-")
if len(parts) < 4 {
return "", fmt.Errorf("invalid pricelist version format: %s", last.Version)
}
n, err := strconv.Atoi(parts[len(parts)-1])
if err != nil {
return "", fmt.Errorf("parsing pricelist sequence %q: %w", parts[len(parts)-1], err)
}
return fmt.Sprintf("%s-%03d", today, n+1), nil
}
// CanWrite checks if the current database user has INSERT permission on qt_pricelists