feat: integrate sequence service for document number generation in inventory services
Deploy Application / deploy (push) Successful in 59s Details

This commit is contained in:
Habib Fatkhul Rohman 2025-12-01 11:22:10 +07:00
parent b9c987a04b
commit 257ffc8af2
7 changed files with 88 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import (
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/dto" dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid" "github.com/google/uuid"
@ -26,9 +27,10 @@ type InventoryIssueService interface {
} }
type inventoryIssueService struct { type inventoryIssueService struct {
db *gorm.DB db *gorm.DB
issueRepo repository.InventoryIssueRepository issueRepo repository.InventoryIssueRepository
issueLineRepo repository.InventoryIssueLineRepository issueLineRepo repository.InventoryIssueLineRepository
sequenceService sequenceservice.SequenceService
} }
// GetLinesByIssueId implements InventoryIssueService. // GetLinesByIssueId implements InventoryIssueService.
@ -40,8 +42,8 @@ func (s *inventoryIssueService) GetLinesByIssueId(ctx context.Context, issueId s
return dtodomain.ToInventoryIssueLineResponses(lines), nil return dtodomain.ToInventoryIssueLineResponses(lines), nil
} }
func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository) InventoryIssueService { func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService) InventoryIssueService {
return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo} return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService}
} }
func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.InventoryIssueCreateRequest) (dtodomain.InventoryIssueResponse, error) { func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.InventoryIssueCreateRequest) (dtodomain.InventoryIssueResponse, error) {
@ -66,7 +68,11 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent
tx.Rollback() tx.Rollback()
return dtodomain.InventoryIssueResponse{}, err return dtodomain.InventoryIssueResponse{}, err
} }
docNum, err := entities.GenerateDocumentNumberInvIssue(s.db, req.ClientID) docNum, err := s.sequenceService.GenerateDocumentNumber(ctx, req.ClientID, "ISSUE", pkgdto.SequenceConfig{
Prefix: "ISSUE",
EntityType: "INV_ISSUE",
Period: "",
})
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return dtodomain.InventoryIssueResponse{}, err return dtodomain.InventoryIssueResponse{}, err

View File

@ -7,6 +7,7 @@ import (
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/dto" dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid" "github.com/google/uuid"
"gorm.io/gorm" "gorm.io/gorm"
@ -28,6 +29,7 @@ type inventoryMovementService struct {
db *gorm.DB db *gorm.DB
movementRepo repository.InventoryMovementRepository movementRepo repository.InventoryMovementRepository
movementLineRepo repository.InventoryMovementLineRepository movementLineRepo repository.InventoryMovementLineRepository
sequenceService sequenceservice.SequenceService
} }
// GetLinesByMovementId implements InventoryMovementService. // GetLinesByMovementId implements InventoryMovementService.
@ -239,10 +241,11 @@ func (s *inventoryMovementService) DeleteLine(ctx context.Context, lineId string
return s.movementLineRepo.Delete(ctx, nil, lineId) return s.movementLineRepo.Delete(ctx, nil, lineId)
} }
func NewInventoryMovementService(db *gorm.DB, movementRepo repository.InventoryMovementRepository, movementLineRepo repository.InventoryMovementLineRepository) InventoryMovementService { func NewInventoryMovementService(db *gorm.DB, movementRepo repository.InventoryMovementRepository, movementLineRepo repository.InventoryMovementLineRepository, sequenceService sequenceservice.SequenceService) InventoryMovementService {
return &inventoryMovementService{ return &inventoryMovementService{
db: db, db: db,
movementRepo: movementRepo, movementRepo: movementRepo,
movementLineRepo: movementLineRepo, movementLineRepo: movementLineRepo,
sequenceService: sequenceService,
} }
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/repository"
invstoragerepository "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/repository" invstoragerepository "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/repository"
productrepository "github.com/Caknoooo/go-gin-clean-starter/modules/product/repository" productrepository "github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
uomrepository "github.com/Caknoooo/go-gin-clean-starter/modules/uom/repository" uomrepository "github.com/Caknoooo/go-gin-clean-starter/modules/uom/repository"
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants" "github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
@ -38,6 +39,7 @@ type inventoryReceiptService struct {
productRepo productrepository.ProductRepository productRepo productrepository.ProductRepository
uomRepo uomrepository.UomRepository uomRepo uomrepository.UomRepository
invStorageRepository invstoragerepository.InventoryStorageRepository invStorageRepository invstoragerepository.InventoryStorageRepository
sequenceService sequenceservice.SequenceService
} }
func (s *inventoryReceiptService) GetLinesByReceiptId(ctx context.Context, id string) ([]dtodomain.InventoryReceiptLineResponse, error) { func (s *inventoryReceiptService) GetLinesByReceiptId(ctx context.Context, id string) ([]dtodomain.InventoryReceiptLineResponse, error) {
@ -294,7 +296,11 @@ func (s *inventoryReceiptService) Create(ctx context.Context, req dtodomain.Inve
tx.Rollback() tx.Rollback()
return dtodomain.InventoryReceiptResponse{}, err return dtodomain.InventoryReceiptResponse{}, err
} }
docNum, err := entities.GenerateDocumentNumber(s.db, req.ClientID) docNum, err := s.sequenceService.GenerateDocumentNumber(ctx, req.ClientID, "RCPT", pkgdto.SequenceConfig{
Prefix: "RCPT",
EntityType: "INV_RECEIPT",
Period: "",
})
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return dtodomain.InventoryReceiptResponse{}, err return dtodomain.InventoryReceiptResponse{}, err
@ -557,7 +563,8 @@ func NewInventoryReceiptService(db *gorm.DB,
receiptLineRepo repository.InventoryReceiptLineRepository, receiptLineRepo repository.InventoryReceiptLineRepository,
productRepo productrepository.ProductRepository, productRepo productrepository.ProductRepository,
uomRepo uomrepository.UomRepository, uomRepo uomrepository.UomRepository,
invStorageRepository invstoragerepository.InventoryStorageRepository) InventoryReceiptService { invStorageRepository invstoragerepository.InventoryStorageRepository,
sequenceService sequenceservice.SequenceService) InventoryReceiptService {
return &inventoryReceiptService{ return &inventoryReceiptService{
db: db, db: db,
receiptRepo: receiptRepo, receiptRepo: receiptRepo,
@ -565,5 +572,6 @@ func NewInventoryReceiptService(db *gorm.DB,
productRepo: productRepo, productRepo: productRepo,
uomRepo: uomRepo, uomRepo: uomRepo,
invStorageRepository: invStorageRepository, invStorageRepository: invStorageRepository,
sequenceService: sequenceService,
} }
} }

View File

@ -7,6 +7,7 @@ import (
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/dto" dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid" "github.com/google/uuid"
@ -29,6 +30,7 @@ type inventoryRequestService struct {
db *gorm.DB db *gorm.DB
requestRepo repository.InventoryRequestRepository requestRepo repository.InventoryRequestRepository
requestLineRepo repository.InventoryRequestLineRepository requestLineRepo repository.InventoryRequestLineRepository
sequenceService sequenceservice.SequenceService
} }
// GetLinesByRequestId implements InventoryRequestService. // GetLinesByRequestId implements InventoryRequestService.
@ -99,7 +101,12 @@ func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.Inve
tx.Rollback() tx.Rollback()
return dtodomain.InventoryRequestResponse{}, err return dtodomain.InventoryRequestResponse{}, err
} }
docNum, err := entities.GenerateDocumentNumberInventoryRequest(s.db, req.ClientID) docNum, err := s.sequenceService.GenerateDocumentNumber(ctx, req.ClientID, "RQST", pkgdto.SequenceConfig{
Prefix: "RQST",
EntityType: "INV_REQUEST",
Period: "",
})
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return dtodomain.InventoryRequestResponse{}, err return dtodomain.InventoryRequestResponse{}, err
@ -276,10 +283,11 @@ func (s *inventoryRequestService) CreateLine(ctx context.Context, requestId stri
}, nil }, nil
} }
func NewInventoryRequestService(db *gorm.DB, requestRepo repository.InventoryRequestRepository, requestLineRepo repository.InventoryRequestLineRepository) InventoryRequestService { func NewInventoryRequestService(db *gorm.DB, requestRepo repository.InventoryRequestRepository, requestLineRepo repository.InventoryRequestLineRepository, sequenceService sequenceservice.SequenceService) InventoryRequestService {
return &inventoryRequestService{ return &inventoryRequestService{
db: db, db: db,
requestRepo: requestRepo, requestRepo: requestRepo,
requestLineRepo: requestLineRepo, requestLineRepo: requestLineRepo,
sequenceService: sequenceService,
} }
} }

View File

@ -10,6 +10,8 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return/repository"
productrepository "github.com/Caknoooo/go-gin-clean-starter/modules/product/repository" productrepository "github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid" "github.com/google/uuid"
"gorm.io/gorm" "gorm.io/gorm"
@ -28,11 +30,12 @@ type InventoryReturnService interface {
} }
type inventoryReturnService struct { type inventoryReturnService struct {
db *gorm.DB db *gorm.DB
returnRepo repository.InventoryReturnRepository returnRepo repository.InventoryReturnRepository
returnLineRepo repository.InventoryReturnLineRepository returnLineRepo repository.InventoryReturnLineRepository
issueLineRepo issueRepository.InventoryIssueLineRepository issueLineRepo issueRepository.InventoryIssueLineRepository
productRepo productrepository.ProductRepository productRepo productrepository.ProductRepository
sequenceService sequenceservice.SequenceService
} }
// GetLinesByReturnId implements InventoryReturnService. // GetLinesByReturnId implements InventoryReturnService.
@ -103,7 +106,12 @@ func (s *inventoryReturnService) Create(ctx context.Context, req dtodomain.Inven
tx.Rollback() tx.Rollback()
return dtodomain.InventoryReturnResponse{}, err return dtodomain.InventoryReturnResponse{}, err
} }
docNum, err := entities.GenerateDocumentNumberInventoryReturn(s.db, req.ClientID) // docNum, err := entities.GenerateDocumentNumberInventoryReturn(s.db, req.ClientID)
docNum, err := s.sequenceService.GenerateDocumentNumber(ctx, req.ClientID, "RTRN", pkgdto.SequenceConfig{
Prefix: "RTRN",
EntityType: "INV_RETURN",
Period: "",
})
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return dtodomain.InventoryReturnResponse{}, err return dtodomain.InventoryReturnResponse{}, err
@ -270,12 +278,14 @@ func NewInventoryReturnService(
returnLineRepo repository.InventoryReturnLineRepository, returnLineRepo repository.InventoryReturnLineRepository,
issueLineRepo issueRepository.InventoryIssueLineRepository, issueLineRepo issueRepository.InventoryIssueLineRepository,
productRepo productrepository.ProductRepository, productRepo productrepository.ProductRepository,
sequenceService sequenceservice.SequenceService,
) InventoryReturnService { ) InventoryReturnService {
return &inventoryReturnService{ return &inventoryReturnService{
db: db, db: db,
returnRepo: returnRepo, returnRepo: returnRepo,
returnLineRepo: returnLineRepo, returnLineRepo: returnLineRepo,
issueLineRepo: issueLineRepo, issueLineRepo: issueLineRepo,
productRepo: productRepo, productRepo: productRepo,
sequenceService: sequenceService,
} }
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/sequence/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/repository"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -15,6 +16,7 @@ type SequenceService interface {
GenerateNumberWithPeriod(ctx context.Context, clientId string, config pkgdto.SequenceConfig, suffix string) (string, error) GenerateNumberWithPeriod(ctx context.Context, clientId string, config pkgdto.SequenceConfig, suffix string) (string, error)
GenerateNumberWithSuffix(ctx context.Context, clientId string, config pkgdto.SequenceConfig, suffix string) (string, error) GenerateNumberWithSuffix(ctx context.Context, clientId string, config pkgdto.SequenceConfig, suffix string) (string, error)
GetNextSequence(ctx context.Context, clientId string, config pkgdto.SequenceConfig) (int, error) GetNextSequence(ctx context.Context, clientId string, config pkgdto.SequenceConfig) (int, error)
GenerateDocumentNumber(ctx context.Context, clientId string, docType string, config pkgdto.SequenceConfig) (string, error)
} }
type sequenceService struct { type sequenceService struct {
@ -93,3 +95,27 @@ func (s *sequenceService) GetNextSequence(ctx context.Context, clientId string,
} }
return seq.CurrentSeq, nil return seq.CurrentSeq, nil
} }
func (s *sequenceService) GenerateDocumentNumber(ctx context.Context, clientId string, docType string, config pkgdto.SequenceConfig) (string, error) {
// Ambil nama client berdasarkan clientId
var client struct {
Name string
}
if err := s.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")
}
initials := utils.GetInitials(client.Name)
// Ambil sequence dari sequence service
seqNum, err := s.GetNextSequence(ctx, clientId, config)
if err != nil {
return "", err
}
docNum := fmt.Sprintf("%s-%s-%04d", docType, initials, seqNum)
return docNum, nil
}

View File

@ -186,12 +186,12 @@ func RegisterDependencies(injector *do.Injector) {
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db) warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db)
zonaServ := zonaService.NewZonaService(zonaRepository, db) zonaServ := zonaService.NewZonaService(zonaRepository, db)
aisleServ := aisleService.NewAisleService(aisleRepository, db) aisleServ := aisleService.NewAisleService(aisleRepository, db)
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository) inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository) assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository) inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ)
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository) inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ)
inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository) inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ)
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository) inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ)
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository) inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository)
quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)