sbn_invoice / utils.js
Nra's picture
Upload 13 files
b39b5c2 verified
// Version: v5
const invoiceDateInput = document.getElementById('invoiceDate');
const invoiceNumberInput = document.getElementById('invoiceNumber');
const courseTopicSelect = document.getElementById('courseTopicSelect'); // New dropdown
const displayInvoiceDate = document.getElementById('displayInvoiceDate');
const displayTopic = document.getElementById('displayTopic');
const displayInvoiceNo = document.getElementById('displayInvoiceNo');
const displayBaseAmount = document.getElementById('displayBaseAmount');
const subTotalSpan = document.getElementById('subTotal');
const cgstAmountSpan = document.getElementById('cgstAmount');
const sgstAmountSpan = document.getElementById('sgstAmount');
const igstAmountSpan = document.getElementById('igstAmount');
const totalAmountSpan = document.getElementById('totalAmount');
const amountInWordsSpan = document.getElementById('amountInWords');
const taxableValueSpan = document.getElementById('taxableValue');
const centralTaxSpan = document.getElementById('centralTax');
const stateTaxSpan = document.getElementById('stateTax');
const interstateTaxSpan = document.getElementById('interstateTax');
const totalTaxableValueSpan = document.getElementById('totalTaxableValue');
const totalCentralTaxSpan = document.getElementById('totalCentralTax');
const totalStateTaxSpan = document.getElementById('totalStateTax');
const totalInterstateTaxSpan = document.getElementById('totalInterstateTax');
const taxAmountInWordsSpan = document.getElementById('taxAmountInWords');
function populateTopicsDropdown() {
const courseTopicSelect = document.getElementById('courseTopicSelect');
// Check if msftCourseTopics exists (for MSFT invoice)
const topicsData = typeof msftCourseTopics !== 'undefined' ? msftCourseTopics : courseTopics;
if (!topicsData) {
console.error('No course topics data found');
return;
}
topicsData.forEach(category => {
const optgroup = document.createElement('optgroup');
optgroup.label = category.category;
category.topics.forEach(topic => {
const option = document.createElement('option');
option.value = topic.amount; // Store the amount in the value
option.textContent = topic.name; // Display the topic name
optgroup.appendChild(option);
});
courseTopicSelect.appendChild(optgroup);
});
}
function convertNumberToWords(num) {
const a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
const b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
if ((num = num.toString()).length > 9) return 'overflow';
const n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return 'Zero';
let str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) : '';
return str.trim().charAt(0).toUpperCase() + str.trim().slice(1) + ' Rupees Only';
}
function generateInvoiceNumber() {
const date = new Date();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Get month (0-11) + 1, pad with 0
const year = date.getFullYear();
const prefix = "INV"; // Example prefix, can be customized
return `${prefix}/${month}/${year}`;
}
function updateInvoice() {
// Get selected values
const selectedDate = invoiceDateInput.value;
const seq = invoiceNumberInput.value;
// Get selected topic name and its associated amount
const selectedOption = courseTopicSelect.options[courseTopicSelect.selectedIndex];
const selectedTopic = selectedOption ? selectedOption.textContent : 'INV_TOPIC';
const baseAmount = parseFloat(selectedOption ? selectedOption.value : 0) || 0;
// Generate full invoice number
let fullInvoiceNo = 'INV_NUMBER';
if (selectedDate && seq) {
const date = new Date(selectedDate);
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
fullInvoiceNo = `${seq}/${month}/${year}`;
}
// Update display elements
displayInvoiceDate.textContent = selectedDate ? new Date(selectedDate).toLocaleDateString('en-GB') : 'INV_DATE';
displayTopic.textContent = 'Education Services : ' + selectedTopic;
displayInvoiceNo.textContent = fullInvoiceNo;
displayBaseAmount.textContent = baseAmount.toFixed(2);
// --- Calculations for GST at 18% ---
const gstRate = 0.18;
const igstAmount = baseAmount * gstRate; // Full 18% IGST for interstate supply
// Set CGST and SGST to 0 for IGST calculation
const cgstAmount = 0;
const sgstAmount = 0;
const totalAmount = baseAmount + igstAmount;
const taxAmount = igstAmount;
// Update total amounts
subTotalSpan.textContent = baseAmount.toFixed(2);
cgstAmountSpan.textContent = cgstAmount.toFixed(2);
sgstAmountSpan.textContent = sgstAmount.toFixed(2);
igstAmountSpan.textContent = igstAmount.toFixed(2);
totalAmountSpan.textContent = totalAmount.toFixed(2);
amountInWordsSpan.textContent = convertNumberToWords(totalAmount.toFixed(0)); // Convert to integer for words
// Update tax summary table
taxableValueSpan.textContent = baseAmount.toFixed(2);
centralTaxSpan.textContent = cgstAmount.toFixed(2);
stateTaxSpan.textContent = sgstAmount.toFixed(2);
interstateTaxSpan.textContent = igstAmount.toFixed(2);
totalTaxableValueSpan.textContent = baseAmount.toFixed(2);
totalCentralTaxSpan.textContent = cgstAmount.toFixed(2);
totalStateTaxSpan.textContent = sgstAmount.toFixed(2);
totalInterstateTaxSpan.textContent = igstAmount.toFixed(2);
taxAmountInWordsSpan.textContent = convertNumberToWords(taxAmount.toFixed(0));
}