File size: 6,316 Bytes
b39b5c2
9d2feae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0af4d4d
4f6bf7b
 
 
 
 
0af4d4d
 
 
4f6bf7b
9d2feae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5f024
9d2feae
 
 
 
 
 
9b5f024
 
 
 
 
 
 
 
 
9d2feae
 
 
9b5f024
9d2feae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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));
}