« Back to History
entry_form.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // erp/partials/entry_form.php // Reusable entry form partial. // Usage: // - Ensure this file is included AFTER auth + $pdo are available and $u = auth_user() is set. // - Provide $tbl (string) indicating table (e.g., 'yarn_in'). // - Optionally provide $record (assoc) when editing a row. // - The including page should handle POST save/update logic. // Notes: // - This partial only renders the form and populates select lists from DB if available. // - Respects company scope for select lists. if (!defined('IN_ERP_PARTIAL')) { // marker to avoid direct access; you can set define('IN_ERP_PARTIAL', true) before require. // but we won't block in case you don't set it. (Optional safety) } // helpers if (!function_exists('h')) { function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } $tbl = $tbl ?? 'yarn_in'; // default table if not provided $record = $record ?? []; // existing values for edit mode $company_id = (int)($u['company_id'] ?? 0); // functions to safely fetch list (if table exists) function fetch_options(PDO $pdo, $table, $where = '', $params = []) { try { // check if table exists quickly $st = $pdo->query("SHOW TABLES LIKE " . $pdo->quote($table)); if ($st->rowCount() === 0) return []; $sql = "SELECT * FROM `$table` " . ($where ? "WHERE $where" : "") . " ORDER BY name ASC"; $q = $pdo->prepare($sql); $q->execute($params); return $q->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { return []; } } // Populate selects (best-effort — adjust table names if your schema differs) $companies = fetch_options($pdo, 'companies', 'company_id = :cid OR company_id IS NULL', [':cid'=>$company_id]); // prefer company-specific or global $yarn_types = fetch_options($pdo, 'yarn_types'); $deniers = fetch_options($pdo, 'deniers'); $colors = fetch_options($pdo, 'colors'); // Helper to output <option>s function opts_html($list, $value_key='id', $label_key='name', $sel=null) { $html = ''; foreach ($list as $row) { $val = $row[$value_key] ?? $row[$label_key] ?? ''; $label = $row[$label_key] ?? $row[$value_key] ?? ''; $selected = ((string)$val === (string)$sel) ? ' selected' : ''; $html .= '<option value="'.h($val).'"'.$selected.'>'.h($label).'</option>'; } return $html; } // Values (use record if set, else defaults) $val = function($k,$def='') use ($record){ if (isset($record[$k]) && $record[$k] !== null) return $record[$k]; return $def; }; ?> <div class="erp-form-wrap"> <form method="post" class="erp-form-card" action=""> <input type="hidden" name="action" value="<?= h($record['id'] ? 'update' : 'save') ?>"> <input type="hidden" name="tbl" value="<?= h($tbl) ?>"> <?php if (!empty($record['id'])): ?> <input type="hidden" name="id" value="<?= h($record['id']) ?>"> <?php endif; ?> <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;"> <h3 style="margin:0;">Entry Form</h3> <div class="erp-hint">Company ID: <?= h($company_id) ?></div> </div> <div class="erp-form-grid"> <div class="erp-field"> <label>Date</label> <input type="date" name="txn_date" value="<?= h(substr($val('txn_date', date('Y-m-d')),0,10)) ?>"> </div> <div class="erp-field"> <label>Yarn Type</label> <?php if (!empty($yarn_types)): ?> <select name="yarn_type"> <option value="">— Select —</option> <?= opts_html($yarn_types, 'id', 'name', $val('yarn_type')) ?> </select> <?php else: ?> <input type="text" name="yarn_type" value="<?= h($val('yarn_type')) ?>" placeholder="e.g., POLYESTER"> <?php endif; ?> </div> <div class="erp-field"> <label>Company</label> <?php if (!empty($companies)): ?> <select name="company"> <option value="">— Select —</option> <?= opts_html($companies, 'id', 'name', $val('company')) ?> </select> <?php else: ?> <input type="text" name="company" value="<?= h($val('company')) ?>" placeholder="Supplier"> <?php endif; ?> </div> <div class="erp-field"> <label>Denier</label> <?php if (!empty($deniers)): ?> <select name="denier"> <option value="">— Select —</option> <?= opts_html($deniers, 'id', 'name', $val('denier')) ?> </select> <?php else: ?> <input type="text" name="denier" value="<?= h($val('denier')) ?>"> <?php endif; ?> </div> <div class="erp-field"> <label>Color</label> <?php if (!empty($colors)): ?> <select name="color"> <option value="">— Select —</option> <?= opts_html($colors, 'id', 'name', $val('color')) ?> </select> <?php else: ?> <input type="text" name="color" value="<?= h($val('color')) ?>"> <?php endif; ?> </div> <div class="erp-field"> <label>Box</label> <input type="text" name="boxes" placeholder="e.g., 10" value="<?= h($val('boxes')) ?>"> </div> <div class="erp-field full"> <label>Weight (kg)</label> <input type="number" step="0.001" name="weight" placeholder="e.g., 250" value="<?= h($val('weight')) ?>"> </div> <div class="erp-field"> <label>Assign To</label> <input type="text" name="assign_to" value="<?= h($val('assign_to')) ?>"> </div> <div class="erp-field"> <label>Remarks</label> <textarea name="remarks"><?= h($val('remarks')) ?></textarea> </div> </div> <div class="erp-actions"> <button type="submit" class="erp-btn erp-btn-primary"><?= $record ? 'Save changes' : 'Submit' ?></button> <?php if (!empty($record)): ?> <a class="erp-btn" href="?tbl=<?= h($tbl) ?>">Cancel</a> <?php else: ?> <a class="erp-btn" href="table_manage.php?tbl=<?= h($tbl) ?>">Records</a> <button type="reset" class="erp-btn erp-btn-ghost">Reset</button> <?php endif; ?> </div> </form> </div>