diff --git a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go index a7fe452b..e70ff005 100644 --- a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go +++ b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go @@ -86,24 +86,14 @@ func (r *ProductWarehouseRepositoryImpl) ProductWarehouseExistByProductAndWareho func (r *ProductWarehouseRepositoryImpl) GetProductWarehouseByProductAndWarehouseID(ctx context.Context, productId, warehouseId uint) (*entity.ProductWarehouse, error) { var productWarehouse entity.ProductWarehouse + // Cari product warehouse dengan project_flock_kandang yang masih aktif (belum closed) err := r.DB().WithContext(ctx). - Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NOT NULL", productId, warehouseId). + Joins("LEFT JOIN project_flock_kandangs ON project_flock_kandangs.id = product_warehouses.project_flock_kandang_id"). + Where("product_id = ? AND warehouse_id = ? AND (project_flock_kandang_id IS NULL OR project_flock_kandangs.closed_at IS NULL)", productId, warehouseId). Order("id DESC"). Preload("ProjectFlockKandang"). First(&productWarehouse).Error - if err == nil { - - if productWarehouse.ProjectFlockKandang.ClosedAt == nil { - return &productWarehouse, nil - } - - } - - err = r.DB().WithContext(ctx). - Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NULL", productId, warehouseId). - First(&productWarehouse).Error - if err != nil { return nil, err }