mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
[FEAT/BE] change current stock with pending in product warehouse
This commit is contained in:
@@ -175,5 +175,47 @@ func (s productStockService) GetOne(c *fiber.Ctx, id uint) (*entity.Product, err
|
||||
s.Log.Errorf("Failed get product by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(product.ProductWarehouses) > 0 {
|
||||
ids := make([]uint, 0, len(product.ProductWarehouses))
|
||||
for _, pw := range product.ProductWarehouses {
|
||||
if pw.Id != 0 {
|
||||
ids = append(ids, pw.Id)
|
||||
}
|
||||
}
|
||||
|
||||
if len(ids) > 0 {
|
||||
type pendingUsageRow struct {
|
||||
ProductWarehouseId uint
|
||||
PendingQty float64
|
||||
}
|
||||
|
||||
var rows []pendingUsageRow
|
||||
if err := s.ProductRepository.DB().WithContext(c.Context()).
|
||||
Table("recording_stocks").
|
||||
Select("product_warehouse_id, COALESCE(SUM(pending_qty), 0) AS pending_qty").
|
||||
Where("pending_qty > 0").
|
||||
Where("product_warehouse_id IN ?", ids).
|
||||
Group("product_warehouse_id").
|
||||
Scan(&rows).Error; err != nil {
|
||||
s.Log.Errorf("Failed to load pending usage for product warehouses: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(rows) > 0 {
|
||||
pendingMap := make(map[uint]float64, len(rows))
|
||||
for _, row := range rows {
|
||||
pendingMap[row.ProductWarehouseId] = row.PendingQty
|
||||
}
|
||||
|
||||
for i := range product.ProductWarehouses {
|
||||
pw := &product.ProductWarehouses[i]
|
||||
if pending, ok := pendingMap[pw.Id]; ok && pending != 0 {
|
||||
pw.Quantity -= pending
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return product, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user