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

328 lines
17 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'];
$discount = $_POST['discount'];
$discountAmount = $_POST['discountAmount'];
// EMI Calculation
$emiAmount = round($totalAmount / $tenure, 2);
$outstandingAmount = $totalAmount;
$emiDate = new DateTime($bookingDate); // Set initial date
// $stmt = $db->prepare("INSERT INTO emi (customerId, emiNumber, emiAmount, emiDate, totalAmount, outstanding, tenure, frequency, bookingDate, invoiceId) VALUES (:customerId, :emiNumber, :emiAmount, :emiDate, :totalAmount, :outstanding, :tenure, :frequency, :bookingDate, :invoiceId)");
// for ($i = 1; $i <= $tenure; $i++) {
// $outstandingAmount -= $emiAmount;
// $emiDateFormatted = $emiDate->format('Y-m-d');
// $stmt->bindParam(':customerId', $_GET['customerId']);
// $stmt->bindParam(':emiNumber', $i);
// $stmt->bindParam(':emiAmount', $emiAmount);
// $stmt->bindParam(':emiDate', $emiDateFormatted);
// $stmt->bindParam(':totalAmount', $totalAmount);
// $stmt->bindParam(':outstanding', $outstandingAmount);
// $stmt->bindParam(':tenure', $tenure);
// $stmt->bindParam(':frequency', $frequency);
// $stmt->bindParam(':bookingDate', $bookingDate);
// $stmt->bindParam(':invoiceId', $invoiceId);
// $stmt->execute();
// // Move to the next EMI date
// if (trim(strtolower($frequency)) === 'weekly') {
// $emiDate->modify('+1 week');
// } elseif (trim(strtolower($frequency)) === 'monthly') {
// $emiDate->modify('+1 month');
// }
// }
// Insert into 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 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->execute();
echo '<div class="alert alert-success">New EMI Plan Saved 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");
$stmt->bindParam(':customerId', $_GET['customerId']);
$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>Create New 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" <?php echo ($invoiceData['frequency'] === 'Weekly') ? 'selected' : ''; ?>>Weekly</option>
<option value="Monthly" <?php echo ($invoiceData['frequency'] === 'Monthly') ? 'selected' : ''; ?>>Monthly</option>
</select>
<input type="hidden" name="frequency" value="<?php echo 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>
<?php
$tenureOptions = [ "1" => "One Time","3" => "3","6" => "6","9" => "9","12" => "12","0" => "Custom" ];
$selectedTenure = $invoiceData['tenure'] ?? ''; // Get selected tenure value
?>
<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
foreach ($tenureOptions as $value => $label) {
$selected = ($selectedTenure == $value) ? 'selected' : '';
echo "<option value=\"$value\" $selected>$label</option>";
}
?>
</select>
<input type="hidden" name="tenure" value="<?php echo 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">Discount&nbsp;&#36;</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']); ?>" readonly/>
</td>
<td>
<input class="form-control w-100" name="rate" id="rate" value="<?= htmlspecialchars($invoiceData['rate']); ?>" readonly/>
</td>
<td>
<select name="discount" id="discount" class="form-control" disabled>
<option value="0" <?php echo ($invoiceData['discount'] === "0") ? 'selected' : ''; ?>>No Discount</option>
<?php
$discountOptions = ["5", "10", "12", "18"];
foreach ($discountOptions as $discountItems) {
$selected = ($invoiceData['discount'] === $discountItems) ? 'selected' : '';
echo "<option value=\"$discountItems\" $selected> $discountItems%</option>";
}
?>
</select>
<input type="hidden" name="discount" value="<?php echo htmlspecialchars($invoiceData['discount']); ?>">
</td>
<td>
<input readonly 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>
<button class="btn btn-secondary">Discard</button>
<button type="submit" class="btn btn-primary">Save</button>
<a href="/customers/print-invoice/?customerId=<?= $_GET['customerId'] . '&invoiceId='. $_GET['invoiceId']; ?>" id="printBtn" class="btn btn-primary visually-">Print Invoice</a>
</div>
</div>
</form>
</div>
<script>
function changeTenureField() {
const tenureAuto = document.getElementById('tenureAuto');
const tenureCustom = document.getElementById('tenureCustom');
if (tenureAuto.value === "0") {
tenureAuto.classList.add('visually-hidden');
tenureAuto.setAttribute('disabled', 'true');
tenureAuto.removeAttribute('name');
tenureCustom.classList.remove('visually-hidden');
tenureCustom.removeAttribute('disabled');
tenureCustom.setAttribute('name', 'tenure');
tenureCustom.focus();
}
}
function restoreDropdown(input) {
const tenureAuto = document.getElementById('tenureAuto');
if (input.value.trim() === "") {
tenureAuto.classList.remove('visually-hidden');
tenureAuto.removeAttribute('disabled');
tenureAuto.setAttribute('name', 'tenure');
tenureAuto.value = ""; // Reset dropdown selection
input.classList.add('visually-hidden');
input.setAttribute('disabled', 'true');
input.removeAttribute('name');
input.value = ""; // Clear input value
} else {
tenureAuto.value = "0"; // Ensure dropdown remains on "Custom" if input has a value
}
}
document.addEventListener("DOMContentLoaded", function () {
const quantityInput = document.getElementById("quantity");
const rateInput = document.getElementById("rate");
const taxSelect = document.getElementById("discount");
const taxAmountInput = document.getElementById("discountAmount");
const totalAmountInput = document.getElementById("totalAmount");
function calculateTotal() {
const quantity = parseFloat(quantityInput.value) || 0;
const rate = parseFloat(rateInput.value) || 0;
const tax = parseFloat(taxSelect.value) || 0;
let subtotal = quantity * rate;
let taxAmount = (subtotal * tax) / 100;
let grandTotal = subtotal - taxAmount;
taxAmountInput.value = taxAmount.toFixed(2); // Format Tax Amount
totalAmountInput.value = grandTotal.toFixed(2); // Format Total Amount
}
// Event listeners for real-time calculation
quantityInput.addEventListener("input", calculateTotal);
rateInput.addEventListener("input", calculateTotal);
taxSelect.addEventListener("change", calculateTotal);
});
</script>
<!-- cust_67c9925ff14f4834489101 -->
<!-- CB03252 -->