mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] adjustment recording and purchase stock log
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
rSupplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
projectFlockKandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
rPurchase "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -830,9 +831,16 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
receivingAction = entity.ApprovalActionUpdated
|
||||
}
|
||||
}
|
||||
noteSuffix := "receive"
|
||||
if receivingAction == entity.ApprovalActionUpdated {
|
||||
noteSuffix = "edit-receive"
|
||||
}
|
||||
receiveNote := fmt.Sprintf("%s#%s", strings.TrimSpace(*purchase.PoNumber), noteSuffix)
|
||||
|
||||
transactionErr := s.PurchaseRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
repoTx := rPurchase.NewPurchaseRepository(tx)
|
||||
pwRepoTx := rProductWarehouse.NewProductWarehouseRepository(tx)
|
||||
stockLogRepoTx := rStockLogs.NewStockLogRepository(tx)
|
||||
|
||||
deltas := make(map[uint]float64)
|
||||
affected := make(map[uint]struct{})
|
||||
@@ -849,6 +857,11 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
pwID uint
|
||||
qty float64
|
||||
}, 0, len(prepared))
|
||||
logEntries := make([]struct {
|
||||
itemID uint
|
||||
pwID uint
|
||||
delta float64
|
||||
}, 0, len(prepared))
|
||||
|
||||
for _, prep := range prepared {
|
||||
item := prep.item
|
||||
@@ -869,6 +882,13 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
newPWID = &pwID
|
||||
|
||||
deltaQty := prep.receivedQty - item.TotalQty
|
||||
if newPWID != nil && deltaQty != 0 {
|
||||
logEntries = append(logEntries, struct {
|
||||
itemID uint
|
||||
pwID uint
|
||||
delta float64
|
||||
}{itemID: item.Id, pwID: *newPWID, delta: deltaQty})
|
||||
}
|
||||
switch {
|
||||
case deltaQty > 0 && newPWID != nil:
|
||||
if s.FifoSvc != nil {
|
||||
@@ -993,6 +1013,33 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
}
|
||||
}
|
||||
|
||||
if len(logEntries) > 0 {
|
||||
logs := make([]*entity.StockLog, 0, len(logEntries))
|
||||
for _, entry := range logEntries {
|
||||
if entry.pwID == 0 || entry.delta == 0 {
|
||||
continue
|
||||
}
|
||||
log := &entity.StockLog{
|
||||
ProductWarehouseId: entry.pwID,
|
||||
CreatedBy: actorID,
|
||||
LoggableType: string(utils.StockLogTypePurchase),
|
||||
LoggableId: purchase.Id,
|
||||
Notes: receiveNote,
|
||||
}
|
||||
if entry.delta > 0 {
|
||||
log.Increase = entry.delta
|
||||
} else {
|
||||
log.Decrease = -entry.delta
|
||||
}
|
||||
logs = append(logs, log)
|
||||
}
|
||||
if len(logs) > 0 {
|
||||
if err := stockLogRepoTx.CreateMany(c.Context(), logs, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(affected) > 0 {
|
||||
if err := pwRepoTx.CleanupEmpty(c.Context(), affected); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user