74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package entities
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TInventoryReceiptEntity struct {
|
|
ID uuid.UUID `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
|
|
ReferenceNumber string `gorm:"type:varchar(100);" json:"reference_number"`
|
|
DocumentNumber string `gorm:"type:varchar(100);" json:"document_number"`
|
|
ReceiptDate time.Time `gorm:"type:timestamp;" json:"receipt_date"`
|
|
Source string `gorm:"type:varchar(50);" json:"source"`
|
|
QrCodeFile string `gorm:"type:text;" json:"qr_code_file"`
|
|
Status string `gorm:"type:varchar(50);default:'draft';" json:"status"`
|
|
|
|
ClientID uuid.UUID `gorm:"type:uuid;index;" json:"client_id"`
|
|
|
|
ReceiptLines []TInventoryReceiptLineEntity `gorm:"foreignKey:InvReceiptID;references:ID"`
|
|
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
|
Assignment TAssignmentEntity `gorm:"-"`
|
|
|
|
FullAuditTrail
|
|
}
|
|
|
|
func (TInventoryReceiptEntity) TableName() string {
|
|
return "t_inventory_receipts"
|
|
}
|
|
|
|
// GenerateDocumentNumber generates a new document number for a client
|
|
func GenerateDocumentNumber(db *gorm.DB, clientId string) (string, error) {
|
|
prefix := "RCPT"
|
|
|
|
// 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 lastReceipt TInventoryReceiptEntity
|
|
err := db.
|
|
Where("client_id = ?", clientId).
|
|
Order("document_number DESC").
|
|
First(&lastReceipt).Error
|
|
|
|
seq := 1
|
|
if err == nil && lastReceipt.DocumentNumber != "" {
|
|
parts := strings.Split(lastReceipt.DocumentNumber, "-")
|
|
if len(parts) == 3 {
|
|
fmt.Sscanf(parts[2], "%d", &seq)
|
|
seq++
|
|
}
|
|
}
|
|
|
|
docNum := fmt.Sprintf("%s-%s-%04d", prefix, initials, seq)
|
|
return docNum, nil
|
|
}
|