wms-be/database/entities/t_inventory_movement_entity.go

75 lines
2.4 KiB
Go

package entities
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type TInventoryMovementEntity struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
MovementNumber string `gorm:"type:varchar(100);" json:"movement_number"`
MovementDate time.Time `gorm:"type:timestamp;" json:"movement_date"`
MovementType string `gorm:"type:varchar(50);" json:"movement_type"`
Status string `gorm:"type:varchar(50);default:'draft'" json:"status"`
ClientID uuid.UUID `gorm:"type:uuid;index;" json:"client_id"`
SourceLocationID uuid.UUID `gorm:"type:uuid;index;" json:"source_location_id"`
DestinationLocationID uuid.UUID `gorm:"type:uuid;index;" json:"destination_location_id"`
MovementLines []TInventoryMovementLineEntity `gorm:"foreignKey:InvMovementID;references:ID"`
SourceLocation MWarehouseEntity `gorm:"foreignKey:SourceLocationID;references:ID"`
DestinationLocation MWarehouseEntity `gorm:"foreignKey:DestinationLocationID;references:ID"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
FullAuditTrail
}
func (TInventoryMovementEntity) TableName() string {
return "t_inventory_movements"
}
// GenerateDocumentNumber generates a new document number for a client
func GenerateDocumentNumberInvMovement(db *gorm.DB, clientId string) (string, error) {
prefix := "MOVEMENT"
// Ambil nama client berdasarkan clientId
var client struct {
Name string
}
if err := db.Table("m_clients").Select("name").Where("id = ?", clientId).First(&client).Error; err != nil {
return "", fmt.Errorf("client not found")
}
if client.Name == "" {
return "", fmt.Errorf("client name is empty")
}
words := strings.Fields(client.Name)
initials := ""
for _, w := range words {
if len(w) > 0 {
initials += strings.ToUpper(string(w[0]))
}
}
// Cari document number terakhir untuk client ini
var lastMovement TInventoryMovementEntity
err := db.
Where("client_id = ?", clientId).
Order("movement_number DESC").
First(&lastMovement).Error
seq := 1
if err == nil && lastMovement.MovementNumber != "" {
parts := strings.Split(lastMovement.MovementNumber, "-")
if len(parts) == 3 {
fmt.Sscanf(parts[2], "%d", &seq)
seq++
}
}
docNum := fmt.Sprintf("%s-%s-%04d", prefix, initials, seq)
return docNum, nil
}