« Back to History
mesr_final_yarn_conume_report.php
|
20260723_000646.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'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; /* ========================= MONTH HANDLING (FIXED) ========================= */ $input_month = $_GET['month'] ?? date('Y-m'); $selected_month = date('Y-m-01', strtotime($input_month)); $prev_month = date('Y-m-01', strtotime($selected_month . ' -1 month')); $start_date = date('Y-m-01', strtotime($selected_month)); $end_date = date('Y-m-t', strtotime($selected_month)); /* ========================= 1. MASTER MAPPING ========================= */ $specific_map = []; $generic_map = []; $master_stmt = $pdo->prepare(" SELECT technical_name, denier, color FROM yarn_master WHERE company_id = ? AND is_active = 1 "); $master_stmt->execute([$company_id]); $all_master_rows = $master_stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($all_master_rows as $m) { $d = strtoupper(trim($m['denier'])); $c = strtoupper(trim($m['color'])); $tech = trim($m['technical_name']); if ($c !== '') { $specific_map[$d . '|' . $c] = $tech; } else { $generic_map[$d] = $tech; } } /* ========================= 2. OPENING & CLOSING ========================= */ $stmt = $pdo->prepare(" SELECT technical_name, total_weight FROM yarn_monthly_closing WHERE month = ? AND company_id = ? "); $stmt->execute([$prev_month, $company_id]); $opening_stock = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $stmt->execute([$selected_month, $company_id]); $closing_stock = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); /* ========================= 3. INWARD PROCESSING ========================= */ $inward_summary = []; $orphan_inwards = []; $tables = ['yarn_in', 'tfo_in', 'zari_in']; foreach ($tables as $table) { $st = $pdo->prepare(" SELECT denier, color, SUM(weight) as total_weight FROM `$table` WHERE company_id = :cid AND txn_date BETWEEN :start AND :end GROUP BY denier, color "); $st->execute([ ':cid' => $company_id, ':start' => $start_date, ':end' => $end_date ]); while ($row = $st->fetch(PDO::FETCH_ASSOC)) { $d = strtoupper(trim($row['denier'])); $c = strtoupper(trim($row['color'])); $weight = (float)$row['total_weight']; $target_tech_name = $specific_map[$d . '|' . $c] ?? $generic_map[$d] ?? null; if ($target_tech_name) { $inward_summary[$target_tech_name] = ($inward_summary[$target_tech_name] ?? 0) + $weight; } else { $orphan_inwards["$d | $c"] = ($orphan_inwards["$d | $c"] ?? 0) + $weight; } } } require_once __DIR__ . '/partials/header.php'; ?> <style> .print-only { display:none; } @media print { @page { size:auto; margin:10mm; } .d-print-none{ display:none !important; } .print-only{ display:block !important; } .table{ width:100% !important; border-collapse:collapse !important; border:1px solid #000 !important; } .table th,.table td{ border:1px solid #000 !important; padding:5px !important; color:#000 !important; } .table-dark{ background:#f2f2f2 !important; color:#000 !important; } .text-success,.text-danger,.text-primary,.text-info{ color:#000 !important; } tfoot tr{ background:#f2f2f2 !important; font-weight:bold !important; } } </style> <div class="container-fluid py-4"> <div class="print-only text-center mb-4"> <h2 class="mb-1">Final Yarn Consumption Report</h2> <h4 class="text-secondary">Month: <?= date('F Y', strtotime($selected_month)) ?></h4> <hr style="border:1px solid #000;"> </div> <div class="card shadow-sm mb-4 border-0 d-print-none"> <div class="card-body bg-light d-flex justify-content-between align-items-center"> <h5 class="fw-bold mb-0">Yarn Consumption Report</h5> <div class="d-flex gap-2"> <form method="GET" class="d-flex gap-2"> <input type="month" name="month" class="form-control" value="<?= date('Y-m', strtotime($selected_month)) ?>"> <button type="submit" class="btn btn-primary"> Generate </button> </form> <button onclick="window.print()" class="btn btn-outline-dark"> Print Report </button> </div> </div> </div> <div class="card shadow-sm border-0"> <div class="table-responsive"> <table class="table table-bordered align-middle mb-0"> <thead class="table-light text-center small fw-bold"> <tr> <th class="text-start py-3">TECHNICAL NAME</th> <th style="width:15%">OPENING (A)</th> <th style="width:15%">INWARD (B)</th> <th style="width:15%">CLOSING (C)</th> <th style="width:15%" class="table-secondary">NET CONSUMPTION</th> </tr> </thead> <tbody> <?php $gt = ['op'=>0,'in'=>0,'cl'=>0,'con'=>0]; $unique_tech_names = array_unique(array_column($all_master_rows,'technical_name')); sort($unique_tech_names); foreach ($unique_tech_names as $name): $op = (float)($opening_stock[$name] ?? 0); $in = (float)($inward_summary[$name] ?? 0); $cl = (float)($closing_stock[$name] ?? 0); $con = ($op + $in) - $cl; if ($op==0 && $in==0 && $cl==0) continue; $gt['op'] += $op; $gt['in'] += $in; $gt['cl'] += $cl; $gt['con'] += $con; ?> <tr> <td class="fw-bold px-3 small text-uppercase"> <?= htmlspecialchars($name) ?> </td> <td class="text-end px-3"><?= number_format($op,3) ?></td> <td class="text-end px-3 text-success font-monospace"> + <?= number_format($in,3) ?> </td> <td class="text-end px-3 text-danger font-monospace"> - <?= number_format($cl,3) ?> </td> <td class="text-end px-3 fw-bold bg-light"> <?= number_format($con,3) ?> </td> </tr> <?php endforeach; ?> </tbody> <tfoot class="bg-light fw-bold text-end"> <tr style="border-top:2px solid #000;"> <td class="text-center py-2">GRAND TOTALS</td> <td class="px-3"><?= number_format($gt['op'],3) ?></td> <td class="px-3 text-success"><?= number_format($gt['in'],3) ?></td> <td class="px-3 text-danger"><?= number_format($gt['cl'],3) ?></td> <td class="px-3 text-primary"><?= number_format($gt['con'],3) ?></td> </tr> </tfoot> </table> </div> </div> <?php if (!empty($orphan_inwards)): ?> <div class="mt-4 d-print-none"> <div class="alert alert-warning border-0 shadow-sm small"> <h6 class="fw-bold"> ⚠️ Check Master: These yarns are inward but not mapped </h6> <div class="d-flex flex-wrap gap-3"> <?php foreach ($orphan_inwards as $k => $v): ?> <span class="badge bg-white text-dark border p-2"> <?= htmlspecialchars($k) ?> : <?= number_format($v,3) ?> </span> <?php endforeach; ?> </div> </div> </div> <?php endif; ?> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>