« Back to History
saree_stock.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= File: /erp/saree_stock.php Title: Saree Stock Summary (direct from saree_in / saree_out) ============================================================================= */ header('X-Frame-Options: SAMEORIGIN'); 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'] ?? 0); $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; } function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function table_exists(PDO $pdo, string $t): bool { try { $pdo->query("SELECT 1 FROM `$t` LIMIT 1"); return true; } catch(Throwable $e){ return false; } } /* ---------- Company name ---------- */ $company_name = ''; try { $st = $pdo->prepare("SELECT company_name FROM company_master WHERE id = :cid LIMIT 1"); $st->execute([':cid'=>$company_id]); $company_name = trim((string)($st->fetchColumn() ?: '')); } catch(Throwable $e){} if ($company_name === '') $company_name = "Company ID: $company_id"; /* ---------- Build stock rows ---------- */ $rows = []; $err = ''; if (!table_exists($pdo, 'saree_in') && !table_exists($pdo, 'saree_out')) { $err = "Neither saree_in nor saree_out table exists."; } else { try { $sql = " SELECT name, COALESCE(SUM(in_total),0) AS total_in, COALESCE(SUM(out_total),0) AS total_out, (COALESCE(SUM(in_total),0) - COALESCE(SUM(out_total),0)) AS current_stock FROM ( SELECT name, company_id, total_saree AS in_total, 0 AS out_total FROM saree_in WHERE company_id = :cid UNION ALL SELECT name, company_id, 0 AS in_total, total_saree AS out_total FROM saree_out WHERE company_id = :cid ) AS t GROUP BY name ORDER BY name COLLATE utf8mb4_general_ci; "; $st = $pdo->prepare($sql); $st->execute([':cid' => $company_id]); $rows = $st->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e){ $err = $e->getMessage(); } } /* ---------- Totals ---------- */ $grand_in = 0; $grand_out = 0; $grand_stock = 0; foreach($rows as $r){ $grand_in += (int)$r['total_in']; $grand_out += (int)$r['total_out']; $grand_stock += (int)$r['current_stock']; } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Saree Stock Summary (<?=h($company_name)?>)</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/erp/public/assets/css/main.css?v=20250917"> <style> body{font-family:system-ui;background:#f6fff9;color:#222;margin:0} .wrap{max-width:1100px;margin:28px auto;padding:0 16px} .card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:18px;margin:16px 0;box-shadow:0 10px 24px rgba(0,0,0,.06)} .table{width:100%;border-collapse:collapse;margin-top:12px} .table th, .table td{padding:10px 12px;border:1px solid #e7eaee;text-align:left} .table th{background:#f8fafb;color:#334155} .row-flex{display:flex;gap:8px;align-items:center} .badge{display:inline-block;padding:6px 10px;border-radius:8px;font-weight:600} .badge.green{background:#e6fcf5;color:#086b3b} .badge.red{background:#fff5f5;color:#9b1111} .actions a.btn{margin-right:8px} .summary{display:flex;gap:18px;flex-wrap:wrap;margin-top:8px} .summary .box{background:#f8fdf9;padding:8px 12px;border-radius:8px;border:1px solid #e6f6ea} .small-muted{font-size:13px;color:#6b7280} .clean-table { width:100%; border-collapse:collapse; margin-top: 10px; } .clean-table th, .clean-table td { border:1px solid #333; padding:6px 8px; text-align:left; font-size:12px; } .clean-table th { background:#eee; font-weight:700; } </style> </head> <body> <?php if (is_file(__DIR__.'/partials/header.php')) include __DIR__.'/partials/header.php'; ?> <div class="wrap"> <div class="card row-flex" style="justify-content:space-between;align-items:center"> <div> <h1 style="margin:0;color:#2f9e44">Saree Stock Summary <span style="color:#555">(<?=h($company_name)?>)</span></h1> <div class="small-muted">Current Stock = Total In − Total Out</div> </div> <div class="actions"> <a class="btn" href="saree_in_add.php">+ Saree In Entry</a> <a class="btn" href="saree_out_add.php">+ Saree Out Entry</a> <a class="btn" href="saree_in_list.php">In List</a> <a class="btn" href="saree_out_list.php">Out List</a> <button class="btn" id="printCleanBtn" type="button">Print Clean</button> <button class="btn" id="downloadPdfBtn" type="button">Download PDF</button> </div> </div> <div class="card" id="stock_area"> <?php if ($err): ?> <div style="color:#9b1111"><?=h($err)?></div> <?php elseif (empty($rows)): ?> <div>No saree names found in saree_in or saree_out for this company.</div> <?php else: ?> <div class="summary"> <div class="box"><strong>Total In:</strong> <?=h($grand_in)?></div> <div class="box"><strong>Total Out:</strong> <?=h($grand_out)?></div> <div class="box"><strong>Net Stock:</strong> <?=h($grand_stock)?></div> </div> <table class="table" style="margin-top:14px" id="stock_table"> <thead> <tr> <th>#</th><th>Saree Name</th><th>Total In</th><th>Total Out</th><th>Current Stock</th> </tr> </thead> <tbody> <?php $i=1; foreach($rows as $r): ?> <tr> <td><?= $i++ ?></td> <td><?= h($r['name']) ?></td> <td><?= h((int)$r['total_in']) ?></td> <td><?= h((int)$r['total_out']) ?></td> <td> <?php $cs=(int)$r['current_stock']; ?> <?php if($cs>=0): ?><span class="badge green"><?=h($cs)?></span> <?php else:?><span class="badge red"><?=h($cs)?></span><?php endif; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </div> <?php if (is_file(__DIR__.'/partials/footer.php')) include __DIR__.'/partials/footer.php'; ?> <script> function createCleanHtml() { var company = <?= json_encode($company_name) ?>; var title = 'Saree Stock Summary (' + company + ')'; var date = new Date().toLocaleString(); var area = document.getElementById('stock_area'); var table = document.getElementById('stock_table'); var html = '<!doctype html><html><head><meta charset="utf-8"><title>'+title+'</title>'+ '<style>body{font-family:Arial;padding:10px}h2{margin-bottom:4px}.meta{font-size:12px;margin-bottom:6px}table{width:100%;border-collapse:collapse}th,td{border:1px solid #333;padding:5px;font-size:12px;text-align:left}th{background:#eee}</style></head><body>'+ '<h2>'+title+'</h2><div class="meta">Generated '+date+'</div>'; if(table){ html+=table.outerHTML; } else { html+='<p>No data</p>'; } html+='</body></html>'; return html; } function openPrintWindow(html, autoPrint){ var w=window.open('','_blank');w.document.write(html);w.document.close(); if(autoPrint) w.onload=function(){w.print();}; } document.getElementById('printCleanBtn').onclick=function(){openPrintWindow(createCleanHtml(),true);}; document.getElementById('downloadPdfBtn').onclick=function(){openPrintWindow(createCleanHtml(),false);}; </script> </body> </html>