46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.php';
|
|
|
|
// Build filter + params based on operation type.
|
|
// Filter warehouses by occupancy:
|
|
// 'to' = destination → only warehouses with at least one empty rack
|
|
// 'from' = source → only warehouses that hold the specific product SKU
|
|
// other = all warehouses (for read-only listings, dashboards, etc.)
|
|
$product_sku_filter = "";
|
|
$params = [":company_id" => $company_id];
|
|
|
|
if ($data["type"] == "to") {
|
|
$product_sku_filter = "AND r.product_sku IS NULL";
|
|
|
|
} elseif ($data["type"] == "from") {
|
|
$product_sku_filter = "AND r.product_sku = :product_sku";
|
|
$params[":product_sku"] = $data["product_sku"];
|
|
}
|
|
|
|
if(!empty($data["id"])){
|
|
$product_sku_filter = "";
|
|
unset($params[":product_sku"]);
|
|
}
|
|
|
|
// Only list warehouses that have racks matching the occupancy filter.
|
|
// EXISTS is efficient here — it stops searching as soon as one match is found.
|
|
$sql = "SELECT w.id, w.warehouse_name
|
|
FROM md_warehouse w
|
|
WHERE w.company_id = :company_id
|
|
AND EXISTS (
|
|
SELECT 1 FROM md_rack r
|
|
WHERE r.company_id = w.company_id
|
|
AND r.warehouse = w.id
|
|
$product_sku_filter
|
|
)
|
|
ORDER BY w.warehouse_name";
|
|
|
|
$sth = $pdo2->prepare($sql);
|
|
$sth->execute($params);
|
|
db_check($sth, $answer);
|
|
|
|
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
$answer["success"] = 1;
|
|
exit(json_encode($answer));
|
|
?>
|