35239-vm/company_setup.php
Flatlogic Bot ccc924ba43 ver1
2025-10-26 14:24:09 +00:00

97 lines
4.0 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/header.php';
$pdo = db();
$user_id = $_SESSION['user_id'];
$company = null;
$errors = [];
$success_message = '';
// Fetch existing company data
$stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ?");
$stmt->execute([$user_id]);
$company = $stmt->fetch();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$address = trim($_POST['address'] ?? '');
$currency = trim($_POST['currency'] ?? 'SGD');
$gst_number = trim($_POST['gst_number'] ?? '');
if (empty($name)) {
$errors[] = 'Company name is required.';
}
if (empty($errors)) {
if ($company) {
// Update existing record
$stmt = $pdo->prepare("UPDATE companies SET name = ?, address = ?, currency = ?, gst_number = ? WHERE user_id = ?");
if ($stmt->execute([$name, $address, $currency, $gst_number, $user_id])) {
$success_message = 'Company settings updated successfully!';
} else {
$errors[] = 'Failed to update settings. Please try again.';
}
} else {
// Insert new record
$stmt = $pdo->prepare("INSERT INTO companies (user_id, name, address, currency, gst_number) VALUES (?, ?, ?, ?, ?)");
if ($stmt->execute([$user_id, $name, $address, $currency, $gst_number])) {
$success_message = 'Company settings saved successfully!';
} else {
$errors[] = 'Failed to save settings. Please try again.';
}
}
// Refresh company data after update/insert
$stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ?");
$stmt->execute([$user_id]);
$company = $stmt->fetch();
}
}
require_once __DIR__ . '/includes/sidebar.php';
?>
<h1 class="h2">Company Settings</h1>
<p>Use this form to set up or update your company's details. This information will be used on invoices and other official documents.</p>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (!empty($success_message)): ?>
<div class="alert alert-success">
<p class="mb-0"><?php echo htmlspecialchars($success_message); ?></p>
</div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<form action="/company_setup.php" method="POST">
<div class="mb-3">
<label for="companyName" class="form-label">Company Name</label>
<input type="text" class="form-control" id="companyName" name="name" value="<?php echo htmlspecialchars($company['name'] ?? ''); ?>" required>
</div>
<div class="mb-3">
<label for="companyAddress" class="form-label">Address</label>
<textarea class="form-control" id="companyAddress" name="address" rows="3"><?php echo htmlspecialchars($company['address'] ?? ''); ?></textarea>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="companyCurrency" class="form-label">Base Currency</label>
<input type="text" class="form-control" id="companyCurrency" name="currency" value="<?php echo htmlspecialchars($company['currency'] ?? 'SGD'); ?>" required>
</div>
<div class="col-md-6 mb-3">
<label for="companyGst" class="form-label">GST Number (if applicable)</label>
<input type="text" class="form-control" id="companyGst" name="gst_number" value="<?php echo htmlspecialchars($company['gst_number'] ?? ''); ?>">
</div>
</div>
<button type="submit" class="btn btn-primary">Save Settings</button>
</form>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>