mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 06:45:43 +00:00
fix: first push need support testing, and implemented fifo v2 to all modules
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
projectFlockKandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
stockLogsRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -167,15 +166,6 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
|
||||
transactionType := utils.ResolveAdjustmentTransactionType(routeMeta.FunctionCode)
|
||||
|
||||
allowPending := false
|
||||
if routeMeta.Lane == adjustmentLaneUsable {
|
||||
allowPending, err = s.resolveOverconsumePolicy(ctx, routeMeta)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to resolve overconsume rule: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve FIFO policy")
|
||||
}
|
||||
}
|
||||
|
||||
var createdAdjustmentStockId uint
|
||||
|
||||
var projectFlockKandangID *uint
|
||||
@@ -228,6 +218,12 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
Price: req.Price,
|
||||
GrandTotal: grandTotal,
|
||||
}
|
||||
switch routeMeta.Lane {
|
||||
case adjustmentLaneStockable:
|
||||
adjustmentStock.TotalQty = qty
|
||||
case adjustmentLaneUsable:
|
||||
adjustmentStock.UsageQty = qty
|
||||
}
|
||||
code, err := adjustmentStockRepoTX.GenerateSequentialNumber(ctx, utils.AdjustmentStockNumberPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -240,60 +236,32 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
var increaseQty float64
|
||||
var decreaseQty float64
|
||||
|
||||
if routeMeta.Lane != adjustmentLaneStockable && routeMeta.Lane != adjustmentLaneUsable {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Unsupported transaction subtype lane")
|
||||
}
|
||||
if s.FifoStockV2Svc == nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||
}
|
||||
|
||||
asOf := adjustmentStock.CreatedAt
|
||||
if _, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: routeMeta.FlagGroupCode,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
AsOf: &asOf,
|
||||
Tx: tx,
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to reflow stock via FIFO v2: %v", err))
|
||||
}
|
||||
|
||||
refreshedAdjustment, err := adjustmentStockRepoTX.GetByID(ctx, adjustmentStock.Id, nil)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to refresh adjustment stock")
|
||||
}
|
||||
switch routeMeta.Lane {
|
||||
case adjustmentLaneStockable:
|
||||
fifoNote := fmt.Sprintf("Stock Adjustment %s #%s", routeMeta.FunctionCode, adjustmentStock.AdjNumber)
|
||||
result, err := s.FifoSvc.Replenish(ctx, common.StockReplenishRequest{
|
||||
StockableKey: fifo.StockableKeyAdjustmentIn,
|
||||
StockableID: adjustmentStock.Id,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
Quantity: qty,
|
||||
Note: &fifoNote,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to replenish stock via FIFO: %v", err))
|
||||
}
|
||||
increaseQty = result.AddedQuantity
|
||||
increaseQty = refreshedAdjustment.TotalQty
|
||||
case adjustmentLaneUsable:
|
||||
if s.FifoStockV2Svc != nil {
|
||||
usableLegacyTypeKey := fifo.UsableKeyAdjustmentOut.String()
|
||||
if routeMeta.SourceTable == "adjustment_stocks" && strings.TrimSpace(routeMeta.LegacyTypeKey) != "" {
|
||||
usableLegacyTypeKey = strings.TrimSpace(routeMeta.LegacyTypeKey)
|
||||
}
|
||||
|
||||
reflowResult, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: routeMeta.FlagGroupCode,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
Usable: common.FifoStockV2Ref{
|
||||
ID: adjustmentStock.Id,
|
||||
LegacyTypeKey: usableLegacyTypeKey,
|
||||
FunctionCode: routeMeta.FunctionCode,
|
||||
},
|
||||
DesiredQty: qty,
|
||||
AllowOverConsume: &allowPending,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to consume stock via FIFO v2: %v", err))
|
||||
}
|
||||
decreaseQty = reflowResult.Allocate.AllocatedQty
|
||||
} else {
|
||||
result, err := s.FifoSvc.Consume(ctx, common.StockConsumeRequest{
|
||||
UsableKey: fifo.UsableKeyAdjustmentOut,
|
||||
UsableID: adjustmentStock.Id,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
Quantity: qty,
|
||||
AllowPending: allowPending,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to consume stock via FIFO: %v", err))
|
||||
}
|
||||
decreaseQty = result.UsageQuantity
|
||||
}
|
||||
default:
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Unsupported transaction subtype lane")
|
||||
decreaseQty = refreshedAdjustment.UsageQty
|
||||
}
|
||||
|
||||
stockLogs, err := stockLogRepoTX.GetByProductWarehouse(ctx, productWarehouse.Id, 1)
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
projectFlockKandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -444,83 +443,79 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
||||
}
|
||||
}
|
||||
|
||||
pakanProducts := map[uint]bool{}
|
||||
if s.FifoStockV2Svc != nil && len(req.Products) > 0 {
|
||||
pakanProducts, err = s.resolvePakanProducts(c.Context(), tx, req.Products)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.FifoStockV2Svc == nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||
}
|
||||
flagGroupByProduct := make(map[uint]string, len(req.Products))
|
||||
|
||||
for _, product := range req.Products {
|
||||
detail := detailMap[uint64(product.ProductID)]
|
||||
if detail == nil || detail.SourceProductWarehouseID == nil || detail.DestProductWarehouseID == nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Data transfer detail tidak valid")
|
||||
}
|
||||
|
||||
outUsageQty := 0.0
|
||||
outPendingQty := 0.0
|
||||
useFifoV2 := s.FifoStockV2Svc != nil && pakanProducts[uint(product.ProductID)]
|
||||
if useFifoV2 {
|
||||
s.Log.Infof(
|
||||
"[fifo-v2][transfer] use reflow movement=%s detail_id=%d product_id=%d source_pw=%d qty=%.3f",
|
||||
entityTransfer.MovementNumber,
|
||||
detail.Id,
|
||||
product.ProductID,
|
||||
*detail.SourceProductWarehouseID,
|
||||
product.ProductQty,
|
||||
)
|
||||
reflowResult, err := s.FifoStockV2Svc.Reflow(c.Context(), commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: "PAKAN",
|
||||
ProductWarehouseID: uint(*detail.SourceProductWarehouseID),
|
||||
Usable: commonSvc.FifoStockV2Ref{
|
||||
ID: uint(detail.Id),
|
||||
LegacyTypeKey: fifo.UsableKeyStockTransferOut.String(),
|
||||
FunctionCode: "STOCK_TRANSFER_OUT",
|
||||
},
|
||||
DesiredQty: product.ProductQty,
|
||||
Tx: tx,
|
||||
})
|
||||
flagGroupCode, ok := flagGroupByProduct[uint(product.ProductID)]
|
||||
if !ok {
|
||||
flagGroupCode, err = s.resolveTransferFlagGroup(c.Context(), tx, uint(product.ProductID))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal. Error: %v", product.ProductID, err))
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("FIFO v2 route tidak ditemukan untuk produk %d: %v", product.ProductID, err))
|
||||
}
|
||||
outUsageQty = reflowResult.Allocate.AllocatedQty
|
||||
outPendingQty = reflowResult.Allocate.PendingQty
|
||||
s.Log.Infof(
|
||||
"[fifo-v2][transfer] reflow result movement=%s detail_id=%d usage=%.3f pending=%.3f",
|
||||
entityTransfer.MovementNumber,
|
||||
detail.Id,
|
||||
outUsageQty,
|
||||
outPendingQty,
|
||||
)
|
||||
} else {
|
||||
consumeResult, err := s.FifoSvc.Consume(c.Context(), commonSvc.StockConsumeRequest{
|
||||
UsableKey: fifo.UsableKeyStockTransferOut,
|
||||
UsableID: uint(detail.Id),
|
||||
ProductWarehouseID: uint(*detail.SourceProductWarehouseID),
|
||||
Quantity: product.ProductQty,
|
||||
AllowPending: false,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal. Error: %v", product.ProductID, err))
|
||||
}
|
||||
outUsageQty = consumeResult.UsageQuantity
|
||||
outPendingQty = consumeResult.PendingQuantity
|
||||
flagGroupByProduct[uint(product.ProductID)] = flagGroupCode
|
||||
}
|
||||
|
||||
if err := tx.Model(&entity.StockTransferDetail{}).
|
||||
Where("id = ?", detail.Id).
|
||||
Updates(map[string]interface{}{
|
||||
"usage_qty": outUsageQty,
|
||||
"pending_qty": outPendingQty,
|
||||
"usage_qty": product.ProductQty,
|
||||
"pending_qty": 0,
|
||||
"total_qty": product.ProductQty,
|
||||
}).Error; err != nil {
|
||||
s.Log.Errorf("Failed to update tracking usage for detail_id=%d, product_id=%d: %+v", detail.Id, product.ProductID, err)
|
||||
s.Log.Errorf("Failed to update transfer detail seed fields for detail_id=%d, product_id=%d: %+v", detail.Id, product.ProductID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui data tracking")
|
||||
}
|
||||
|
||||
asOf := transferDate
|
||||
if _, err := s.FifoStockV2Svc.Reflow(c.Context(), commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: flagGroupCode,
|
||||
ProductWarehouseID: uint(*detail.SourceProductWarehouseID),
|
||||
AsOf: &asOf,
|
||||
Tx: tx,
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal. Error: %v", product.ProductID, err))
|
||||
}
|
||||
if _, err := s.FifoStockV2Svc.Reflow(c.Context(), commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: flagGroupCode,
|
||||
ProductWarehouseID: uint(*detail.DestProductWarehouseID),
|
||||
AsOf: &asOf,
|
||||
Tx: tx,
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Gagal reflow stok tujuan untuk produk %d. Error: %v", product.ProductID, err))
|
||||
}
|
||||
|
||||
type usageSnapshot struct {
|
||||
UsageQty float64 `gorm:"column:usage_qty"`
|
||||
PendingQty float64 `gorm:"column:pending_qty"`
|
||||
}
|
||||
var usage usageSnapshot
|
||||
if err := tx.WithContext(c.Context()).
|
||||
Table("stock_transfer_details").
|
||||
Select("usage_qty, pending_qty").
|
||||
Where("id = ?", detail.Id).
|
||||
Take(&usage).Error; err != nil {
|
||||
s.Log.Errorf("Failed to read transfer usage snapshot detail_id=%d, product_id=%d: %+v", detail.Id, product.ProductID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data tracking")
|
||||
}
|
||||
outUsageQty := usage.UsageQty
|
||||
outPendingQty := usage.PendingQty
|
||||
if outPendingQty > 1e-6 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal", product.ProductID))
|
||||
}
|
||||
|
||||
stockLogDecrease := &entity.StockLog{
|
||||
ProductWarehouseId: uint(*detail.SourceProductWarehouseID),
|
||||
CreatedBy: uint(actorID),
|
||||
Increase: 0,
|
||||
Decrease: product.ProductQty,
|
||||
Decrease: outUsageQty,
|
||||
LoggableType: string(utils.StockLogTypeTransfer),
|
||||
LoggableId: uint(detail.Id),
|
||||
Notes: "",
|
||||
@@ -541,45 +536,12 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal membuat log stok keluar")
|
||||
}
|
||||
|
||||
note := fmt.Sprintf("Transfer #%s", entityTransfer.MovementNumber)
|
||||
inAddedQty := 0.0
|
||||
if useFifoV2 {
|
||||
s.Log.Infof(
|
||||
"[fifo-v2][transfer] stock-in uses replenish path movement=%s detail_id=%d product_id=%d dest_pw=%d qty=%.3f",
|
||||
entityTransfer.MovementNumber,
|
||||
detail.Id,
|
||||
product.ProductID,
|
||||
*detail.DestProductWarehouseID,
|
||||
product.ProductQty,
|
||||
)
|
||||
}
|
||||
replenishResult, err := s.FifoSvc.Replenish(c.Context(), commonSvc.StockReplenishRequest{
|
||||
StockableKey: fifo.StockableKeyStockTransferIn,
|
||||
StockableID: uint(detail.Id),
|
||||
ProductWarehouseID: uint(*detail.DestProductWarehouseID),
|
||||
Quantity: product.ProductQty,
|
||||
Note: ¬e,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to replenish stock for product_id=%d, pw_id=%d, qty=%.2f: %+v", product.ProductID, *detail.DestProductWarehouseID, product.ProductQty, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menambah stok gudang tujuan")
|
||||
}
|
||||
inAddedQty = replenishResult.AddedQuantity
|
||||
|
||||
if err := tx.Model(&entity.StockTransferDetail{}).
|
||||
Where("id = ?", detail.Id).
|
||||
Updates(map[string]interface{}{
|
||||
"total_qty": inAddedQty,
|
||||
}).Error; err != nil {
|
||||
s.Log.Errorf("Failed to update tracking total for detail_id=%d, product_id=%d: %+v", detail.Id, product.ProductID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui data tracking")
|
||||
}
|
||||
inAddedQty := outUsageQty
|
||||
|
||||
stockLogIncrease := &entity.StockLog{
|
||||
ProductWarehouseId: uint(*detail.DestProductWarehouseID),
|
||||
CreatedBy: uint(actorID),
|
||||
Increase: product.ProductQty,
|
||||
Increase: inAddedQty,
|
||||
Decrease: 0,
|
||||
LoggableType: string(utils.StockLogTypeTransfer),
|
||||
LoggableId: uint(detail.Id),
|
||||
@@ -657,51 +619,45 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *transferService) resolvePakanProducts(
|
||||
func (s *transferService) resolveTransferFlagGroup(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
products []validation.TransferProduct,
|
||||
) (map[uint]bool, error) {
|
||||
out := make(map[uint]bool, len(products))
|
||||
if len(products) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
productIDs := make([]uint, 0, len(products))
|
||||
seen := make(map[uint]struct{}, len(products))
|
||||
for _, product := range products {
|
||||
if product.ProductID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[product.ProductID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[product.ProductID] = struct{}{}
|
||||
productIDs = append(productIDs, product.ProductID)
|
||||
}
|
||||
if len(productIDs) == 0 {
|
||||
return out, nil
|
||||
productID uint,
|
||||
) (string, error) {
|
||||
if productID == 0 {
|
||||
return "", fmt.Errorf("product id is required")
|
||||
}
|
||||
|
||||
type row struct {
|
||||
ProductID uint `gorm:"column:product_id"`
|
||||
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||
}
|
||||
var rows []row
|
||||
var selected row
|
||||
err := tx.WithContext(ctx).
|
||||
Table("flags f").
|
||||
Select("DISTINCT f.flagable_id AS product_id").
|
||||
Where("f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||
Where("f.name IN ?", []string{"PAKAN", "PRE-STARTER", "STARTER", "FINISHER"}).
|
||||
Where("f.flagable_id IN ?", productIDs).
|
||||
Scan(&rows).Error
|
||||
Table("fifo_stock_v2_route_rules rr").
|
||||
Select("rr.flag_group_code").
|
||||
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
||||
Where("rr.is_active = TRUE").
|
||||
Where("rr.lane = ?", "USABLE").
|
||||
Where("rr.function_code = ?", "STOCK_TRANSFER_OUT").
|
||||
Where("rr.source_table = ?", "stock_transfer_details").
|
||||
Where(`
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM flags f
|
||||
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
||||
WHERE f.flagable_type = ?
|
||||
AND f.flagable_id = ?
|
||||
AND fm.flag_group_code = rr.flag_group_code
|
||||
)
|
||||
`, entity.FlagableTypeProduct, productID).
|
||||
Order("rr.id ASC").
|
||||
Limit(1).
|
||||
Take(&selected).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
out[row.ProductID] = true
|
||||
}
|
||||
return out, nil
|
||||
return strings.TrimSpace(selected.FlagGroupCode), nil
|
||||
}
|
||||
|
||||
func (s *transferService) notifyExpenseItemsDelivered(c *fiber.Ctx, transferID uint64, payloads []TransferExpenseReceivingPayload) error {
|
||||
|
||||
Reference in New Issue
Block a user