« Back to History
production_last_date.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================ API: GET last recorded date for machine → returns next_date = last+1 Table assumed: loom_production (company_id, machine_id, entry_date, employee_id, meters, is_extra, created_by, created_at) Adjust table name/column if different. ============================================================================ */ header('Content-Type: application/json; charset=utf-8'); header('X-Frame-Options: SAMEORIGIN'); error_reporting(E_ALL); ini_set('display_errors','0'); if (session_status() !== PHP_SESSION_ACTIVE) session_start(); require __DIR__ . '/../modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/../core/db.php'; } function jexit($arr,$code=200){ http_response_code($code); echo json_encode($arr,JSON_UNESCAPED_UNICODE); exit; } $machine_id = isset($_GET['machine_id']) ? (int)$_GET['machine_id'] : 0; if($machine_id<=0){ jexit(['ok'=>false,'msg'=>'machine_id required'],400); } try{ // Try table loom_production $stmt = $pdo->prepare("SELECT MAX(entry_date) AS last_date FROM loom_production WHERE company_id=? AND machine_id=?"); $stmt->execute([$company_id,$machine_id]); $row = $stmt->fetch(PDO::FETCH_ASSOC); $last = $row && $row['last_date'] ? $row['last_date'] : null; $next = null; if($last){ $d = new DateTime($last); $d->modify('+1 day'); $next = $d->format('Y-m-d'); } else { // No entries yet → today $next = (new DateTime('today'))->format('Y-m-d'); } jexit(['ok'=>true,'next_date'=>$next]); }catch(Throwable $e){ jexit(['ok'=>false,'msg'=>'DB error'],500); }