« Back to History
company_edit.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================= File: /erp/company_edit.php Purpose: Edit company + (optional) machine seeding + company bank mapping Scope: This page only (no global setting changes) ============================================= */ error_reporting(E_ALL); ini_set('display_errors', '1'); /* ---- Auth + ACL ---- */ require __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('company_edit'); // your ACL slug $u = $ctx['user'] ?? null; $role = strtolower($u['role'] ?? ''); $pdo = (isset($ctx['pdo']) && $ctx['pdo'] instanceof PDO) ? $ctx['pdo'] : null; if (!$pdo) { require __DIR__ . '/core/db.php'; } // fallback — core/db.php must set $pdo (PDO) require_once __DIR__ . '/helpers/activity_helper.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function v($k,$d=null){ return isset($_POST[$k]) ? (is_string($_POST[$k])?trim($_POST[$k]):$_POST[$k]) : $d; } /* ---- Which company to edit ---- */ $cid = (int)($_GET['id'] ?? $_POST['id'] ?? 0); if ($cid <= 0) { http_response_code(400); exit('Missing id'); } /* ---- Role rule: Owner/Admin any; others only own company ---- */ $session_cid = (int)($ctx['company_id'] ?? 0); $is_owner_admin = in_array($role, ['owner','admin'], true); if (!$is_owner_admin && $cid !== $session_cid) { http_response_code(403); exit('Forbidden'); } /* ---- Load company (fits your schema: id, name, is_active) ---- */ $st = $pdo->prepare("SELECT id, name, is_active FROM companies WHERE id = :id"); $st->execute([':id' => $cid]); $company = $st->fetch(PDO::FETCH_ASSOC); if (!$company) { http_response_code(404); exit('Company not found'); } /* ===== Helpers ===== */ function get_columns(PDO $pdo, string $table): array { try { $rs = $pdo->query("SHOW COLUMNS FROM `$table`")->fetchAll(PDO::FETCH_ASSOC); $out = []; foreach($rs as $r){ $out[strtolower($r['Field'])] = true; } return $out; } catch(Throwable $e) { return []; } } /* seed machines — BLOCK if company column missing */ function seedMachines(PDO $pdo, int $companyId, string $typeKey, int $count, string $label, array $cols){ if ($count <= 0) return; if (empty($cols['company'])) { throw new Exception("machines table me company column nahi mila — machine create blocked."); } // try a conservative insert (adjust columns if needed) $has_type = !empty($cols['type']) || isset($cols['machine_type']); $sql = $has_type ? "INSERT IGNORE INTO machines (company, type, name, code) VALUES (?,?,?,?)" : "INSERT IGNORE INTO machines (company, name, code) VALUES (?,?,?)"; $st = $pdo->prepare($sql); for ($i=1; $i<=$count; $i++){ $code = sprintf("%s %03d", $label, $i); if ($has_type) { $st->execute([$companyId, $typeKey, $code, $code]); } else { $st->execute([$companyId, $code, $code]); } } } /* salary windows table (scoped) */ $pdo->exec("CREATE TABLE IF NOT EXISTS company_pay_windows ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, window_no TINYINT NOT NULL, day_start TINYINT NOT NULL, day_end TINYINT NOT NULL, UNIQUE KEY uniq_company_window (company_id,window_no) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); function upsertPayWindow(PDO $pdo,int $cid,int $no,int $s,int $e){ if($s<1||$s>31||$e<1||$e>31) throw new Exception("Invalid days for window $no"); $pdo->prepare("INSERT INTO company_pay_windows (company_id,window_no,day_start,day_end) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE day_start=VALUES(day_start), day_end=VALUES(day_end)") ->execute([$cid,$no,$s,$e]); } /* ===== Types for UI ===== */ const MACHINE_TYPES_UI = [ 'LOOM' => 'Loom', 'RAPIER' => 'Rapier', 'LACE' => 'Lace', 'ZARI' => 'Zari' ]; /* ===== Controller ===== */ $cols_m = get_columns($pdo, 'machines'); $machines_enabled = !empty($cols_m['company']); // company column present? $msg=''; $err=''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['_form']) && $_POST['_form'] === 'company_edit')) { try { // optional: update company basic name $new_name = trim(v('name','')); if ($new_name !== '') { $pdo->prepare("UPDATE companies SET name=? WHERE id=?")->execute([$new_name, $cid]); } // salary windows upsertPayWindow($pdo,$cid,1,(int)v('w1_start',10),(int)v('w1_end',15)); upsertPayWindow($pdo,$cid,2,(int)v('w2_start',25),(int)v('w2_end',30)); // machines seeding only if enabled if ($machines_enabled) { foreach (MACHINE_TYPES_UI as $t=>$label){ $flag = v("mk_$t",'0')==='1'; $cnt = (int)v("count_$t",0); if ($flag && $cnt>0) seedMachines($pdo,$cid,$t,$cnt,$label,$cols_m); } $msg = "Saved. Missing machines (if any) were created."; } else { $msg = "Saved."; } activity_update('company', 'company_edit_update', $cid, 'Updated company settings'); } catch(Throwable $e) { $err = $e->getMessage(); } } /* windows current */ $w1=[10,15]; $w2=[25,30]; $win = $pdo->prepare("SELECT window_no,day_start,day_end FROM company_pay_windows WHERE company_id=?"); $win->execute([$cid]); foreach($win as $r){ if ($r['window_no']==1){ $w1=[(int)$r['day_start'],(int)$r['day_end']]; } if ($r['window_no']==2){ $w2=[(int)$r['day_start'],(int)$r['day_end']]; } } /* company bank account (for Debit A/c) */ $pdo->exec("CREATE TABLE IF NOT EXISTS company_bank_accounts ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, account_holder VARCHAR(120), bank_name VARCHAR(120), account_no VARCHAR(30) NOT NULL, ifsc VARCHAR(15) NOT NULL, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY uniq_company_ac (company_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); $bank = ['account_holder'=>'','bank_name'=>'','account_no'=>'','ifsc'=>'']; try{ $bq = $pdo->prepare("SELECT account_holder, bank_name, account_no, ifsc FROM company_bank_accounts WHERE company_id=:cid"); $bq->execute([':cid'=>$cid]); if ($row = $bq->fetch(PDO::FETCH_ASSOC)) { $bank = $row; } }catch(Throwable $e){ /* ignore */ } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"> <title>Edit Company</title> <style> body{font-family:Inter,system-ui,-apple-system,Segoe UI,Roboto,Arial;background:#f6f7f8;margin:0;padding:24px;color:#222} .card{max-width:1100px;margin:0 auto;background:#fff;border:1px solid #eee;border-radius:16px;padding:24px;box-shadow:0 6px 24px rgba(0,0,0,.06)} .row{display:grid;grid-template-columns:1fr 1fr;gap:16px} label{display:block;font-size:14px;margin:6px 0} input[type=number], input[type=text]{width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:10px;background:#fff} .btn{appearance:none;border:none;background:#34A853;color:#fff;padding:12px 18px;border-radius:12px;font-weight:600;cursor:pointer} .muted{color:#666;font-size:12px} .ok{background:#ecfdf5;border:1px solid #a7f3d0;color:#065f46;padding:10px;border-radius:10px;margin-bottom:12px} .err{background:#fee2e2;border:1px solid #fecaca;color:#7f1d1d;padding:10px;border-radius:10px;margin-bottom:12px} .mgrid{display:grid;grid-template-columns: 180px 160px; gap:10px; align-items:center} .section{margin-top:24px} .btn-secondary{background:#0b57d0} </style> </head> <body> <div class="card"> <h2>Edit Company: <?= h($company['name'] ?? ('#'.$cid)) ?></h2> <?php if($msg): ?><div class="ok"><?= h($msg) ?></div><?php endif; ?> <?php if($err): ?><div class="err"><?= h($err) ?></div><?php endif; ?> <!-- Company basic + Salary windows + (optional) Machine seeding --> <form method="post"> <input type="hidden" name="_form" value="company_edit"> <input type="hidden" name="id" value="<?= (int)$cid ?>"> <div class="section"> <h3>Company Basic</h3> <label>Name</label> <input type="text" name="name" value="<?= h($company['name'] ?? '') ?>"> </div> <div class="section"> <h3>Salary Windows</h3> <div class="row"> <div> <label>Window 1 Start</label> <input type="number" name="w1_start" min="1" max="31" value="<?= $w1[0] ?>"> </div> <div> <label>Window 1 End</label> <input type="number" name="w1_end" min="1" max="31" value="<?= $w1[1] ?>"> </div> </div> <div class="row"> <div> <label>Window 2 Start</label> <input type="number" name="w2_start" min="1" max="31" value="<?= $w2[0] ?>"> </div> <div> <label>Window 2 End</label> <input type="number" name="w2_end" min="1" max="31" value="<?= $w2[1] ?>"> </div> </div> </div> <?php if ($machines_enabled): ?> <div class="section"> <h3>Auto-Create Machines</h3> <div class="mgrid"> <?php foreach(MACHINE_TYPES_UI as $t=>$label): ?> <div> <input type="hidden" name="mk_<?= $t ?>" value="0"> <label><input type="checkbox" name="mk_<?= $t ?>" value="1"> <strong><?= h($label) ?></strong></label> </div> <div><input type="number" name="count_<?= $t ?>" min="0" placeholder="Target count"></div> <?php endforeach; ?> </div> </div> <?php endif; ?> <div class="section"> <button class="btn" type="submit">Save</button> </div> </form> <!-- Company Debit Bank Account --> <div class="section"> <h3>Company Debit Bank Account</h3> <div class="muted">Ye account payment export me <b>Debit A/c Number</b> ke liye use hoga.</div> <form method="post" action="company_bank_save.php"> <input type="hidden" name="company_id" value="<?= (int)$cid ?>"> <div class="row"> <div> <label>Account Holder</label> <input type="text" name="account_holder" value="<?= h($bank['account_holder']) ?>"> </div> <div> <label>Bank Name</label> <input type="text" name="bank_name" value="<?= h($bank['bank_name']) ?>"> </div> </div> <div class="row"> <div> <label>Account Number *</label> <input type="text" name="account_no" required value="<?= h($bank['account_no']) ?>"> </div> <div> <label>IFSC *</label> <input type="text" name="ifsc" required value="<?= h($bank['ifsc']) ?>"> </div> </div> <div class="section"> <button class="btn btn-secondary" type="submit">Save Bank</button> </div> </form> </div> </div> </body> </html>