feat: implement GenerateCodeUom function for automatic UOM code generation and update UOM creation logic
Deploy Application / deploy (push) Successful in 21s
Details
Deploy Application / deploy (push) Successful in 21s
Details
This commit is contained in:
parent
1d3d69a0fb
commit
b2de10c414
|
|
@ -1,7 +1,11 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MUomEntity struct {
|
||||
|
|
@ -31,3 +35,27 @@ type MUomEntity struct {
|
|||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,10 +26,10 @@ var (
|
|||
)
|
||||
|
||||
type UomCreateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Symbol string `json:"symbol"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Symbol string `json:"symbol"`
|
||||
// Code string `json:"code"`
|
||||
StdPrecision int `json:"std_precision"`
|
||||
IsActive bool `json:"is_active"`
|
||||
ClientID string `json:"client_id" binding:"required"`
|
||||
|
|
|
|||
|
|
@ -39,14 +39,21 @@ func (s *uomService) Create(ctx context.Context, req dtodomain.UomCreateRequest)
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
code, err := entities.GenerateCodeUom(s.db, req.ClientID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return dtodomain.UomResponse{}, err
|
||||
}
|
||||
|
||||
uom := entities.MUomEntity{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Symbol: req.Symbol,
|
||||
Code: req.Code,
|
||||
Code: code,
|
||||
StdPrecision: req.StdPrecision,
|
||||
IsActive: req.IsActive,
|
||||
}
|
||||
|
||||
clientUUID, err := uuid.Parse(req.ClientID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
|
|||
Loading…
Reference in New Issue