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
|
package entities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MUomEntity struct {
|
type MUomEntity struct {
|
||||||
|
|
@ -31,3 +35,27 @@ type MUomEntity struct {
|
||||||
func (MUomEntity) TableName() string {
|
func (MUomEntity) TableName() string {
|
||||||
return "m_uoms"
|
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 {
|
type UomCreateRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
Code string `json:"code"`
|
// Code string `json:"code"`
|
||||||
StdPrecision int `json:"std_precision"`
|
StdPrecision int `json:"std_precision"`
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
ClientID string `json:"client_id" binding:"required"`
|
ClientID string `json:"client_id" binding:"required"`
|
||||||
|
|
|
||||||
|
|
@ -39,14 +39,21 @@ func (s *uomService) Create(ctx context.Context, req dtodomain.UomCreateRequest)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
code, err := entities.GenerateCodeUom(s.db, req.ClientID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.UomResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
uom := entities.MUomEntity{
|
uom := entities.MUomEntity{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
Symbol: req.Symbol,
|
Symbol: req.Symbol,
|
||||||
Code: req.Code,
|
Code: code,
|
||||||
StdPrecision: req.StdPrecision,
|
StdPrecision: req.StdPrecision,
|
||||||
IsActive: req.IsActive,
|
IsActive: req.IsActive,
|
||||||
}
|
}
|
||||||
|
|
||||||
clientUUID, err := uuid.Parse(req.ClientID)
|
clientUUID, err := uuid.Parse(req.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue