wms-be/database/entities/m_uom_entitiy.go

62 lines
2.0 KiB
Go

package entities
import (
"fmt"
"strings"
"github.com/google/uuid"
"gorm.io/gorm"
)
type MUomEntity struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
Name string `gorm:"type:varchar(100);not null;" json:"name"`
Description string `gorm:"type:text" json:"description"`
Symbol string `gorm:"type:varchar(20);" json:"symbol"`
Code string `gorm:"type:varchar(50);" json:"code"`
StdPrecision int `gorm:"type:int;" json:"std_precision"`
IsActive bool `gorm:"type:boolean;default:true" json:"is_active"`
ClientID uuid.UUID `gorm:"type:uuid;index;" json:"client_id"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID" json:"client"`
Products []MProductEntity `gorm:"foreignKey:UomID;references:ID"`
DimProducts []MProductEntity `gorm:"foreignKey:DimUomID;references:ID"`
WeightProducts []MProductEntity `gorm:"foreignKey:WeightUomID;references:ID"`
VolumeProducts []MProductEntity `gorm:"foreignKey:VolumeUomID;references:ID"`
MinStockProducts []MProductEntity `gorm:"foreignKey:MinStockUomID;references:ID"`
MaxStockProducts []MProductEntity `gorm:"foreignKey:MaxStockUomID;references:ID"`
LeadTimeProducts []MProductEntity `gorm:"foreignKey:LeadTimeUomID;references:ID"`
UomToUomProducts []MProductEntity `gorm:"foreignKey:UomToUomID;references:ID"`
FullAuditTrail
}
func (MUomEntity) TableName() string {
return "m_uoms"
}
// GenerateCodeUom generates a new UOM code with sequence per client
func GenerateCodeUom(db *gorm.DB, clientId string) (string, error) {
prefix := "UOM"
var lastUom MUomEntity
err := db.
Where("client_id = ?", clientId).
Where("code LIKE ?", prefix+"-%").
Order("code DESC").
First(&lastUom).Error
seq := 1
if err == nil && lastUom.Code != "" {
parts := strings.Split(lastUom.Code, "-")
if len(parts) == 2 {
fmt.Sscanf(parts[1], "%d", &seq)
seq++
}
}
code := fmt.Sprintf("%s-%04d", prefix, seq)
return code, nil
}