« Back to History
get_karigars_by_machine.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // File: /erp/ajax/get_karigars_by_machine.php // Returns assigned karigars for a given machine (secure + company-scoped). require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('loom_downtime_entry'); // restrict to authorized users $pdo = $ctx['pdo'] ?? null; $cid = (int)($ctx['company_id'] ?? 0); if (!($pdo instanceof PDO)) { http_response_code(500); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['ok'=>false,'msg'=>'DB missing']); exit; } header('Content-Type: application/json; charset=utf-8'); $machine_raw = trim((string)($_GET['machine_no'] ?? '')); if ($machine_raw === '') { echo json_encode(['ok'=>false,'msg'=>'Missing machine_no']); exit; } // Helper: try to extract a numeric machine id $mn = null; if (preg_match('/\d+/', $machine_raw, $m)) { $mn = (int)$m[0]; } // First try numeric range match if we have a number try { if ($mn !== null && $mn > 0) { $sql = "SELECT id, karigar_name, entry_name, machine_from, machine_to FROM loom_karigar_master WHERE company_id = :cid AND :mn BETWEEN CAST(IFNULL(machine_from,'0') AS UNSIGNED) AND CAST(IFNULL(machine_to,'999999') AS UNSIGNED) AND is_active = 1 ORDER BY CAST(machine_from AS UNSIGNED), karigar_name LIMIT 500"; $st = $pdo->prepare($sql); $st->execute([':cid'=>$cid, ':mn'=>$mn]); } else { // fallback: substring match on entry_name OR karigar_name $sql = "SELECT id, karigar_name, entry_name, machine_from, machine_to FROM loom_karigar_master WHERE company_id = :cid AND is_active = 1 AND (entry_name LIKE :q OR karigar_name LIKE :q) ORDER BY karigar_name LIMIT 500"; $st = $pdo->prepare($sql); $st->execute([':cid'=>$cid, ':q'=>"%{$machine_raw}%"]); } $rows = $st->fetchAll(PDO::FETCH_ASSOC); $out = []; foreach($rows as $r){ $out[] = [ 'id' => (int)($r['id'] ?? 0), 'name' => trim((string)($r['karigar_name'] ?? $r['entry_name'] ?? 'K#'.$r['id'])), 'from' => $r['machine_from'] ?? null, 'to' => $r['machine_to'] ?? null ]; } echo json_encode(['ok'=>true,'data'=>$out]); exit; } catch(Throwable $e){ http_response_code(500); echo json_encode(['ok'=>false,'msg'=>'Query failed','detail'=>$e->getMessage()]); exit; }