From e503a84660ebc9d515d49ce880f96f08402a94ab Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Tue, 27 Jan 2026 20:43:01 +0700 Subject: [PATCH] [FEAT] Add stock logging for transfer laying process and introduce new StockLogType for transfer laying --- .../services/transfer_laying.service.go | 43 ++++++++++++++++--- internal/utils/constant.go | 13 +++--- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/internal/modules/production/transfer_layings/services/transfer_laying.service.go b/internal/modules/production/transfer_layings/services/transfer_laying.service.go index a5d0ba88..3aa4788b 100644 --- a/internal/modules/production/transfer_layings/services/transfer_laying.service.go +++ b/internal/modules/production/transfer_layings/services/transfer_laying.service.go @@ -650,9 +650,9 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) ( repoTx := s.Repository.WithTx(dbTransaction) approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction)) - // Gunakan repo baru untuk transaction scope agar bisa akses method custom sourceRepoTx := repository.NewLayingTransferSourceRepository(dbTransaction) targetRepoTx := repository.NewLayingTransferTargetRepository(dbTransaction) + stockLogRepoTx := rStockLogs.NewStockLogRepository(dbTransaction) for _, approvableID := range approvableIDs { transfer, err := repoTx.GetByID(c.Context(), approvableID, nil) @@ -687,23 +687,28 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) ( return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil targets transfer") } - // Hitung total quantity dari targets untuk di-consume dari sources totalTargetQty := 0.0 for _, target := range targets { totalTargetQty += target.TotalQty } - // Consume dari laying_transfer_sources (Usable) - akan consume dari ProjectFlockPopulation (Stockable) + totalSourceRequested := 0.0 + for _, source := range sources { + totalSourceRequested += source.RequestedQty + } + for _, source := range sources { if source.ProductWarehouseId == nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Source product warehouse tidak ditemukan untuk transfer %d", approvableID)) } + sourceShare := (source.RequestedQty / totalSourceRequested) * totalTargetQty + consumeResult, err := s.FifoSvc.Consume(c.Context(), commonSvc.StockConsumeRequest{ UsableKey: fifo.UsableKeyTransferToLayingOut, UsableID: source.Id, ProductWarehouseID: *source.ProductWarehouseId, - Quantity: totalTargetQty, + Quantity: sourceShare, AllowPending: false, Tx: dbTransaction, }) @@ -717,6 +722,19 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) ( }, nil); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Gagal update source usage qty") } + + stockLogDecrease := &entity.StockLog{ + ProductWarehouseId: *source.ProductWarehouseId, + CreatedBy: actorID, + Increase: 0, + Decrease: sourceShare, + LoggableType: string(utils.StockLogTypeTransferLaying), + LoggableId: approvableID, + Notes: fmt.Sprintf("TL #%s", transfer.TransferNumber), + } + if err := stockLogRepoTx.CreateOne(c.Context(), stockLogDecrease, nil); err != nil { + return fiber.NewError(fiber.StatusInternalServerError, "Gagal membuat log stok keluar") + } } for _, target := range targets { @@ -725,7 +743,7 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) ( } note := fmt.Sprintf("Transfer to Laying #%s", transfer.TransferNumber) - replenishResult, err := s.FifoSvc.Replenish(c.Context(), commonSvc.StockReplenishRequest{ + _, err := s.FifoSvc.Replenish(c.Context(), commonSvc.StockReplenishRequest{ StockableKey: fifo.StockableKeyTransferToLayingIn, StockableID: target.Id, ProductWarehouseID: *target.ProductWarehouseId, @@ -738,10 +756,23 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) ( } if err := targetRepoTx.PatchOne(c.Context(), target.Id, map[string]interface{}{ - "total_qty": replenishResult.AddedQuantity, + "total_qty": target.TotalQty, }, nil); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Gagal update target total qty") } + + stockLogIncrease := &entity.StockLog{ + ProductWarehouseId: *target.ProductWarehouseId, + CreatedBy: actorID, + Increase: target.TotalQty, + Decrease: 0, + LoggableType: string(utils.StockLogTypeTransferLaying), + LoggableId: approvableID, + Notes: fmt.Sprintf("TL #%s", transfer.TransferNumber), + } + if err := stockLogRepoTx.CreateOne(c.Context(), stockLogIncrease, nil); err != nil { + return fiber.NewError(fiber.StatusInternalServerError, "Gagal membuat log stok masuk") + } } } } diff --git a/internal/utils/constant.go b/internal/utils/constant.go index d27b07ef..cb8586ac 100644 --- a/internal/utils/constant.go +++ b/internal/utils/constant.go @@ -109,12 +109,13 @@ const ( type StockLogType string const ( - StockLogTypeAdjustment StockLogType = "ADJUSTMENT" - StockLogTypeTransfer StockLogType = "TRANSFER" - StockLogTypeMarketing StockLogType = "MARKETING" - StockLogTypeChikin StockLogType = "CHICKIN" - StockLogTypePurchase StockLogType = "PURCHASE" - StockLogTypeRecording StockLogType = "RECORDING" + StockLogTypeAdjustment StockLogType = "ADJUSTMENT" + StockLogTypeTransfer StockLogType = "TRANSFER" + StockLogTypeTransferLaying StockLogType = "TRANSFER_LAYING" + StockLogTypeMarketing StockLogType = "MARKETING" + StockLogTypeChikin StockLogType = "CHICKIN" + StockLogTypePurchase StockLogType = "PURCHASE" + StockLogTypeRecording StockLogType = "RECORDING" ) // -------------------------------------------------------------------