diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index ad8443b..aa3b373 100755 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -27,6 +27,7 @@ use Mpdf\Mpdf; use CodeIgniter\API\ResponseTrait; use App\Helpers\InvNoHelper; use App\Helpers\ClientDetailsUpdate; +use Config\Services; class Invoice extends BaseController { @@ -145,6 +146,8 @@ class Invoice extends BaseController 'status'=> $status, 'isactive' => 1, 'branch_id'=> $this->session->get('logged_user_branch_id'), + 'paid_advance' => $this->request->getPost('paid_advance'), + 'discount' => $this->request->getPost('discount'), ]; @@ -299,10 +302,11 @@ class Invoice extends BaseController } - public function download_invoice($invoice_id) + public function download_invoice($enc_id) { if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); } // Fetch the invoice data based on $invoice_id + $invoice_id = base64_decode($enc_id); $InvoiceModel = new InvoiceModel(); $InvoiceChildModel = new InvoiceChildModel(); diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index de9bc81..d492e57 100755 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -6,7 +6,7 @@ class InvoiceModel extends Model protected $table = 'invoice'; protected $primaryKey = 'invoice_id'; protected $allowedFields = ['invoice_id','invoice_number','sale_order','invoice_date','sales_commision','client_name','status','vehicle_reg_number','billing_address','billing_city','billing_state','billing_country','billing_postal_code','terms_condition','job_card_id','isactive', - 'type','mobile_no','email','invoice_child_id','order_number','mode_of_payment','subtotal','tax','discount','total','terms_condition','branch_id','vehicle_id' ]; + 'type','mobile_no','email','invoice_child_id','order_number','mode_of_payment','subtotal','tax','discount','total','terms_condition','branch_id','vehicle_id','paid_advance' ]; protected $beforeInsert = ['setCreatedBy']; protected $beforeUpdate = ['setUpdatedBy']; diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php index 44669d6..af6c67b 100755 --- a/app/Views/invoice_form.php +++ b/app/Views/invoice_form.php @@ -342,19 +342,23 @@ - - - - -
- +
+
+ + +
+
+ + +
+
+ + +
@@ -1047,32 +1051,28 @@ $(document).ready(function() { // Function to calculate total discount - function calculateDiscount() { - var totalDiscount = 0; - $('.item-discount-amount').each(function(index, element) { - var discountAmount = parseFloat($(element).val()) || 0; - var discountType = $(element).closest('tr').find('.item-discount-type').val(); - if (discountType === '%') { - var rate = $(element).closest('tr').find('.item-rate').val(); - var quantity = $(element).closest('tr').find('.item-quantity').val(); - var amount = rate * quantity; - discountAmount = amount * discountAmount / 100; - } - totalDiscount += discountAmount; - }); - return totalDiscount; - } + // function calculateDiscount() { + // var totalDiscount = 0; + // $('.item-discount-amount').each(function(index, element) { + // var discountAmount = parseFloat($(element).val()) || 0; + // var discountType = $(element).closest('tr').find('.item-discount-type').val(); + // if (discountType === '%') { + // var rate = $(element).closest('tr').find('.item-rate').val(); + // var quantity = $(element).closest('tr').find('.item-quantity').val(); + // var amount = rate * quantity; + // discountAmount = amount * discountAmount / 100; + // } + // totalDiscount += discountAmount; + // }); + // return totalDiscount; + // } // Function to calculate total function calculateTotal() { var subtotal = calculateSubtotal(); - var total_tax_amount = $('#invoicetax').val(); - // console.log(total_tax_amount + parseInt(subtotal)); - - var discount = calculateDiscount(); - var total = parseFloat(total_tax_amount) + parseFloat(subtotal) - discount; + var total = parseFloat(total_tax_amount) + parseFloat(subtotal); // return total; return Math.round(total); } @@ -1081,8 +1081,9 @@ $(document).ready(function() { function updateCalculations() { $('#subtotal').val(calculateSubtotal().toFixed(2)); $('#invoicetax').val(calculateTax().toFixed(2)); - $('#discount').val(calculateDiscount().toFixed(2)); + // $('#discount').val(calculateDiscount().toFixed(2)); $('#grandtotal').val(calculateTotal().toFixed(2)); + calculateBalance(); } // Event listener for discount change @@ -1139,6 +1140,8 @@ $(document).ready(function() { $('#itemTable tbody').append(newRow); + $('#discount').val(0); + $('#paid_advance').val(0); initializeSelect2(); updateCalculations(); initializeRowCalculations(newRow); @@ -1160,8 +1163,7 @@ function initializeRowCalculations(row) { calculateAmount(row); updateCalculations(); } - - + + diff --git a/app/Views/invoice_list.php b/app/Views/invoice_list.php index 9e5fb5a..cbee336 100755 --- a/app/Views/invoice_list.php +++ b/app/Views/invoice_list.php @@ -79,7 +79,8 @@ @@ -201,6 +202,8 @@ $(document).on('click', '.preview-invoice', function() { var Id = $(this).data('invoices-id'); + var encId = $(this).data('enc-id'); + var link = '' + encId; $.ajax({ type: 'GET', url: 'invoices/preview/'+Id, @@ -210,7 +213,7 @@ $('#PreviewInvoiceModal').modal('show'); $('#PreviewInvoiceModalLabel').text('Invoice Preview '); $('#PreviewInvoiceModal .modal-body').html(response.data); - $('#downloadInvoiceButton').attr('href', '' + Id); + $('#downloadInvoiceButton').attr('href', link); $('#printInvoiceButton').attr('data-id', Id); }else{ $('#PreviewInvoiceModal').modal('hide'); @@ -271,4 +274,20 @@ } }); }); + + $(document).on('click', '.pdf-invoice-link', function(e) { + e.preventDefault(); + + var Id = $(this).data('invoices-id'); + var encId = $(this).data('enc-id'); + var link = '' + encId; + + // Copy the link to clipboard + navigator.clipboard.writeText(link).then(() => { + alert("Invoice link copied! You can paste it into WhatsApp."); + }).catch(() => { + alert("Failed to copy link."); + }); + }); + \ No newline at end of file diff --git a/app/Views/invoice_pdf_layout.php b/app/Views/invoice_pdf_layout.php index 177ac73..9e380ef 100755 --- a/app/Views/invoice_pdf_layout.php +++ b/app/Views/invoice_pdf_layout.php @@ -228,16 +228,32 @@ Total - -  -Discount -0.00 -   Grand Total ₹  + +  +Discount +discount) ? number_format((float)$invoice[0]->discount, 2, '.', '') : '0.00'; ?> + + +  +Paid Advance +paid_advance) ? number_format((float)$invoice[0]->paid_advance, 2, '.', '') : '0.00'; ?> + + +  +Balance Amount + + discount; + $paid_advance = (float) $invoice[0]->paid_advance; + $balance = $grandTotal - ($discount + $paid_advance); ?> + ₹  + +

 

diff --git a/app/Views/product_form.php b/app/Views/product_form.php index 86ba8fa..ae8bcba 100755 --- a/app/Views/product_form.php +++ b/app/Views/product_form.php @@ -421,6 +421,7 @@ $(document).ready(function() { }else{ var total_amount = parseFloat($("input[name='total_amount']").val()); var unitPrice = total_amount / parseFloat((tax / 100) + 1); + if (isNaN(unitPrice)) unitPrice = 0; $("input[name='unit_price']").val(unitPrice.toFixed(2)); }