'That purchase order no longer exists.'])); } if ($po['po_status'] !== 'draft') { exit(json_encode(['error' => 'Only a draft can be changed - send it back to draft first.'])); } $line = null; if (intval($_GET['item_id'] ?? 0)) { $line = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM po_items WHERE po_item_id = " . intval($_GET['item_id']) . " AND po_item_po_id = " . intval($po['po_id']))); if (!$line) { exit(json_encode(['error' => 'That line is no longer on the purchase order.'])); } } $product_id = intval($line['po_item_product_id'] ?? 0); $client_id = intval($line['po_item_client_id'] ?? 0); $products = []; $sql = mysqli_query($mysqli, "SELECT product_id, product_name, product_cost, product_vendor_part, product_vendor_id FROM products WHERE product_archived_at IS NULL" . ($product_id ? " OR product_id = $product_id" : '') . " ORDER BY product_vendor_id = " . intval($po['po_vendor_id']) . " DESC, product_name"); while ($row = mysqli_fetch_assoc($sql)) { $products[] = $row; } $clients = []; $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE (client_archived_at IS NULL" . ($client_id ? " OR client_id = $client_id" : '') . ")" . clientNotLeadSql($client_id) . clientScopeSql('client_id') . " ORDER BY client_name"); while ($row = mysqli_fetch_assoc($sql)) { $clients[] = $row; } $form_id = 'poLine' . ($line ? intval($line['po_item_id']) : 'New'); ob_start(); ?>