mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix: all implemented fifo v2
This commit is contained in:
@@ -11,12 +11,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/database"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -61,13 +59,7 @@ func main() {
|
||||
ctx := context.Background()
|
||||
db := database.Connect(config.DBHost, config.DBName)
|
||||
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
stockAllocRepo := commonRepo.NewStockAllocationRepository(db)
|
||||
fifoSvc := commonSvc.NewFifoService(db, stockAllocRepo, productWarehouseRepo, nil)
|
||||
fifoStockV2Svc := commonSvc.NewFifoStockV2Service(db, nil)
|
||||
if err := registerAdjustmentFIFO(fifoSvc); err != nil {
|
||||
log.Fatalf("failed to register adjustment fifo config: %v", err)
|
||||
}
|
||||
|
||||
adjustments, err := loadAdjustments(ctx, db, ids)
|
||||
if err != nil {
|
||||
@@ -185,7 +177,7 @@ func main() {
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
"PLAN adj=%d lane=STOCKABLE function=%s total=%.3f remove_qty=%.3f action=reverse_stock+delete\n",
|
||||
"PLAN adj=%d lane=STOCKABLE function=%s total=%.3f remove_qty=%.3f action=reflow_to_zero+delete\n",
|
||||
adj.ID,
|
||||
route.FunctionCode,
|
||||
adj.TotalQty,
|
||||
@@ -198,16 +190,25 @@ func main() {
|
||||
}
|
||||
|
||||
err = db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if removeQty > 0 {
|
||||
if err := fifoSvc.AdjustStockableQuantity(ctx, commonSvc.StockAdjustRequest{
|
||||
StockableKey: fifo.StockableKeyAdjustmentIn,
|
||||
StockableID: adj.ID,
|
||||
ProductWarehouseID: adj.ProductWarehouseID,
|
||||
Quantity: -removeQty,
|
||||
Tx: tx,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("reverse stockable quantity: %w", err)
|
||||
}
|
||||
if err := tx.WithContext(ctx).
|
||||
Table("adjustment_stocks").
|
||||
Where("id = ?", adj.ID).
|
||||
Updates(map[string]any{
|
||||
"total_qty": 0,
|
||||
"total_used": 0,
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("set stockable qty to zero: %w", err)
|
||||
}
|
||||
|
||||
reflowReq := commonSvc.FifoStockV2ReflowRequest{
|
||||
FlagGroupCode: route.FlagGroupCode,
|
||||
ProductWarehouseID: adj.ProductWarehouseID,
|
||||
AsOf: &adj.CreatedAt,
|
||||
IdempotencyKey: fmt.Sprintf("delete-adjustment-stockable-%d-%d", adj.ID, time.Now().UnixNano()),
|
||||
Tx: tx,
|
||||
}
|
||||
if _, err := fifoStockV2Svc.Reflow(ctx, reflowReq); err != nil {
|
||||
return fmt.Errorf("reflow stockable to zero: %w", err)
|
||||
}
|
||||
|
||||
if err := hardDeleteStockableAllocations(ctx, tx, fifo.StockableKeyAdjustmentIn.String(), adj.ID); err != nil {
|
||||
@@ -243,39 +244,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func registerAdjustmentFIFO(fifoSvc commonSvc.FifoService) error {
|
||||
if err := fifoSvc.RegisterStockable(fifo.StockableConfig{
|
||||
Key: fifo.StockableKeyAdjustmentIn,
|
||||
Table: "adjustment_stocks",
|
||||
Columns: fifo.StockableColumns{
|
||||
ID: "id",
|
||||
ProductWarehouseID: "product_warehouse_id",
|
||||
TotalQuantity: "total_qty",
|
||||
TotalUsedQuantity: "total_used",
|
||||
CreatedAt: "created_at",
|
||||
},
|
||||
OrderBy: []string{"created_at ASC", "id ASC"},
|
||||
}); err != nil && !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := fifoSvc.RegisterUsable(fifo.UsableConfig{
|
||||
Key: fifo.UsableKeyAdjustmentOut,
|
||||
Table: "adjustment_stocks",
|
||||
Columns: fifo.UsableColumns{
|
||||
ID: "id",
|
||||
ProductWarehouseID: "product_warehouse_id",
|
||||
UsageQuantity: "usage_qty",
|
||||
PendingQuantity: "pending_qty",
|
||||
CreatedAt: "created_at",
|
||||
},
|
||||
OrderBy: []string{"created_at ASC", "id ASC"},
|
||||
}); err != nil && !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func modeLabel(apply bool) string {
|
||||
if apply {
|
||||
return "APPLY"
|
||||
|
||||
Reference in New Issue
Block a user