<?php
require_once('/home/leadltht/fastlinkinternet.com/administrator/data-provider/config.php'); // Ensure the correct path to config.php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['userdata']['type']) || !isset($_SESSION['userdata']['firstname']) || !isset($_SESSION['userdata']['lastname'])) {
echo '<center>Unknown User Type</center>';
exit;
}
$firstname = $_SESSION['userdata']['firstname'];
$lastname = $_SESSION['userdata']['lastname'];
$full_name = htmlspecialchars($firstname . ' ' . $lastname);
$type = $_SESSION['userdata']['type'];
if ($type == 3) {
// Fetch member ID based on the client's name
$member_id = $conn->query("SELECT id FROM member_list WHERE LOWER(CONCAT(firstname, ' ', lastname)) = '". strtolower($full_name) ."'")->fetch_assoc()['id'];
}
$installation_fee = 0;
$total_installation_balance = 0;
// Fetch installation fee and total installation balance from the database
if (isset($member_id)) {
$qry = $conn->query("SELECT installation_fee, (installation_fee - IFNULL((SELECT SUM(total_amount) FROM collection_list WHERE name_of_payment = 'Installation Fee' AND member_id = '$member_id'), 0)) AS total_installation_balance FROM member_list WHERE id = '$member_id'");
if ($qry->num_rows > 0) {
$row = $qry->fetch_assoc();
$installation_fee = $row['installation_fee'];
$total_installation_balance = $row['total_installation_balance'];
}
}
if (isset($_GET['id']) && $_GET['id'] > 0) {
$qry = $conn->query("SELECT * from `collection_list` where id = '{$_GET['id']}'");
if ($qry->num_rows > 0) {
foreach ($qry->fetch_assoc() as $k => $v) {
$$k = $v;
}
} else {
?>
<center>Unknown Collection ID</center>
<style>
#uni_modal .modal-footer {
display: none;
}
</style>
<div class="text-right">
<button class="btn btn-default bg-gradient-dark btn-flat" data-dismiss="modal"><i class="fa fa-times"></i> Close</button>
</div>
<?php
exit;
}
}
?>
<div class="container-fluid">
<form action="save_collection.php" method="POST" id="collection-form">
<input type="hidden" name="id" value="<?php echo isset($id) ? $id : '' ?>">
<?php if ($type == 3): ?>
<input type="hidden" name="member_id" value="<?= $member_id ?>">
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="date_collected" class="control-label">Date Collected *</label>
<input name="date_collected" id="date_collected" type="date" class="form-control form-control-sm rounded-0" value="<?php echo isset($date_collected) ? $date_collected : ''; ?>" required>
</div>
<div class="form-group">
<label for="receipt" class="control-label">Receipt Number</label>
<input name="receipt" id="receipt" type="text" class="form-control form-control-sm rounded-0" value="<?php echo isset($receipt) ? $receipt : ''; ?>">
</div>
<div class="form-group">
<label for="collection_method" class="control-label">Payment Method *</label>
<select name="collection_method" id="collection_method" class="form-control form-control-sm rounded-0 select2" required>
<option value="" disabled <?= !isset($collection_method) ? "selected" : "" ?>>Select Payment Method</option>
<?php
$payment_method = $conn->query("SELECT * FROM `payment_method` ORDER BY `payment_method_name` ASC");
while ($row = $payment_method->fetch_assoc()):
?>
<option value="<?= $row['payment_method_name'] ?>" <?= isset($collection_method) && $collection_method == $row['payment_method_name'] ? "selected" : "" ?>><?= $row['payment_method_name'] ?></option>
<?php endwhile; ?>
</select>
</div>
<?php if ($type == 3): ?>
<div class="form-group">
<label for="client_name_display" class="control-label">Client Name</label>
<input type="text" name="client_name_display" id="client_name_display" class="form-control form-control-sm rounded-0" value="<?= $full_name ?>" readonly>
</div>
<?php else: ?>
<div class="form-group">
<label for="member_id" class="control-label">Client Name *</label>
<select name="member_id" id="member_id" class="form-control form-control-sm rounded-0 select2" required>
<option value="" disabled <?= !isset($member_id) ? "selected" : "" ?>></option>
<?php
$member = $conn->query("SELECT *, CONCAT(firstname, ' ', COALESCE(middlename, ''), ' ', lastname) as fullname FROM `member_list` ORDER BY CONCAT(firstname, ' ', COALESCE(middlename, ''), ' ', lastname) ASC");
while ($row = $member->fetch_assoc()):
?>
<option value="<?= $row['id'] ?>" <?= isset($member_id) && $member_id == $row['id'] ? "selected" : "" ?>><?= $row['fullname'] ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; ?>
<div class="form-group">
<label for="collected_by" class="control-label">Collected By</label>
<input type="text" name="collected_by" id="collected_by" class="form-control form-control-sm rounded-0" value="<?= $type == 3 ? 'Client' : $full_name ?>" readonly>
</div>
<div class="form-group">
<label for="note" class="control-label">Note</label>
<textarea name="note" id="note" class="form-control form-control-sm rounded-0"><?php echo isset($note) ? htmlspecialchars($note) : ''; ?></textarea>
</div>
</div>
<div class="col-md-6">
<table class="table table-stripped table-bordered">
<thead>
<tr>
<th class="text-center">
<div class="custom-control custom-checkbox">
<input class="custom-control-input custom-control-input-primary custom-control-input-outline" type="checkbox" id="checkall">
<label for="checkall" class="custom-control-label">All</label>
</div>
</th>
<th class="text-center">Category</th>
<th class="text-center">Fee</th>
</tr>
</thead>
<tbody>
<?php
$collection_item = [];
if (isset($id)) {
$collection_qry = $conn->query("SELECT * FROM `collection_items` WHERE collection_id = '{$id}' ");
while ($row = $collection_qry->fetch_assoc()) {
$collection_item[$row['category_id']] = $row;
}
}
// Condition to filter categories based on installation fee and total installation balance
$category_query = "SELECT * FROM `category_list`";
$category_query .= " ORDER BY `name` ASC";
$category = $conn->query($category_query);
while ($row = $category->fetch_assoc()):
?>
<tr>
<td class="px-2 py-1 align-middle text-center">
<input type="hidden" class="fee" id="fee<?= $row['id'] ?>" name="fee[<?= $row['id'] ?>]" value="<?= (isset($collection_item[$row['id']])) ? ($collection_item[$row['id']]['fee']) : ($row['fee']) ?>">
<div class="custom-control custom-checkbox">
<input name="category_id[<?= $row['id'] ?>]" class="custom-control-input custom-control-input-primary custom-control-input-outline check-item" type="checkbox" id="cat_<?= $row['id'] ?>" value="<?= $row['id'] ?>" <?= (isset($collection_item[$row['id']])) ? 'checked' : '' ?>>
<label for="cat_<?= $row['id'] ?>" class="custom-control-label"></label>
</div>
</td>
<td class="px-2 py-1 align-middle"><?= $row['name'] ?></td>
<td class="px-2 py-1 align-middle"><input class="form-control form-control-sm rounded-0 fee-input" value="<?= $row['fee'] ?>" type="text" name="" id="<?= $row['id'] ?>"></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<div class="form-group">
<label for="total_amount" class="control-label">Total Collection</label>
<input name="total_amount" id="total_amount" type="text" class="form-control form-control-sm rounded-0" value="<?php echo isset($total_amount) ? $total_amount : 0; ?>" readonly tabindex="-1">
</div>
<div class="form-group">
<label for="discount" class="control-label">Discount</label>
<input type="number" class="form-control form-control-sm rounded-0" id="discount-amount" name="discount_amount" placeholder="Enter discount amount" value="0">
</div>
<div class="form-group">
<label for="discount_reason" class="control-label">Discount Reason</label>
<input type="text" name="discount_reason" id="discount_reason" class="form-control form-control-sm rounded-0" placeholder="Enter reason for discount">
</div>
<div class="form-group">
<label for="discounted_total_amount" class="control-label">Discounted Total Collection</label>
<input name="discounted_total_amount" id="discounted_total_amount" type="text" class="form-control form-control-sm rounded-0" value="0" readonly tabindex="-1">
</div>
</div>
</div>
</form>
</div>
<!-- Include Select2 CSS and JS files -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
function _checkAll(){
var total = $('.check-item').length;
var checked = $('.check-item:checked').length;
if(total == checked){
$('#checkall').prop('checked',true);
}else{
$('#checkall').prop('checked',false);
}
}
function calc_total(){
var total = 0;
$('.check-item:checked').each(function(){
var tr = $(this).closest('tr');
var fee = tr.find('input.fee').val();
total += parseFloat(fee) || 0;
});
var discount = parseFloat($('#discount-amount').val()) || 0;
$('#total_amount').val(total.toFixed(2)); // Ensure the total is formatted correctly
var discountedTotal = total - discount;
$('#discounted_total_amount').val(discountedTotal.toFixed(2)); // Ensure the discounted total is formatted correctly
}
function delete_collection(id) {
start_loader();
$.ajax({
url: "https://fastlinkinternet.com/administrator/data-provider/classes/Master.php?f=delete_collection",
method: "POST",
data: { id: id },
dataType: "json",
error: err => {
console.log(err);
alert_toast("An error occurred.", 'error');
end_loader();
},
success: function(resp) {
if (typeof resp === 'object' && resp.status === 'success') {
location.reload();
} else {
alert_toast("An error occurred.", 'error');
end_loader();
}
}
});
}
$(document).ready(function(){
_checkAll();
calc_total();
$('.check-item').change(function(){
_checkAll();
calc_total();
});
$('#checkall').change(function(){
if($(this).is(':checked') == true){
$('.check-item').prop('checked', true).trigger('change');
}else{
$('.check-item').prop('checked', false).trigger('change');
}
_checkAll();
calc_total();
});
$('input.fee-input').on('input', function() {
var id = $(this).attr('id');
var value = $(this).val();
$('#fee' + id).val(value);
calc_total();
});
$('#discount-amount').on('input', function() {
calc_total();
});
$('#collection-form').submit(function(e){
e.preventDefault();
var _this = $(this);
$('.err-msg').remove();
var el = $('<div>');
el.addClass("alert err-msg");
el.hide();
if(_this[0].checkValidity() == false){
_this[0].reportValidity();
return false;
}
if($('.check-item:checked').length <= 0){
alert_toast("Please Select at least 1 category first.", 'error');
return false;
}
start_loader();
// Log form data before submission
console.log("Form data before submission:", new FormData($(this)[0]));
$.ajax({
url: "https://fastlinkinternet.com/administrator/data-provider/classes/Master.php?f=save_collection",
data: new FormData($(this)[0]),
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST',
dataType: 'json',
error: err => {
console.error("Full Error Response:", err);
el.addClass('alert-danger').text("An error occurred");
_this.prepend(el);
el.show('.modal');
end_loader();
},
success: function(resp){
// Log the server response for debugging
console.log("Server response:", resp);
if (typeof resp == 'object' && resp.status == 'success') {
location.reload();
} else if (resp.status == 'failed' && !!resp.msg) {
el.addClass('alert-danger').text(resp.msg);
_this.prepend(el);
el.show('.modal');
} else {
el.text("An error occurred");
console.error("Response:", resp);
}
$("html, body").scrollTop(0);
end_loader();
}
});
});
// Initialize Select2 with type search
$('.select2').select2({
placeholder: 'Select an option',
allowClear: true
});
});
</script>