mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
FIX[BE] :remove unused product warehouse repository import and streamline stock consumption logic in consumeDeliveryStock method
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
productWarehouseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/dto"
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/validations"
|
||||
@@ -502,69 +501,24 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
||||
Tx: tx,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock for product warehouse %d: %v", marketingProduct.ProductWarehouseId, err))
|
||||
}
|
||||
|
||||
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
||||
|
||||
totalConsumed := 0.0
|
||||
var fifoConsumed float64
|
||||
var directConsumed float64
|
||||
|
||||
if result != nil && result.UsageQuantity > 0 {
|
||||
fifoConsumed = result.UsageQuantity
|
||||
totalConsumed = result.UsageQuantity
|
||||
}
|
||||
|
||||
if err != nil || (totalConsumed < requestedQty) {
|
||||
remainder := requestedQty - totalConsumed
|
||||
|
||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||
pw, err2 := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||
if err2 != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check product warehouse stock")
|
||||
}
|
||||
|
||||
if pw == nil || pw.Quantity < remainder {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock. FIFO: %.2f, Direct Available: %.2f, Total Needed: %.2f", func() float64 {
|
||||
if pw != nil {
|
||||
return pw.Quantity
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}(), remainder, requestedQty))
|
||||
}
|
||||
|
||||
if err := pwRepo.AdjustQuantities(ctx, map[uint]float64{
|
||||
marketingProduct.ProductWarehouseId: -remainder,
|
||||
}, func(db *gorm.DB) *gorm.DB {
|
||||
return tx
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to adjust product warehouse quantity")
|
||||
}
|
||||
|
||||
directConsumed = remainder
|
||||
totalConsumed += remainder
|
||||
}
|
||||
|
||||
if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, totalConsumed, 0); err != nil {
|
||||
if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, result.UsageQuantity, 0); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
|
||||
if actorID > 0 && totalConsumed > 0 {
|
||||
notes := ""
|
||||
if fifoConsumed > 0 && directConsumed > 0 {
|
||||
notes = fmt.Sprintf("Partial FIFO (%.2f) + Direct (%.2f)", fifoConsumed, directConsumed)
|
||||
} else if fifoConsumed > 0 {
|
||||
notes = fmt.Sprintf("FIFO stock only (%.2f)", fifoConsumed)
|
||||
} else if directConsumed > 0 {
|
||||
notes = fmt.Sprintf("Direct stock only (%.2f)", directConsumed)
|
||||
}
|
||||
|
||||
if actorID > 0 && result.UsageQuantity > 0 {
|
||||
decreaseLog := &entity.StockLog{
|
||||
Decrease: totalConsumed,
|
||||
Decrease: result.UsageQuantity,
|
||||
LoggableType: string(utils.StockLogTypeMarketing),
|
||||
LoggableId: deliveryProduct.Id,
|
||||
ProductWarehouseId: marketingProduct.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: notes,
|
||||
Notes: fmt.Sprintf("FIFO consume (%.2f)", result.UsageQuantity),
|
||||
}
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, marketingProduct.ProductWarehouseId, 1)
|
||||
if err != nil {
|
||||
@@ -572,8 +526,7 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
||||
}
|
||||
if len(stockLogs) > 0 {
|
||||
latestStockLog := stockLogs[0]
|
||||
decreaseLog.Stock = latestStockLog.Stock
|
||||
decreaseLog.Stock -= decreaseLog.Decrease
|
||||
decreaseLog.Stock = latestStockLog.Stock - decreaseLog.Decrease
|
||||
} else {
|
||||
decreaseLog.Stock -= decreaseLog.Decrease
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user