billing2/customers/.hta_slug/edit-invoice.php

236 lines
13 KiB
PHP

<?php
require('../.hta_config/conf.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_GET['customerId'])) {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$totalAmount = $_POST['totalAmount'];
$tenure = $_POST['tenure'];
$frequency = $_POST['frequency'];
$bookingDate = $_POST['bookingDate'];
$paymentMode = $_POST['paymentMode'];
$salesAgent = $_POST['salesAgent'];
$adminNote = $_POST['adminNote'];
$invoiceId = $_POST['invoiceId'];
$customerName = $_POST['customerName'];
$customerAddress = $_POST['customerAddress'];
$marketingAgents = $_POST['marketingAgent'];
$marketingAgentList = implode(", ", $marketingAgents);
$item = $_POST['item'];
$description = $_POST['description'];
$quantity = $_POST['quantity'];
$rate = $_POST['rate'];
$discountAmount = $_POST['discountAmount'];
// Update invoice table
$stmt2 = $db->prepare("UPDATE invoice SET customerName = :customerName, address = :address, invoiceDate = :invoiceDate, paymentMode = :paymentMode, salesAgent = :salesAgent, marketingAgent = :marketingAgent, item = :item, description = :description, adminNote = :adminNote, discountAmount = :discountAmount, totalAmount = :totalAmount WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt2->bindParam(':customerId', $_GET['customerId']);
$stmt2->bindParam(':invoiceId', $invoiceId);
$stmt2->bindParam(':customerName', $customerName);
$stmt2->bindParam(':address', $customerAddress);
$stmt2->bindParam(':invoiceDate', $bookingDate);
$stmt2->bindParam(':paymentMode', $paymentMode);
$stmt2->bindParam(':salesAgent', $salesAgent);
$stmt2->bindParam(':marketingAgent', $marketingAgentList);
$stmt2->bindParam(':item', $item);
$stmt2->bindParam(':description', $description);
$stmt2->bindParam(':adminNote', $adminNote);
$stmt2->bindParam(':discountAmount', $discountAmount);
$stmt2->bindParam(':totalAmount', $totalAmount);
$stmt2->execute();
echo '<div class="alert alert-success">Invoice Updated Successfully!</div>';
echo '<script>
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("printBtn").classList.remove("visually-hidden");
});
</script>';
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
$invoiceData = null;
if (!empty($_GET['customerId'])) {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':invoiceId', $_GET['invoiceId']);
$stmt->execute();
$invoiceData = $stmt->fetch(PDO::FETCH_ASSOC);
$invoiceDate = date('Y-m-d', strtotime($invoiceData['invoiceDate']));
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
?>
<div class="container mt-4">
<h4>Edit Invoice</h4>
<hr>
<form method="POST">
<div class="d-flex justify-content-between gap-4">
<div class="w-100">
<div class="mb-3">
<label for="invoiceId" class="form-label">Invoice Id:</label>
<input type="text" class="form-control bg-white" id="invoiceId" name="invoiceId" value="<?= $invoiceData['invoiceId']; ?>" readonly>
</div>
<div class="mb-3">
<label for="name" class="form-label">Customer:</label>
<input readonly type="text" class="form-control bg-white" id="customerName" name="customerName" value="<?= htmlspecialchars($invoiceData['customerName']); ?>">
</div>
<div class="mb-3">
<label for="address" class="form-label">Address:</label>
<input id="address" name="customerAddress" class="form-control bg-white" type="text" value="<?= htmlspecialchars($invoiceData['address']); ?>">
</div>
<div class="mb-3">
<label for="frequency" class="form-label">Frequency:</label>
<select name="frequency" class="form-control" disabled>
<option value="Weekly" <?= ($invoiceData['frequency'] === 'Weekly') ? 'selected' : ''; ?>>Weekly</option>
<option value="Monthly" <?= ($invoiceData['frequency'] === 'Monthly') ? 'selected' : ''; ?>>Monthly</option>
</select>
<input type="hidden" name="frequency" value="<?= htmlspecialchars($invoiceData['frequency']); ?>">
</div>
<div class="d-flex w-100 gap-2">
<div class="mb-3 w-100">
<label for="bookingDate" class="form-label">Invoice Date:</label>
<input type="date" class="form-control" id="bookingDate" name="bookingDate" value="<?= htmlspecialchars($invoiceDate); ?>" required>
</div>
</div>
</div>
<div class="w-100">
<div class="mb-3">
<label for="paymentMode" class="form-label">Payment Methods:</label>
<select name="paymentMode" class="form-control" required>
<option value="">-Select-</option>
<?php
$paymentModes = ["Stripe", "Zelle", "Bank Transfer", "Cheque", "Other"];
foreach ($paymentModes as $mode) {
$selected = ($invoiceData['paymentMode'] === $mode) ? 'selected' : '';
echo "<option value=\"$mode\" $selected>$mode</option>";
}
?>
</select>
</div>
<div class="mb-3">
<label for="salesAgent" class="form-label">Sales Agent:</label>
<select name="salesAgent" class="form-control" required>
<option value="">-Select-</option>
<?php
$salesAgentOptions = ["Prabhat Mishra", "Suvojit Mishra"];
foreach ($salesAgentOptions as $salesAgentItems) {
$selected = ($invoiceData['salesAgent'] === $salesAgentItems) ? 'selected' : '';
echo "<option value=\"$salesAgentItems\" $selected>$salesAgentItems</option>";
}
?>
</select>
</div>
<div class="mb-3">
<label for="marketingAgent" class="form-label">Marketing Agent:</label>
<select name="marketingAgent[]" class="form-control" required multiple>
<?php
$marketingAgents = ["Prabhat Mishra", "Suvojit Mishra", "Aditya Sarkar", "Suvankar Sarkar"];
$selectedAgents = isset($invoiceData['marketingAgent']) && !empty($invoiceData['marketingAgent']) ? explode(',', $invoiceData['marketingAgent']) : [];
foreach ($marketingAgents as $agent) {
$selected = in_array(trim($agent), array_map('trim', $selectedAgents)) ? 'selected' : '';
echo "<option value=\"$agent\" $selected>$agent</option>";
}
?>
</select>
</div>
<div class="mb-3">
<label for="tenure" class="form-label">Total Cycles:</label>
<select id="tenureAuto" name="tenure" class="form-control" disabled>
<option value="">-Select-</option>
<?php
$tenureOptions = ["1" => "One Time", "3" => "3", "6" => "6", "9" => "9", "12" => "12", "0" => "Custom"];
$selectedTenure = $invoiceData['tenure'] ?? '';
foreach ($tenureOptions as $value => $label) {
$selected = ($selectedTenure == $value) ? 'selected' : '';
echo "<option value=\"$value\" $selected>$label</option>";
}
?>
</select>
<input type="hidden" name="tenure" value="<?= htmlspecialchars($selectedTenure); ?>">
<input type="text" name="tenure" id="tenureCustom" class="form-control visually-hidden" placeholder="Enter custom value" disabled onblur="restoreDropdown(this)" />
</div>
<div class="mb-3">
<label for="adminNote" class="form-label">Admin Note:</label>
<textarea class="form-control" name="adminNote" id="adminNote" cols="30" rows="4"><?= htmlspecialchars($invoiceData['adminNote']); ?></textarea>
</div>
</div>
</div>
<div>
<table class="w-100">
<thead>
<tr class="bg-secondary text-white">
<th class="p-2">Item</th>
<th class="p-2">Description</th>
<th class="p-2">Qty</th>
<th class="p-2">Rate</th>
<th class="p-2">Discount&nbsp;$</th>
<th class="p-2">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control w-100" name="item" id="item" value="<?= htmlspecialchars($invoiceData['item']); ?>" />
</td>
<td>
<input type="text" class="form-control w-100" name="description" id="description" value="<?= htmlspecialchars($invoiceData['description']); ?>" />
</td>
<td>
<input class="form-control w-100" name="quantity" id="quantity" value="<?= htmlspecialchars($invoiceData['qty']); ?>" />
</td>
<td>
<input class="form-control w-100" name="rate" id="rate" value="<?= htmlspecialchars($invoiceData['rate']); ?>" />
</td>
<td>
<input class="form-control" name="discountAmount" id="discountAmount" value="<?= htmlspecialchars($invoiceData['discountAmount']); ?>" />
</td>
<td>
<input readonly class="form-control" name="totalAmount" id="totalAmount" value="<?= htmlspecialchars($invoiceData['totalAmount']); ?>"/>
</td>
</tr>
</tbody>
</table>
</div>
<div class="d-flex justify-content-end align-items-center">
<div class="mt-2">
<button class="btn btn-secondary">Discard</button>
<button type="submit" class="btn text-white" style="background-color: #374151;">Save</button>
<a href="/customers/print-invoice/?customerId=<?= $_GET['customerId'] . '&invoiceId='. $_GET['invoiceId']; ?>" id="printBtn" class="btn text-white visually-" style="background-color: #374151;">Print Invoice</a>
</div>
</div>
</form>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
const quantityInput = document.getElementById("quantity");
const rateInput = document.getElementById("rate");
const discountAmountInput = document.getElementById("discountAmount");
const totalAmountInput = document.getElementById("totalAmount");
function calculateTotal() {
const quantity = parseFloat(quantityInput.value) || 0;
const rate = parseFloat(rateInput.value) || 0;
const discountAmount = parseFloat(discountAmountInput.value) || 0;
let subtotal = quantity * rate;
let grandTotal = subtotal - discountAmount;
totalAmountInput.value = grandTotal.toFixed(2);
}
// Event listeners for real-time calculation
quantityInput.addEventListener("input", calculateTotal);
rateInput.addEventListener("input", calculateTotal);
discountAmountInput.addEventListener("input", calculateTotal);
});
</script>