From 257ffc8af22539c855b96147234c26d2b5319836 Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Mon, 1 Dec 2025 11:22:10 +0700 Subject: [PATCH] feat: integrate sequence service for document number generation in inventory services --- .../service/inventory_issue_service.go | 18 +++++++---- .../service/inventory_movement_service.go | 5 ++- .../service/inventory_receipt_service.go | 12 +++++-- .../service/inventory_request_service.go | 12 +++++-- .../service/inventory_return_service.go | 32 ++++++++++++------- modules/sequence/service/sequence_service.go | 26 +++++++++++++++ providers/core.go | 10 +++--- 7 files changed, 88 insertions(+), 27 deletions(-) diff --git a/modules/inventory_issue/service/inventory_issue_service.go b/modules/inventory_issue/service/inventory_issue_service.go index 1c6df58..8369bb2 100644 --- a/modules/inventory_issue/service/inventory_issue_service.go +++ b/modules/inventory_issue/service/inventory_issue_service.go @@ -7,6 +7,7 @@ import ( 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/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/google/uuid" @@ -26,9 +27,10 @@ type InventoryIssueService interface { } type inventoryIssueService struct { - db *gorm.DB - issueRepo repository.InventoryIssueRepository - issueLineRepo repository.InventoryIssueLineRepository + db *gorm.DB + issueRepo repository.InventoryIssueRepository + issueLineRepo repository.InventoryIssueLineRepository + sequenceService sequenceservice.SequenceService } // GetLinesByIssueId implements InventoryIssueService. @@ -40,8 +42,8 @@ func (s *inventoryIssueService) GetLinesByIssueId(ctx context.Context, issueId s return dtodomain.ToInventoryIssueLineResponses(lines), nil } -func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository) InventoryIssueService { - return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo} +func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService) InventoryIssueService { + return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService} } 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() 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 { tx.Rollback() return dtodomain.InventoryIssueResponse{}, err diff --git a/modules/inventory_movement/service/inventory_movement_service.go b/modules/inventory_movement/service/inventory_movement_service.go index 3ef657d..10ac130 100644 --- a/modules/inventory_movement/service/inventory_movement_service.go +++ b/modules/inventory_movement/service/inventory_movement_service.go @@ -7,6 +7,7 @@ import ( 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/repository" + sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/google/uuid" "gorm.io/gorm" @@ -28,6 +29,7 @@ type inventoryMovementService struct { db *gorm.DB movementRepo repository.InventoryMovementRepository movementLineRepo repository.InventoryMovementLineRepository + sequenceService sequenceservice.SequenceService } // GetLinesByMovementId implements InventoryMovementService. @@ -239,10 +241,11 @@ func (s *inventoryMovementService) DeleteLine(ctx context.Context, lineId string 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{ db: db, movementRepo: movementRepo, movementLineRepo: movementLineRepo, + sequenceService: sequenceService, } } diff --git a/modules/inventory_receipt/service/inventory_receipt_service.go b/modules/inventory_receipt/service/inventory_receipt_service.go index 22a48ac..13b866e 100644 --- a/modules/inventory_receipt/service/inventory_receipt_service.go +++ b/modules/inventory_receipt/service/inventory_receipt_service.go @@ -10,6 +10,7 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/repository" invstoragerepository "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/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" "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" @@ -38,6 +39,7 @@ type inventoryReceiptService struct { productRepo productrepository.ProductRepository uomRepo uomrepository.UomRepository invStorageRepository invstoragerepository.InventoryStorageRepository + sequenceService sequenceservice.SequenceService } 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() 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 { tx.Rollback() return dtodomain.InventoryReceiptResponse{}, err @@ -557,7 +563,8 @@ func NewInventoryReceiptService(db *gorm.DB, receiptLineRepo repository.InventoryReceiptLineRepository, productRepo productrepository.ProductRepository, uomRepo uomrepository.UomRepository, - invStorageRepository invstoragerepository.InventoryStorageRepository) InventoryReceiptService { + invStorageRepository invstoragerepository.InventoryStorageRepository, + sequenceService sequenceservice.SequenceService) InventoryReceiptService { return &inventoryReceiptService{ db: db, receiptRepo: receiptRepo, @@ -565,5 +572,6 @@ func NewInventoryReceiptService(db *gorm.DB, productRepo: productRepo, uomRepo: uomRepo, invStorageRepository: invStorageRepository, + sequenceService: sequenceService, } } diff --git a/modules/inventory_request/service/inventory_request_service.go b/modules/inventory_request/service/inventory_request_service.go index a0aa3b0..c0ae942 100644 --- a/modules/inventory_request/service/inventory_request_service.go +++ b/modules/inventory_request/service/inventory_request_service.go @@ -7,6 +7,7 @@ import ( 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/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/google/uuid" @@ -29,6 +30,7 @@ type inventoryRequestService struct { db *gorm.DB requestRepo repository.InventoryRequestRepository requestLineRepo repository.InventoryRequestLineRepository + sequenceService sequenceservice.SequenceService } // GetLinesByRequestId implements InventoryRequestService. @@ -99,7 +101,12 @@ func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.Inve tx.Rollback() 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 { tx.Rollback() return dtodomain.InventoryRequestResponse{}, err @@ -276,10 +283,11 @@ func (s *inventoryRequestService) CreateLine(ctx context.Context, requestId stri }, 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{ db: db, requestRepo: requestRepo, requestLineRepo: requestLineRepo, + sequenceService: sequenceService, } } diff --git a/modules/inventory_return/service/inventory_return_service.go b/modules/inventory_return/service/inventory_return_service.go index 2d65bc8..03ebd78 100644 --- a/modules/inventory_return/service/inventory_return_service.go +++ b/modules/inventory_return/service/inventory_return_service.go @@ -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/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/google/uuid" "gorm.io/gorm" @@ -28,11 +30,12 @@ type InventoryReturnService interface { } type inventoryReturnService struct { - db *gorm.DB - returnRepo repository.InventoryReturnRepository - returnLineRepo repository.InventoryReturnLineRepository - issueLineRepo issueRepository.InventoryIssueLineRepository - productRepo productrepository.ProductRepository + db *gorm.DB + returnRepo repository.InventoryReturnRepository + returnLineRepo repository.InventoryReturnLineRepository + issueLineRepo issueRepository.InventoryIssueLineRepository + productRepo productrepository.ProductRepository + sequenceService sequenceservice.SequenceService } // GetLinesByReturnId implements InventoryReturnService. @@ -103,7 +106,12 @@ func (s *inventoryReturnService) Create(ctx context.Context, req dtodomain.Inven tx.Rollback() 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 { tx.Rollback() return dtodomain.InventoryReturnResponse{}, err @@ -270,12 +278,14 @@ func NewInventoryReturnService( returnLineRepo repository.InventoryReturnLineRepository, issueLineRepo issueRepository.InventoryIssueLineRepository, productRepo productrepository.ProductRepository, + sequenceService sequenceservice.SequenceService, ) InventoryReturnService { return &inventoryReturnService{ - db: db, - returnRepo: returnRepo, - returnLineRepo: returnLineRepo, - issueLineRepo: issueLineRepo, - productRepo: productRepo, + db: db, + returnRepo: returnRepo, + returnLineRepo: returnLineRepo, + issueLineRepo: issueLineRepo, + productRepo: productRepo, + sequenceService: sequenceService, } } diff --git a/modules/sequence/service/sequence_service.go b/modules/sequence/service/sequence_service.go index e4ed93b..ecf3dfc 100644 --- a/modules/sequence/service/sequence_service.go +++ b/modules/sequence/service/sequence_service.go @@ -7,6 +7,7 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/repository" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" + "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "gorm.io/gorm" ) @@ -15,6 +16,7 @@ type SequenceService interface { 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) 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 { @@ -93,3 +95,27 @@ func (s *sequenceService) GetNextSequence(ctx context.Context, clientId string, } 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 +} diff --git a/providers/core.go b/providers/core.go index f1ad156..dda6f92 100644 --- a/providers/core.go +++ b/providers/core.go @@ -186,12 +186,12 @@ func RegisterDependencies(injector *do.Injector) { warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db) zonaServ := zonaService.NewZonaService(zonaRepository, 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) - inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository) - inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository) - inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository) - inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository) + inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ) + inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ) + inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ) + inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository) inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)