« Back to History
mesr_yarn_stock.php
|
20261119_140743.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('yarn_entry'); // Access check $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; if (!$pdo) exit('DB Error'); function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ---------- Company Name ---------- */ $cmp = $pdo->prepare("SELECT name FROM companies WHERE id=?"); $cmp->execute([$company_id]); $company_name = $cmp->fetchColumn() ?: 'Company'; $current_date = date('d-m-Y'); /* ---------- Table Resolver ---------- */ function tables_for($type) { switch (strtolower($type)) { case 'tfo': return ['tfo_in', 'tfo_out']; case 'tpm': return ['tpm_in', 'tpm_out']; case 'zari': return ['zari_in', 'zari_out']; case 'kasab': return ['kasab_in', 'kasab_out']; default: return ['yarn_in', 'yarn_out']; } } /* ---------- Stock Builder Logic ---------- */ function build_stock(PDO $pdo, $cid, $type, $hide_zero) { [$in_t, $out_t] = tables_for($type); // Check if table exists $check = $pdo->prepare("SHOW TABLES LIKE ?"); $check->execute([$in_t]); if (!$check->rowCount()) return ['rows' => [], 'tot_boxes' => 0, 'tot_weight' => 0]; // IN Summary $sql_in = "SELECT yarn_type, company as supplier_name, denier, color, SUM(boxes) as boxes, SUM(weight) as weight FROM `$in_t` WHERE company_id = :cid GROUP BY yarn_type, supplier_name, denier, color"; $st = $pdo->prepare($sql_in); $st->execute([':cid' => $cid]); $in_data = $st->fetchAll(PDO::FETCH_ASSOC); // OUT Summary $sql_out = str_replace($in_t, $out_t, $sql_in); $st2 = $pdo->prepare($sql_out); $st2->execute([':cid' => $cid]); $out_data = $st2->fetchAll(PDO::FETCH_ASSOC); $omap = []; foreach ($out_data as $r) { $k = strtoupper(trim($r['yarn_type'])) . '|' . strtoupper(trim($r['supplier_name'])) . '|' . strtoupper(trim($r['denier'])) . '|' . strtoupper(trim($r['color'])); $omap[$k] = ['boxes' => (float)$r['boxes'], 'weight' => (float)$r['weight']]; } $final_rows = []; $tb = 0; $tw = 0; foreach ($in_data as $r) { $k = strtoupper(trim($r['yarn_type'])) . '|' . strtoupper(trim($r['supplier_name'])) . '|' . strtoupper(trim($r['denier'])) . '|' . strtoupper(trim($r['color'])); $out_b = $omap[$k]['boxes'] ?? 0; $out_w = $omap[$k]['weight'] ?? 0; $cb = (float)$r['boxes'] - $out_b; $cw = (float)$r['weight'] - $out_w; if ($hide_zero && $cb <= 0 && $cw <= 0) continue; $final_rows[] = [ 'yarn_type' => $r['yarn_type'], 'supplier_name' => $r['supplier_name'], 'denier' => $r['denier'], 'color' => $r['color'], 'boxes' => $cb, 'weight' => $cw ]; $tb += $cb; $tw += $cw; } return ['rows' => $final_rows, 'tot_boxes' => $tb, 'tot_weight' => $tw]; } /* ========================================================= PUSH TO STOCK ACTION (Delete-then-Insert Pattern) ========================================================= */ $message = ""; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['push_stock'])) { $target_month = $_POST['target_month'] . "-01"; // Format: YYYY-MM-01 $stock_types = ['Yarn', 'TFO', 'TPM', 'Zari', 'Kasab']; try { $pdo->beginTransaction(); // 1. Delete existing records for this company and month (Delete-then-Insert) $del = $pdo->prepare("DELETE FROM mesr_yarn_in_yarn_stock WHERE company_id = ? AND month = ?"); $del->execute([$company_id, $target_month]); // 2. Prepare Insert $ins = $pdo->prepare("INSERT INTO mesr_yarn_in_yarn_stock (company_id, month, location_id, stock_type, yarn_type, supplier_name, denier, color, total_boxes, total_weight, net_weight, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'OK', NOW())"); foreach ($stock_types as $stype) { $data = build_stock($pdo, $company_id, $stype, true); // Always push non-zero foreach ($data['rows'] as $row) { $ins->execute([ $company_id, $target_month, 16, // Default location_id as per your screenshot $stype, $row['yarn_type'], $row['supplier_name'], $row['denier'], $row['color'], $row['boxes'], $row['weight'], $row['weight'] ]); } } $pdo->commit(); $message = "<div class='alert alert-success'>Stock successfully pushed for $target_month!</div>"; } catch (Exception $e) { $pdo->rollBack(); $message = "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>"; } } /* ---------- Filters ---------- */ $type = $_GET['stock_type'] ?? 'All'; // Default is now WITHOUT zero (hide_zero = true) $hide_zero = (($_GET['show_zero'] ?? 'without_zero') === 'without_zero'); $types = ($type === 'All') ? ['Yarn', 'TFO', 'TPM', 'Zari', 'Kasab'] : [$type]; require_once __DIR__ . '/partials/header.php'; // Bootstrap included here ?> <div class="container-fluid py-4"> <?= $message ?> <div class="row align-items-center mb-4"> <div class="col-md-6"> <h4 class="fw-bold m-0"><?= h($company_name) ?> - Master Stock Management</h4> </div> <div class="col-md-6 text-end"> <form method="post" class="d-inline-flex gap-2 align-items-center bg-white p-2 border rounded shadow-sm"> <label class="small fw-bold">Target Month:</label> <input type="month" name="target_month" class="form-control form-control-sm" value="<?= date('Y-m') ?>" required> <button type="submit" name="push_stock" class="btn btn-dark btn-sm" onclick="return confirm('Purana data delete ho jayega. Continue?')"> Push to Master Stock Table </button> </form> </div> </div> <div class="card shadow-sm mb-4"> <div class="card-body"> <form method="get" class="row g-2"> <div class="col-md-3"> <select name="stock_type" class="form-select form-select-sm"> <?php foreach (['All', 'Yarn', 'TFO', 'TPM', 'Zari', 'Kasab'] as $s): ?> <option <?= $type == $s ? 'selected' : '' ?>><?= $s ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <select name="show_zero" class="form-select form-select-sm"> <option value="without_zero" <?= $hide_zero ? 'selected' : '' ?>>Hide Zero Stock</option> <option value="with_zero" <?= !$hide_zero ? 'selected' : '' ?>>Show All (Incl. Zero)</option> </select> </div> <div class="col-md-2"> <button class="btn btn-primary btn-sm w-100">Filter View</button> </div> </form> </div> </div> <?php $grand_boxes = 0; $grand_weight = 0; foreach ($types as $t): $res = build_stock($pdo, $company_id, $t, $hide_zero); if (empty($res['rows'])) continue; $grand_boxes += $res['tot_boxes']; $grand_weight += $res['tot_weight']; ?> <div class="card mb-4 shadow-sm"> <div class="card-header bg-primary text-white d-flex justify-content-between"> <span><?= $t ?> Stock</span> <span>Total Wt: <?= number_format($res['tot_weight'], 2) ?></span> </div> <div class="table-responsive"> <table class="table table-bordered table-hover mb-0"> <thead class="table-light"> <tr> <th>Yarn Type</th> <th>Supplier</th> <th>Denier</th> <th>Color</th> <th class="text-end">Boxes</th> <th class="text-end">Weight</th> </tr> </thead> <tbody> <?php foreach ($res['rows'] as $r): ?> <tr> <td><?= h($r['yarn_type']) ?></td> <td><?= h($r['supplier_name']) ?></td> <td><?= h($r['denier']) ?></td> <td><?= h($r['color']) ?></td> <td class="text-end"><?= number_format($r['boxes'], 2) ?></td> <td class="text-end fw-bold"><?= number_format($r['weight'], 2) ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <?php endforeach; ?> <div class="alert alert-info shadow-sm text-center"> <h5 class="m-0">Grand Total Boxes: <b><?= number_format($grand_boxes, 2) ?></b> | Grand Total Weight: <b><?= number_format($grand_weight, 2) ?></b></h5> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>