mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 23:05:44 +00:00
feat: refactor module adjusment stock, adjust constant, adjust table migration and create command reflow and delete module adjusment stock
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -40,8 +41,14 @@ type adjustmentService struct {
|
||||
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
||||
AdjustmentStockRepository adjustmentStockRepo.AdjustmentStockRepository
|
||||
FifoSvc common.FifoService
|
||||
FifoStockV2Svc common.FifoStockV2Service
|
||||
}
|
||||
|
||||
const (
|
||||
adjustmentLaneStockable = "STOCKABLE"
|
||||
adjustmentLaneUsable = "USABLE"
|
||||
)
|
||||
|
||||
func NewAdjustmentService(
|
||||
productRepo productRepo.ProductRepository,
|
||||
stockLogsRepo stockLogsRepo.StockLogRepository,
|
||||
@@ -49,6 +56,7 @@ func NewAdjustmentService(
|
||||
productWarehouseRepo ProductWarehouse.ProductWarehouseRepository,
|
||||
adjustmentStockRepo adjustmentStockRepo.AdjustmentStockRepository,
|
||||
fifoSvc common.FifoService,
|
||||
fifoStockV2Svc common.FifoStockV2Service,
|
||||
validate *validator.Validate,
|
||||
projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository,
|
||||
) AdjustmentService {
|
||||
@@ -62,6 +70,7 @@ func NewAdjustmentService(
|
||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||
AdjustmentStockRepository: adjustmentStockRepo,
|
||||
FifoSvc: fifoSvc,
|
||||
FifoStockV2Svc: fifoStockV2Svc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +79,9 @@ func (s *adjustmentService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
Preload("ProductWarehouse").
|
||||
Preload("ProductWarehouse.Product").
|
||||
Preload("ProductWarehouse.Warehouse").
|
||||
Preload("ProductWarehouse.Warehouse.Location").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock").
|
||||
Preload("StockLog.CreatedUser")
|
||||
}
|
||||
|
||||
@@ -94,47 +106,93 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
productID := req.ProductID
|
||||
if productID == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Product is required")
|
||||
}
|
||||
|
||||
qty := req.Qty
|
||||
if qty <= 0 {
|
||||
qty = req.Quantity
|
||||
}
|
||||
if qty <= 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
||||
}
|
||||
|
||||
functionCode := strings.ToUpper(strings.TrimSpace(req.TransactionSubtype))
|
||||
if functionCode == "" {
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(req.TransactionSubType))
|
||||
}
|
||||
if functionCode == "" {
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(req.FunctionCode))
|
||||
}
|
||||
if functionCode == "" {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype is required")
|
||||
}
|
||||
if functionCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionIn) {
|
||||
functionCode = string(utils.AdjustmentTransactionSubtypeRecordingDepletionOut)
|
||||
}
|
||||
|
||||
warehouseID, err := s.resolveWarehouseID(c.Context(), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
note := strings.TrimSpace(req.Notes)
|
||||
if note == "" {
|
||||
note = strings.TrimSpace(req.Note)
|
||||
}
|
||||
grandTotal := math.Round((qty*req.Price)*1000) / 1000
|
||||
|
||||
ctx := c.Context()
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureWarehouseAccess(c, s.WarehouseRepo.DB(), uint(req.WarehouseID)); err != nil {
|
||||
if err := m.EnsureWarehouseAccess(c, s.WarehouseRepo.DB(), warehouseID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
common.RelationCheck{Name: "Product", ID: &req.ProductID, Exists: s.ProductRepo.IdExists},
|
||||
common.RelationCheck{Name: "Warehouse", ID: &req.WarehouseID, Exists: s.WarehouseRepo.IdExists},
|
||||
if err := common.EnsureRelations(ctx,
|
||||
common.RelationCheck{Name: "Product", ID: &productID, Exists: s.ProductRepo.IdExists},
|
||||
common.RelationCheck{Name: "Warehouse", ID: &warehouseID, Exists: s.WarehouseRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Quantity <= 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
||||
routeMeta, err := s.resolveRouteByFunctionCode(ctx, productID, functionCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
transactionType := strings.ToUpper(req.TransactionType)
|
||||
if transactionType != string(utils.StockLogTransactionTypeIncrease) && transactionType != string(utils.StockLogTransactionTypeDecrease) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transaction type")
|
||||
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
|
||||
pfkID, err := s.getActiveProjectFlockKandangID(ctx, uint(req.WarehouseID))
|
||||
pfkID, err := s.getActiveProjectFlockKandangID(ctx, warehouseID)
|
||||
if err == nil && pfkID > 0 {
|
||||
projectFlockKandangID = &pfkID
|
||||
}
|
||||
|
||||
pw, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk(ctx, uint(req.ProductID), uint(req.WarehouseID), projectFlockKandangID)
|
||||
pw, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk(ctx, productID, warehouseID, projectFlockKandangID)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to get product warehouse")
|
||||
}
|
||||
|
||||
newPW := &entity.ProductWarehouse{
|
||||
ProductId: uint(req.ProductID),
|
||||
WarehouseId: uint(req.WarehouseID),
|
||||
ProductId: productID,
|
||||
WarehouseId: warehouseID,
|
||||
Quantity: 0,
|
||||
ProjectFlockKandangId: projectFlockKandangID,
|
||||
}
|
||||
@@ -153,96 +211,115 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return nil, err
|
||||
}
|
||||
err = s.StockLogsRepository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
productWarehouseRepoTX := ProductWarehouse.NewProductWarehouseRepository(tx)
|
||||
stockLogRepoTX := stockLogsRepo.NewStockLogRepository(tx)
|
||||
adjustmentStockRepoTX := s.AdjustmentStockRepository.WithTx(tx)
|
||||
|
||||
productWarehouse, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk(ctx, uint(req.ProductID), uint(req.WarehouseID), projectFlockKandangID)
|
||||
productWarehouse, err := productWarehouseRepoTX.FindByProductWarehouseAndPfk(ctx, productID, warehouseID, projectFlockKandangID)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get product warehouse: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get product warehouse")
|
||||
}
|
||||
|
||||
newLog := &entity.StockLog{
|
||||
LoggableType: string(utils.StockLogTypeAdjustment),
|
||||
LoggableId: 0,
|
||||
Notes: req.Note,
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
stockLogs, err := s.StockLogsRepository.GetByProductWarehouse(ctx, productWarehouse.Id, 1)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get stock logs: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||
}
|
||||
|
||||
if len(stockLogs) > 0 {
|
||||
latestStockLog := stockLogs[0]
|
||||
newLog.Stock = latestStockLog.Stock
|
||||
} else {
|
||||
newLog.Stock = 0
|
||||
}
|
||||
|
||||
if transactionType == string(utils.StockLogTransactionTypeIncrease) {
|
||||
newLog.Increase = req.Quantity
|
||||
newLog.Stock += newLog.Increase
|
||||
} else {
|
||||
if productWarehouse.Quantity < req.Quantity {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk pengurangan. Stok saat ini: %.2f, Jumlah yang akan dikurangi: %.2f", productWarehouse.Quantity, req.Quantity))
|
||||
}
|
||||
newLog.Decrease = req.Quantity
|
||||
newLog.Stock -= newLog.Decrease
|
||||
}
|
||||
|
||||
if err := s.StockLogsRepository.WithTx(tx).CreateOne(ctx, newLog, nil); err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
adjustmentStock := &entity.AdjustmentStock{
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
TransactionType: transactionType,
|
||||
FunctionCode: routeMeta.FunctionCode,
|
||||
Price: req.Price,
|
||||
GrandTotal: grandTotal,
|
||||
}
|
||||
code, err := s.AdjustmentStockRepository.GenerateSequentialNumber(ctx, utils.AdjustmentStockNumberPrefix)
|
||||
code, err := adjustmentStockRepoTX.GenerateSequentialNumber(ctx, utils.AdjustmentStockNumberPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adjustmentStock.AdjNumber = code
|
||||
if err := s.AdjustmentStockRepository.WithTx(tx).CreateOne(ctx, adjustmentStock, nil); err != nil {
|
||||
|
||||
if err := adjustmentStockRepoTX.CreateOne(ctx, adjustmentStock, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create adjustment stock record")
|
||||
}
|
||||
|
||||
newLog.LoggableType = string(utils.StockLogTypeAdjustment)
|
||||
newLog.LoggableId = adjustmentStock.Id
|
||||
if err := s.StockLogsRepository.WithTx(tx).UpdateOne(ctx, newLog.Id, newLog, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to link stock log")
|
||||
}
|
||||
var increaseQty float64
|
||||
var decreaseQty float64
|
||||
|
||||
if transactionType == string(utils.StockLogTransactionTypeIncrease) {
|
||||
|
||||
note := fmt.Sprintf("Stock Adjustment IN #%d", newLog.Id)
|
||||
_, err := s.FifoSvc.Replenish(ctx, common.StockReplenishRequest{
|
||||
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: uint(productWarehouse.Id),
|
||||
Quantity: req.Quantity,
|
||||
Note: ¬e,
|
||||
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
|
||||
case adjustmentLaneUsable:
|
||||
if s.FifoStockV2Svc != nil {
|
||||
usableLegacyTypeKey := fifo.UsableKeyAdjustmentOut.String()
|
||||
if routeMeta.SourceTable == "adjustment_stocks" && strings.TrimSpace(routeMeta.LegacyTypeKey) != "" {
|
||||
usableLegacyTypeKey = strings.TrimSpace(routeMeta.LegacyTypeKey)
|
||||
}
|
||||
|
||||
} else {
|
||||
_, err := s.FifoSvc.Consume(ctx, common.StockConsumeRequest{
|
||||
UsableKey: fifo.UsableKeyAdjustmentOut,
|
||||
UsableID: adjustmentStock.Id,
|
||||
ProductWarehouseID: uint(productWarehouse.Id),
|
||||
Quantity: req.Quantity,
|
||||
AllowPending: false,
|
||||
Tx: tx,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to consume stock via FIFO: %v", err))
|
||||
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")
|
||||
}
|
||||
|
||||
stockLogs, err := stockLogRepoTX.GetByProductWarehouse(ctx, productWarehouse.Id, 1)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get stock logs: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||
}
|
||||
|
||||
currentStock := 0.0
|
||||
if len(stockLogs) > 0 {
|
||||
currentStock = stockLogs[0].Stock
|
||||
}
|
||||
|
||||
newLog := &entity.StockLog{
|
||||
LoggableType: string(utils.StockLogTypeAdjustment),
|
||||
LoggableId: adjustmentStock.Id,
|
||||
Notes: note,
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
CreatedBy: actorID,
|
||||
Increase: increaseQty,
|
||||
Decrease: decreaseQty,
|
||||
Stock: currentStock + increaseQty - decreaseQty,
|
||||
}
|
||||
|
||||
if err := stockLogRepoTX.CreateOne(ctx, newLog, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
createdAdjustmentStockId = adjustmentStock.Id
|
||||
@@ -261,6 +338,91 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return s.GetOne(c, createdAdjustmentStockId)
|
||||
}
|
||||
|
||||
func (s *adjustmentService) resolveWarehouseID(ctx context.Context, req *validation.Create) (uint, error) {
|
||||
if req == nil {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Invalid request")
|
||||
}
|
||||
|
||||
if req.WarehouseID > 0 {
|
||||
return req.WarehouseID, nil
|
||||
}
|
||||
|
||||
if req.ProjectFlockKandangID != nil && *req.ProjectFlockKandangID > 0 {
|
||||
kandangID, err := s.AdjustmentStockRepository.FindKandangIDByProjectFlockKandangID(ctx, *req.ProjectFlockKandangID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id tidak valid atau tidak aktif")
|
||||
}
|
||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate project_flock_kandang_id context")
|
||||
}
|
||||
|
||||
warehouse, err := s.WarehouseRepo.GetLatestByKandangID(ctx, kandangID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Warehouse untuk project_flock_kandang_id %d tidak ditemukan", *req.ProjectFlockKandangID))
|
||||
}
|
||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve warehouse by project_flock_kandang_id")
|
||||
}
|
||||
return warehouse.Id, nil
|
||||
}
|
||||
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "warehouse_id atau project_flock_kandang_id wajib diisi")
|
||||
}
|
||||
|
||||
func (s *adjustmentService) resolveRouteByFunctionCode(
|
||||
ctx context.Context,
|
||||
productID uint,
|
||||
functionCode string,
|
||||
) (*adjustmentStockRepo.AdjustmentRouteResolution, error) {
|
||||
rows, err := s.AdjustmentStockRepository.FindRoutesByFunctionCode(ctx, productID, functionCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype tidak kompatibel dengan konfigurasi FIFO v2 produk")
|
||||
}
|
||||
|
||||
selected := rows[0]
|
||||
for _, row := range rows {
|
||||
if row.Lane != selected.Lane {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype ambigu: lane FIFO v2 lebih dari satu")
|
||||
}
|
||||
}
|
||||
|
||||
selected.FunctionCode = functionCode
|
||||
switch selected.Lane {
|
||||
case adjustmentLaneStockable, adjustmentLaneUsable:
|
||||
return &selected, nil
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype memiliki lane FIFO v2 yang tidak didukung")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *adjustmentService) resolveOverconsumePolicy(
|
||||
ctx context.Context,
|
||||
route *adjustmentStockRepo.AdjustmentRouteResolution,
|
||||
) (bool, error) {
|
||||
if route == nil {
|
||||
return false, fmt.Errorf("route is required")
|
||||
}
|
||||
|
||||
defaultValue := route.AllowPendingDefault
|
||||
selected, err := s.AdjustmentStockRepository.FindOverconsumeRule(
|
||||
ctx,
|
||||
route.Lane,
|
||||
route.FlagGroupCode,
|
||||
route.FunctionCode,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if selected == nil {
|
||||
return defaultValue, nil
|
||||
}
|
||||
|
||||
return *selected, nil
|
||||
}
|
||||
|
||||
func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context, warehouseID uint) (uint, error) {
|
||||
warehouse, err := s.WarehouseRepo.GetByID(ctx, warehouseID, nil)
|
||||
if err != nil {
|
||||
@@ -291,6 +453,12 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
||||
if err := s.Validate.Struct(query); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if query.Page <= 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.Limit <= 0 {
|
||||
query.Limit = 10
|
||||
}
|
||||
offset := (query.Page - 1) * query.Limit
|
||||
|
||||
var isProductsExist bool
|
||||
@@ -313,15 +481,6 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
||||
}
|
||||
|
||||
var adjustmentStocks []entity.AdjustmentStock
|
||||
var total int64
|
||||
|
||||
q := s.AdjustmentStockRepository.DB().WithContext(c.Context()).Model(&entity.AdjustmentStock{}).
|
||||
Preload("ProductWarehouse").
|
||||
Preload("ProductWarehouse.Product").
|
||||
Preload("ProductWarehouse.Warehouse").
|
||||
Preload("StockLog.CreatedUser")
|
||||
|
||||
scope, scopeErr := m.ResolveLocationScope(c, s.AdjustmentStockRepository.DB())
|
||||
if scopeErr != nil {
|
||||
return nil, 0, scopeErr
|
||||
@@ -330,42 +489,32 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
||||
if len(scope.IDs) == 0 {
|
||||
return []*entity.AdjustmentStock{}, 0, nil
|
||||
}
|
||||
q = q.Joins("JOIN product_warehouses pw_scope ON pw_scope.id = adjustment_stocks.product_warehouse_id").
|
||||
Joins("JOIN warehouses w_scope ON w_scope.id = pw_scope.warehouse_id")
|
||||
q = m.ApplyScopeFilter(q, scope, "w_scope.location_id")
|
||||
}
|
||||
|
||||
if query.ProductID > 0 {
|
||||
q = q.Joins("JOIN product_warehouses ON product_warehouses.id = adjustment_stocks.product_warehouse_id").
|
||||
Where("product_warehouses.product_id = ?", query.ProductID)
|
||||
functionCode := strings.ToUpper(strings.TrimSpace(query.TransactionSubtype))
|
||||
if functionCode == "" {
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(query.FunctionCode))
|
||||
}
|
||||
transactionType := strings.ToUpper(strings.TrimSpace(query.TransactionType))
|
||||
|
||||
if query.WarehouseID > 0 {
|
||||
q = q.Joins("JOIN product_warehouses ON product_warehouses.id = adjustment_stocks.product_warehouse_id").
|
||||
Where("product_warehouses.warehouse_id = ?", query.WarehouseID)
|
||||
}
|
||||
|
||||
if query.TransactionType != "" {
|
||||
q = q.Joins("JOIN stock_logs ON stock_logs.loggable_type = ? AND stock_logs.loggable_id = adjustment_stocks.id", "ADJUSTMENT").
|
||||
Where("stock_logs.transaction_type = ?", strings.ToUpper(query.TransactionType))
|
||||
}
|
||||
|
||||
if err = q.Count(&total).Error; err != nil {
|
||||
s.Log.Errorf("Failed to get adjustments: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to get adjustment history")
|
||||
}
|
||||
|
||||
err = q.Offset(offset).Limit(query.Limit).Order("created_at DESC").Find(&adjustmentStocks).Error
|
||||
|
||||
adjustmentStocks, total, err := s.AdjustmentStockRepository.FindHistory(
|
||||
c.Context(),
|
||||
adjustmentStockRepo.AdjustmentHistoryFilter{
|
||||
ProductID: query.ProductID,
|
||||
WarehouseID: query.WarehouseID,
|
||||
TransactionType: transactionType,
|
||||
FunctionCode: functionCode,
|
||||
ScopeRestrict: scope.Restrict,
|
||||
ScopeIDs: scope.IDs,
|
||||
Offset: offset,
|
||||
Limit: query.Limit,
|
||||
},
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get adjustments: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to get adjustment history")
|
||||
}
|
||||
|
||||
result := make([]*entity.AdjustmentStock, len(adjustmentStocks))
|
||||
for i := range adjustmentStocks {
|
||||
result[i] = &adjustmentStocks[i]
|
||||
}
|
||||
|
||||
return result, total, nil
|
||||
return adjustmentStocks, total, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user