35050-vm/add_deal.php
Flatlogic Bot b4397b355f Sfepro v1
2025-10-19 03:26:15 +00:00

87 lines
3.8 KiB
PHP

<?php
require_once 'db/config.php';
$error_message = '';
// Fetch contacts for the dropdown
$contacts = db()->query("SELECT id, name FROM contacts ORDER BY name ASC")->fetchAll();
$deal_stages = ['Lead', 'Qualified', 'Proposal', 'Won', 'Lost'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = trim($_POST['title']);
$value = trim($_POST['value']);
$stage = trim($_POST['stage']);
$close_date = trim($_POST['close_date']);
$contact_id = trim($_POST['contact_id']);
if (empty($title) || empty($value) || empty($stage) || empty($contact_id)) {
$error_message = "Title, Value, Stage, and Contact are required fields.";
} elseif (!is_numeric($value)) {
$error_message = "Deal value must be a number.";
} else {
try {
$stmt = db()->prepare("INSERT INTO deals (title, value, stage, close_date, contact_id) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$title, $value, $stage, $close_date, $contact_id]);
header("Location: index.php?success_deal=1");
exit();
} catch (PDOException $e) {
$error_message = "Error: " . $e->getMessage();
}
}
}
include 'header.php';
?>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h4 class="mb-0">Add New Deal</h4>
</div>
<div class="card-body">
<?php if ($error_message): ?>
<div class="alert alert-danger" role="alert">
<?php echo $error_message; ?>
</div>
<?php endif; ?>
<form action="add_deal.php" method="post">
<div class="mb-3">
<label for="title" class="form-label">Title <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="value" class="form-label">Value ($) <span class="text-danger">*</span></label>
<input type="number" step="0.01" class="form-control" id="value" name="value" required>
</div>
<div class="mb-3">
<label for="stage" class="form-label">Stage <span class="text-danger">*</span></label>
<select class="form-select" id="stage" name="stage" required>
<?php foreach($deal_stages as $stage): ?>
<option value="<?php echo $stage; ?>"><?php echo $stage; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="close_date" class="form-label">Expected Close Date</label>
<input type="date" class="form-control" id="close_date" name="close_date">
</div>
<div class="mb-3">
<label for="contact_id" class="form-label">Contact <span class="text-danger">*</span></label>
<select class="form-select" id="contact_id" name="contact_id" required>
<option value="">Select a contact</option>
<?php foreach($contacts as $contact): ?>
<option value="<?php echo $contact['id']; ?>"><?php echo htmlspecialchars($contact['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary w-100">Add Deal</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>