File size: 10,557 Bytes
063f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c96d1
063f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
38c96d1
 
 
 
 
 
 
 
063f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c96d1
063f5af
 
 
 
 
 
94dc12a
 
063f5af
 
 
 
 
94dc12a
 
063f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409f62b
 
063f5af
409f62b
 
063f5af
 
c280f45
 
 
 
 
 
 
94dc12a
 
c280f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94dc12a
 
 
 
c280f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
063f5af
c280f45
 
 
 
 
 
 
 
 
 
 
063f5af
c280f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94dc12a
063f5af
 
 
 
 
 
 
 
409f62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
063f5af
 
409f62b
38c96d1
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// helper: convert number to Indian-notation words (Crore, Lakh, Thousand, Hundred)
function numberToWords(num) {
  const small = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
    'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
  const tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
  function twoDigit(n) {
    if (n < 20) return small[n];
    return tens[Math.floor(n / 10)] + (n % 10 ? ' ' + small[n % 10] : '');
  }
  let words = '';
  const crore = Math.floor(num / 10000000); num %= 10000000;
  if (crore) words += twoDigit(crore) + ' Crore ';
  const lakh = Math.floor(num / 100000); num %= 100000;
  if (lakh) words += twoDigit(lakh) + ' Lakh ';
  const thousand = Math.floor(num / 1000); num %= 1000;
  if (thousand) words += twoDigit(thousand) + ' Thousand ';
  const hundred = Math.floor(num / 100); num %= 100;
  if (hundred) words += small[hundred] + ' Hundred ';
  if (num) words += (words ? 'and ' : '') + twoDigit(num) + ' ';
  return words.trim() || 'Zero';
}

if (typeof document !== 'undefined') {
  document.addEventListener('DOMContentLoaded', function () {
    const addItemBtn = document.getElementById('add-item');
    const itemsTableBody = document.querySelector('#items-table tbody');
    const form = document.getElementById('quotation-form');
    const output = document.getElementById('quotation-output');

    function updateSerialNumbers() {
      itemsTableBody.querySelectorAll('tr').forEach((row, i) => {
        row.querySelector('.item-slno').textContent = i + 1;
      });
    }

    function addItemRow() {
      const row = document.createElement('tr');
      row.innerHTML = `
        <td class="item-slno" data-label="S.No"></td>
        <td data-label="Description"><input type="text" class="item-desc" placeholder="Item Description" required></td>
        <td data-label="HSN Code"><input type="text" class="item-hsn" placeholder="HSN Code"></td>
        <td data-label="Qty"><input type="number" class="item-qty" value="1" min="0" required></td>
        <td data-label="Unit Price"><input type="number" class="item-price" value="0" min="0" required></td>
        <td data-label="Discount (%)"><input type="number" class="item-discount" value="0" min="0" max="100" step="0.01" placeholder="Discount %"></td>
        <td class="item-amount" data-label="Amount">0.00</td>
        <td data-label="Action"><button type="button" class="remove-item">Remove</button></td>
      `;
      itemsTableBody.appendChild(row);
      updateSerialNumbers();
      const inputs = row.querySelectorAll('input');
      inputs.forEach(input => input.addEventListener('input', updateItemAmount));
      row.querySelector('.remove-item').addEventListener('click', () => {
        row.remove();
        updateSerialNumbers();
      });
    }

    function updateItemAmount(e) {
      const row = e.target.closest('tr');
      const qty = parseFloat(row.querySelector('.item-qty').value) || 0;
      const price = parseFloat(row.querySelector('.item-price').value) || 0;
      const discountRate = parseFloat(row.querySelector('.item-discount').value) || 0;
      const discountAmount = qty * price * discountRate / 100;
      const amount = qty * price - discountAmount;
      row.querySelector('.item-amount').textContent = amount.toFixed(2);
    }

    addItemBtn.addEventListener('click', addItemRow);

    form.addEventListener('submit', function (e) {
      e.preventDefault();
      const data = new FormData(form);
      const company = {
        name: data.get('company-name'),
        address: data.get('company-address'),
        phone: data.get('company-phone'),
        email: data.get('company-email'),
        gstin: data.get('company-gstin')
      };
      const customer = {
        name: data.get('customer-name'),
        address: data.get('customer-address'),
        phone: data.get('customer-phone'),
        email: data.get('customer-email'),
        gstin: data.get('customer-gstin')
      };
      const quotationNumber = data.get('quotation-number');
      const quotationDate = data.get('quotation-date');
      const igstRate = parseFloat(data.get('igst-rate')) || 0;
      const freightCharges = parseFloat(data.get('freight-charges')) || 0;
      const bank = {
        name: data.get('bank-name'),
        account: data.get('bank-account'),
        ifsc: data.get('bank-ifsc'),
        branch: data.get('bank-branch')
      };
      const items = [];
      itemsTableBody.querySelectorAll('tr').forEach(row => {
        items.push({
          description: row.querySelector('.item-desc').value,
          hsn: row.querySelector('.item-hsn').value,
          qty: parseFloat(row.querySelector('.item-qty').value) || 0,
          price: parseFloat(row.querySelector('.item-price').value) || 0,
          discount: parseFloat(row.querySelector('.item-discount').value) || 0,
          amount: parseFloat(row.querySelector('.item-amount').textContent) || 0
        });
      });
      const { subtotal, igstAmount, _total, rOff, finalTotal } = calculateQuotation(items, igstRate, freightCharges);

      // convert total to words (Rupees and Paise)
      const rupeePart = Math.floor(finalTotal);
      const paisePart = Math.round((finalTotal - rupeePart) * 100);
      const rupeeWords = numberToWords(rupeePart);
      const paiseWords = paisePart > 0 ? numberToWords(paisePart) : '';

      let html = `
        <div class="quotation-print">
          <div class="header">
            <div class="company-details">
              <strong>${company.name}</strong><br>
              {{address}}<br>
              GST NO. : ${company.gstin || ''}<br>
              CONTACT NO : ${company.phone} ${company.email}
            </div>
            <div class="quotation-title">
              <h1>QUOTATION</h1>
              <table class="quote-meta">
                <tr>
                  <td>QUO. NO</td>
                  <td>${quotationNumber}</td>
                </tr>
                <tr>
                  <td>DATE</td>
                  <td>${quotationDate}</td>
                </tr>
              </table>
            </div>
          </div>

          <div class="customer-info">
            <strong>CUSTOMER INFO</strong><br>
            ${customer.name}<br>
            {{cutomer_address}}<br>
            GST NO. : ${customer.gstin || ''}<br>
            CONTACT NO : ${customer.phone} ${customer.email}
          </div>

          <table class="items-table-print">
            <thead>
              <tr>
                <th>SL NO</th>
                <th>DESCRIPTION</th>
                <th>HSN CODE</th>
                <th>QTY</th>
                <th>UNIT PRICE</th>
                <th>DISCOUNT</th>
                <th>AMOUNT</th>
              </tr>
            </thead>
            <tbody>
      `;
      items.forEach((item, idx) => {
        html += `
              <tr>
                <td>${idx + 1}</td>
                <td>${item.description}</td>
                <td>${item.hsn}</td>
                <td>${item.qty}</td>
                <td>${item.price.toFixed(2)}</td>
                <td>${item.discount.toFixed(2)}%</td>
                <td>${item.amount.toFixed(2)}</td>
              </tr>
        `;
      });
      // Add empty rows to fill the page
      for (let i = items.length; i < 10; i++) {
        html += '<tr><td>&nbsp;</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
      }

      html += `
            </tbody>
          </table>

          <div class="footer">
            <div class="notes-and-total">
              <div class="notes">
                SPECIAL NOTES:<br><br>
                AMOUNT IN WORDS:<br>
                <strong>${rupeeWords}${paiseWords ? ' and ' + paiseWords + ' Paise' : ''} only</strong><br><br>
                Delivery : 7 To 10 working days after receiving PO<br><br>
                BANK DETAILS FOR RTGS/ NEFT ${bank.name}, BRANCH: ${bank.branch}<br>
                A/C NO: ${bank.account}<br>
                IFSC CODE: ${bank.ifsc}
              </div>
              <div class="totals">
                <table>
                  <tr>
                    <td>SUB TOTAL</td>
                    <td>${subtotal.toFixed(2)}</td>
                  </tr>
                  <tr>
                    <td>FREIGHT CHARGES</td>
                    <td>${freightCharges.toFixed(2)}</td>
                  </tr>
                  <tr>
                    <td>IGST</td>
                    <td>${igstAmount.toFixed(2)}</td>
                  </tr>
                  <tr>
                    <td>R/OFF</td>
                    <td>${rOff.toFixed(2)}</td>
                  </tr>
                  <tr>
                    <td><strong>TOTAL</strong></td>
                    <td><strong>${finalTotal.toFixed(2)}</strong></td>
                  </tr>
                </table>
              </div>
            </div>
            <div class="disclaimer">
              This quotation is not a contract or a bill. It is our best guess at the total price for the service and goods described above. The customer will be billed after indicating acceptance of this quote. Payment will be due prior to the delivery of service and goods. Please fax or mail the signed quote to the address listed above.
            </div>
            <div class="thank-you">
              Thank you for your business!
            </div>
          </div>
          <button onclick="window.print()">Print / Save as PDF</button>
        </div>
      `;
      html = html.replace('{{address}}', company.address.replace(/\n/g, '<br>'));
      html = html.replace('{{cutomer_address}}', company.address.replace(/\n/g, '<br>'));
      output.innerHTML = html;
      output.style.display = 'block';
      document.getElementById('form-container').style.display = 'none';
      addItemRow();
    });
  });
}

function calculateQuotation(items, igstRate, freightCharges) {
  const subtotal = items.reduce((sum, i) => sum + i.amount, 0);
  const igstAmount = (subtotal * igstRate) / 100;
  const total = subtotal + igstAmount + freightCharges;
  const totalBeforeRoundOff = total;
  const finalTotal = Math.round(totalBeforeRoundOff);
  const rOff = totalBeforeRoundOff - finalTotal;

  return {
    subtotal,
    igstAmount,
    total,
    rOff,
    finalTotal
  };
}

// Export for testing (Node.js)
if (typeof module !== 'undefined' && module.exports) {
  module.exports = { numberToWords, calculateQuotation };
}