CHANGE_INVOICE_DISCOUNT : AADHAVAN

This commit is contained in:
aadhavan valli 2024-05-07 14:41:10 +05:30
parent ae0f2bc613
commit fceaa9e1cd
4 changed files with 242 additions and 33 deletions

View File

@ -187,6 +187,8 @@ class Invoice extends BaseController
public function save_invoice()
{
// echo "<pre>";
// print_r($this->request->getPost());die;
$this->logger->info("Invoice: Insert/Update Details");
try {
@ -241,6 +243,7 @@ class Invoice extends BaseController
'due_date' => (!empty($duedate)) ? date("Y-m-d", strtotime($duedate)) : NULL,
'subtotal' => (float)$this->request->getPost('sub_total'),
'tax' => (float)$this->request->getPost('invoice_tax'),
'dis_type' => $this->request->getPost('dis_type'),
'discount' => (float)$this->request->getPost('discount'),
'exact_total_amount' => (float)$this->request->getPost('exact_total_amount'),
'total_amount' => (int)$this->request->getPost('grand_total'),

View File

@ -8,7 +8,7 @@ class InvoiceModel extends Model
protected $table = 'invoice';
protected $primaryKey = 'invoice_id';
// protected $allowedFields = ['invoice_id','invoice_number','customer_id','invoice_date','event_id','business_id','created_on','business_id'];
protected $allowedFields = ['invoice_id', 'invoice_number', 'customer_id','notes', 'invoice_date', 'due_date', 'subtotal', 'tax', 'discount', 'shipping_charge','shipping_label','total_amount', 'payment_status', 'order_number','invoice_type', 'payment_method', 'event_id', 'business_id', 'shipping_address_id', 'billing_address_id', 'created_on', 'created_by', 'updated_on', 'updated_by','category','isactive','billing_address','shipping_address','status','payment_note','exact_total_amount'];
protected $allowedFields = ['invoice_id', 'invoice_number', 'customer_id','notes', 'invoice_date', 'due_date', 'subtotal', 'tax','dis_type', 'discount', 'shipping_charge','shipping_label','total_amount', 'payment_status', 'order_number','invoice_type', 'payment_method', 'event_id', 'business_id', 'shipping_address_id', 'billing_address_id', 'created_on', 'created_by', 'updated_on', 'updated_by','category','isactive','billing_address','shipping_address','status','payment_note','exact_total_amount'];
public function saveInvoiceItemDetails($data){
$i = 0;

View File

@ -16,7 +16,7 @@
<h4 class="header-title"><?= $page_name; ?></h4>
<br>
<form method="POST" enctype="multipart/form-data" action="<?= base_url() . "save_invoice"; ?>" id="myForm">
<input type="hidden" id="customer_id_set" >
<input type="hidden" id="customer_id_set" >
<div class="form-group">
<div class="form-row">
@ -142,7 +142,7 @@
<?php if (!empty($invoice_item_details)) {
foreach ($invoice_item_details as $inx => $ii_details) : ?>
<tr>
<td style="width:20%">
<td style="width:17% !important;">
<select class="form-control book-select" id="<?= "book" . $inx; ?>bookSelect" name="item_details[]" onchange="displayBookTag(this,<?= $inx; ?>)" required data-toggle="select2" style="width: 249px !important;">
<option value="">Select a <?= $flag_name; ?></option>
<?php if ($invoice_type === '1') { ?>
@ -162,28 +162,28 @@
</select>
<textarea name="description[]" class="form-control" placeholder="Enter Description" id="<?= "description" . $inx; ?>" cols="26" rows="2" style="margin-top: 16px;border-color: lightgray;width:247px;"><?= isset($ii_details['description']) ? $ii_details['description'] : '' ?></textarea>
</td>
<td style="width:7%;"><input type="number" class="form-control item-quantity" id="<?= "quantity" . $inx; ?>" name="quantity[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['quantity']) ? $ii_details['quantity'] : '' ?>" min="1" />
<td style="width:8%;"><input type="number" class="form-control item-quantity" id="<?= "quantity" . $inx; ?>" name="quantity[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['quantity']) ? $ii_details['quantity'] : '' ?>" min="1" />
</td>
<td style="width:9%;"><input type="text" class="form-control item-rate" id="<?= "rate" . $inx; ?>" name="rate[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['unit_price']) ? $ii_details['unit_price'] : '' ?>" readonly />
<td style="width:12%;"><input type="text" class="form-control item-rate" id="<?= "rate" . $inx; ?>" name="rate[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['unit_price']) ? $ii_details['unit_price'] : '' ?>" readonly />
</td>
<td style="width:8%;display:none;"><input type="number" class="form-control item-tax" id="<?= "tax" . $inx; ?>" name="tax[]" value="<?= isset($ii_details['tax']) ? $ii_details['tax'] : '' ?>" readonly />
</td>
<td style="width:10%;"><input type="number" class="form-control item-amount" id="<?= "amount" . $inx; ?>" name="amount[]" value="<?= isset($ii_details['subtotal']) ? $ii_details['subtotal'] : '' ?>" readonly />
</td>
<td style="width:7%;"><input type="number" class="form-control item-discount-amount" id="<?= "item_discount_amount" . $inx; ?>" name="discount_amount[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['discount_amount']) ? $ii_details['discount_amount'] : '' ?>" min="0" step="0.01" />
<td style="width:10%;"><input type="number" class="form-control item-discount-amount" id="<?= "item_discount_amount" . $inx; ?>" name="discount_amount[]" oninput="calculateInvoice(this,<?= $inx; ?>)" value="<?= isset($ii_details['discount_amount']) ? $ii_details['discount_amount'] : '' ?>" min="0" step="1" />
</td>
<td style="width:8%;">
<td style="width:7%;">
<select class="form-control item-discount-type" id="<?= "discount_type" . $inx; ?>" name="discount_type[]" oninput="calculateInvoice(this,<?= $inx; ?>)">
<option value="" <?php if (isset($ii_details['discount_type']) && $ii_details['discount_type'] === '₹') echo "selected"; ?>>
<option value="%" <?php if (isset($ii_details['discount_type']) && $ii_details['discount_type'] === '%') echo "selected"; ?>>
%</option>
<option value="" <?php if (isset($ii_details['discount_type']) && $ii_details['discount_type'] === '₹') echo "selected"; ?>>
</option>
<option value="%" <?php if (isset($ii_details['discount_type']) && $ii_details['discount_type'] === '%') echo "selected"; ?>>
%</option>
</select>
</td>
<td hidden><input type="hidden" class="form-control" id="<?= "hiddenInvoiceChildId" . $inx; ?>" name="invoice_child_id[]" value="" placeholder="hidden for invoice child/item id" value="<?= isset($ii_details['invoice_child_id']) ? $ii_details['invoice_child_id'] : '' ?>" /></td>
<?php if ($invoice_type === '1') : ?>
<td style="width:10%">
<td style="width:5%">
<center><i class="fa fa-trash remove-item "></i></center>
</td>
<?php endif; ?>
@ -192,7 +192,7 @@
<?php endforeach;
} else { ?>
<tr>
<td style="width:30% !important;">
<td style="width:17% !important;">
<select class="form-control book-select" id="book0" name="item_details[]" onchange="displayBookTag(this,0)" required data-toggle="select2" style="width: 249px !important;">
<option value="">Select a <?= $flag_name ?></option>
<?php if ($invoice_type === '1') { ?>
@ -207,24 +207,24 @@
<textarea name="description[]" class="form-control" placeholder="Enter Description" id="" cols="26" rows="2" style="margin-top: 16px;border-color: lightgray;width:247px;"></textarea>
</td>
<td style="width:10%;"><input type="number" class="form-control item-quantity" id="quantity0" name="quantity[]" oninput="calculateInvoice(this,0)" min="1" />
<td style="width:8%;"><input type="number" class="form-control item-quantity" id="quantity0" name="quantity[]" oninput="calculateInvoice(this,0)" min="1" />
</td>
<td style="width:10%;"><input type="text" class="form-control item-rate" id="rate0" name="rate[]" oninput="calculateInvoice(this,0)" /></td>
<td style="width:12%;"><input type="text" class="form-control item-rate" id="rate0" name="rate[]" oninput="calculateInvoice(this,0)" /></td>
<td style="width:8%;display:none"><input type="text" class="form-control item-tax" id="tax0" name="tax[]" /></td>
<td style="width:12%;"><input type="text" class="form-control item-amount" id="amount0" name="amount[]" />
</td>
<td class="discount-cell" style="width:12%;"><input type="number" class="form-control item-discount-amount" id="item_discount_amount0" name="discount_amount[]" oninput="calculateInvoice(this,0)" min="0" step="0.01" />
<td class="discount-cell" style="width:10%;"><input type="number" class="form-control item-discount-amount" id="item_discount_amount0" name="discount_amount[]" oninput="calculateInvoice(this,0)" min="0" step="1" />
</td>
<td style="width:12%;">
<td style="width:7%;">
<select class="form-control item-discount-type" id="<?= "discount_type0"; ?>" name="discount_type[]" oninput="calculateInvoice(this,0)">
<option value=""></option>
<option value="%">%</option>
<option value=""></option>
</select>
</td>
<td hidden><input type="hidden" class="form-control" id="hiddenInvoiceChildId0" name="invoice_child_id[]" value="" placeholder="hidden for invoice child/item id" /></td>
<td hidden ><input type="hidden" class="form-control" id="hiddenInvoiceChildId0" name="invoice_child_id[]" value="" placeholder="hidden for invoice child/item id" /></td>
<?php if ($invoice_type === '1') : ?>
<td>
<td style="width: 5%;">
<center><i class="fa fa-trash remove-item "></i></center>
</td>
<?php endif; ?>
@ -260,9 +260,25 @@
</div>
<div class="form-group col-md-6">
<label for="discount">Discount</label>
<input type="number" class="form-control" id="discount" name="discount" onchange="calculateGrandTotal(this.value)" value="<?= isset($invoice_details['discount']) ? $invoice_details['discount'] : 0 ?>" min="0" step="0.01">
</div>
<div class="input-group">
<input type="number" style="width: 60%;" class="form-control" id="discount" name="discount" value="<?= isset($invoice_details['discount']) ? $invoice_details['discount'] : 0 ?>" onchange=" discountTypeKeyup(this)" value="0" min="0" step="1">
<?php
$disType = isset($invoice_details['dis_type']) ? $invoice_details['dis_type'] : '';
if (strlen($disType) > 0) {
echo '<select class="custom-select" id="discount-type" name="dis_type">';
// Length is greater than 0, proceed with displaying the select element without the name attribute
} else {
// Length is 0 or $invoice_details['dis_type'] is not set, proceed with displaying the select element with the name attribute
echo '<select style="display: none;" class="custom-select" id="discount-type">';
}
?>
<option value="%" <?php if (isset($invoice_details['dis_type']) && $invoice_details['dis_type'] === '%') echo "selected"; ?>>%</option>
<option value="" <?php if (isset($invoice_details['dis_type']) && $invoice_details['dis_type'] === '₹') echo "selected"; ?>>₹</option>
</select>
</div>
</div>
</div>
<?php
// Check if it's a subscription invoice
$isSubscriptionInvoice = ($page_name === 'Add Subscription Invoice Details' || $page_name === 'Edit Subscription Invoice Details');
@ -726,15 +742,20 @@
var newRow = table.insertRow(table.rows.length);
var cell1 = newRow.insertCell(0);
cell1.style.width ="17%";
var cell2 = newRow.insertCell(1);
cell2.style.width = "8%";
var cell3 = newRow.insertCell(2);
cell3.style.width = "12%";
var cell4 = newRow.insertCell(3);
cell4.style.display = "none";
var cell5 = newRow.insertCell(4);
var cell6 = newRow.insertCell(5);
cell6.style.width = "10%";
var cell7 = newRow.insertCell(6);
var cell8 = newRow.insertCell(7);
cell8.style.width = " 5%";
// var cell9 = newRow.insertCell(8);
@ -755,11 +776,11 @@
// cell1.innerHTML = '<input type="text" class="form-control" name="item_details[]">';
cell1.innerHTML += '<textarea name="description[]" class="form-control" placeholder="Enter Description" id="description'+counter+'" cols="26" rows="2" style="margin-top: 16px;border-color: lightgray;width:247px;"></textarea>';
cell2.innerHTML = '<input type="number" class="form-control item-quantity" id="quantity' + counter + '" name="quantity[]" oninput="calculateInvoice(this,' + counter + ')" min="1">';
cell3.innerHTML = '<input type="number" class="form-control item-rate" id="rate' + counter + '" name="rate[]" oninput="calculateInvoice(this,' + counter + ')" min="0" step="0.01">';
cell4.innerHTML = '<input type="hidden" type="number" class="form-control item-tax" id="tax' + counter + '" name="tax[]" min="0" step="0.01">';
cell5.innerHTML = '<input type="number" class="form-control item-amount" id="amount' + counter + '" name="amount[]" min="0" step="0.01">';
cell6.innerHTML = '<input type="number" class="form-control item-discount-amount" id="item_discount_amount' + counter + '" name="discount_amount[]" oninput="calculateInvoice(this,' + counter + ')" min="0" step="0.01">';
cell7.innerHTML = '<select class="form-control item-discount-type" style=""width:12%;" id="item_discount_type' + counter + '" name="discount_type[]" oninput="calculateInvoice(this,' + counter + ')"><option value="₹">₹</option><option value="%">%</option></select>';
cell3.innerHTML = '<input type="number" class="form-control item-rate" id="rate' + counter + '" name="rate[]" oninput="calculateInvoice(this,' + counter + ')" min="0" step="1">';
cell4.innerHTML = '<input type="hidden" type="number" class="form-control item-tax" id="tax' + counter + '" name="tax[]" min="0" step="1">';
cell5.innerHTML = '<input type="number" class="form-control item-amount" id="amount' + counter + '" name="amount[]" min="0" step="1">';
cell6.innerHTML = '<input type="number" class="form-control item-discount-amount" id="item_discount_amount' + counter + '" name="discount_amount[]" oninput="calculateInvoice(this,' + counter + ')" min="0" step="1">';
cell7.innerHTML = '<select class="form-control item-discount-type" style=""width:12%;" id="item_discount_type' + counter + '" name="discount_type[]" oninput="calculateInvoice(this,' + counter + ')"><option value="%">%</option><option value="₹">₹</option></select>';
cell8.innerHTML = '<input type="hidden" class="form-control" id="hiddenInvoiceChildId' + counter + '" placeholder="hidden for invoice child/item id" name="invoice_child_id[]" value=""><center><i class="fa fa-trash remove-item "></center>';
$(newRow.querySelector('.book-select')).select2();
localStorage.setItem('add_book_index', counter);
@ -782,7 +803,7 @@
var selectedBook = bookData.find(book => book.book_id == bookId);
if (selectedBook) {
console.log(row);
// console.log(row);
// Update the fields in the current row with book details
row.querySelector('#quantity' + counter).value = 1;
row.querySelector('#rate' + counter).value = selectedBook.price;
@ -802,11 +823,20 @@
function calculateInvoice(inputElement, counter) {
if (!inputElement.classList.contains('item-quantity') && !inputElement.classList.contains('book-select')) {
$('#discount-type').hide();
$('#discount-type').removeAttr('name');
}
var row = inputElement.parentElement.parentElement;
var quantity = parseFloat(row.querySelector('#quantity' + counter).value);
var rate = parseFloat(row.querySelector('#rate' + counter).value);
var discountType = row.querySelector('select[name="discount_type[]"]').value; // Get the selected discount type
var discountAmount = parseFloat(row.querySelector('input[name="discount_amount[]"]').value) || 0;
if (!inputElement.classList.contains('item-quantity')) {
var discountAmount = parseFloat(row.querySelector('input[name="discount_amount[]"]').value) || 0;
}
console.log(rate);
if (!isNaN(quantity) && !isNaN(rate)) {
@ -830,10 +860,10 @@
row.querySelector('#amount' + counter).value = ''; // Clear the amount if either quantity or rate is not a number
}
row.querySelector('#amount' + counter).readOnly = true;
calculateOverallTotals();
calculateOverallTotals(inputElement);
}
function calculateOverallTotals() {
function calculateOverallTotals(inputElement = 0) {
var itemAmountInputs = document.querySelectorAll('.item-amount');
var itemTaxInputs = document.querySelectorAll('.item-tax');
var itemDiscountAmountInputs = document.querySelectorAll('.item-discount-amount');
@ -868,6 +898,7 @@
// Calculate the total using the formula: subtotal - tax + shipping - discount
var total = overallAmount - overallTax + shippingCharges - overallDiscount;
console.log(total);
// Update the subtotal, tax, shipping charges, discount, and total in the calculation card
var subtotalElement = parseFloat(overallAmount.toFixed(2));
@ -877,7 +908,9 @@
$("#invoicetax").val(taxElement);
var discountElement = parseFloat(overallDiscount.toFixed(2));
$("#discount").val(discountElement);
if ($('#discount-type').css('display') == 'none') {
$("#discount").val(discountElement);
}
var totalElement = Math.round(total);
$("#exacttotal").val(parseFloat(total.toFixed(2)));
@ -893,6 +926,72 @@
$("#paidAmount").val(totalElement);
}
if (inputElement == 0) {
if ($('#discount-type').css('display') == 'block') {
var dis_count_value = 0;
var shippingCharges = parseInt($('#shippingCharges').val());
var subtotal = $('#subtotal').val();
if ($('#discount-type').val() == '%') {
dis_count_value = subtotal * $('#discount').val() / 100;
}else{
dis_count_value =subtotal - $('#discount').val();
}
if (isNaN(shippingCharges)) {
shippingCharges =0;
}
$('#exacttotal').val((subtotal - dis_count_value) + shippingCharges);
$('#grandtotal').val((subtotal - dis_count_value) + shippingCharges);
$('#balanceAmount').val((subtotal - dis_count_value) + shippingCharges);
}
}else if (inputElement.classList.contains('item-quantity')) {
var hasValue = false;
$('.item-discount-amount').each(function() {
if ($(this).val().trim() !== "") {
hasValue = true;
} else {
hasValue = false;
}
});
if(!hasValue){
$('#discount').trigger('change');
}
}
if (inputElement.classList.contains('book-select')) {
if ($('#discount-type').css('display') != 'none') {
var discount =$('#discount').val();
var subtotal= $('#subtotal').val();
var shippingCharges = parseInt($('#shippingCharges').val());
var last_multiple_value = 0 ;
if ($('#discount-type').val() == '%') {
last_multiple_value = (subtotal * discount) / 100;
} else {
last_multiple_value = subtotal - discount;
}
if (isNaN(shippingCharges)) {
shippingCharges =0;
}
$('#exacttotal').val(subtotal-last_multiple_value + shippingCharges);
$('#grandtotal').val(parseInt(subtotal - last_multiple_value + shippingCharges));
$('#balanceAmount').val(parseInt(subtotal - last_multiple_value + shippingCharges));
}
}
}
// Define a function to calculate the grand total
@ -934,6 +1033,8 @@
// Add the manually entered discount to the grand total
grandTotal -= manuallyEnteredDiscount;
console.log($('#discount-type'));
// Update the grand total in the UI
var grandTotalElement = parseFloat(grandTotal.toFixed(2));
document.getElementById("exacttotal").value = grandTotalElement;
@ -1935,5 +2036,110 @@ function saveInvoiceCustomer() {
}
$('#discount').keyup(function() {
if($('#discount-type').css('display') == 'none'){
$('#discount-type').val('%');
}
$('#discount-type').show();
$('#discount-type').attr('name', 'dis_type');
$('.item-discount-amount').val('');
var discountValue = $(this).val();
var discountType = $('#discount-type').val();
var totalAmount = 0 ;
var discountLastValue = 0;
$('.item-amount').each(function() {
totalAmount += parseInt($(this).val());
});
if (discountType == '%') {
discountLastValue = totalAmount - (totalAmount * discountValue / 100 );
}else{
discountLastValue = totalAmount - discountValue;
}
var shippingCharges = $('#shippingCharges').val();
if (isNaN(shippingCharges)) {
shippingCharges =0;
}
discountLastValue += parseInt(shippingCharges);
$('#exacttotal').val(discountLastValue);
$('#grandtotal').val(parseInt(discountLastValue));
$('#balanceAmount').val(parseInt(discountLastValue));
console.log("last Discount value");
console.log(discountLastValue);
// var discountedPrice = calculateDiscountedPrice(discountValue);
// $('#discounted-price').text(discountedPrice);
});
function discountTypeKeyup(params) {
if($('#discount-type').css('display') == 'none'){
$('#discount-type').val('%');
}
$('#discount-type').show();
$('.item-discount-amount').val('');
var discountValue = $('#discount').val();
var discountType = $('#discount-type').val();
var totalAmount = 0 ;
var discountLastValue = 0;
$('.item-amount').each(function() {
totalAmount += parseInt($(this).val());
});
if (discountType == '%') {
discountLastValue = totalAmount - (totalAmount * discountValue / 100 );
}else{
discountLastValue = totalAmount - discountValue;
}
var shippingCharges = $('#shippingCharges').val();
if (isNaN(shippingCharges)) {
shippingCharges =0;
}
discountLastValue += parseInt(shippingCharges);
$('#exacttotal').val(discountLastValue );
$('#grandtotal').val(parseInt(discountLastValue));
$('#balanceAmount').val(parseInt(discountLastValue));
}
$('#discount-type').change(function () {
var discountValue = $('#discount').val();
var discountType = $(this).val();
var totalAmount = 0 ;
var discountLastValue = 0;
$('.item-amount').each(function() {
totalAmount += parseInt( $(this).val());
});
if (discountType == '%') {
discountLastValue = totalAmount - (totalAmount * discountValue / 100 );
}else{
discountLastValue = totalAmount - discountValue;
}
var shippingCharges = $('#shippingCharges').val();
if (isNaN(shippingCharges)) {
shippingCharges =0;
}
discountLastValue += parseInt(shippingCharges);
$('#exacttotal').val(discountLastValue);
$('#grandtotal').val(parseInt(discountLastValue));
$('#balanceAmount').val(parseInt(discountLastValue));
})
</script>

View File

@ -364,11 +364,11 @@
<br>
<div class="" style="margin-left: 9px;margin-top: -135px;">
<label>Bank Details</label><br>
<h4 style="margin-bottom: 5px;">Bank Details</h4>
<?php echo nl2br($business['terms']); ?>
</div>
</div>
<?php endforeach; ?>
<!-- ... (your existing HTML template) -->