« Back to History
fixed_karigars.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================ API: Return fixed 2 karigars for a machine if mapping exists. Expected (example) mapping table (optional): machine_karigar_fixed(company_id,machine_id, day_karigar_id, night_karigar_id) If not found, returns nulls; UI will keep dropdowns selectable. ============================================================================ */ header('Content-Type: application/json; charset=utf-8'); header('X-Frame-Options: SAMEORIGIN'); error_reporting(E_ALL); ini_set('display_errors','0'); if (session_status() !== PHP_SESSION_ACTIVE) session_start(); require __DIR__ . '/../modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/../core/db.php'; } function jexit($arr,$code=200){ http_response_code($code); echo json_encode($arr,JSON_UNESCAPED_UNICODE); exit; } $machine_id = isset($_GET['machine_id']) ? (int)$_GET['machine_id'] : 0; if($machine_id<=0){ jexit(['ok'=>false,'msg'=>'machine_id required'],400); } try{ // Check mapping table existence $exists = false; try{ $pdo->query("SELECT 1 FROM machine_karigar_fixed LIMIT 1"); $exists = true; }catch(Throwable $e){ $exists = false; } $out = ['k1'=>null,'k2'=>null]; if($exists){ $q = $pdo->prepare("SELECT day_karigar_id, night_karigar_id FROM machine_karigar_fixed WHERE company_id=? AND machine_id=? LIMIT 1"); $q->execute([$company_id,$machine_id]); if($r = $q->fetch(PDO::FETCH_ASSOC)){ $ids = []; if(!empty($r['day_karigar_id'])) $ids[] = (int)$r['day_karigar_id']; if(!empty($r['night_karigar_id'])) $ids[] = (int)$r['night_karigar_id']; if($ids){ $in = implode(',', array_fill(0,count($ids),'?')); $p = $pdo->prepare("SELECT id,name FROM employees WHERE company_id=? AND id IN($in)"); $p->execute(array_merge([$company_id], $ids)); $map = []; while($er = $p->fetch(PDO::FETCH_ASSOC)){ $map[(int)$er['id']] = $er['name']; } if(!empty($r['day_karigar_id']) && isset($map[(int)$r['day_karigar_id']])){ $out['k1'] = ['id'=>(int)$r['day_karigar_id'],'name'=>$map[(int)$r['day_karigar_id']]; } if(!empty($r['night_karigar_id']) && isset($map[(int)$r['night_karigar_id']])){ $out['k2'] = ['id'=>(int)$r['night_karigar_id'],'name'=>$map[(int)$r['night_karigar_id']]; } } } } jexit(['ok'=>true] + $out); }catch(Throwable $e){ jexit(['ok'=>false,'msg'=>'DB error'],500); }