« Back to History
kasab_stock_report.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('kasab_stock_report'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; require_once __DIR__ . '/partials/header.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } $from_date = $_GET['from_date'] ?? ''; $to_date = $_GET['to_date'] ?? ''; $report = []; /* ===== ROLL MASTER (tare weight) ===== */ $rollMaster = [ 'Big' => ['weight' => 0], 'Small' => ['weight' => 0], ]; $rm = $pdo->prepare(" SELECT roll, roll_weight FROM roll_detail WHERE company_id = :cid "); $rm->execute([':cid'=>$company_id]); while($r = $rm->fetch(PDO::FETCH_ASSOC)){ if(stripos($r['roll'],'big') !== false){ $rollMaster['Big']['weight'] = (float)$r['roll_weight']; } if(stripos($r['roll'],'small') !== false){ $rollMaster['Small']['weight'] = (float)$r['roll_weight']; } } /* ===== REPORT ===== */ if ($from_date && $to_date) { $sql = " SELECT color, rolls_json, weight_json, roll_type FROM kasab_entry WHERE company_id = :cid AND entry_date >= :from AND entry_date < DATE_ADD(:to, INTERVAL 1 DAY) "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid'=>$company_id, ':from'=>$from_date, ':to'=>$to_date ]); while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $color = $row['color'] ?: 'N/A'; if(!isset($report[$color])){ $report[$color] = [ 'entries'=>0, 'bori'=>0, 'gross'=>0, 'big_rolls'=>0, 'small_rolls'=>0, ]; } $report[$color]['entries']++; /* ===== GROSS WEIGHT ===== */ $weights = json_decode($row['weight_json'], true)['weights'] ?? []; $report[$color]['gross'] += array_sum($weights); $bori_count = count($weights); $report[$color]['bori'] += $bori_count; /* ===== ROLLS ===== */ $rolls = (int)(json_decode($row['rolls_json'], true)['rolls'] ?? 0); if($row['roll_type'] === 'Big'){ $report[$color]['big_rolls'] += $rolls; } else { $report[$color]['small_rolls'] += $rolls; } } } ?> <div class="container-fluid mt-3"> <div class="card"> <div class="card-header"> <h5>Kasab Stock Report (Net Weight Corrected)</h5> </div> <div class="card-body"> <form method="get" class="row g-3 mb-4"> <div class="col-md-3"> <label>From</label> <input type="date" name="from_date" value="<?=h($from_date)?>" class="form-control" required> </div> <div class="col-md-3"> <label>To</label> <input type="date" name="to_date" value="<?=h($to_date)?>" class="form-control" required> </div> <div class="col-md-2 align-self-end"> <button class="btn btn-primary w-100">Generate</button> </div> </form> <?php if($from_date && $to_date): ?> <div class="table-responsive"> <table class="table table-bordered table-striped"> <thead> <tr> <th>#</th> <th>Color</th> <th>Entries</th> <th>Boriyan</th> <th>Gross Weight</th> <th>Roll Weight</th> <th>Net Weight</th> <th>Avg / Roll</th> </tr> </thead> <tbody> <?php $i=1; $g_entries=0; $g_bori=0; $g_gross=0; $g_roll_wt=0; $g_net=0; foreach($report as $color=>$d){ $roll_wt = ($d['big_rolls'] * $rollMaster['Big']['weight']) + ($d['small_rolls'] * $rollMaster['Small']['weight']); $net = $d['gross'] - $roll_wt; $total_rolls = $d['big_rolls'] + $d['small_rolls']; $avg = $total_rolls>0 ? $net/$total_rolls : 0; $g_entries += $d['entries']; $g_bori += $d['bori']; $g_gross += $d['gross']; $g_roll_wt += $roll_wt; $g_net += $net; ?> <tr> <td><?= $i++ ?></td> <td><?= h($color) ?></td> <td><?= $d['entries'] ?></td> <td><?= $d['bori'] ?></td> <td><?= number_format($d['gross'],2) ?></td> <td><?= number_format($roll_wt,2) ?></td> <td><b><?= number_format($net,2) ?></b></td> <td><?= number_format($avg,2) ?></td> </tr> <?php } ?> <tr class="fw-bold"> <td colspan="2">Grand Total</td> <td><?= $g_entries ?></td> <td><?= $g_bori ?></td> <td><?= number_format($g_gross,2) ?></td> <td><?= number_format($g_roll_wt,2) ?></td> <td><?= number_format($g_net,2) ?></td> <td> <?= ($g_net>0 && ($g_bori>0)) ? number_format($g_net/($g_bori),2) : 0 ?> </td> </tr> </tbody> </table> </div> <?php endif; ?> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>