« Back to History
karigar_register.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= File: /erp/karigar_register.php Purpose: Register a Karigar directly into loom_karigar_master (Khata = Khata Code only) Scope: Page-local only; DO NOT change global/base settings. ============================================================================= */ require_once __DIR__ . '/modules/dev/page_catalog.php'; page_meta([ 'page_name' => '/erp/karigar_register.php', // 👈 सही file name 'functions' => ['save_karigar','validate_form','upload_dp'], 'tables' => ['loom_karigar_master','users','companies'], 'settings' => ['uses_acl'=>true,'role_guard'=>['Owner','Admin']], 'description' => 'Karigar register form with Khata Code, DP upload & user link', ], ['log'=>true]); error_reporting(E_ALL); ini_set('display_errors',1); 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 h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function pv($k,$d=null){ return isset($_POST[$k])?trim((string)$_POST[$k]):$d; } $flash=''; $new_id=null; /* Masters (best-effort): departments & roles */ $DEPTS = []; $ROLES = []; try{ $st=$pdo->prepare("SELECT id,name FROM departments WHERE company_id=? ORDER BY name"); $st->execute([$company_id]); foreach($st as $r){ $DEPTS[(int)$r['id']]=$r['name']; } }catch(Exception $e){} try{ $st=$pdo->prepare("SELECT id,name FROM roles WHERE company_id=? ORDER BY name"); $st->execute([$company_id]); foreach($st as $r){ $ROLES[(int)$r['id']]=$r['name']; } }catch(Exception $e){} /* Robust lookups (preserve keys) + fallbacks */ $DEPT_LOOM_ID = null; foreach($DEPTS as $id=>$nm){ if (strtoupper($nm)==='LOOM'){ $DEPT_LOOM_ID=(int)$id; break; } } $ROLE_KARIGAR_ID = null; foreach($ROLES as $id=>$nm){ if (strtoupper($nm)==='KARIGAR'){ $ROLE_KARIGAR_ID=(int)$id; break; } } if($DEPT_LOOM_ID===null) $DEPT_LOOM_ID = 19; if($ROLE_KARIGAR_ID===null) $ROLE_KARIGAR_ID = 12; /* Existing Khata Codes (distinct) */ $KHATA_CODES=[]; try{ $st=$pdo->prepare("SELECT DISTINCT khata_code FROM loom_karigar_master WHERE company_id=? AND khata_code IS NOT NULL AND khata_code<>'' ORDER BY khata_code"); $st->execute([$company_id]); foreach($st as $r){ $KHATA_CODES[] = (string)$r['khata_code']; } }catch(Exception $e){} /* Handle submit */ if($_SERVER['REQUEST_METHOD']==='POST'){ $karigar_name = pv('karigar_name',''); $dept_id = (int)pv('department_id',$DEPT_LOOM_ID); $role_id = (int)pv('role_id',$ROLE_KARIGAR_ID); $is_active = (int)pv('is_active',1); // Khata = Khata Code only $khata_code_sel = pv('khata_code_select',''); // dropdown $khata_code_inp = pv('khata_code',''); // manual input $khata_code = $khata_code_inp !== '' ? $khata_code_inp : $khata_code_sel; $khata_code = trim($khata_code); $machine_from = pv('machine_from',''); $machine_to = pv('machine_to',''); $entry_name = pv('entry_name',''); // Auto Entry Name: "Name from-to" | "Name from" | "Name" if($entry_name===''){ $entry_name = $karigar_name; if($machine_from !== ''){ $entry_name .= ' ' . $machine_from; if($machine_to !== ''){ $entry_name .= '-' . $machine_to; } } $entry_name = trim($entry_name); } if($karigar_name===''){ $flash = 'Karigar name is required.'; } else { try{ // Ensure table (scoped) $pdo->exec("CREATE TABLE IF NOT EXISTS loom_karigar_master ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, khata_id BIGINT NULL, khata_code VARCHAR(50) NULL, karigar_name VARCHAR(120) NOT NULL, machine_from INT NULL, machine_to INT NULL, entry_name VARCHAR(120) NULL, department_id BIGINT NULL, role_id BIGINT NULL, is_active TINYINT(1) DEFAULT 1, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX(company_id, khata_code), INDEX(company_id, department_id, role_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); // Insert (NO address, NO khata_id) $ins=$pdo->prepare("INSERT INTO loom_karigar_master (company_id, khata_code, karigar_name, machine_from, machine_to, entry_name, department_id, role_id, is_active) VALUES (?,?,?,?,?,?,?,?,?)"); $ins->execute([ $company_id, $khata_code !== '' ? $khata_code : null, $karigar_name, $machine_from !== '' ? (int)$machine_from : null, $machine_to !== '' ? (int)$machine_to : null, $entry_name !== '' ? $entry_name : null, $dept_id, $role_id, $is_active ]); $new_id = (int)$pdo->lastInsertId(); $flash = "Karigar saved (ID: {$new_id})."; }catch(Exception $e){ $flash = "Error: ".$e->getMessage(); } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Loom Karigar Register</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> :root{ --green:#34A853; --mint:#F5FFF7; } body{margin:0;background:var(--mint);font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Arial;color:#222} .wrap{max-width:900px;margin:20px auto;padding:0 12px} h2{color:#2c7d3f;margin:6px 0 12px} .card{background:#fff;border:1px solid #e6f1e6;border-radius:12px;padding:12px;margin-bottom:14px} .row{display:flex;gap:10px;flex-wrap:wrap} .row>div{flex:1 1 230px} label{font-size:12px;color:#333;display:block;margin-bottom:4px} input,select{width:100%;padding:8px;border:1px solid #cfe8d0;border-radius:10px} .btn{background:var(--green);color:#fff;border:none;border-radius:10px;padding:9px 14px;font-weight:600;cursor:pointer} .flash{background:#e7ffe9;border:1px solid #c7f0cb;color:#1e5a2c;padding:8px 12px;border-radius:10px;margin:8px 0;display:inline-block} .small{font-size:12px;color:#555} </style> </head> <body> <div class="wrap"> <h2>Loom Karigar Registration</h2> <?php if($flash): ?><div class="flash"><?= h($flash) ?></div><?php endif; ?> <form method="post" class="card"> <div class="row"> <div><label>Karigar Name</label><input name="karigar_name" required></div> <div> <label>Status</label> <select name="is_active"> <option value="1" selected>Active</option> <option value="0">Inactive</option> </select> </div> </div> <div class="row"> <div> <label>Department</label> <select name="department_id"> <?php foreach($DEPTS as $id=>$nm): ?> <option value="<?= $id ?>" <?= ($id===$DEPT_LOOM_ID)?'selected':'' ?>><?= h($nm) ?></option> <?php endforeach; ?> <?php if(empty($DEPTS)): ?> <option value="<?= $DEPT_LOOM_ID ?>" selected>LOOM</option> <?php endif; ?> </select> </div> <div> <label>Role</label> <select name="role_id"> <?php foreach($ROLES as $id=>$nm): ?> <option value="<?= $id ?>" <?= ($id===$ROLE_KARIGAR_ID)?'selected':'' ?>><?= h($nm) ?></option> <?php endforeach; ?> <?php if(empty($ROLES)): ?> <option value="<?= $ROLE_KARIGAR_ID ?>" selected>KARIGAR</option> <?php endif; ?> </select> </div> </div> <!-- Khata = Khata Code --> <div class="row"> <div> <label>Khata Code (existing)</label> <select name="khata_code_select"> <option value="">-- Select --</option> <?php foreach($KHATA_CODES as $code): ?> <option value="<?= h($code) ?>"><?= h($code) ?></option> <?php endforeach; ?> </select> </div> <div> <label>Khata Code (new or override)</label> <input name="khata_code" placeholder="e.g., MST1"> </div> </div> <div class="row"> <div><label>Machine From</label><input name="machine_from" type="number" inputmode="numeric"></div> <div><label>Machine To</label><input name="machine_to" type="number" inputmode="numeric"></div> <div><label>Entry Name (auto)</label><input name="entry_name" placeholder="auto: Suraj 5-8 (leave blank to auto)"></div> </div> <div style="margin-top:10px"> <button class="btn" type="submit">Save Karigar</button> </div> </form> </div> </body> </html>