« Back to History
pasaria_entry.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ========================================================================= File: /erp/pasaria_entry.php Purpose: Pasaria Entry (stock-only beams + manual beams) + recent list + edit mode Fixes: - Uses DB-backed checks for beam existence (company-scoped) instead of relying on a pre-built $available_beams array which could be stale or mismatched. - Preserves original behavior: Fani/Jog exceptions, edit-mode locking of beams, insertion of idempotent beam_installation_record if table exists. ========================================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); /* --- Optional debug (set true during testing) ----------------------------- */ $DEBUG = false; /* --- DB + Auth (page-local) --- */ if (!isset($pdo) || !($pdo instanceof PDO)) { require __DIR__ . '/core/db.php'; } // Keep your auth bootstrap (don't change global bootstrap here) require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)($u['company_id'] ?? 0); $user_id = (int)($u['id'] ?? 0); /* --- Legacy header expectation fix ------------------------------------- */ if (!isset($company) || !is_array($company)) { try { $stmt = $pdo->prepare("SELECT COALESCE(NULLIF(name,''), NULLIF(company_name,''), CONCAT('Company #', id)) FROM companies WHERE id=? LIMIT 1"); $stmt->execute([$company_id]); $company = ['name' => (string)($stmt->fetchColumn() ?: 'Company #'.$company_id)]; } catch (Throwable $e) { $company = ['name' => 'Company #'.$company_id]; } } /* --- Helpers ------------------------------------------------------------ */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function postv(string $k, $def=''){ return array_key_exists($k, $_POST) ? $_POST[$k] : $def; } function cols(PDO $pdo,$t){ try{ $r=$pdo->query("SHOW COLUMNS FROM `$t`")->fetchAll(PDO::FETCH_ASSOC); $m=[]; foreach($r as $c){ $m[strtolower($c['Field'])]=1; } return $m; }catch(Throwable $e){ return []; } } function idx_exists(PDO $pdo,string $t,string $idx):bool{ try{ $db=$pdo->query("SELECT DATABASE()")->fetchColumn(); $st=$pdo->prepare("SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND INDEX_NAME=? LIMIT 1"); $st->execute([$db,$t,$idx]); return (bool)$st->fetchColumn(); }catch(Throwable $e){ return false; }} /* Idempotent insertion into beam_installation_record (safe if table missing) */ function insert_beam_installation_record(PDO $pdo, int $company_id, string $source_table, int $source_id, string $beam_no, ?string $beam_type, ?string $machine_no, string $installation_date, array $details, int $created_by): bool { // If table doesn't exist, return false silently try { $pdo->query("SELECT 1 FROM beam_installation_record LIMIT 1"); } catch(Throwable $e){ return false; } try { $st = $pdo->prepare("SELECT id FROM beam_installation_record WHERE company_id=:cid AND source_table=:st AND source_id=:sid AND beam_no=:bn LIMIT 1"); $st->execute([':cid'=>$company_id, ':st'=>$source_table, ':sid'=>$source_id, ':bn'=>$beam_no]); if ($st->fetch()) return false; $ins = $pdo->prepare("INSERT INTO beam_installation_record (company_id,source_table,source_id,beam_no,beam_type,machine_no, installation_date,note,details,created_by,created_at) VALUES (:cid,:st,:sid,:bn,:bt,:mno,:dt,:note,:details,:uid,NOW())"); $ins->execute([ ':cid'=>$company_id, ':st'=>$source_table, ':sid'=>$source_id, ':bn'=>$beam_no, ':bt'=>$beam_type, ':mno'=>$machine_no, ':dt'=>$installation_date, ':note'=>"Inserted from $source_table #$source_id", ':details'=>json_encode($details, JSON_UNESCAPED_UNICODE), ':uid'=>$created_by ]); return true; } catch(Throwable $e){ return false; } } /* --- Ensure pasaria_entry (idempotent) ---------------------------------- */ $pdo->exec("CREATE TABLE IF NOT EXISTS pasaria_entry( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, entry_date DATE NOT NULL, quality_id BIGINT NULL, quality_name VARCHAR(150) NULL, machine_quality_id BIGINT NULL, machine_quality VARCHAR(150) NULL, primary_beam VARCHAR(120) NULL, primary_beam_no VARCHAR(60) NULL, primary_beam_quality_id BIGINT NULL, secondary_beam VARCHAR(120) NULL, secondary_beam_no VARCHAR(60) NULL, secondary_beam_quality_id BIGINT NULL, pasaria_type VARCHAR(80) NULL, machine_no INT NULL, employee_id BIGINT NULL, employee_name VARCHAR(150) NULL, remark VARCHAR(255) NULL, created_by BIGINT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX idx1 (company_id, entry_date) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"); /* Add missing columns (safe) */ $pc = cols($pdo,'pasaria_entry'); $add=[]; if(!isset($pc['machine_quality_id'])) $add[]="ADD COLUMN machine_quality_id BIGINT NULL AFTER quality_name"; if(!isset($pc['machine_quality'])) $add[]="ADD COLUMN machine_quality VARCHAR(150) NULL AFTER machine_quality_id"; if(!isset($pc['primary_beam_quality_id'])) $add[]="ADD COLUMN primary_beam_quality_id BIGINT NULL AFTER primary_beam_no"; if(!isset($pc['secondary_beam_quality_id'])) $add[]="ADD COLUMN secondary_beam_quality_id BIGINT NULL AFTER secondary_beam_no"; if($add){ try{ $pdo->exec("ALTER TABLE pasaria_entry ".implode(',', $add)); }catch(Throwable $e){} } /* --- beam_entry: ensure install columns + indexes ------------------------ */ $cc = cols($pdo,'beam_entry'); if (!isset($cc['installation_date'])) { try{ $pdo->exec("ALTER TABLE beam_entry ADD COLUMN installation_date DATE NULL AFTER entry_date"); }catch(Throwable $e){} } if (!isset($cc['installation_source'])) { try{ $pdo->exec("ALTER TABLE beam_entry ADD COLUMN installation_source ENUM('pissing','pasaria') NULL AFTER installation_date"); }catch(Throwable $e){} } if (!idx_exists($pdo,'beam_entry','idx_be_company_install')) { try{ $pdo->exec("CREATE INDEX idx_be_company_install ON beam_entry(company_id, installation_date)"); }catch(Throwable $e){} } if (!idx_exists($pdo,'beam_entry','idx_be_company_beamno')) { try{ $pdo->exec("CREATE INDEX idx_be_company_beamno ON beam_entry(company_id, beam_no)"); }catch(Throwable $e){} } /* --- Employees: Pasaria department -------------------------------------- */ $emps = []; $default_emp_id = null; try{ $q = $pdo->prepare("SELECT id, name FROM company_employee_master WHERE company_id=? AND (LOWER(department_name)='pasaria' OR department_id=35) ORDER BY name"); $q->execute([$company_id]); $emps = $q->fetchAll(PDO::FETCH_ASSOC); foreach($emps as $e){ if ($default_emp_id===null && strcasecmp(trim($e['name']),'Prakash')===0) { $default_emp_id = (int)$e['id']; break; } } if(!$emps){ $q = $pdo->prepare("SELECT id,name FROM employees WHERE company_id=? AND department_id=35 ORDER BY name"); $q->execute([$company_id]); $emps=$q->fetchAll(PDO::FETCH_ASSOC); } }catch(Throwable $e){} /* --- Pasaria Types + Rates ---------------------------------------------- */ $types = []; try{ $sql = " SELECT t.pasaria_type AS tname, COALESCE(r.rate,NULL) AS rate FROM pasaria_types t LEFT JOIN pasaria_rates r ON r.company_id=t.company_id AND r.pasaria_type COLLATE utf8mb4_unicode_ci = t.pasaria_type COLLATE utf8mb4_unicode_ci WHERE t.company_id=? AND t.is_active=1 ORDER BY t.sort_order ASC, t.pasaria_type ASC"; $st=$pdo->prepare($sql); $st->execute([$company_id]); foreach($st->fetchAll(PDO::FETCH_ASSOC) as $r){ $types[]=['type'=>$r['tname'],'rate'=>($r['rate']!==null?(float)$r['rate']:null)]; } }catch(Throwable $e){} /* --- Machine Qualities --------------------------------------------------- */ $qualities = []; try{ $st=$pdo->prepare("SELECT id, COALESCE(NULLIF(code,''), quality_name, CONCAT('Q#',id)) AS code, quality_name FROM qualities WHERE company_id=? AND (is_active=1 OR is_active IS NULL) ORDER BY quality_name ASC, id ASC"); $st->execute([$company_id]); foreach($st->fetchAll(PDO::FETCH_ASSOC) as $q){ $label = trim($q['quality_name'] ?? ''); $code = trim($q['code'] ?? ''); if ($code && strtolower($code)!==strtolower($label)) $label = $code.' — '.$label; $qualities[(int)$q['id']] = $label; } }catch(Throwable $e){} /* --- Machines ------------------------------------------------------------ */ $machines=[]; try{ $sql="SELECT code, CAST(TRIM(SUBSTRING_INDEX(code,' ', -1)) AS UNSIGNED) AS machine_no FROM machines WHERE company_id=? AND (is_active=1 OR is_active IS NULL) ORDER BY machine_no ASC, code ASC"; $st=$pdo->prepare($sql); $st->execute([$company_id]); foreach($st->fetchAll(PDO::FETCH_ASSOC) as $m){ if((int)$m['machine_no']>0) $machines[]=['no'=>(int)$m['machine_no'],'code'=>$m['code']]; } }catch(Throwable $e){} /* --- Beam Suggestions (for datalist only; non-authoritative) ------------- */ $available_beams=[]; $q1=null; try{ $q1=$pdo->prepare("SELECT DISTINCT bs.beam_no FROM beam_stock bs WHERE bs.company_id=? AND bs.beam_no IS NOT NULL AND bs.beam_no<>'' ORDER BY bs.beam_no"); $q1->execute([$company_id]); $available_beams=array_map(fn($r)=>$r['beam_no'],$q1->fetchAll(PDO::FETCH_ASSOC)); }catch(Throwable $e){ $available_beams=[]; } /* --- Load for EDIT ------------------------------------------------------- */ $edit_id = isset($_GET['edit_id']) ? (int)$_GET['edit_id'] : (int)postv('edit_id', 0); $edit_row = null; if ($edit_id > 0) { $g = $pdo->prepare("SELECT * FROM pasaria_entry WHERE company_id=? AND id=? LIMIT 1"); $g->execute([$company_id,$edit_id]); $edit_row = $g->fetch(PDO::FETCH_ASSOC) ?: null; if(!$edit_row){ $edit_id=0; } } /* --- Save/Update --------------------------------------------------------- */ $msg=''; $err=''; if($_SERVER['REQUEST_METHOD']==='POST' && postv('_action')==='save'){ $entry_date = trim(postv('entry_date', date('Y-m-d'))); $pasaria_type = trim(postv('pasaria_type', '')); $machine_quality_id= (postv('machine_quality_id','') !== '') ? (int)postv('machine_quality_id') : null; $machine_no = (postv('machine_no','') !== '') ? (int)postv('machine_no') : null; $employee_id = (postv('employee_id','') !== '') ? (int)postv('employee_id') : null; // Beam inputs (create mode only; edit mode me locked/ignored) $primary_beam_no = trim(postv('primary_beam_no','')); $secondary_beam_no = trim(postv('secondary_beam_no','')); $isFani = (strcasecmp($pasaria_type,'Fani')===0); $isJog = (strcasecmp($pasaria_type,'Jog')===0); $isEdit = (int)postv('edit_id',0) > 0; if(!preg_match('/^\d{4}-\d{2}-\d{2}$/',$entry_date)) $err='Invalid date.'; elseif($pasaria_type==='') $err='Pasaria type is required.'; // machine_no required except for Jog elseif(!$isJog && !$machine_no) $err='Machine is required (unless type is Jog).'; // Employee validation (visible in all cases; optional) if(!$err && $employee_id){ $chk=$pdo->prepare("SELECT 1 FROM company_employee_master WHERE company_id=? AND id=? AND (LOWER(department_name)='pasaria' OR department_id=35) LIMIT 1"); $chk->execute([$company_id,$employee_id]); if(!$chk->fetchColumn()) $err='Invalid employee: only Pasaria department allowed.'; } $machine_quality=null; if(!$err && $machine_quality_id){ $q=$pdo->prepare("SELECT COALESCE(NULLIF(code,''), quality_name) FROM qualities WHERE company_id=? AND id=? LIMIT 1"); $q->execute([$company_id,$machine_quality_id]); $machine_quality=$q->fetchColumn(); } if(!$err){ try{ // NEW RULE: If not Fani and not edit-mode, any entered beams MUST exist in beam_stock. if(!$isEdit && !$isFani){ // check primary (DB-backed authoritative check) if($primary_beam_no!==''){ $stp = $pdo->prepare("SELECT 1 FROM beam_stock WHERE company_id = ? AND TRIM(CAST(beam_no AS CHAR)) = ? LIMIT 1"); $stp->execute([$company_id, trim((string)$primary_beam_no)]); if (!$stp->fetchColumn()){ $err = "Primary beam '".h($primary_beam_no)."' not found in stock. Entry blocked by new rule."; if ($DEBUG) error_log("PASARIA DEBUG: primary beam check failed for company_id={$company_id}, beam={$primary_beam_no}"); } } // check secondary (DB-backed authoritative check) if(!$err && $secondary_beam_no!==''){ $sts = $pdo->prepare("SELECT 1 FROM beam_stock WHERE company_id = ? AND TRIM(CAST(beam_no AS CHAR)) = ? LIMIT 1"); $sts->execute([$company_id, trim((string)$secondary_beam_no)]); if (!$sts->fetchColumn()){ $err = "Secondary beam '".h($secondary_beam_no)."' not found in stock. Entry blocked by new rule."; if ($DEBUG) error_log("PASARIA DEBUG: secondary beam check failed for company_id={$company_id}, beam={$secondary_beam_no}"); } } } }catch(Throwable $e){ $err = "Beam stock check failed. Entry blocked."; if ($DEBUG) error_log("PASARIA DEBUG: beam stock check threw: ".$e->getMessage()); } } if(!$err){ try{ $pdo->beginTransaction(); $primary_beam_quality_id = null; $secondary_beam_quality_id = null; if(!$isEdit){ if(!$isFani){ // If beam present in beam_entry, claim (update installation_date) if($primary_beam_no !== ''){ $get = $pdo->prepare("SELECT id, COALESCE(quality_id, NULL) AS qid, installation_date FROM beam_entry WHERE company_id=? AND beam_no=? LIMIT 1"); $get->execute([$company_id, $primary_beam_no]); $row = $get->fetch(PDO::FETCH_ASSOC); if ($row) { if (!empty($row['qid'])) $primary_beam_quality_id = (int)$row['qid']; if (empty($row['installation_date']) || $row['installation_date']==='0000-00-00') { $upd = $pdo->prepare("UPDATE beam_entry SET installation_date=:dt, installation_source='pasaria' WHERE id=:id"); $upd->execute([':dt'=>$entry_date, ':id'=>$row['id']]); } } } if($secondary_beam_no !== ''){ $get2 = $pdo->prepare("SELECT id, COALESCE(quality_id, NULL) AS qid, installation_date FROM beam_entry WHERE company_id=? AND beam_no=? LIMIT 1"); $get2->execute([$company_id, $secondary_beam_no]); $row2 = $get2->fetch(PDO::FETCH_ASSOC); if ($row2) { if (!empty($row2['qid'])) $secondary_beam_quality_id = (int)$row2['qid']; if (empty($row2['installation_date']) || $row2['installation_date']==='0000-00-00') { $upd2 = $pdo->prepare("UPDATE beam_entry SET installation_date=:dt, installation_source='pasaria' WHERE id=:id"); $upd2->execute([':dt'=>$entry_date, ':id'=>$row2['id']]); } } } } // Insert pasaria_entry $st_ins = $pdo->prepare("INSERT INTO pasaria_entry (company_id,entry_date,quality_id,quality_name,machine_quality_id,machine_quality, primary_beam,primary_beam_no,primary_beam_quality_id, secondary_beam,secondary_beam_no,secondary_beam_quality_id, pasaria_type,machine_no,employee_id,employee_name,remark,created_by) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); $st_ins->execute([ $company_id,$entry_date, null,null, $machine_quality_id?:null, ($machine_quality!==''?$machine_quality:null), null, ($primary_beam_no!==''? $primary_beam_no : null), ($primary_beam_quality_id?:null), null, ($secondary_beam_no!==''? $secondary_beam_no : null), ($secondary_beam_quality_id?:null), $pasaria_type?:null, ($isJog? null : ($machine_no?:null)), $employee_id?:null, null, null, $user_id ]); $new_id = (int)$pdo->lastInsertId(); // Insert installation_record for primary/secondary beams (idempotent) if ($new_id>0){ if ($primary_beam_no!==''){ insert_beam_installation_record($pdo, $company_id, 'pasaria_entry', $new_id, $primary_beam_no, null, ($machine_no?:null), $entry_date, ['kind'=>'pasaria','which'=>'primary','type'=>$pasaria_type], $user_id); } if ($secondary_beam_no!==''){ insert_beam_installation_record($pdo, $company_id, 'pasaria_entry', $new_id, $secondary_beam_no, null, ($machine_no?:null), $entry_date, ['kind'=>'pasaria','which'=>'secondary','type'=>$pasaria_type], $user_id); } } $msg = $isFani ? 'Saved (Fani). Beams not required.' : 'Saved. (Beams claimed if present in beam_entry.)'; $_POST=[]; } else { // UPDATE (do not change beams) $eid = (int)postv('edit_id',0); $st_u = $pdo->prepare("UPDATE pasaria_entry SET entry_date=?, machine_quality_id=?, machine_quality=?, pasaria_type=?, machine_no=?, employee_id=?, employee_name=NULL WHERE company_id=? AND id=?"); $st_u->execute([ $entry_date, $machine_quality_id?:null, ($machine_quality!==''?$machine_quality:null), $pasaria_type?:null, ($isJog? null : ($machine_no?:null)), $employee_id?:null, $company_id, $eid ]); $msg='Updated.'; } $pdo->commit(); // refresh available_beams datalist for UI if($q1 instanceof PDOStatement){ $q1->execute([$company_id]); $available_beams=array_map(fn($r)=>$r['beam_no'],$q1->fetchAll(PDO::FETCH_ASSOC)); } if(isset($eid) && $eid){ $edit_id=0; $edit_row=null; } }catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); $err='Save failed: '.$e->getMessage(); if ($DEBUG) error_log("PASARIA DEBUG: save failed: ".$e->getMessage()); } } } /* --- Recent (with Edit link) -------------------------------------------- */ $recent=[]; try{ $st=$pdo->prepare(" SELECT pe.id, pe.entry_date, pe.machine_quality, pe.primary_beam_no, pe.secondary_beam_no, pe.pasaria_type, pe.machine_no, COALESCE(pe.employee_name, cem.name) AS emp_name FROM pasaria_entry pe LEFT JOIN company_employee_master cem ON cem.id = pe.employee_id AND cem.company_id = pe.company_id WHERE pe.company_id = ? ORDER BY pe.id DESC LIMIT 50"); $st->execute([$company_id]); $recent=$st->fetchAll(PDO::FETCH_ASSOC); }catch(Throwable $e){} /* --- Header include ------------------------------------------------------ */ $page_title = 'Pasaria entry'; $__header = __DIR__ . '/partials/header.php'; if (is_file($__header)) { $PAGE='pasaria_entry'; include_once $__header; } /* --- Form defaults (prefer POST -> edit_row -> defaults) ---------------- */ $today = date('Y-m-d'); $def_type = postv('pasaria_type', $edit_row['pasaria_type'] ?? ''); $def_date = postv('entry_date', $edit_row['entry_date'] ?? $today); $def_mq = (int)postv('machine_quality_id', (string)($edit_row['machine_quality_id'] ?? '')); $def_m = (int)postv('machine_no', (string)($edit_row['machine_no'] ?? '')); $def_emp = (int)postv('employee_id', (string)($edit_row['employee_id'] ?? ($default_emp_id ?: ''))); $def_pbn = postv('primary_beam_no', (string)($edit_row['primary_beam_no'] ?? '')); $def_sbn = postv('secondary_beam_no', (string)($edit_row['secondary_beam_no'] ?? '')); $is_edit_mode = ($edit_id>0); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"> <title>Pasaria Entry</title> </head> <body> <div class="wrap"> <div class="nav"> <div class="chip"><a href="/erp/pasaria_types.php">Pasaria Types</a></div> <div class="chip"><a href="/erp/pasaria_rates.php">Pasaria Rates</a></div> </div> <div class="card"> <h2><?= $is_edit_mode ? 'Edit Pasaria Entry #'.(int)$edit_id : 'Pasaria Entry' ?></h2> <?php if($msg):?><div class="ok"><?=h($msg)?></div><?php endif;?> <?php if($err):?><div class="err"><?=h($err)?></div><?php endif;?> <!-- Beam suggestion list (create mode only) --> <datalist id="beam_list"> <?php foreach($available_beams as $bn): ?> <option value="<?=h($bn)?>"></option> <?php endforeach; ?> </datalist> <form method="post" autocomplete="off"> <input type="hidden" name="_action" value="save"> <?php if($is_edit_mode): ?><input type="hidden" name="edit_id" value="<?=$edit_id?>"><?php endif;?> <div class="grid"> <div class="row-date"> <label for="f_date">Date</label> <input id="f_date" type="date" name="entry_date" value="<?=h($def_date)?>" required> </div> <div class="row-type"> <label for="f_type">Pasaria Type</label> <select id="f_type" name="pasaria_type" required> <option value="">-- Select --</option> <?php foreach($types as $t){ $lbl=$t['type'].($t['rate']!==null?(' (₹'.number_format($t['rate'],2).')'):''); $sel=(strcasecmp($def_type,$t['type'])===0)?'selected':''; echo '<option value="'.h($t['type']).'" '.$sel.'>'.h($lbl).'</option>'; } ?> </select> </div> <!-- Machine Quality --> <div class="row-machine-quality"> <label for="f_mq">Machine Quality</label> <select id="f_mq" name="machine_quality_id"> <option value="">-- Select Machine Quality --</option> <?php foreach($qualities as $qid=>$label){ $sel=($def_mq===$qid)?'selected':''; echo '<option value="'.$qid.'" '.$sel.'>'.h($label).'</option>'; } ?> </select> </div> <!-- Employee (always visible, incl. Fani) --> <div class="row-emp"> <label for="f_emp">Employee (Pasaria)</label> <select id="f_emp" name="employee_id"> <option value="">-- Select --</option> <?php $pref=$def_emp; foreach($emps as $e){ $sel=($pref==(int)$e['id'])?'selected':''; echo '<option value="'.$e['id'].'" '.$sel.'>'.h($e['name']).' (ID '.$e['id'].')</option>'; } ?> </select> </div> <!-- Primary Beam (create only; hidden on Fani via JS) --> <div class="row-beam <?= $is_edit_mode ? 'lock' : '' ?>"> <label for="f_pbn">Primary Beam No. <span class="small">(In Stock suggestions shown)</span></label> <input id="f_pbn" name="primary_beam_no" list="beam_list" placeholder="Type or select beam no." value="<?=h($def_pbn)?>" <?= $is_edit_mode?'disabled':'' ?>> <div class="help"><?= $is_edit_mode ? 'Beams are locked in edit mode.' : 'Beam must exist in stock (unless type is Fani).' ?></div> </div> <!-- Secondary Beam (create only) --> <div class="row-beam <?= $is_edit_mode ? 'lock' : '' ?>"> <label for="f_sbn">Secondary Beam No. <span class="small">(In Stock suggestions shown)</span></label> <input id="f_sbn" name="secondary_beam_no" list="beam_list" placeholder="Type or select beam no." value="<?=h($def_sbn)?>" <?= $is_edit_mode?'disabled':'' ?>> <div class="help"><?= $is_edit_mode ? 'Beams are locked in edit mode.' : 'Beam must exist in stock (unless type is Fani).' ?></div> </div> <!-- Machine --> <div class="row-machine"> <label for="f_machine">Machine <span class="small">(required except for Jog)</span></label> <select id="f_machine" name="machine_no" <?= (strcasecmp($def_type,'Jog')===0 ? '' : 'required') ?>> <option value="">-- Select Machine --</option> <?php foreach($machines as $m){ $sel=($def_m===$m['no'])?'selected':''; echo '<option value="'.$m['no'].'" '.$sel.'>'.h($m['code']).'</option>'; } ?> </select> </div> </div> <div style="margin-top:12px;display:flex;gap:10px;flex-wrap:wrap"> <button class="btn btn-pri"><?= $is_edit_mode ? 'Update' : 'Save' ?></button> <?php if($is_edit_mode): ?><a class="btn btn-alt" href="/erp/pasaria_entry.php">Cancel Edit</a><?php endif; ?> </div> </form> </div> <div class="card" style="margin-top:16px"> <h3>Recent (last 50)</h3> <table> <thead><tr> <th>Date</th><th>Machine Quality</th> <th>P Beam#</th><th>S Beam#</th> <th>Type</th><th>Machine</th><th>Employee</th><th>Action</th> </tr></thead> <tbody> <?php if(!$recent):?> <tr><td colspan="8" style="color:#666">No rows.</td></tr> <?php else: foreach($recent as $r): ?> <tr> <td><?=h($r['entry_date'] ?? '')?></td> <td><?=h($r['machine_quality'] ?? '')?></td> <td><?=h($r['primary_beam_no'] ?? '')?></td> <td><?=h($r['secondary_beam_no'] ?? '')?></td> <td><?=h($r['pasaria_type'] ?? '')?></td> <td><?=h($r['machine_no'] ?? '')?></td> <td><?=h($r['emp_name'] ?? '')?></td> <td><a href="/erp/pasaria_entry.php?edit_id=<?= (int)$r['id'] ?>">Edit</a></td> </tr> <?php endforeach; endif;?> </tbody> </table> </div> </div> <script> (function(){ const typeSel = document.getElementById('f_type'); const beamRows = document.querySelectorAll('.row-beam'); function updateVisibility(){ const val = (typeSel.value||'').toLowerCase().trim(); const isFani = (val==='fani'); beamRows.forEach(r=> r.classList.toggle('hide', isFani)); const machine = document.getElementById('f_machine'); if (machine) { if (val==='jog') { machine.removeAttribute('required'); } else { machine.setAttribute('required','required'); } } } if(typeSel){ typeSel.addEventListener('change', updateVisibility); updateVisibility(); } })(); </script> <?php /* --- Footer include (requested) ---------------------------------------- */ $__footer = __DIR__ . '/partials/footer.php'; if (is_file($__footer)) { include_once $__footer; } ?> </body> </html>