« Back to History
production_entry_save.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================ API: Save production entries Input JSON: { machine_id: number, entries: [ { date: 'YYYY-MM-DD', k1: employee_id|null, k2: employee_id|null, extras: [{employee_id, meters}] } ] } Writes to: loom_production (assumed) Columns: company_id, machine_id, entry_date, employee_id, meters, is_extra TINYINT, created_by, created_at ============================================================================ */ 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_once __DIR__ . '/../modules/activity/activity_logger.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['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; } $raw = file_get_contents('php://input'); $in = json_decode($raw, true); if(!is_array($in)){ jexit(['ok'=>false,'msg'=>'Invalid JSON'],400); } $machine_id = isset($in['machine_id']) ? (int)$in['machine_id'] : 0; $entries = isset($in['entries']) && is_array($in['entries']) ? $in['entries'] : []; if($machine_id<=0){ jexit(['ok'=>false,'msg'=>'machine_id required'],400); } if(!$entries){ jexit(['ok'=>false,'msg'=>'No entries'],400); } // Ensure table exists (page-local guard) try{ $pdo->exec("CREATE TABLE IF NOT EXISTS loom_production ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, machine_id BIGINT NOT NULL, entry_date DATE NOT NULL, employee_id BIGINT NOT NULL, meters DECIMAL(10,2) NOT NULL DEFAULT 0, is_extra TINYINT(1) NOT NULL DEFAULT 0, created_by BIGINT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, KEY idx_company_machine_date (company_id,machine_id,entry_date), KEY idx_company_employee_date (company_id,employee_id,entry_date) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); }catch(Throwable $e){ jexit(['ok'=>false,'msg'=>'Table create failed'],500); } try{ $pdo->beginTransaction(); $insert_id = 0; $ins = $pdo->prepare("INSERT INTO loom_production (company_id,machine_id,entry_date,employee_id,meters,is_extra,created_by) VALUES (?,?,?,?,?,?,?)"); foreach($entries as $row){ $date = trim((string)($row['date'] ?? '')); if(!$date){ continue; } // Fixed K1 $k1 = isset($row['k1']) && $row['k1']!=='' ? (int)$row['k1'] : 0; if($k1>0){ $ins->execute([$company_id,$machine_id,$date,$k1,0,0,$user_id]); // meters 0 if not provided (UI keeps meters only for extras) $insert_id = (int)$pdo->lastInsertId(); } // Fixed K2 $k2 = isset($row['k2']) && $row['k2']!=='' ? (int)$row['k2'] : 0; if($k2>0){ $ins->execute([$company_id,$machine_id,$date,$k2,0,0,$user_id]); $insert_id = (int)$pdo->lastInsertId(); } // Extras if(!empty($row['extras']) && is_array($row['extras'])){ foreach($row['extras'] as $ex){ $emp = isset($ex['employee_id']) ? (int)$ex['employee_id'] : 0; $mtr = isset($ex['meters']) ? (float)$ex['meters'] : 0.0; if($emp>0 && $mtr>0){ $ins->execute([$company_id,$machine_id,$date,$emp,$mtr,1,$user_id]); $insert_id = (int)$pdo->lastInsertId(); } } } } $pdo->commit(); activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'production', 'action_name' => 'create', 'entity_type' => 'production_entry', 'entity_id' => $insert_id ?? 0, 'remarks' => 'Production entry created' ]); // Compute client reset seed: last submitted max date + 1 $maxDate = null; foreach($entries as $r){ if(!empty($r['date']) && ($maxDate===null || $r['date']>$maxDate)) $maxDate=$r['date']; } $next_date = null; if($maxDate){ $d = new DateTime($maxDate); $d->modify('+1 day'); $next_date = $d->format('Y-m-d'); } jexit(['ok'=>true,'next_date'=>$next_date]); }catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); jexit(['ok'=>false,'msg'=>'Save failed'],500); }