« Back to History
production_meter_calculator.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ========================================================================= FINAL WORKING VERSION Reason: - production_entry.machine_id ≠ machines.id - So calculation + dropdown BOTH production_entry based ========================================================================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('production_meter_calculator'); $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ----------------------------------------------------------- MACHINE LIST (FROM PRODUCTION DATA ITSELF) ----------------------------------------------------------- */ $mStmt = $pdo->prepare(" SELECT DISTINCT machine_id FROM production_entry WHERE company_id = :cid ORDER BY machine_id "); $mStmt->execute([':cid'=>$company_id]); $machines = $mStmt->fetchAll(PDO::FETCH_ASSOC); /* ----------------------------------------------------------- INPUTS ----------------------------------------------------------- */ $from_date = $_GET['from_date'] ?? ''; $to_date = $_GET['to_date'] ?? ''; $machine_id = $_GET['machine_id'] ?? ''; $total_meter = null; /* ----------------------------------------------------------- CALCULATION (THIS WILL MATCH YOUR SCREENSHOT DATA) ----------------------------------------------------------- */ if ($from_date && $to_date && $machine_id !== '') { $stmt = $pdo->prepare(" SELECT COALESCE(SUM(meter_total),0) FROM production_entry WHERE company_id = :cid AND machine_id = :mid AND entry_date BETWEEN :from AND :to "); $stmt->execute([ ':cid' => $company_id, ':mid' => $machine_id, ':from' => $from_date, ':to' => $to_date ]); $total_meter = (float)$stmt->fetchColumn(); } ?> <?php require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap"> <div class="card"> <h2>Production Meter Calculator</h2> <form method="get" class="form-card"> <div class="form-cols-4"> <div class="form-group"> <label>From Date</label> <input type="date" name="from_date" value="<?=h($from_date)?>" required> </div> <div class="form-group"> <label>To Date</label> <input type="date" name="to_date" value="<?=h($to_date)?>" required> </div> <div class="form-group"> <label>Machine (Legacy ID)</label> <select name="machine_id" required> <option value="">-- Select --</option> <?php foreach($machines as $m): ?> <option value="<?=$m['machine_id']?>" <?=$machine_id==$m['machine_id']?'selected':''?>> Machine <?=$m['machine_id']?> </option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label> </label> <button class="btn btn-primary">Calculate</button> </div> </div> </form> </div> <?php if($total_meter !== null): ?> <div class="card" style="margin-top:15px;"> <h3>Result</h3> <strong>Total Production Meter:</strong> <?=number_format($total_meter,2)?> </div> <?php endif; ?> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>