233 lines
6.0 KiB
Go
233 lines
6.0 KiB
Go
package localdb
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.mchus.pro/mchus/quoteforge/internal/models"
|
|
)
|
|
|
|
// ConfigurationToLocal converts models.Configuration to LocalConfiguration
|
|
func ConfigurationToLocal(cfg *models.Configuration) *LocalConfiguration {
|
|
items := make(LocalConfigItems, len(cfg.Items))
|
|
for i, item := range cfg.Items {
|
|
items[i] = LocalConfigItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
}
|
|
}
|
|
|
|
local := &LocalConfiguration{
|
|
UUID: cfg.UUID,
|
|
ProjectUUID: cfg.ProjectUUID,
|
|
IsActive: true,
|
|
Name: cfg.Name,
|
|
Items: items,
|
|
TotalPrice: cfg.TotalPrice,
|
|
CustomPrice: cfg.CustomPrice,
|
|
Notes: cfg.Notes,
|
|
IsTemplate: cfg.IsTemplate,
|
|
ServerCount: cfg.ServerCount,
|
|
PricelistID: cfg.PricelistID,
|
|
OnlyInStock: cfg.OnlyInStock,
|
|
PriceUpdatedAt: cfg.PriceUpdatedAt,
|
|
CreatedAt: cfg.CreatedAt,
|
|
UpdatedAt: time.Now(),
|
|
SyncStatus: "pending",
|
|
OriginalUserID: derefUint(cfg.UserID),
|
|
OriginalUsername: cfg.OwnerUsername,
|
|
}
|
|
|
|
if local.OriginalUsername == "" && cfg.User != nil {
|
|
local.OriginalUsername = cfg.User.Username
|
|
}
|
|
|
|
if cfg.ID > 0 {
|
|
serverID := cfg.ID
|
|
local.ServerID = &serverID
|
|
}
|
|
|
|
return local
|
|
}
|
|
|
|
// LocalToConfiguration converts LocalConfiguration to models.Configuration
|
|
func LocalToConfiguration(local *LocalConfiguration) *models.Configuration {
|
|
items := make(models.ConfigItems, len(local.Items))
|
|
for i, item := range local.Items {
|
|
items[i] = models.ConfigItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
}
|
|
}
|
|
|
|
cfg := &models.Configuration{
|
|
UUID: local.UUID,
|
|
OwnerUsername: local.OriginalUsername,
|
|
ProjectUUID: local.ProjectUUID,
|
|
Name: local.Name,
|
|
Items: items,
|
|
TotalPrice: local.TotalPrice,
|
|
CustomPrice: local.CustomPrice,
|
|
Notes: local.Notes,
|
|
IsTemplate: local.IsTemplate,
|
|
ServerCount: local.ServerCount,
|
|
PricelistID: local.PricelistID,
|
|
OnlyInStock: local.OnlyInStock,
|
|
PriceUpdatedAt: local.PriceUpdatedAt,
|
|
CreatedAt: local.CreatedAt,
|
|
}
|
|
|
|
if local.ServerID != nil {
|
|
cfg.ID = *local.ServerID
|
|
}
|
|
if local.OriginalUserID != 0 {
|
|
userID := local.OriginalUserID
|
|
cfg.UserID = &userID
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func derefUint(v *uint) uint {
|
|
if v == nil {
|
|
return 0
|
|
}
|
|
return *v
|
|
}
|
|
|
|
func ProjectToLocal(project *models.Project) *LocalProject {
|
|
local := &LocalProject{
|
|
UUID: project.UUID,
|
|
OwnerUsername: project.OwnerUsername,
|
|
Name: project.Name,
|
|
TrackerURL: project.TrackerURL,
|
|
IsActive: project.IsActive,
|
|
IsSystem: project.IsSystem,
|
|
CreatedAt: project.CreatedAt,
|
|
UpdatedAt: project.UpdatedAt,
|
|
SyncStatus: "pending",
|
|
}
|
|
if project.ID > 0 {
|
|
serverID := project.ID
|
|
local.ServerID = &serverID
|
|
}
|
|
return local
|
|
}
|
|
|
|
func LocalToProject(local *LocalProject) *models.Project {
|
|
project := &models.Project{
|
|
UUID: local.UUID,
|
|
OwnerUsername: local.OwnerUsername,
|
|
Name: local.Name,
|
|
TrackerURL: local.TrackerURL,
|
|
IsActive: local.IsActive,
|
|
IsSystem: local.IsSystem,
|
|
CreatedAt: local.CreatedAt,
|
|
UpdatedAt: local.UpdatedAt,
|
|
}
|
|
if local.ServerID != nil {
|
|
project.ID = *local.ServerID
|
|
}
|
|
return project
|
|
}
|
|
|
|
// PricelistToLocal converts models.Pricelist to LocalPricelist
|
|
func PricelistToLocal(pl *models.Pricelist) *LocalPricelist {
|
|
name := pl.Notification
|
|
if name == "" {
|
|
name = pl.Version
|
|
}
|
|
|
|
return &LocalPricelist{
|
|
ServerID: pl.ID,
|
|
Source: pl.Source,
|
|
Version: pl.Version,
|
|
Name: name,
|
|
CreatedAt: pl.CreatedAt,
|
|
SyncedAt: time.Now(),
|
|
IsUsed: false,
|
|
}
|
|
}
|
|
|
|
// LocalToPricelist converts LocalPricelist to models.Pricelist
|
|
func LocalToPricelist(local *LocalPricelist) *models.Pricelist {
|
|
return &models.Pricelist{
|
|
ID: local.ServerID,
|
|
Source: local.Source,
|
|
Version: local.Version,
|
|
Notification: local.Name,
|
|
CreatedAt: local.CreatedAt,
|
|
IsActive: true,
|
|
}
|
|
}
|
|
|
|
// PricelistItemToLocal converts models.PricelistItem to LocalPricelistItem
|
|
func PricelistItemToLocal(item *models.PricelistItem, localPricelistID uint) *LocalPricelistItem {
|
|
partnumbers := make(LocalStringList, 0, len(item.Partnumbers))
|
|
partnumbers = append(partnumbers, item.Partnumbers...)
|
|
return &LocalPricelistItem{
|
|
PricelistID: localPricelistID,
|
|
LotName: item.LotName,
|
|
Price: item.Price,
|
|
AvailableQty: item.AvailableQty,
|
|
Partnumbers: partnumbers,
|
|
}
|
|
}
|
|
|
|
// LocalToPricelistItem converts LocalPricelistItem to models.PricelistItem
|
|
func LocalToPricelistItem(local *LocalPricelistItem, serverPricelistID uint) *models.PricelistItem {
|
|
partnumbers := make([]string, 0, len(local.Partnumbers))
|
|
partnumbers = append(partnumbers, local.Partnumbers...)
|
|
return &models.PricelistItem{
|
|
ID: local.ID,
|
|
PricelistID: serverPricelistID,
|
|
LotName: local.LotName,
|
|
Price: local.Price,
|
|
AvailableQty: local.AvailableQty,
|
|
Partnumbers: partnumbers,
|
|
}
|
|
}
|
|
|
|
// ComponentToLocal converts models.LotMetadata to LocalComponent
|
|
func ComponentToLocal(meta *models.LotMetadata) *LocalComponent {
|
|
var lotDesc string
|
|
var category string
|
|
|
|
if meta.Lot != nil {
|
|
lotDesc = meta.Lot.LotDescription
|
|
}
|
|
|
|
// Extract category from lot_name (e.g., "CPU_AMD_9654" -> "CPU")
|
|
if len(meta.LotName) > 0 {
|
|
for i, ch := range meta.LotName {
|
|
if ch == '_' {
|
|
category = meta.LotName[:i]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return &LocalComponent{
|
|
LotName: meta.LotName,
|
|
LotDescription: lotDesc,
|
|
Category: category,
|
|
Model: meta.Model,
|
|
CurrentPrice: meta.CurrentPrice,
|
|
SyncedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
// LocalToComponent converts LocalComponent to models.LotMetadata
|
|
func LocalToComponent(local *LocalComponent) *models.LotMetadata {
|
|
return &models.LotMetadata{
|
|
LotName: local.LotName,
|
|
Model: local.Model,
|
|
CurrentPrice: local.CurrentPrice,
|
|
Lot: &models.Lot{
|
|
LotName: local.LotName,
|
|
LotDescription: local.LotDescription,
|
|
},
|
|
}
|
|
}
|