payfrit-api/api/businesses/update.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

85 lines
2.5 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Update Business Info
* POST: { BusinessID, Name, Phone, TaxRatePercent?, TaxRate?, Line1, City, State, Zip }
*/
try {
$data = readJsonBody();
if (empty($data)) {
apiAbort(['OK' => 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()]);
}