« Back to History
machine_defaults.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php /** * Returns defaults + dropdown for a loom (machine) inside a khata. * Output: { ok, khata:{id,from,to}, all_karigars:[{id,name}], fixed_karigars:[id,id] } * Notes: * - Uses machine_id (loom number) only (no machine_no column needed) * - Filters karigars whose machine_from..machine_to contains the given machine * - Always outputs JSON (fatal-safe) */ /* ---------- SECTION 0: headers + fatal catcher ---------- */ header('Content-Type: application/json; charset=utf-8'); error_reporting(E_ALL); ini_set('display_errors', '0'); function jexit($arr, $code=200){ http_response_code($code); echo json_encode($arr, JSON_UNESCAPED_UNICODE); exit; } register_shutdown_function(function () { $e = error_get_last(); if ($e && in_array($e['type'], [E_ERROR,E_PARSE,E_CORE_ERROR,E_COMPILE_ERROR])) { jexit(['ok'=>false,'msg'=>'Fatal','detail'=>$e['message'],'file'=>basename($e['file']??''),'line'=>$e['line']??null],500); } }); /* ---------- SECTION 1: auth ---------- */ require __DIR__ . '/../modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; try{ /* ---------- SECTION 2: DB ---------- */ if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo']) require __DIR__ . '/../core/db.php'; if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo']) jexit(['ok'=>false,'msg'=>'DB connect fail'],500); /** @var PDO $pdo */ $pdo = $GLOBALS['pdo']; $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* ---------- SECTION 3: inputs ---------- */ $khata_code = isset($_GET['khata']) ? trim($_GET['khata']) : ''; $machine_no = isset($_GET['machine']) ? (int)$_GET['machine'] : 0; // loom number if ($khata_code==='' || $machine_no<=0) jexit(['ok'=>false,'msg'=>'Missing params: khata, machine']); /* ---------- SECTION 4: khata row ---------- */ $ks = $pdo->prepare("SELECT id, machine_from, machine_to FROM khatas WHERE company_id=? AND code=? AND is_active=1 LIMIT 1"); $ks->execute([$company_id,$khata_code]); $kh = $ks->fetch(PDO::FETCH_ASSOC); if (!$kh) jexit(['ok'=>false,'msg'=>"Khata not found: {$khata_code}"]); $khata_id = (int)$kh['id']; /* ---------- SECTION 5: machine-range filtered karigar list ---------- machine_from & machine_to may be stored as text; cast to UNSIGNED. We keep NULL-safe check so rows without range never match. ------------------------------------------------------------------- */ $sql = "SELECT id, karigar_name, CAST(machine_from AS UNSIGNED) AS mf, CAST(machine_to AS UNSIGNED) AS mt FROM loom_karigar_master WHERE company_id = :c AND khata_id = :k AND is_active = 1 AND :m BETWEEN CAST(IFNULL(machine_from, :m0) AS UNSIGNED) AND CAST(IFNULL(machine_to, :m1) AS UNSIGNED)"; $st = $pdo->prepare($sql); $st->execute([':c'=>$company_id, ':k'=>$khata_id, ':m'=>$machine_no, ':m0'=>$machine_no, ':m1'=>$machine_no]); $all = []; while ($r = $st->fetch(PDO::FETCH_ASSOC)) { $label = $r['karigar_name']; // Append "from-to" if present, e.g., "ALOK 33-36" if ($r['mf'] && $r['mt']) $label .= ' '.$r['mf'].'-'.$r['mt']; $all[] = ['id'=>(int)$r['id'], 'name'=>$label]; } // If nothing matched (edge case), don't leave the UI empty: fall back to ZERO results (no crash) // (You can remove the fallback below if you prefer an empty list strictly) // if (empty($all)) { jexit(['ok'=>true,'khata'=>$kh,'all_karigars'=>[],'fixed_karigars'=>[]]); } /* ---------- SECTION 6: fixed pair from last entry (if available) ---------- */ $fixed = []; $q = $pdo->prepare("SELECT lines_json FROM loom_production_entry WHERE company_id=? AND khata_id=? AND machine_id=? ORDER BY entry_date DESC, id DESC LIMIT 1"); $q->execute([$company_id,$khata_id,$machine_no]); if ($row = $q->fetch(PDO::FETCH_ASSOC)) { $lines = json_decode($row['lines_json'] ?? '[]', true); if (is_array($lines)) { // pick top two by meter usort($lines, fn($a,$b)=>($b['meter']??0)<=>($a['meter']??0)); foreach ($lines as $ln) { if (isset($ln['karigar_id'])) $fixed[] = (int)$ln['karigar_id']; if (count($fixed) >= 2) break; } } } // If no previous lines, default to first two from filtered list if (count($fixed) < 2) { foreach ($all as $opt) { if (!in_array($opt['id'], $fixed, true)) $fixed[] = $opt['id']; if (count($fixed) >= 2) break; } } /* ---------- SECTION 7: output ---------- */ jexit([ 'ok' => true, 'khata' => ['id'=>(int)$kh['id'], 'from'=>(int)$kh['machine_from'], 'to'=>(int)$kh['machine_to']], 'all_karigars' => $all, 'fixed_karigars' => $fixed ]); } catch (Throwable $e) { jexit(['ok'=>false,'msg'=>'Server error','detail'=>$e->getMessage()],500); }