false, 'ERROR' => 'No request body provided']); } $businessId = (int) ($data['BusinessID'] ?? 0); if ($businessId <= 0) { apiAbort(['OK' => false, 'ERROR' => 'BusinessID is required']); } $bizName = trim($data['Name'] ?? ''); $bizPhone = trim($data['Phone'] ?? ''); // Handle tax rate $taxRate = null; if (isset($data['TaxRatePercent']) && is_numeric($data['TaxRatePercent'])) { $taxRate = $data['TaxRatePercent'] / 100; } elseif (isset($data['TaxRate']) && is_numeric($data['TaxRate'])) { $taxRate = (float) $data['TaxRate']; } if ($bizName !== '') { if ($taxRate !== null) { queryTimed(" UPDATE Businesses SET Name = ?, Phone = ?, TaxRate = ? WHERE ID = ? ", [$bizName, $bizPhone, $taxRate, $businessId]); } else { queryTimed(" UPDATE Businesses SET Name = ?, Phone = ? WHERE ID = ? ", [$bizName, $bizPhone, $businessId]); } } // Update or create address $line1 = trim($data['Line1'] ?? ''); $city = trim($data['City'] ?? ''); $state = trim($data['State'] ?? ''); $zip = trim($data['Zip'] ?? ''); // Clean trailing punctuation from city $city = preg_replace('/[,.\s]+$/', '', $city); // Get state ID $stateID = 0; if ($state !== '') { $qState = queryOne("SELECT ID FROM tt_States WHERE Abbreviation = ?", [strtoupper($state)]); if ($qState) $stateID = (int) $qState['ID']; } // Check existing address $qAddr = queryOne(" SELECT ID FROM Addresses WHERE BusinessID = ? AND UserID = 0 AND IsDeleted = 0 LIMIT 1 ", [$businessId]); if ($qAddr) { queryTimed(" UPDATE Addresses SET Line1 = ?, City = ?, StateID = ?, ZIPCode = ? WHERE ID = ? ", [$line1, $city, $stateID, $zip, $qAddr['ID']]); } else { queryTimed(" INSERT INTO Addresses (Line1, City, StateID, ZIPCode, BusinessID, UserID, AddressTypeID, AddedOn) VALUES (?, ?, ?, ?, ?, 0, 2, NOW()) ", [$line1, $city, $stateID, $zip, $businessId]); } jsonResponse(['OK' => true]); } catch (Exception $e) { jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]); }