ochoarobert1 / ccontrol

Simple plugin for Client control, budgeting and invoice. Manage your business operations with ease.
https://robertochoaweb.com/
GNU General Public License v2.0
3 stars 1 forks source link

Invoice Total Calculations #8

Open LebToki opened 8 months ago

LebToki commented 8 months ago
<?php endif; ?>
            </div>

`

Invoice Total: 0
            <script>
        jQuery(document).ready(function($) {
            // Function to format numbers with commas for thousands
            function numberWithCommas(x) {
                return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            }

            // Function to calculate and update the total price
            function calculateTotal() {
                var total = 0;
                $('input[name="item_factura_price[]"]').each(function() {
                    var price = parseFloat($(this).val()) || 0;
                    var qtySelector = $(this).closest('.row-postmeta-items').find('input[name="item_factura_qty[]"]');
                    var qty = parseInt(qtySelector.val()) || 0;
                    total += price * qty;
                });
                // Format the total with commas and update the text
                $('#totalPrice').text(numberWithCommas(total.toFixed(2)));
            }

            // Calculate total when the page loads
            calculateTotal();

            // Recalculate total whenever quantity or price fields change
            $(document).on('change', 'input[name="item_factura_qty[]"], input[name="item_factura_price[]"]', function() {
                calculateTotal();
            });

            // Also recalculate when items are added or removed
            $(document).on('click', '.item-factura-add, .item-factura-remove', function() {
                // Add a slight delay to allow the DOM to update
                setTimeout(calculateTotal, 100);
            });
        });

            </script>
        </div>
        `
ochoarobert1 commented 8 months ago

MAAAN, amazing!!!