<?php
// ====================== CONFIG ======================
$host = 'localhost';
$db = 'order_management_db';
$user = 'root';
$pass = ''; // Change if you have password
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
// ===================================================
// Fetch Parties, Departments, Suppliers
$parties = $conn->query("SELECT party_id, party_name FROM parties ORDER BY party_name");
$depts = $conn->query("SELECT dept_id, dept_name FROM departments ORDER BY dept_name");
$suppliers = $conn->query("SELECT supplier_id, supplier_name FROM suppliers ORDER BY supplier_name");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Order Entry</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
input, select, textarea { width: 100%; padding: 8px; box-sizing: border-box; }
.error { color: red; font-size: 14px; margin-top: 5px; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
.total { font-size: 20px; font-weight: bold; color: green; }
button { padding: 10px 15px; margin: 5px; }
</style>
</head>
<body>
<h1>New Order Form</h1>
<form id="orderForm" method="POST" action="order_form.php">
<div class="form-group">
<label>Serial Number</label>
<input type="text" id="serial_number" name="serial_number" required>
<span class="error" id="err_serial"></span>
</div>
<div class="form-group">
<label>Current Date</label>
<input type="date" id="order_date" name="order_date" value="<?= date('Y-m-d') ?>" required>
</div>
<div class="form-group">
<label>Party Name</label>
<select id="party_id" name="party_id" required>
<option value="">-- Select Party --</option>
<?php while($p = $parties->fetch_assoc()): ?>
<option value="<?= $p['party_id'] ?>"><?= $p['party_name'] ?></option>
<?php endwhile; ?>
</select>
<span class="error" id="err_party"></span>
</div>
<div class="form-group">
<label>Party PO Number</label>
<input type="text" id="party_po_number" name="party_po_number">
</div>
<div class="form-group">
<label>P Date (PO Date)</label>
<input type="date" id="po_date" name="po_date">
</div>
<div class="form-group">
<label>Promise Date</label>
<input type="date" id="promise_date" name="promise_date">
</div>
<div class="form-group">
<label>Communication Mode</label>
<select id="communication_mode" name="communication_mode" required>
<option value="">-- Select --</option>
<option value="email">Email</option>
<option value="whatsapp">WhatsApp</option>
<option value="sms">SMS</option>
<option value="call">Call</option>
</select>
</div>
<div class="form-group">
<label>Order Type</label>
<select id="order_type" name="order_type" required>
<option value="">-- Select --</option>
<option value="Kgs">Kgs</option>
<option value="Pcs">Pcs</option>
</select>
</div>
<div class="form-group">
<label>Remarks</label>
<textarea id="remarks" name="remarks" rows="3"></textarea>
</div>
<h2>Order Items</h2>
<button type="button" onclick="addItemRow()">+ Add Item</button>
<table id="itemsTable">
<thead>
<tr>
<th>Sr #</th>
<th>Item Code</th>
<th>Item Name</th>
<th>Size</th>
<th>Price</th>
<th>Specification</th>
<th>Department</th>
<th>Qty Kg</th>
<th>Qty Pcs</th>
<th>Supplier</th>
<th>Production Instruction</th>
<th>Effect on Other</th>
<th>Notes</th>
<th>Line Total</th>
<th>Action</th>
</tr>
</thead>
<tbody id="itemsBody"></tbody>
</table>
<div class="total">Grand Total: <span id="grandTotal">0.00</span></div>
<br><br>
<button type="submit" name="submit_order" style="background:green;color:white;font-size:18px;padding:12px 30px;">Save Order</button>
</form>
<script>
// Real-time Live Total + Validation
let srCounter = 1;
function addItemRow() {
const tbody = document.getElementById('itemsBody');
const row = document.createElement('tr');
row.innerHTML = `
<td>${srCounter}</td>
<td><input type="text" name="item_code[]" class="item_code" onkeyup="calculateTotal()"></td>
<td><input type="text" name="item_name[]" class="item_name" required></td>
<td><input type="text" name="size[]" ></td>
<td><input type="number" step="0.01" name="price[]" class="price" onkeyup="calculateTotal()" required></td>
<td><input type="text" name="specification[]" ></td>
<td>
<select name="department_id[]">
<option value="">-- Dept --</option>
<?php
$depts->data_seek(0);
while($d = $depts->fetch_assoc()): ?>
<option value="<?= $d['dept_id'] ?>"><?= $d['dept_name'] ?></option>
<?php endwhile; ?>
</select>
</td>
<td><input type="number" step="0.001" name="qty_kg[]" class="qty_kg" onkeyup="calculateTotal()"></td>
<td><input type="number" name="qty_pcs[]" class="qty_pcs" onkeyup="calculateTotal()"></td>
<td>
<select name="supplier_id[]">
<option value="">-- Supplier --</option>
<?php
$suppliers->data_seek(0);
while($s = $suppliers->fetch_assoc()): ?>
<option value="<?= $s['supplier_id'] ?>"><?= $s['supplier_name'] ?></option>
<?php endwhile; ?>
</select>
</td>
<td><textarea name="production_instructions[]" rows="2" style="width:180px"></textarea></td>
<td><textarea name="effect_on_other[]" rows="2" style="width:180px"></textarea></td>
<td><textarea name="notes[]" rows="2" style="width:180px"></textarea></td>
<td class="line_total">0.00</td>
<td><button type="button" onclick="this.parentElement.parentElement.remove(); calculateTotal()">Remove</button></td>
`;
tbody.appendChild(row);
srCounter++;
calculateTotal();
}
// Live Total Calculation
function calculateTotal() {
let grand = 0;
document.querySelectorAll('#itemsBody tr').forEach(row => {
const price = parseFloat(row.querySelector('.price')?.value) || 0;
const qtyKg = parseFloat(row.querySelector('.qty_kg')?.value) || 0;
const qtyPcs = parseFloat(row.querySelector('.qty_pcs')?.value) || 0;
const qty = Math.max(qtyKg, qtyPcs);
const lineTotal = price * qty;
row.querySelector('.line_total').textContent = lineTotal.toFixed(2);
grand += lineTotal;
});
document.getElementById('grandTotal').textContent = grand.toFixed(2);
}
// Inline Real-time Validation
document.getElementById('orderForm').addEventListener('submit', function(e) {
let valid = true;
// Serial Number
if (!document.getElementById('serial_number').value.trim()) {
document.getElementById('err_serial').textContent = "Serial Number is required";
valid = false;
} else {
document.getElementById('err_serial').textContent = "";
}
// Party
if (!document.getElementById('party_id').value) {
document.getElementById('err_party').textContent = "Please select a party";
valid = false;
} else {
document.getElementById('err_party').textContent = "";
}
if (!valid) e.preventDefault();
});
</script>
</body>
</html>
<?php