fix this codes (<?php
// public/orders/add_order.php
// Enable comprehensive error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Start output buffering to prevent header issues
ob_start();
// Include authentication/database/functions -- adjust paths as needed
require_once DIR . '/../../includes/auth.php';
require_once DIR . '/../../includes/db_connect.php';
require_once DIR . '/../../includes/functions.php';
// Ensure sanitize_input() is available
if (!function_exists('sanitize_input')) {
die("Error: sanitize_input() function is not defined.");
}
// Initialize variables
error=′′;customer_name = '';
fullcontact=′′;priority = 'Normal'; // Default priority
paymentmeselected_country = '+965'; // Default country code
// An array of countries with their dialing codes
countries=["Kuwait"=>"+965",];kuwaitCities = [ "Al Asimah (Capital)", "Hawalli", "Farwaniya", "Mubarak Al-Kabeer", "Ahmadi", "Jahra" ];
// Fetch products for selection
productsresult=conn->query("SELECT * FROM products ORDER BY name ASC");
if (!products_result) {
die("Error fetching products: " . conn->error);
}
proproducts_result->fetch_all(MYSQLI_ASSOC);
// If form submitted
if (_SERVER['REQUEST_METHOD'] === 'POST') {
// Gather data
contact_input = sanitize_input(POST[′contactcity_selected = sanitize_input(address_details = sanitize_input(customer_name_input = sanitize_input(pickup = isset(notes = sanitize_input($_POST['notes'] ?? ''); // We'll store in orders.notes
// Payment items
$payment_method_post = $_POST['payment_method'] ?? 'Payment Link'; // Get raw post value for debugging
$payment_method = sanitize_input($payment_method_post); // Get and sanitize payment method
if (!in_array($payment_method, ['Cash','Payment Link','Free Order'])) {
$payment_method = 'Payment Link'; // Default if something fishy happens
}
$free_order_reason = sanitize_input($_POST['free_order_reason'] ?? '');
// Delivery & shipping
$free_shipping = (!$pickup && isset($_POST['free_shipping'])) ? 1 : 0;
$delivery_fee = $pickup ? 0.00 : floatval($_POST['delivery_fee'] ?? 0.00);
// Priority
$priority = sanitize_input($_POST['priority'] ?? 'Normal');
if (!in_array($priority, ['Normal','Important','Quick'])) {
$priority = 'Normal';
}
// Validate contact
$contact_digits = preg_replace('/\D/', '', $contact_input);
if (empty($contact_digits)) {
$error = "A valid contact number is required.";
} else {
// If user selected a country code (or default +965)
$selected_country = sanitize_input($_POST['country'] ?? '+965');
$full_contact = $selected_country . $contact_digits;
// Auto-generate customer name if not provided
$customer_name = $customer_name_input ?: (
$pickup ? 'pickup_' . $contact_digits : 'order_' . $contact_digits
);
}
// Validate date/time
$delivery_datetime = null;
if (!empty($_POST['delivery_date']) && !empty($_POST['delivery_time'])) {
$delivery_date_input = sanitize_input($_POST['delivery_date']);
$delivery_time_input = sanitize_input($_POST['delivery_time']);
$dt_string = $delivery_date_input . ' ' . $delivery_time_input . ':00';
$delivery_parsed = DateTime::createFromFormat('Y-m-d H:i:s', $dt_string);
if (!$delivery_parsed) {
$error = "Invalid delivery date/time format.";
} else {
// Check if in the past
$now = new DateTime('now');
if ($delivery_parsed < $now) {
$error = "Delivery date/time cannot be in the past.";
} else {
$delivery_datetime = $delivery_parsed->format('Y-m-d H:i:s');
}
}
} else {
$error = "Delivery date and time are required fields.";
}
// Validate city if not pickup
if (!$pickup) {
if (!in_array($city_selected, $kuwaitCities) || empty($city_selected)) {
$error = "Please select a valid city in Kuwait.";
}
}
// If free order, require reason
if ($payment_method === 'Free Order' && empty($free_order_reason)) {
$error = "Please provide a reason for the free order.";
}
// Check selected products
$product_ids = $_POST['product_id'] ?? [];
$quantities = $_POST['quantity'] ?? [];
if (empty($product_ids)) {
$error = "At least one product must be selected.";
}
// If no errors, insert
if (empty($error)) {
$conn->begin_transaction();
try {
// Insert or find existing customer
$full_address = $pickup ? '' : ($city_selected . "\n" . $address_details);
// Check if a customer with same contact exists
$stmt = $conn->prepare("SELECT id FROM customers WHERE contact = ?");
$stmt->bind_param("s", $full_contact);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$customer_id = $row['id'];
// Possibly update name/address
$updateStmt = $conn->prepare("UPDATE customers SET name = ?, delivery_address = ? WHERE id = ?");
$updateStmt->bind_param("ssi", $customer_name, $full_address, $customer_id);
$updateStmt->execute();
$updateStmt->close();
} else {
// Insert new
$stmt = $conn->prepare("INSERT INTO customers (name, contact, delivery_address) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $customer_name, $full_contact, $full_address);
$stmt->execute();
$customer_id = $stmt->insert_id;
$stmt->close();
}
// --- DEBUGGING: Log payment method values RIGHT BEFORE INSERT ---
error_log("DEBUG: Payment Method value before order insert: " . $payment_method);
error_log("DEBUG: Payment Method raw POST value: " . $payment_method_post);
error_log("DEBUG: Final payment_method value being inserted: " . $payment_method);
// -------------------------------------------------------
// Insert order
// ADDED: default status is "Pending"
$status = 'Pending';
// Ensure the payment method is stored (important part)
$sql_insert_order = "
INSERT INTO orders
(customer_id, status, priority, order_date, delivery_datetime, delivery_fee, free_shipping, pickup, payment_method, free_order_reason, notes)
VALUES
(?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?)
";
$stmt = $conn->prepare($sql_insert_order);
// --- DEBUGGING: Check for prepare statement error ---
if (!$stmt) {
$error_message = "Prepare statement failed: " . $conn->error;
error_log("DEBUG ERROR: " . $error_message);
throw new Exception($error_message);
}
// ----------------------------------------------------
// --- DEBUGGING: Check the values being bound ---
error_log("DEBUG: Binding parameters - customer_id: " . $customer_id . ", status: " . $status . ", priority: " . $priority . ", delivery_datetime: " . $delivery_datetime . ", delivery_fee: " . $delivery_fee . ", free_shipping: " . $free_shipping . ", pickup: " . $pickup . ", payment_method: " . $payment_method . ", free_order_reason: " . $free_order_reason . ", notes: " . $notes);
// ----------------------------------------------------
$stmt->bind_param(
"isssdiiiss",
$customer_id,
$status, // "Pending"
$priority,
$delivery_datetime,
$delivery_fee,
$free_shipping,
$pickup,
$payment_method, // <<-- Payment method is here!
$free_order_reason,
$notes
);
if (!$stmt->execute()) {
$error_message = "Execute statement failed: " . $stmt->error;
error_log("DEBUG ERROR: " . $error_message); // Log to server logs
throw new Exception($error_message); // Throw exception to rollback and show error
}
$order_id = $stmt->insert_id;
$stmt->close();
// Insert order items
$stmt = $conn->prepare("
INSERT INTO order_items
(order_id, product_id, quantity, packaging_preferences, discount_percentage, discount_reason)
VALUES
(?, ?, ?, ?, ?, ?)
");
foreach ($product_ids as $index => $pid) {
$qty = intval($quantities[$index] ?? 1);
$pack_pref = sanitize_input($_POST['packaging_preferences'][$index] ?? '');
// Check discount
$discount_checked = isset($_POST['apply_discount'][$index]);
if ($discount_checked) {
$discount_percent = floatval($_POST['discount_percentage'][$index] ?? 0);
$discount_reason = sanitize_input($_POST['discount_reason'][$index] ?? '');
} else {
$discount_percent = 0.0;
$discount_reason = '';
}
$stmt->bind_param(
"iiisds",
$order_id,
$pid,
$qty,
$pack_pref,
$discount_percent,
$discount_reason
);
$stmt->execute();
}
$stmt->close();
// Commit changes
$conn->commit();
// Redirect to view_orders.php with a success flag AND the new order ID
header("Location: view_orders.php?success=1&order_id=" . $order_id);
exit;
} catch (Exception $e) {
$conn->rollback();
$error = "Error processing order: " . $e->getMessage();
error_log("Full Error: " . $error); // Log full error for detailed debugging
}
}
}
?>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css"
>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css"
>
<style>
/* minimal styling */
.section-card {
background-color: #fff;
border-radius: 0.5rem;
padding: 1.5rem;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 250px;
background-color: #6B7280;
color: #fff;
text-align: left;
border-radius: 4px;
padding: 8px;
position: absolute;
z-index: 10;
bottom: 125%;
left: 50%;
margin-left: -125px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<div class="section-card">
<?php if (!empty($error)): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4 flex items-center">
<i class="bi bi-exclamation-triangle-fill mr-2"></i>
<span><?= htmlspecialchars($error) ?></span>
</div>
<?php endif; ?>
<form method="POST" action="add_order.php">
<!-- Customer -->
<h2 class="text-xl font-semibold text-gray-800 mb-4">Customer Details</h2>
<div class="flex flex-col md:flex-row md:space-x-6 mb-4">
<!-- name -->
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700" for="customer_name">
Customer Name
</label>
<input
type="text"
id="customer_name"
name="customer_name"
class="w-full border border-gray-300 rounded-md p-2"
value="<?= htmlspecialchars($customer_name ?? '') ?>"
placeholder="e.g. Ahmed"
>
</div>
<!-- country -->
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700" for="country">
Country
</label>
<select
name="country"
id="country"
class="w-full border border-gray-300 rounded-md p-2"
>
<?php foreach($countries as $cName => $cCode): ?>
<option value="<?= htmlspecialchars($cCode) ?>"
<?= ($selected_country === $cCode) ? 'selected' : '' ?>
>
<?= htmlspecialchars($cName . " (" . $cCode . ")") ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<!-- contact -->
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700" for="contact">
Contact Number
</label>
<input
type="text"
id="contact"
name="contact"
class="w-full border border-gray-300 rounded-md p-2"
value="<?= htmlspecialchars($_POST['contact'] ?? '') ?>"
placeholder="e.g., 66667777"
oninput="autoGenerateCustomerName()"
>
</div>
<hr class="my-6">
<!-- Priority / Products -->
<h2 class="text-xl font-semibold text-gray-800 mb-4">Order Details</h2>
<!-- priority -->
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700">Order Priority</label>
<div class="flex space-x-4">
<?php foreach(['Normal','Important','Quick'] as $pOpt): ?>
<label class="inline-flex items-center">
<input
type="radio"
name="priority"
value="<?= htmlspecialchars($pOpt) ?>"
<?= ($priority === $pOpt) ? 'checked' : '' ?>
class="form-radio text-teal-600"
>
<span class="ml-2"><?= htmlspecialchars($pOpt) ?></span>
</label>
<?php endforeach; ?>
</div>
</div>
<!-- products area -->
<div class="mb-4">
<div class="flex justify-between items-center mb-2">
<label class="block text-sm font-medium text-gray-700">
Products
</label>
<button
type="button"
class="bg-teal-500 text-white px-4 py-2 rounded hover:bg-teal-600 transition flex items-center gap-2"
onclick="addProductRow()"
>
<i class="bi bi-plus-circle-fill"></i> Add Product
</button>
</div>
<div id="products-container">
<?php
// If there's old POST data, re-populate. Otherwise, show one default row.
$product_ids = $_POST['product_id'] ?? [];
$quantities = $_POST['quantity'] ?? [];
if (!empty($error) && !empty($product_ids) && is_array($product_ids)) {
foreach($product_ids as $idx => $pid) {
$qty = intval($quantities[$idx] ?? 1);
$pack_pref = $_POST['packaging_preferences'][$idx] ?? '';
$discount_checked = isset($_POST['apply_discount'][$idx]);
$discount_percent = $_POST['discount_percentage'][$idx] ?? '';
$discount_reason = $_POST['discount_reason'][$idx] ?? '';
?>
<div class="product-row bg-gray-50 p-4 rounded-lg shadow-md mb-4">
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700">Product</label>
<select name="product_id[]" class="w-full border border-gray-300 rounded-md p-2">
<option value="">Select Product</option>
<?php foreach($products as $p): ?>
<?php
$selected = ($p['id'] == $pid) ? 'selected' : '';
$pLabel = htmlspecialchars($p['name']." - ".$p['size']." (".number_format($p['price'],2)." KD)");
?>
<option value="<?= $p['id'] ?>" <?= $selected ?>><?= $pLabel ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Quantity</label>
<input
type="number"
name="quantity[]"
min="1"
value="<?= htmlspecialchars($qty) ?>"
class="w-full border border-gray-300 rounded-md p-2"
>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Packaging</label>
<input
type="text"
name="packaging_preferences[]"
value="<?= htmlspecialchars($pack_pref) ?>"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., Gift Wrap"
>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Discount</label>
<div class="flex items-center mt-1">
<input
type="checkbox"
name="apply_discount[<?= $idx ?>]"
class="h-4 w-4 text-teal-600 border-gray-300 rounded"
<?php if ($discount_checked) echo 'checked'; ?>
onchange="toggleDiscountFields(this, <?= $idx ?>)"
>
<label class="ml-2">Apply</label>
</div>
<div id="discount_fields_<?= $idx ?>" class="mt-2" style="<?= $discount_checked ? '' : 'display:none;' ?>">
<label class="block text-sm font-medium text-gray-700">Discount (%)</label>
<input
type="number"
name="discount_percentage[<?= $idx ?>]"
min="0"
max="100"
step="0.01"
value="<?= htmlspecialchars($discount_percent) ?>"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., 10"
>
<label class="block text-sm font-medium mt-2">Reason</label>
<input
type="text"
name="discount_reason[<?= $idx ?>]"
value="<?= htmlspecialchars($discount_reason) ?>"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., Special promo"
>
</div>
</div>
</div>
<div class="flex justify-end mt-2">
<button
type="button"
class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 transition flex items-center gap-2"
onclick="removeProductRow(this)"
>
<i class="bi bi-trash-fill"></i> Remove
</button>
</div>
</div>
<?php
}
} else {
// Show one default row if no error data
?>
<div class="product-row bg-gray-50 p-4 rounded-lg shadow-md mb-4">
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700">Product</label>
<select name="product_id[]" class="w-full border border-gray-300 rounded-md p-2" required>
<option value="">Select Product</option>
<?php foreach($products as $p): ?>
<?php
$pLabel = htmlspecialchars($p['name']." - ".$p['size']." (".number_format($p['price'],2)." KD)");
?>
<option value="<?= $p['id'] ?>"><?= $pLabel ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Quantity</label>
<input
type="number"
name="quantity[]"
min="1"
value="1"
class="w-full border border-gray-300 rounded-md p-2"
required
>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Packaging Preferences</label>
<input
type="text"
name="packaging_preferences[]"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., Gift Wrap"
>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Discount</label>
<div class="flex items-center mt-1">
<input
type="checkbox"
name="apply_discount[0]"
class="h-4 w-4 text-teal-600 border-gray-300 rounded"
onchange="toggleDiscountFields(this, 0)"
>
<label class="ml-2">Apply</label>
</div>
<div id="discount_fields_0" class="mt-2" style="display:none;">
<label class="block text-sm font-medium text-gray-700">Discount (%)</label>
<input
type="number"
name="discount_percentage[0]"
min="0"
max="100"
step="0.01"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., 10"
>
<label class="block text-sm font-medium text-gray-700 mt-2">Reason</label>
<input
type="text"
name="discount_reason[0]"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., Special promo"
>
</div>
</div>
</div>
<div class="flex justify-end mt-2">
<button
type="button"
class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 transition flex items-center gap-2"
onclick="removeProductRow(this)"
>
<i class="bi bi-trash-fill"></i> Remove
</button>
</div>
</div>
<?php
}
?>
</div>
<button
type="button"
class="bg-teal-500 text-white px-4 py-2 rounded hover:bg-teal-600 transition flex items-center gap-2"
onclick="addProductRow()"
>
<i class="bi bi-plus-circle-fill"></i> Add Another Product
</button>
</div>
<!-- Delivery Details -->
<hr class="my-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Delivery Details</h2>
<div class="mb-4">
<input
type="checkbox"
id="pickup"
name="pickup"
class="h-4 w-4 text-teal-600 border-gray-300 rounded"
<?php if(isset($_POST['pickup'])) echo 'checked'; ?>
onchange="togglePickup()"
>
<label for="pickup" class="ml-2">Customer Pickup</label>
</div>
<div id="delivery_details">
<div class="mb-4" id="city_container">
<label class="block text-sm font-medium text-gray-700" for="city">
City (Kuwait)
</label>
<select
name="city"
id="city"
class="w-full border border-gray-300 rounded-md p-2"
>
<option value="">Select City</option>
<?php foreach ($kuwaitCities as $city): ?>
<option
value="<?= htmlspecialchars($city) ?>"
<?php if(isset($_POST['city']) && $_POST['city'] === $city) echo 'selected'; ?>
>
<?= htmlspecialchars($city) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-4" id="address_details_container">
<label class="block text-sm font-medium text-gray-700">Address Details</label>
<textarea
name="address_details"
rows="3"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., Block 2, Street 5, House 10"
><?= htmlspecialchars($_POST['address_details'] ?? '') ?></textarea>
</div>
<div class="mb-4" id="free_shipping_field">
<input
type="checkbox"
id="free_shipping"
name="free_shipping"
class="h-4 w-4 text-teal-600 border-gray-300 rounded"
<?php if(isset($_POST['free_shipping']) && !$pickup) echo 'checked'; ?>
onchange="toggleDeliveryFee()"
>
<label for="free_shipping" class="ml-2">Free Shipping</label>
</div>
<div class="mb-4" id="delivery_fee_field">
<label class="block text-sm font-medium text-gray-700" for="delivery_fee">
Delivery Fee (KWD)
</label>
<input
type="number"
step="0.01"
min="0"
name="delivery_fee"
id="delivery_fee"
class="w-full border border-gray-300 rounded-md p-2"
value="<?= htmlspecialchars($_POST['delivery_fee'] ?? '0.00') ?>"
placeholder="e.g., 1.00"
>
</div>
</div>
<div class="flex flex-col md:flex-row md:space-x-6">
<div class="flex-1 mb-4">
<label class="block text-sm font-medium text-gray-700" for="delivery_date">
Delivery Date
</label>
<input
type="date"
name="delivery_date"
id="delivery_date"
class="w-full border border-gray-300 rounded-md p-2"
value="<?= htmlspecialchars($_POST['delivery_date'] ?? '') ?>"
>
</div>
<div class="flex-1 mb-4">
<label class="block text-sm font-medium text-gray-700" for="delivery_time">
Delivery Time
</label>
<input
type="time"
name="delivery_time"
id="delivery_time"
class="w-full border border-gray-300 rounded-md p-2"
value="<?= htmlspecialchars($_POST['delivery_time'] ?? '') ?>"
>
</div>
</div>
<!-- Other Instructions -->
<hr class="my-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Other Instructions</h2>
<div class="mb-4">
<!-- Notes -->
<label class="block text-sm font-medium text-gray-700" for="notes">
Notes
</label>
<textarea
name="notes"
id="notes"
rows="3"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="Any extra instructions or special requests"
><?= htmlspecialchars($_POST['notes'] ?? '') ?></textarea>
</div>
<!-- Payment Method -->
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700">Payment Method</label>
<select
name="payment_method"
id="payment_method"
class="w-full border border-gray-300 rounded-md p-2"
onchange="toggleFreeOrderReason()"
>
<option value="Cash" <?= ($payment_method === 'Cash') ? 'selected' : '' ?>>Cash</option>
<option value="Payment Link" <?= ($payment_method === 'Payment Link') ? 'selected' : '' ?>>Payment Link</option>
<option value="Free Order" <?= ($payment_method === 'Free Order') ? 'selected' : '' ?>>Free Order</option>
</select>
</div>
<div class="mb-4" id="free_order_reason_field" style="display:none;">
<label class="block text-sm font-medium text-gray-700" for="free_order_reason">
Reason for Free Order
</label>
<textarea
name="free_order_reason"
id="free_order_reason"
rows="2"
class="w-full border border-gray-300 rounded-md p-2"
placeholder="e.g., influencer collaboration"
><?= htmlspecialchars($free_order_reason) ?></textarea>
</div>
<!-- (EXTRA) Add a Status selection with 4 statuses + 'Canceled' -->
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700">Order Status</label>
<select class="w-full border border-gray-300 rounded-md p-2" disabled>
<!-- Not used in the DB insertion (since we default to Pending),
but you said you want these 5 statuses, so let's display them. -->
<option value="Pending" selected>Pending</option>
<option value="In Progress">In Progress</option>
<option value="Completed">Completed</option>
<option value="Delivered">Delivered</option>
<option value="Canceled">Canceled</option>
</select>
<p class="text-sm text-gray-500 mt-1">Note: Actual DB insertion defaults to "Pending"</p>
</div>
<!-- Submit -->
<div class="mt-6">
<button
type="submit"
class="w-full bg-teal-500 text-white py-3 rounded hover:bg-teal-600 transition flex items-center justify-center gap-2"
>
<i class="bi bi-plus-lg"></i> Add Order
</button>
</div>
</form>
</div>
-- Host: 127.0.0.1:3306
-- Generation Time: Feb 05, 2025 at 06:29 PM
-- Server version: 10.11.10-MariaDB
-- PHP Version: 7.2.34
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8mb4 */;
--
-- Database: u406552082_kaakalia
--
-- Table structure for table ads
CREATE TABLE ads (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
name varchar(255) NOT NULL,
ad_type enum('Post','Story') NOT NULL,
related_id int(11) NOT NULL,
budget decimal(10,2) NOT NULL,
total_spent decimal(10,2) DEFAULT 0.00,
start_date date NOT NULL,
end_date date NOT NULL,
status enum('Active','Paused','Completed') DEFAULT 'Active',
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table ad_spend_logs
CREATE TABLE ad_spend_logs (
id int(11) NOT NULL,
ad_id int(11) NOT NULL,
amount decimal(10,2) NOT NULL,
spend_date datetime DEFAULT current_timestamp(),
description varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table customers
CREATE TABLE customers (
id int(11) NOT NULL,
name varchar(100) NOT NULL,
contact varchar(50) DEFAULT NULL,
delivery_address text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table deals
CREATE TABLE deals (
id int(11) NOT NULL,
influencer_id int(11) NOT NULL,
deal_type enum('Sponsorship','Booth','Other') NOT NULL,
description text DEFAULT NULL,
compensation decimal(10,2) DEFAULT 0.00,
status enum('Pending','Confirmed','Completed','Cancelled') DEFAULT 'Pending',
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table drivers
CREATE TABLE drivers (
id int(11) NOT NULL,
name varchar(100) NOT NULL,
number varchar(15) NOT NULL,
is_star tinyint(1) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table influencers
CREATE TABLE influencers (
id int(11) NOT NULL,
name varchar(255) NOT NULL,
contact_number varchar(50) NOT NULL,
contact_info varchar(255) NOT NULL,
address text NOT NULL,
social_media varchar(255) DEFAULT NULL,
notes text DEFAULT NULL,
delivery_datetime datetime DEFAULT NULL,
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table interactions
CREATE TABLE interactions (
id int(11) NOT NULL,
influencer_id int(11) NOT NULL,
interaction_date datetime NOT NULL,
method enum('Email','Phone','In-Person','Social Media','Other') NOT NULL,
notes text DEFAULT NULL,
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table notifications
CREATE TABLE notifications (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
type enum('order','post','plan','note','ad') NOT NULL,
message varchar(255) NOT NULL,
link varchar(255) NOT NULL,
created_at datetime DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table orders
CREATE TABLE orders (
id int(11) NOT NULL,
customer_id int(11) NOT NULL,
status enum('Pending','In Progress','Completed','Delivered','Canceled') DEFAULT 'Pending',
priority enum('Normal','Important','Quick') DEFAULT 'Normal',
order_date datetime DEFAULT current_timestamp(),
delivery_datetime datetime DEFAULT NULL,
delivery_date date DEFAULT NULL,
delivery_fee decimal(10,2) NOT NULL DEFAULT 0.00,
free_shipping tinyint(1) NOT NULL DEFAULT 0,
is_paid tinyint(1) NOT NULL DEFAULT 0,
pickup tinyint(1) NOT NULL DEFAULT 0,
payment_method enum('Cash','Payment Link','Free Order') DEFAULT 'Cash',
free_order_reason text DEFAULT NULL,
notes longtext DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table order_items
CREATE TABLE order_items (
id int(11) NOT NULL,
order_id int(11) NOT NULL,
product_id int(11) NOT NULL,
quantity int(11) NOT NULL DEFAULT 1,
packaging_preferences varchar(255) DEFAULT NULL,
discount_percentage decimal(5,2) DEFAULT 0.00,
discount_reason varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table products
CREATE TABLE products (
id int(11) NOT NULL,
name varchar(100) NOT NULL,
size varchar(50) DEFAULT NULL,
price decimal(10,2) NOT NULL,
category varchar(50) DEFAULT NULL,
thumbnail varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table users
CREATE TABLE users (
id int(11) NOT NULL,
username varchar(50) NOT NULL,
email varchar(100) DEFAULT NULL,
password varchar(255) NOT NULL,
role enum('admin','staff') NOT NULL,
profile_image varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table workspace_notes
CREATE TABLE workspace_notes (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
title varchar(255) NOT NULL,
content text NOT NULL,
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table workspace_plans
CREATE TABLE workspace_plans (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
title varchar(255) NOT NULL,
description text DEFAULT NULL,
audio_path varchar(255) DEFAULT NULL,
due_date datetime DEFAULT NULL,
status enum('Not Started','In Progress','Completed') DEFAULT 'Not Started',
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
priority enum('Normal','Important','Critical') NOT NULL DEFAULT 'Normal'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table workspace_posts
CREATE TABLE workspace_posts (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
platform enum('Instagram','Facebook','Twitter','Other') NOT NULL,
content text NOT NULL,
media_url varchar(255) DEFAULT NULL,
scheduled_datetime datetime DEFAULT NULL,
status enum('Scheduled','Posted','Failed') DEFAULT 'Scheduled',
created_at datetime DEFAULT current_timestamp(),
updated_at datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
type enum('Post','Story') DEFAULT 'Post'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Indexes for dumped tables
--
-- Indexes for table ads
ALTER TABLE ads
ADD PRIMARY KEY (id),
ADD KEY user_id (user_id),
ADD KEY related_id (related_id);
--
-- Indexes for table ad_spend_logs
ALTER TABLE ad_spend_logs
ADD PRIMARY KEY (id),
ADD KEY ad_id (ad_id);
--
-- Indexes for table customers
ALTER TABLE customers
ADD PRIMARY KEY (id);
--
-- Indexes for table deals
ALTER TABLE deals
ADD PRIMARY KEY (id),
ADD KEY influencer_id (influencer_id);
--
-- Indexes for table drivers
ALTER TABLE drivers
ADD PRIMARY KEY (id);
--
-- Indexes for table influencers
ALTER TABLE influencers
ADD PRIMARY KEY (id);
--
-- Indexes for table interactions
ALTER TABLE interactions
ADD PRIMARY KEY (id),
ADD KEY influencer_id (influencer_id);
--
-- Indexes for table notifications
ALTER TABLE notifications
ADD PRIMARY KEY (id),
ADD KEY user_id (user_id),
ADD KEY created_at (created_at);
--
-- Indexes for table orders
ALTER TABLE orders
ADD PRIMARY KEY (id),
ADD KEY customer_id (customer_id),
ADD KEY idx_orders_customer_id (customer_id);
--
-- Indexes for table order_items
ALTER TABLE order_items
ADD PRIMARY KEY (id),
ADD KEY order_id (order_id),
ADD KEY product_id (product_id),
ADD KEY idx_order_items_order_id (order_id),
ADD KEY idx_order_items_product_id (product_id);
--
-- Indexes for table products
ALTER TABLE products
ADD PRIMARY KEY (id);
--
-- Indexes for table users
ALTER TABLE users
ADD PRIMARY KEY (id),
ADD UNIQUE KEY username (username);
--
-- Indexes for table workspace_notes
ALTER TABLE workspace_notes
ADD PRIMARY KEY (id),
ADD KEY user_id (user_id);
--
-- Indexes for table workspace_plans
ALTER TABLE workspace_plans
ADD PRIMARY KEY (id),
ADD KEY user_id (user_id);
--
-- Indexes for table workspace_posts
ALTER TABLE workspace_posts
ADD PRIMARY KEY (id),
ADD KEY user_id (user_id);
--
-- AUTO_INCREMENT for dumped tables
--
-- AUTO_INCREMENT for table ads
ALTER TABLE ads
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table ad_spend_logs
ALTER TABLE ad_spend_logs
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table customers
ALTER TABLE customers
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table deals
ALTER TABLE deals
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table drivers
ALTER TABLE drivers
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table influencers
ALTER TABLE influencers
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table interactions
ALTER TABLE interactions
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table notifications
ALTER TABLE notifications
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table orders
ALTER TABLE orders
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table order_items
ALTER TABLE order_items
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table products
ALTER TABLE products
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table users
ALTER TABLE users
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table workspace_notes
ALTER TABLE workspace_notes
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table workspace_plans
ALTER TABLE workspace_plans
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table workspace_posts
ALTER TABLE workspace_posts
MODIFY id int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--
-- Constraints for table ads
ALTER TABLE ads
ADD CONSTRAINT fk_ads_related_id FOREIGN KEY (related_id) REFERENCES workspace_posts (id) ON DELETE CASCADE,
ADD CONSTRAINT fk_ads_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
--
-- Constraints for table ad_spend_logs
ALTER TABLE ad_spend_logs
ADD CONSTRAINT fk_ad_spend_logs_ad_id FOREIGN KEY (ad_id) REFERENCES ads (id) ON DELETE CASCADE;
--
-- Constraints for table deals
ALTER TABLE deals
ADD CONSTRAINT deals_ibfk_1 FOREIGN KEY (influencer_id) REFERENCES influencers (id) ON DELETE CASCADE;
--
-- Constraints for table interactions
ALTER TABLE interactions
ADD CONSTRAINT interactions_ibfk_1 FOREIGN KEY (influencer_id) REFERENCES influencers (id) ON DELETE CASCADE;
--
-- Constraints for table notifications
ALTER TABLE notifications
ADD CONSTRAINT notifications_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
--
-- Constraints for table orders
ALTER TABLE orders
ADD CONSTRAINT fk_orders_customer_id FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT orders_ibfk_1 FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
--
-- Constraints for table order_items
ALTER TABLE order_items
ADD CONSTRAINT fk_order_items_order_id FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT fk_order_items_product_id FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT order_items_ibfk_1 FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE,
ADD CONSTRAINT order_items_ibfk_2 FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE;
--
-- Constraints for table workspace_notes
ALTER TABLE workspace_notes
ADD CONSTRAINT fk_workspace_notes_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
--
-- Constraints for table workspace_plans
ALTER TABLE workspace_plans
ADD CONSTRAINT fk_workspace_plans_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
--
-- Constraints for table workspace_posts
ALTER TABLE workspace_posts
ADD CONSTRAINT fk_workspace_posts_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;) and amke it cool page add order and fix it and make payment moethiod wokring well and add on datadase and type full codes and don't remove anything codes and add more codes