mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] fixing stock log delete recording and purchase
This commit is contained in:
@@ -711,6 +711,11 @@ func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]ent
|
||||
|
||||
func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
ctx := c.Context()
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
note := fmt.Sprintf("Recording-Delete#%d", id)
|
||||
|
||||
return s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
oldDepletions, err := s.Repository.ListDepletions(tx, id)
|
||||
@@ -719,7 +724,7 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
return err
|
||||
}
|
||||
if s.FifoSvc != nil {
|
||||
if err := s.releaseRecordingDepletions(ctx, tx, oldDepletions, "", 0); err != nil {
|
||||
if err := s.releaseRecordingDepletions(ctx, tx, oldDepletions, note, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -741,7 +746,7 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.releaseRecordingStocks(ctx, tx, oldStocks, "", 0); err != nil {
|
||||
if err := s.releaseRecordingStocks(ctx, tx, oldStocks, note, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -749,6 +754,10 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.logRecordingEggRollback(ctx, tx, oldEggs, note, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Repository.WithTx(tx).DeleteOne(ctx, id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Recording not found")
|
||||
@@ -761,6 +770,40 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *recordingService) logRecordingEggRollback(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
eggs []entity.RecordingEgg,
|
||||
note string,
|
||||
actorID uint,
|
||||
) error {
|
||||
if len(eggs) == 0 || s.StockLogRepo == nil {
|
||||
return nil
|
||||
}
|
||||
if strings.TrimSpace(note) == "" || actorID == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, egg := range eggs {
|
||||
if egg.ProductWarehouseId == 0 || egg.Qty <= 0 {
|
||||
continue
|
||||
}
|
||||
log := &entity.StockLog{
|
||||
ProductWarehouseId: egg.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Decrease: float64(egg.Qty),
|
||||
LoggableType: string(utils.StockLogTypeRecording),
|
||||
LoggableId: egg.RecordingId,
|
||||
Notes: note,
|
||||
}
|
||||
if err := s.StockLogRepo.WithTx(tx).CreateOne(ctx, log, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// === Persistence Helpers ===
|
||||
|
||||
func (s *recordingService) ensureProductWarehousesExist(c *fiber.Ctx, stocks []validation.Stock, depletions []validation.Depletion, eggs []validation.Egg) error {
|
||||
|
||||
Reference in New Issue
Block a user