mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +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)
|
||||
|
||||
Reference in New Issue
Block a user