mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 15:55:44 +00:00
add restrict create/edit/delete depletion
This commit is contained in:
+20
-21
@@ -13,7 +13,6 @@ import (
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/validations"
|
||||
kandangrepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -259,28 +258,28 @@ func (s productWarehouseService) applyTransferAvailableQty(c *fiber.Ctx, params
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
type usageRow struct {
|
||||
type populationRemainingRow struct {
|
||||
ProductWarehouseID uint `gorm:"column:product_warehouse_id"`
|
||||
UsedQty float64 `gorm:"column:used_qty"`
|
||||
}
|
||||
usageRows := make([]usageRow, 0)
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("stock_allocations").
|
||||
Select("product_warehouse_id, COALESCE(SUM(qty), 0) AS used_qty").
|
||||
Where("product_warehouse_id IN ?", ayamPWIDs).
|
||||
Where("usable_type = ?", fifo.UsableKeyProjectChickin.String()).
|
||||
Where("status = ?", entity.StockAllocationStatusActive).
|
||||
Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||
Where("deleted_at IS NULL").
|
||||
Group("product_warehouse_id").
|
||||
Scan(&usageRows).Error; err != nil {
|
||||
s.Log.Errorf("Failed to calculate available transfer stock after chickin consumption: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal menghitung stok tersedia untuk transfer")
|
||||
RemainingQty float64 `gorm:"column:remaining_qty"`
|
||||
}
|
||||
|
||||
usageMap := make(map[uint]float64, len(usageRows))
|
||||
for _, row := range usageRows {
|
||||
usageMap[row.ProductWarehouseID] = row.UsedQty
|
||||
var populationRows []populationRemainingRow
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("project_flock_populations pfp").
|
||||
Select("pfp.product_warehouse_id, COALESCE(SUM(GREATEST(pfp.total_qty - pfp.total_used_qty, 0)), 0) AS remaining_qty").
|
||||
Joins("JOIN project_chickins pc ON pc.id = pfp.project_chickin_id").
|
||||
Where("pfp.product_warehouse_id IN ?", ayamPWIDs).
|
||||
Where("pfp.deleted_at IS NULL").
|
||||
Where("pc.deleted_at IS NULL").
|
||||
Group("pfp.product_warehouse_id").
|
||||
Scan(&populationRows).Error; err != nil {
|
||||
s.Log.Errorf("Failed to resolve chickin population remaining for transfer stock filter: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve transfer stock availability")
|
||||
}
|
||||
|
||||
populationRemainingByPW := make(map[uint]float64, len(populationRows))
|
||||
for _, row := range populationRows {
|
||||
populationRemainingByPW[row.ProductWarehouseID] = row.RemainingQty
|
||||
}
|
||||
|
||||
filtered := make([]entity.ProductWarehouse, 0, len(rows))
|
||||
@@ -291,7 +290,7 @@ func (s productWarehouseService) applyTransferAvailableQty(c *fiber.Ctx, params
|
||||
continue
|
||||
}
|
||||
|
||||
available := row.Quantity - usageMap[row.Id]
|
||||
available := row.Quantity - populationRemainingByPW[row.Id]
|
||||
if available < 0 {
|
||||
available = 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user