« Back to History
update_kasab_data.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_stock_entry'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; header('Content-Type: application/json'); try { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception("Invalid request"); } if (!isset($_POST['entry_id'])) { throw new Exception("Missing ID"); } $id = (int)$_POST['entry_id']; $entry_date = $_POST['entry_date'] ?? ''; $color = trim($_POST['color'] ?? ''); $rolls = $_POST['rolls'] ?? []; $weights = $_POST['weights'] ?? []; if (!$id || !$entry_date || !$color) { throw new Exception("Required fields missing"); } if (count($rolls) !== count($weights)) { throw new Exception("Data mismatch"); } $cleanRolls = []; $cleanWeights = []; foreach ($rolls as $i => $r) { $r = (int)$r; $w = (float)($weights[$i] ?? 0); if ($r <= 0 || $w <= 0) { throw new Exception("Invalid rolls or weight"); } $cleanRolls[] = $r; $cleanWeights[] = $w; } $primaryRoll = (int)($cleanRolls[0] ?? 0); foreach ($cleanRolls as $savedRoll) { if ((int)$savedRoll !== $primaryRoll) { throw new Exception("All rolls must be same for one entry"); } } $rolls_json = json_encode(['rolls' => $primaryRoll], JSON_UNESCAPED_UNICODE); $weight_json = json_encode(['weights' => array_values($cleanWeights)], JSON_UNESCAPED_UNICODE); $stmt = $pdo->prepare(" UPDATE kasab_entry SET entry_date = ?, color = ?, rolls_json = ?, weight_json = ? WHERE id = ? AND company_id = ? "); $stmt->execute([ $entry_date, $color, $rolls_json, $weight_json, $id, $company_id ]); echo json_encode([ 'success' => true ]); } catch (Exception $e) { echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); }