mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
fix: first push need support testing, and implemented fifo v2 to all modules
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/validations"
|
||||
rShared "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"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -36,7 +35,7 @@ type deliveryOrdersService struct {
|
||||
MarketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
StockLogRepo rShared.StockLogRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
FifoSvc commonSvc.FifoService
|
||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||
}
|
||||
|
||||
func NewDeliveryOrdersService(
|
||||
@@ -45,7 +44,7 @@ func NewDeliveryOrdersService(
|
||||
marketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository,
|
||||
stockLogRepo rShared.StockLogRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
fifoSvc commonSvc.FifoService,
|
||||
fifoStockV2Svc commonSvc.FifoStockV2Service,
|
||||
validate *validator.Validate,
|
||||
) DeliveryOrdersService {
|
||||
return &deliveryOrdersService{
|
||||
@@ -55,7 +54,7 @@ func NewDeliveryOrdersService(
|
||||
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
||||
StockLogRepo: stockLogRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
FifoSvc: fifoSvc,
|
||||
FifoStockV2Svc: fifoStockV2Svc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -549,33 +548,42 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Delivery product not found")
|
||||
}
|
||||
|
||||
result, err := s.FifoSvc.Consume(ctx, commonSvc.StockConsumeRequest{
|
||||
UsableKey: fifo.UsableKeyMarketingDelivery,
|
||||
UsableID: deliveryProduct.Id,
|
||||
ProductWarehouseID: marketingProduct.ProductWarehouseId,
|
||||
Quantity: requestedQty,
|
||||
AllowPending: false,
|
||||
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)
|
||||
previousUsage := deliveryProduct.UsageQty
|
||||
deliveryProduct.UsageQty = requestedQty
|
||||
deliveryProduct.PendingQty = 0
|
||||
|
||||
if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, result.UsageQuantity, 0); err != nil {
|
||||
if err := deliveryProductRepo.UpdateOne(ctx, deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
|
||||
if actorID > 0 && result.UsageQuantity > 0 {
|
||||
if err := reflowMarketingScope(
|
||||
ctx,
|
||||
s.FifoStockV2Svc,
|
||||
tx,
|
||||
marketingProduct.ProductWarehouseId,
|
||||
resolveMarketingAsOf(deliveryProduct.DeliveryDate, deliveryProduct.CreatedAt),
|
||||
); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock for product warehouse %d: %v", marketingProduct.ProductWarehouseId, err))
|
||||
}
|
||||
|
||||
refreshed, err := deliveryProductRepo.GetByID(ctx, deliveryProduct.Id, nil)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to refresh delivery product")
|
||||
}
|
||||
deliveryProduct.UsageQty = refreshed.UsageQty
|
||||
deliveryProduct.PendingQty = refreshed.PendingQty
|
||||
deliveryProduct.CreatedAt = refreshed.CreatedAt
|
||||
|
||||
allocatedDelta := deliveryProduct.UsageQty - previousUsage
|
||||
if actorID > 0 && allocatedDelta > 0 {
|
||||
decreaseLog := &entity.StockLog{
|
||||
Decrease: result.UsageQuantity,
|
||||
Decrease: allocatedDelta,
|
||||
LoggableType: string(utils.StockLogTypeMarketing),
|
||||
LoggableId: deliveryProduct.Id,
|
||||
ProductWarehouseId: marketingProduct.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: fmt.Sprintf("FIFO consume (%.2f)", result.UsageQuantity),
|
||||
Notes: fmt.Sprintf("FIFO v2 reflow consume (%.2f)", allocatedDelta),
|
||||
}
|
||||
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, marketingProduct.ProductWarehouseId, 1)
|
||||
@@ -604,35 +612,45 @@ func (s deliveryOrdersService) releaseDeliveryStock(ctx context.Context, tx *gor
|
||||
}
|
||||
|
||||
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
||||
currentUsage, err := deliveryProductRepo.GetUsageQty(ctx, deliveryProduct.Id)
|
||||
if err != nil {
|
||||
currentUsage = 0
|
||||
}
|
||||
|
||||
if currentUsage == 0 {
|
||||
currentUsage := deliveryProduct.UsageQty
|
||||
currentPending := deliveryProduct.PendingQty
|
||||
if currentUsage <= 0 && currentPending <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := s.FifoSvc.ReleaseUsage(ctx, commonSvc.StockReleaseRequest{
|
||||
UsableKey: fifo.UsableKeyMarketingDelivery,
|
||||
UsableID: deliveryProduct.Id,
|
||||
Tx: tx,
|
||||
}); err != nil {
|
||||
return err
|
||||
deliveryProduct.UsageQty = 0
|
||||
deliveryProduct.PendingQty = 0
|
||||
if err := deliveryProductRepo.UpdateOne(ctx, deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to reset delivery product")
|
||||
}
|
||||
|
||||
if err := deliveryProductRepo.ResetFifoFields(ctx, deliveryProduct.Id); err != nil {
|
||||
return err
|
||||
if err := reflowMarketingScope(
|
||||
ctx,
|
||||
s.FifoStockV2Svc,
|
||||
tx,
|
||||
marketingProduct.ProductWarehouseId,
|
||||
resolveMarketingAsOf(deliveryProduct.DeliveryDate, deliveryProduct.CreatedAt),
|
||||
); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to reflow stock for product warehouse %d: %v", marketingProduct.ProductWarehouseId, err))
|
||||
}
|
||||
|
||||
if actorID > 0 && currentUsage > 0 {
|
||||
refreshed, err := deliveryProductRepo.GetByID(ctx, deliveryProduct.Id, nil)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to refresh delivery product")
|
||||
}
|
||||
deliveryProduct.UsageQty = refreshed.UsageQty
|
||||
deliveryProduct.PendingQty = refreshed.PendingQty
|
||||
deliveryProduct.CreatedAt = refreshed.CreatedAt
|
||||
|
||||
releasedUsage := currentUsage - deliveryProduct.UsageQty
|
||||
if actorID > 0 && releasedUsage > 0 {
|
||||
increaseLog := &entity.StockLog{
|
||||
Increase: currentUsage,
|
||||
Increase: releasedUsage,
|
||||
LoggableType: string(utils.StockLogTypeMarketing),
|
||||
LoggableId: deliveryProduct.Id,
|
||||
ProductWarehouseId: marketingProduct.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: fmt.Sprintf("Release delivery stock (%.2f)", currentUsage),
|
||||
Notes: fmt.Sprintf("FIFO v2 reflow release (%.2f)", releasedUsage),
|
||||
}
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, marketingProduct.ProductWarehouseId, 1)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user