« Back to History
saree_by_month.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // saree_by_month.php // ----------------- bootstrap page_acl (unchanged) ----------------- $acl_candidates = [ __DIR__ . '/modules/auth/page_acl.php', __DIR__ . '/../modules/auth/page_acl.php', __DIR__ . '/../../modules/auth/page_acl.php', __DIR__ . '/erp/modules/auth/page_acl.php', ]; $acl_loaded = false; foreach ($acl_candidates as $p) { if (is_file($p)) { require_once $p; $acl_loaded = true; break; } } if (!$acl_loaded) { header('Content-Type: text/plain; charset=utf-8', true, 500); echo "Missing modules/auth/page_acl.php. Please ensure project bootstrap exists."; exit; } $ctx = page_require_access('saree_party_report'); $user = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; // DB fallback includes (unchanged) if (!$pdo) { $db_candidates = [ __DIR__ . '/core/db.php', __DIR__ . '/../core/db.php', __DIR__ . '/../../core/db.php', __DIR__ . '/config.php', ]; foreach ($db_candidates as $p) { if (is_file($p)) { require_once $p; break; } } if (!($pdo instanceof PDO)) { if (isset($db) && $db instanceof PDO) $pdo = $db; if (isset($dbh) && $dbh instanceof PDO) $pdo = $dbh; if (isset($conn) && $conn instanceof PDO) $pdo = $conn; } } if (!function_exists('h')) { function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } if (session_status() === PHP_SESSION_NONE) { @session_start(); } // prepare dropdown values (company-scoped) // initialize empty arrays to avoid warnings if DB missing $qualities = []; $khatas = []; if ($pdo instanceof PDO && $company_id) { try { $st = $pdo->prepare("SELECT code, quality_name FROM qualities WHERE is_active=1 AND company_id = ? ORDER BY quality_name"); $st->execute([$company_id]); $qualities = $st->fetchAll(PDO::FETCH_ASSOC); } catch (\Throwable $e) { // ignore - keep empty } try { $st = $pdo->prepare("SELECT code FROM khatas WHERE is_active=1 AND company_id = ? ORDER BY code"); $st->execute([$company_id]); $khatas = $st->fetchAll(PDO::FETCH_COLUMN, 0); } catch (\Throwable $e) { // ignore - keep empty } } // months: last 12 months (newest first) $months = []; for ($i = 0; $i < 12; $i++) { $months[] = date('M-Y', strtotime("-$i months")); } $months = array_values($months); // keep order newest->older; we'll choose default later // initialize form variables and last entry $month_val = $months[0] ?? date('M-Y'); $quality_val = ''; $khata_val = ''; $total_saree_val = ''; $cut_val = ''; $last_entry_text = ''; $errors = []; // handle POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { // CSRF $posted_csrf = $_POST['csrf_token'] ?? ''; if (empty($_SESSION['csrf_token']) || !hash_equals($_SESSION['csrf_token'], (string)$posted_csrf)) { $errors[] = 'Invalid CSRF token. Please refresh and try again.'; } // read values from POST (trim) $month_val = trim($_POST['month'] ?? $month_val); $quality_val = trim($_POST['quality'] ?? ''); $khata_val = trim($_POST['khata'] ?? ''); $total_saree_val = trim($_POST['total_saree'] ?? ''); $cut_val = trim($_POST['cut'] ?? ''); // validation if ($month_val === '') $errors[] = 'Month is required.'; if ($quality_val === '') $errors[] = 'Quality is required.'; if ($khata_val === '') $errors[] = 'Khata is required.'; if ($total_saree_val === '' || !is_numeric($total_saree_val)) $errors[] = 'Total Saree must be a number.'; if ($cut_val === '' || !is_numeric($cut_val)) $errors[] = 'Cut must be a number.'; // normalize month to date $month_date = null; if ($month_val !== '') { $ts = @strtotime("01-" . $month_val); // e.g. "01-Sep-2025" if ($ts !== false) { $month_date = date('Y-m-01', $ts); } else { $errors[] = 'Invalid month format.'; } } if (empty($errors) && $pdo instanceof PDO && $company_id) { try { $ins = $pdo->prepare("INSERT INTO saree_by_month (company_id, month_label, month_date, quality, khata, total_saree, cut, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $ins->execute([ $company_id, $month_val, $month_date, $quality_val, $khata_val, (int)$total_saree_val, (float)$cut_val, date('Y-m-d H:i:s') ]); // PRG $_SESSION['saree_flash'] = 'saved'; header('Location: ' . (strpos($_SERVER['REQUEST_URI'], '?') !== false ? strtok($_SERVER['REQUEST_URI'], '?') : $_SERVER['REQUEST_URI'])); exit; } catch (\Throwable $ex) { $errors[] = 'Database error: ' . $ex->getMessage(); } } } // flash $flash_saved = false; if (!empty($_SESSION['saree_flash'])) { $flash_saved = true; unset($_SESSION['saree_flash']); } // fetch last entry to show if ($pdo instanceof PDO && $company_id) { try { $st = $pdo->prepare("SELECT month_label, quality, khata, total_saree, cut FROM saree_by_month WHERE company_id = ? ORDER BY id DESC LIMIT 1"); $st->execute([$company_id]); $row = $st->fetch(PDO::FETCH_ASSOC); if ($row) { if ($_SERVER['REQUEST_METHOD'] !== 'POST' || $flash_saved) { $month_val = $row['month_label'] ?? $month_val; $quality_val = $row['quality'] ?? $quality_val; $khata_val = $row['khata'] ?? $khata_val; $total_saree_val = $row['total_saree'] ?? $total_saree_val; $cut_val = $row['cut'] ?? $cut_val; } $last_entry_text = sprintf("%s | %s | %s | %s | %s", h($row['month_label'] ?? ''), h($row['quality'] ?? ''), h($row['khata'] ?? ''), h($row['total_saree'] ?? ''), h($row['cut'] ?? '') ); } } catch (\Throwable $e) { // ignore } } // CSRF token if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); } $csrf_token = $_SESSION['csrf_token']; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Saree by month</title> <meta name="viewport" content="width=device-width,initial-scale=1" /> <style> /* (Your original CSS kept exactly as requested — do not change) */ body { background: #edabaa; font-family: 'Segoe UI', Arial, sans-serif; margin: 0; } .container, .last-entry-section { background: #354046; color: #f2f6f5; border-radius: 10px; width: 96%; max-width: 560px; margin: 38px auto 0 auto; box-shadow: 0 16px 32px 0 #7a7a7a3d, 0 2px 4px 0 #42424238; padding: 32px 32px 20px 32px; } .last-entry-section { margin-top: 42px; box-shadow: 0 7px 18px 0 #7a7a7a37, 0 1px 3px 0 #42424225; border-radius: 10px; padding: 22px 32px 14px 32px; } h1 { text-align: center; font-size: 2.3rem; font-weight: 400; letter-spacing: 1px; margin-bottom: 32px; } h2 { font-weight: 400; text-align: left; margin-bottom: 14px; font-size: 1.15em; } .saree-form { margin-bottom: 20px; } .row { display: flex; gap: 22px; margin-bottom: 21px; } .column { flex: 1; display: flex; flex-direction: column; } .column-full { flex: 1 1 100%; display: flex; flex-direction: column; width: 100%; } label { font-size: 1em; margin-bottom: 5px; } input[type="text"], input[type="number"], select { padding: 12px 16px; border-radius: 5px; border: none; background: #f2f6f5; color: #354046; font-size: 1.12em; box-sizing: border-box; width: 100%; margin-bottom: 0; transition: outline 0.2s; } input[type="text"]:focus, input[type="number"]:focus, select:focus { outline: 2px solid #24744a; } .actions { display: flex; gap: 20px; margin-top: 10px; } .btn { padding: 12px 38px; background: #318165; color: #fff; font-size: 1.05em; border: none; border-radius: 4px; cursor: pointer; font-family: inherit; margin-right: 6px; } .btn.btn-report { background: #24744a; } .last-entry-section input[type="text"] { width: 100%; background: #f2f6f5; color: #354046; border-radius: 5px; border: none; padding: 12px 16px; font-size: 1.12em; margin-top: 2px; box-sizing: border-box; } /* small error message style (non intrusive) */ .form-errors { color:#ffdddd; background:#5f1e1e; padding:10px; border-radius:6px; margin-bottom:12px; font-weight:600; display:none; } .form-errors ul { margin:0 0 0 18px; padding:0; } .form-success { color:#e6ffeb; background:#1f5c3d; padding:10px; border-radius:6px; margin-bottom:12px; font-weight:700; display:none; } </style> </head> <body> <div class="container"> <h1>Saree by month</h1> <!-- show errors if any (non-blocking) --> <?php if (!empty($errors)): ?> <div class="form-errors" id="formErrors" style="display:block;"> <strong>Fix these:</strong> <ul> <?php foreach ($errors as $e): ?> <li><?= h($e) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if (!empty($flash_saved)): ?> <div class="form-success" id="formSuccess" style="display:block;"> Saved successfully. </div> <?php endif; ?> <form class="saree-form" method="POST" id="sareeForm" novalidate> <!-- CSRF --> <input type="hidden" name="csrf_token" value="<?= h($csrf_token) ?>"> <div class="row"> <div class="column"> <label for="month">Month</label> <select id="month" name="month" required> <option value="">Select month</option> <?php foreach ($months as $m): ?> <option value="<?= h($m) ?>" <?= ($m === $month_val) ? 'selected' : '' ?>><?= h($m) ?></option> <?php endforeach; ?> </select> </div> <div class="column"> <label for="quality">Quality</label> <select id="quality" name="quality" required> <option value="">Select quality</option> <?php if (!empty($qualities)): foreach ($qualities as $q): ?> <option value="<?= h($q['code']) ?>" <?= ($q['code'] === $quality_val) ? 'selected' : '' ?>><?= h($q['quality_name'] . ' (' . $q['code'] . ')') ?></option> <?php endforeach; else: ?> <!-- fallback if qualities empty --> <option value="<?= h($quality_val) ?>" <?= $quality_val ? 'selected' : '' ?>><?= h($quality_val ?: 'No qualities') ?></option> <?php endif; ?> </select> </div> </div> <div class="row"> <div class="column-full"> <label for="khata">Khata</label> <select id="khata" name="khata" required> <option value="">Select khata</option> <?php if (!empty($khatas)): foreach ($khatas as $k): ?> <option value="<?= h($k) ?>" <?= ($k === $khata_val) ? 'selected' : '' ?>><?= h($k) ?></option> <?php endforeach; else: ?> <option value="<?= h($khata_val) ?>" <?= $khata_val ? 'selected' : '' ?>><?= h($khata_val ?: 'No khatas') ?></option> <?php endif; ?> </select> </div> </div> <div class="row"> <div class="column"> <label for="total_saree">Total Saree</label> <input type="number" id="total_saree" name="total_saree" value="<?= h($total_saree_val) ?>" required> </div> <div class="column"> <label for="cut">Cut</label> <input type="number" step="0.01" id="cut" name="cut" value="<?= h($cut_val) ?>" required> </div> </div> <div class="actions"> <button type="submit" class="btn" id="btnSubmit">Submit</button> <button type="button" class="btn btn-report" id="btnReport" onclick="window.location.href='/erp/saree_by_month_report.php'">Report</button> </div> </form> </div> <div class="last-entry-section" aria-live="polite"> <h2>Last entry</h2> <input type="text" value="<?= h($last_entry_text) ?>" disabled> </div> <script> (function(){ // Minimal client-side guard - keep look unchanged const form = document.getElementById('sareeForm'); form.addEventListener('submit', function(e){ // basic client-side validation to prevent silly submits const month = form.querySelector('[name="month"]').value.trim(); const quality = form.querySelector('[name="quality"]').value.trim(); const khata = form.querySelector('[name="khata"]').value.trim(); const total = form.querySelector('[name="total_saree"]').value.trim(); const cut = form.querySelector('[name="cut"]').value.trim(); let clientErrors = []; if (!month) clientErrors.push('Month is required.'); if (!quality) clientErrors.push('Quality is required.'); if (!khata) clientErrors.push('Khata is required.'); if (!total || isNaN(Number(total))) clientErrors.push('Total Saree must be a number.'); if (!cut || isNaN(Number(cut))) clientErrors.push('Cut must be a number.'); if (clientErrors.length) { e.preventDefault(); let errEl = document.getElementById('formErrors'); if (!errEl) { errEl = document.createElement('div'); errEl.id = 'formErrors'; errEl.className = 'form-errors'; form.parentNode.insertBefore(errEl, form); } let html = '<strong>Fix these:</strong><ul>'; clientErrors.forEach(function(it){ html += '<li>'+ it.replace(/</g,'<') +'</li>'; }); html += '</ul>'; errEl.innerHTML = html; errEl.style.display = 'block'; window.scrollTo({ top: form.getBoundingClientRect().top + window.scrollY - 20, behavior: 'smooth' }); } }); // hide success msg after a short time const successEl = document.getElementById('formSuccess'); if (successEl) { setTimeout(function(){ successEl.style.display = 'none'; }, 2200); } })(); </script> </body> </html>