diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index e7d5a377..e61466b5 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -187,6 +187,8 @@ class Invoice extends BaseController public function save_invoice() { + // echo "
";
+        // 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'),
diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php
index 2389f44a..35579b0d 100644
--- a/app/Models/InvoiceModel.php
+++ b/app/Models/InvoiceModel.php
@@ -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;
diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php
index 179d9374..edb39306 100644
--- a/app/Views/invoice_form.php
+++ b/app/Views/invoice_form.php
@@ -16,7 +16,7 @@
                 


" id="myForm"> - +
@@ -142,7 +142,7 @@ $ii_details) : ?> - + - " name="quantity[]" oninput="calculateInvoice(this,)" value="" min="1" /> + " name="quantity[]" oninput="calculateInvoice(this,)" value="" min="1" /> - " name="rate[]" oninput="calculateInvoice(this,)" value="" readonly /> + " name="rate[]" oninput="calculateInvoice(this,)" value="" readonly /> " name="tax[]" value="" readonly /> " name="amount[]" value="" readonly /> - " name="discount_amount[]" oninput="calculateInvoice(this,)" value="" min="0" step="0.01" /> + " name="discount_amount[]" oninput="calculateInvoice(this,)" value="" min="0" step="1" /> - + " name="invoice_child_id[]" value="" placeholder="hidden for invoice child/item id" value="" /> - +
@@ -192,7 +192,7 @@ - + - + - + - + - + - + - +
@@ -260,9 +260,25 @@
- -
+
+ + 0) { + echo ''; + } + ?> + + + +
+ '; cell1.innerHTML += ''; cell2.innerHTML = ''; - cell3.innerHTML = ''; - cell4.innerHTML = ''; - cell5.innerHTML = ''; - cell6.innerHTML = ''; - cell7.innerHTML = ''; + cell3.innerHTML = ''; + cell4.innerHTML = ''; + cell5.innerHTML = ''; + cell6.innerHTML = ''; + cell7.innerHTML = ''; cell8.innerHTML = '
'; $(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)); + }) + \ No newline at end of file diff --git a/app/Views/invoice_pdf_template.php b/app/Views/invoice_pdf_template.php index 95f5f662..33b52517 100644 --- a/app/Views/invoice_pdf_template.php +++ b/app/Views/invoice_pdf_template.php @@ -364,11 +364,11 @@
-
+

Bank Details

-
+