سامانه صدور فاکتور و حسابداری

Update print statement from 'Hello' to 'Goodbye'

davoodmehraban89 committed Sep 17, 2026 at 08:28 UTC 13e1004b8f6e2a4476e9906c7771593e03613432
1 file changed +444 -335
index.html
+444 -335
@@ -365,15 +365,25 @@
365 <span>مالیات ارزش افزوده</span>
366 <select id="invoice-vat-mode" class="form-control" style="width:140px; min-height:36px;" onchange="handleVatModeChange()">
367 <option value="none">غیرفعال</option>
368 - <option value="with_vat">فعال (درصدی)</option>
368 + <option value="with_vat">فعال</option>
369 </select>
370 </div>
371 - <div id="box-invoice-vat-rate" style="display:none; display:flex; justify-content:space-between; align-items:center; margin-bottom:8px;">
372 - <span>درصد مالیات</span>
371 + <div id="box-invoice-vat-rate" style="display:none; justify-content:space-between; align-items:center; margin-bottom:8px; gap:8px;">
372 + <select id="invoice-vat-type" class="form-control" style="width:110px; min-height:36px;" onchange="handleVatTypeChange()">
373 + <option value="percent">درصدی</option>
374 + <option value="fixed">مبلغ ثابت</option>
375 + </select>
376 <input type="number" id="invoice-vat-rate-input" class="form-control" style="width:100px; min-height:36px;" value="10" oninput="recomputeTotals()" />
377 </div>
375 - <div style="display:flex; justify-content:space-between; align-items:center;">
378 + <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:8px; gap:8px;">
379 <span>تخفیف کلی فاکتور</span>
380 + <select id="invoice-discount-type" class="form-control" style="width:110px; min-height:36px;" onchange="handleDiscountTypeChange()">
381 + <option value="fixed">مبلغ ثابت</option>
382 + <option value="percent">درصدی</option>
383 + </select>
384 + </div>
385 + <div style="display:flex; justify-content:space-between; align-items:center;">
386 + <span id="label-invoice-discount-value">مبلغ تخفیف (ریال)</span>
387 <input type="number" id="invoice-discount-fixed" class="form-control" style="width:140px; min-height:36px;" value="0" oninput="recomputeTotals()" />
388 </div>
389 </div>
@@ -1683,31 +1693,33 @@
1693 }
1694
1695 function handleInvoiceKindChange() {
1686 - const kind = document.getElementById('invoice-kind').value;
1687 - const vatModeSelect = document.getElementById('invoice-vat-mode');
1688 - const rateBox = document.getElementById('box-invoice-vat-rate');
1689 -
1690 - if (kind === 'formal') {
1691 - vatModeSelect.disabled = false;
1692 - handleVatModeChange();
1693 - } else {
1694 - vatModeSelect.value = 'none';
1695 - vatModeSelect.disabled = true;
1696 - rateBox.style.display = 'none';
1697 - }
1696 + // توجه: قبلاً مالیات فقط برای فاکتور «رسمی» مجاز بود. طبق نمونه واقعی مرجع
1697 + // (پیش‌فاکتور غیررسمی که مالیات ۱۰٪ در آن محاسبه شده بود) این محدودیت با
1698 + // رفتار واقعی مرجع در تناقض بود، پس حذف شد: مالیات برای هر نوع سند قابل فعال‌سازی است.
1699 + handleVatModeChange();
1700 recomputeTotals();
1701 }
1702
1703 function handleVatModeChange() {
1702 - const kind = document.getElementById('invoice-kind').value;
1704 const mode = document.getElementById('invoice-vat-mode').value;
1705 const rateBox = document.getElementById('box-invoice-vat-rate');
1706 + rateBox.style.display = (mode === 'with_vat') ? 'flex' : 'none';
1707 + recomputeTotals();
1708 + }
1709
1706 - if (kind === 'formal' && mode === 'with_vat') {
1707 - rateBox.style.display = 'flex';
1708 - } else {
1709 - rateBox.style.display = 'none';
1710 - }
1710 + function handleVatTypeChange() {
1711 + const type = document.getElementById('invoice-vat-type').value;
1712 + const input = document.getElementById('invoice-vat-rate-input');
1713 + input.value = (type === 'percent') ? 10 : 0;
1714 + recomputeTotals();
1715 + }
1716 +
1717 + function handleDiscountTypeChange() {
1718 + const type = document.getElementById('invoice-discount-type').value;
1719 + const label = document.getElementById('label-invoice-discount-value');
1720 + const input = document.getElementById('invoice-discount-fixed');
1721 + label.innerText = (type === 'percent') ? 'درصد تخفیف (٪)' : 'مبلغ تخفیف (ریال)';
1722 + input.value = 0;
1723 recomputeTotals();
1724 }
1725
@@ -1870,15 +1882,19 @@
1882 subtotal += line;
1883 });
1884
1873 - const fixedDiscount = parseFloat(document.getElementById('invoice-discount-fixed')?.value) || 0;
1874 - const kind = document.getElementById('invoice-kind')?.value || 'non_formal';
1885 + const discountType = document.getElementById('invoice-discount-type')?.value || 'fixed';
1886 + const discountInput = parseFloat(document.getElementById('invoice-discount-fixed')?.value) || 0;
1887 + const rawDiscount = (discountType === 'percent') ? Math.round(subtotal * (discountInput / 100)) : discountInput;
1888 + const fixedDiscount = Math.max(0, Math.min(rawDiscount, subtotal)); // تخفیف هرگز نباید از جمع کل بیشتر شود
1889 +
1890 const vatMode = document.getElementById('invoice-vat-mode')?.value || 'none';
1891 + const vatType = document.getElementById('invoice-vat-type')?.value || 'percent';
1892 const vatRate = parseFloat(document.getElementById('invoice-vat-rate-input')?.value) || 0;
1893
1894 const afterDiscount = Math.max(0, subtotal - fixedDiscount);
1895 let vatAmount = 0;
1880 - if (kind === 'formal' && vatMode === 'with_vat' && vatRate > 0) {
1881 - vatAmount = Math.round(afterDiscount * (vatRate / 100));
1896 + if (vatMode === 'with_vat') {
1897 + vatAmount = (vatType === 'fixed') ? Math.max(0, Math.round(vatRate)) : Math.round(afterDiscount * (vatRate / 100));
1898 }
1899 const grandTotal = afterDiscount + vatAmount;
1900
@@ -1892,7 +1908,7 @@
1908 if (grandEl) grandEl.innerText = grandTotal.toLocaleString('fa-IR') + ' ریال';
1909 if (wordsEl) wordsEl.innerText = 'به حروف: ' + numberToPersianWords(grandTotal);
1910
1895 - return { subtotal, fixedDiscount, vatAmount, grandTotal, vatRate };
1911 + return { subtotal, fixedDiscount, discountType, discountInput, vatAmount, vatType, grandTotal, vatRate };
1912 }
1913
1914 function commitSaveInvoice() {
@@ -1959,7 +1975,10 @@
1975 paymentMethodLabel: 'نقدی',
1976 subtotal: totals.subtotal,
1977 discount: totals.fixedDiscount,
1978 + discountType: totals.discountType,
1979 + discountInput: totals.discountInput,
1980 vat: totals.vatAmount,
1981 + vatType: totals.vatType,
1982 vatRate: totals.vatRate,
1983 grandTotal: totals.grandTotal,
1984 description,
@@ -1979,7 +1998,10 @@
1998 paymentMethodLabel: 'نقدی',
1999 subtotal: totals.subtotal,
2000 discount: totals.fixedDiscount,
2001 + discountType: totals.discountType,
2002 + discountInput: totals.discountInput,
2003 vat: totals.vatAmount,
2004 + vatType: totals.vatType,
2005 vatRate: totals.vatRate,
2006 grandTotal: totals.grandTotal,
2007 description,
@@ -2008,11 +2030,14 @@
2030 document.getElementById('invoice-company-id').value = inv.companyId;
2031 document.getElementById('invoice-kind').value = inv.kind;
2032 document.getElementById('invoice-vat-mode').value = inv.vat > 0 ? 'with_vat' : 'none';
2033 + document.getElementById('invoice-vat-type').value = inv.vatType || 'percent';
2034 document.getElementById('invoice-vat-rate-input').value = inv.vatRate || 10;
2035 document.getElementById('invoice-number').value = inv.number;
2036 document.getElementById('invoice-contact-id').value = inv.contactId;
2037 document.getElementById('invoice-date-input').value = inv.date;
2015 - document.getElementById('invoice-discount-fixed').value = inv.discount || 0;
2038 + document.getElementById('invoice-discount-type').value = inv.discountType || 'fixed';
2039 + document.getElementById('label-invoice-discount-value').innerText = (inv.discountType === 'percent') ? 'درصد تخفیف (٪)' : 'مبلغ تخفیف (ریال)';
2040 + document.getElementById('invoice-discount-fixed').value = (inv.discountType === 'percent') ? (inv.discountInput || 0) : (inv.discount || 0);
2041 document.getElementById('invoice-desc-input').value = inv.description || '';
2042
2043 handleInvoiceKindChange();
@@ -2037,7 +2062,10 @@
2062 document.getElementById('btn-save-invoice').innerText = '💾 ثبت فاکتور';
2063 document.getElementById('invoice-items-table-body').innerHTML = '';
2064 document.getElementById('invoice-number').value = '';
2065 + document.getElementById('invoice-discount-type').value = 'fixed';
2066 + document.getElementById('label-invoice-discount-value').innerText = 'مبلغ تخفیف (ریال)';
2067 document.getElementById('invoice-discount-fixed').value = 0;
2068 + document.getElementById('invoice-vat-type').value = 'percent';
2069 document.getElementById('invoice-desc-input').value = '';
2070 addInvoiceItemRow();
2071 applyDefaultSettingsToForm();
@@ -2075,335 +2103,373 @@
2103 }
2104
2105 /* =========================================================================
2078 - سیستم چاپ پیشرفته فاکتورهای رسمی و غیررسمی با کنترل حداکثر ۱۰ ردیف در صفحه
2106 + سیستم چاپ فاکتور رسمی/غیررسمی با صفحه‌بندی بر اساس اندازه‌گیری واقعی ارتفاع
2107 + (اصلاح‌شده: قبلاً سقف ثابت ۱۰ ردیف در صفحه باعث می‌شد فاکتورهای کم‌ردیف هم
2108 + به‌اشتباه ۲ صفحه‌ای چاپ شوند — دقیقاً همان باگی که در تست شما رخ داد.
2109 + الان ارتفاع واقعی هر صفحه در مرورگر اندازه‌گیری می‌شود و فقط وقتی محتوا
2110 + واقعاً جا نمی‌شود صفحه جدید باز می‌شود.)
2111 ========================================================================= */
2080 - function renderAndPrintDirect(invId) {
2081 - const inv = getMyInvoices().find(i => i.id === invId);
2082 - if (!inv) return;
2083 -
2084 - const myCompanies = getMyCompanies();
2085 - const myContacts = getMyContacts();
2112
2087 - const seller = myCompanies.find(c => c.id === inv.companyId) || myCompanies[0] || {};
2088 - const contact = myContacts.find(c => c.id === inv.contactId) || {};
2089 -
2090 - const isFormal = (inv.kind === 'formal');
2091 - const itemsPerPage = 10;
2092 - const totalPages = Math.ceil(inv.items.length / itemsPerPage) || 1;
2093 - let containerHtml = '';
2094 -
2095 - let runningSubtotal = 0;
2096 - let runningDiscount = 0;
2097 - let runningVat = 0;
2098 -
2099 - for (let p = 0; p < totalPages; p++) {
2100 - const pageItems = inv.items.slice(p * itemsPerPage, (p + 1) * itemsPerPage);
2101 - const isLastPage = (p === totalPages - 1);
2102 -
2103 - let pageSubtotal = 0;
2104 - let pageDiscount = 0;
2105 - let pageVat = 0;
2106 -
2107 - let rowsHtml = '';
2108 -
2109 - if (isFormal) {
2110 - rowsHtml = pageItems.map((it, idx) => {
2111 - const absoluteIndex = (p * itemsPerPage) + idx + 1;
2112 - const lineSubtotal = it.lineTotal;
2113 - pageSubtotal += lineSubtotal;
2114 -
2115 - const lineDiscount = (inv.subtotal > 0) ? Math.round((inv.discount || 0) * (lineSubtotal / inv.subtotal)) : 0;
2116 - pageDiscount += lineDiscount;
2117 -
2118 - const lineVat = (inv.subtotal > 0 && inv.vat > 0) ? Math.round((lineSubtotal - lineDiscount) * ((inv.vatRate || 10) / 100)) : 0;
2119 - pageVat += lineVat;
2120 -
2121 - const lineNet = lineSubtotal - lineDiscount + lineVat;
2122 -
2123 - return `
2124 - <tr style="text-align:center;">
2125 - <td style="border:1px solid #1e293b; padding:4px 6px; font-size:11px;">${absoluteIndex}</td>
2126 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:right; font-size:11px; font-weight:600;">${it.prodName}</td>
2127 - <td style="border:1px solid #1e293b; padding:4px 6px; font-size:11px;">${it.qty}</td>
2128 - <td style="border:1px solid #1e293b; padding:4px 6px; font-size:11px;">${it.unit || 'عدد'}</td>
2129 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:left; font-size:11px;">${it.price.toLocaleString('fa-IR')}</td>
2130 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:left; font-size:11px;">${lineSubtotal.toLocaleString('fa-IR')}</td>
2131 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:left; font-size:11px;">${lineDiscount.toLocaleString('fa-IR')}</td>
2132 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:left; font-size:11px;">${lineVat.toLocaleString('fa-IR')}</td>
2133 - <td style="border:1px solid #1e293b; padding:4px 6px; text-align:left; font-size:11px; font-weight:bold;">${lineNet.toLocaleString('fa-IR')}</td>
2134 - </tr>
2135 - `;
2136 - }).join('');
2113 + function mmToPx(mm) { return mm * (96 / 25.4); }
2114 + const PAGE_USABLE_HEIGHT_PX = mmToPx(271); // A4(297) - حاشیه بالا/پایین(۸+۸) - حاشیه احتیاط
2115 +
2116 + // محاسبه ردیف‌به‌ردیف تخفیف/مالیات با اصلاح باقیمانده روی آخرین ردیف،
2117 + // تا جمع ردیف‌ها همیشه دقیقاً با جمع کل فاکتور یکی باشد (نه فقط تقریبی).
2118 + function computeInvoiceLines(inv) {
2119 + const n = inv.items.length;
2120 + const afterDiscountTotal = Math.max(0, (inv.subtotal || 0) - (inv.discount || 0));
2121 + let usedDiscount = 0, usedVat = 0;
2122 + return inv.items.map((it, idx) => {
2123 + const lineSubtotal = it.lineTotal;
2124 + const isLastItem = (idx === n - 1);
2125 + let lineDiscount, lineVat;
2126 + if (isLastItem) {
2127 + lineDiscount = Math.max(0, (inv.discount || 0) - usedDiscount);
2128 + lineVat = Math.max(0, (inv.vat || 0) - usedVat);
2129 } else {
2138 - rowsHtml = pageItems.map((it, idx) => {
2139 - const absoluteIndex = (p * itemsPerPage) + idx + 1;
2140 - pageSubtotal += it.lineTotal;
2141 -
2142 - return `
2143 - <tr style="text-align:center;">
2144 - <td style="border:1px solid #334155; padding:5px 6px; font-size:11px;">${absoluteIndex}</td>
2145 - <td style="border:1px solid #334155; padding:5px 8px; text-align:right; font-size:11px; font-weight:600;">${it.prodName}</td>
2146 - <td style="border:1px solid #334155; padding:5px 6px; font-size:11px;">${it.unit || 'عدد'}</td>
2147 - <td style="border:1px solid #334155; padding:5px 6px; font-size:11px;">${it.qty}</td>
2148 - <td style="border:1px solid #334155; padding:5px 6px; text-align:left; font-size:11px;">${it.price.toLocaleString('fa-IR')}</td>
2149 - <td style="border:1px solid #334155; padding:5px 6px; text-align:left; font-size:11px; font-weight:bold;">${it.lineTotal.toLocaleString('fa-IR')}</td>
2150 - </tr>
2151 - `;
2152 - }).join('');
2130 + lineDiscount = (inv.subtotal > 0) ? Math.round((inv.discount || 0) * (lineSubtotal / inv.subtotal)) : 0;
2131 + const lineAfterDiscount = lineSubtotal - lineDiscount;
2132 + lineVat = (afterDiscountTotal > 0 && inv.vat > 0) ? Math.round((inv.vat || 0) * (lineAfterDiscount / afterDiscountTotal)) : 0;
2133 }
2134 + usedDiscount += lineDiscount;
2135 + usedVat += lineVat;
2136 + const lineNet = lineSubtotal - lineDiscount + lineVat;
2137 + return { rowNo: idx + 1, prodName: it.prodName, qty: it.qty, unit: it.unit || 'عدد', price: it.price, lineSubtotal, lineDiscount, lineVat, lineNet };
2138 + });
2139 + }
2140
2155 - const cumulativeBefore = runningSubtotal;
2156 - runningSubtotal += pageSubtotal;
2157 - runningDiscount += pageDiscount;
2158 - runningVat += pageVat;
2141 + function buildFormalPage(items, seller, contact, inv, opt) {
2142 + const rowsHtml = items.map(li => `
2143 + <tr style="text-align:center;">
2144 + <td style="border:1px solid #1e293b; padding:3px 6px; font-size:10.5px;">${li.rowNo.toLocaleString('fa-IR')}</td>
2145 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:right; font-size:10.5px; font-weight:600;">${li.prodName}</td>
2146 + <td style="border:1px solid #1e293b; padding:3px 6px; font-size:10.5px;">${li.qty.toLocaleString('fa-IR')}</td>
2147 + <td style="border:1px solid #1e293b; padding:3px 6px; font-size:10.5px;">${li.unit}</td>
2148 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:left; font-size:10.5px;">${li.price.toLocaleString('fa-IR')}</td>
2149 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:left; font-size:10.5px;">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
2150 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:left; font-size:10.5px;">${li.lineDiscount.toLocaleString('fa-IR')}</td>
2151 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:left; font-size:10.5px;">${li.lineVat.toLocaleString('fa-IR')}</td>
2152 + <td style="border:1px solid #1e293b; padding:3px 6px; text-align:left; font-size:10.5px; font-weight:bold;">${li.lineNet.toLocaleString('fa-IR')}</td>
2153 + </tr>
2154 + `).join('');
2155
2160 - if (isFormal) {
2161 - containerHtml += `
2162 - <div class="invoice-a4-page" style="background:#fff; padding:8mm 6mm; color:#000; font-family:'Vazirmatn FD', sans-serif;">
2163 - <!-- سربرگ رسمی -->
2164 - <table style="width:100%; border-collapse:collapse; border:2px solid #1e3a8a; margin-bottom:4px;">
2165 - <tr>
2166 - <td style="width:28%; border:1px solid #1e3a8a; padding:6px; font-size:11px; vertical-align:middle;">
2167 - <div style="font-weight:bold; font-size:13px; color:#1e3a8a;">${seller.name || 'فروشگاه'}</div>
2168 - <div style="font-size:10px; color:#475569; margin-top:2px;">صادرکننده فاکتور رسمی</div>
2169 - </td>
2170 - <td style="width:44%; border:1px solid #1e3a8a; padding:8px; text-align:center; vertical-align:middle; background:#eff6ff;">
2171 - <h1 style="font-size:16px; margin:0; font-weight:bold; color:#1e3a8a;">صورتحساب فروش کالا و خدمات</h1>
2172 - <div style="font-size:11px; margin-top:3px;">(ماده ۱۶۹ مکرر قانون مالیات‌های مستقیم)</div>
2173 - </td>
2174 - <td style="width:28%; border:1px solid #1e3a8a; padding:6px; font-size:11px; vertical-align:middle; text-align:left;">
2175 - <div>شماره سریال: <strong style="color:#b91c1c;">${inv.number}</strong></div>
2176 - <div style="margin-top:2px;">تاریخ صدور: <strong>${inv.date}</strong></div>
2177 - <div style="margin-top:2px; color:#64748b; font-size:10px;">صفحه: ${p + 1} از ${totalPages}</div>
2178 - </td>
2179 - </tr>
2180 - </table>
2156 + const pageSubtotal = items.reduce((s, li) => s + li.lineSubtotal, 0);
2157 + const cumulativeAfter = (opt.cumulativeBefore || 0) + pageSubtotal;
2158
2182 - <!-- بخش الف: مشخصات فروشنده -->
2183 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10.5px; margin-bottom:4px;">
2184 - <tr style="background:#f8fafc; font-weight:bold;">
2185 - <td colspan="4" style="border:1px solid #1e3a8a; padding:3px 6px; color:#1e3a8a;">الف) مشخصات فروشنده</td>
2186 - </tr>
2187 - <tr>
2188 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:15%; background:#f1f5f9;">نام شخص حقیقی/حقوقی:</td>
2189 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:35%;"><strong>${seller.name || '—'}</strong></td>
2190 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:15%; background:#f1f5f9;">شماره اقتصادی:</td>
2191 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:35%;"><span>${seller.economic_code || '—'}</span></td>
2192 - </tr>
2193 - <tr>
2194 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">شماره ثبت / شناسه ملی:</td>
2195 - <td style="border:1px solid #cbd5e1; padding:4px 6px;"><span>${seller.national_id || seller.reg_number || '—'}</span></td>
2196 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">کد پستی ۱۰ رقمی:</td>
2197 - <td style="border:1px solid #cbd5e1; padding:4px 6px;"><span>${seller.postal_code || '—'}</span></td>
2198 - </tr>
2199 - <tr>
2200 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">نشانی و تلفن:</td>
2201 - <td colspan="3" style="border:1px solid #cbd5e1; padding:4px 6px;">${seller.address || '—'} ${seller.phone ? ' | تلفن: ' + seller.phone : ''}</td>
2202 - </tr>
2203 - </table>
2159 + // «شناسه ملی» و «شماره ثبت» دو مقدار جدا هستند و باید هر دو چاپ شوند —
2160 + // قبلاً با || فقط یکی نمایش داده می‌شد و دیگری از سند رسمی حذف می‌شد.
2161 + const sellerIds = `${seller.national_id || '—'} / ${seller.reg_number || '—'}`;
2162 + const contactIds = `${contact.national_id || '—'} / ${contact.reg_number || '—'}`;
2163 +
2164 + return `
2165 + <div class="invoice-a4-page" style="background:#fff; padding:7mm 6mm; color:#000; font-family:'Vazirmatn FD', sans-serif;">
2166 + <table style="width:100%; border-collapse:collapse; border:2px solid #1e3a8a; margin-bottom:3px;">
2167 + <tr>
2168 + <td style="width:28%; border:1px solid #1e3a8a; padding:5px; font-size:11px; vertical-align:middle;">
2169 + <div style="font-weight:bold; font-size:13px; color:#1e3a8a;">${seller.name || 'فروشگاه'}</div>
2170 + <div style="font-size:9.5px; color:#475569; margin-top:1px;">صادرکننده فاکتور رسمی</div>
2171 + </td>
2172 + <td style="width:44%; border:1px solid #1e3a8a; padding:6px; text-align:center; vertical-align:middle; background:#eff6ff;">
2173 + <h1 style="font-size:15px; margin:0; font-weight:bold; color:#1e3a8a;">صورتحساب فروش کالا و خدمات</h1>
2174 + <div style="font-size:10px; margin-top:2px;">(ماده ۱۶۹ مکرر قانون مالیات‌های مستقیم)</div>
2175 + </td>
2176 + <td style="width:28%; border:1px solid #1e3a8a; padding:5px; font-size:10.5px; vertical-align:middle; text-align:left;">
2177 + <div>شماره سریال: <strong style="color:#b91c1c;">${inv.number}</strong></div>
2178 + <div style="margin-top:1px;">تاریخ صدور: <strong>${inv.date}</strong></div>
2179 + <div style="margin-top:1px; color:#64748b; font-size:9.5px;">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
2180 + </td>
2181 + </tr>
2182 + </table>
2183 +
2184 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10px; margin-bottom:3px;">
2185 + <tr style="background:#f8fafc; font-weight:bold;">
2186 + <td colspan="4" style="border:1px solid #1e3a8a; padding:2px 6px; color:#1e3a8a;">الف) مشخصات فروشنده</td>
2187 + </tr>
2188 + <tr>
2189 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:15%; background:#f1f5f9;">نام شخص حقیقی/حقوقی:</td>
2190 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:35%;"><strong>${seller.name || '—'}</strong></td>
2191 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:15%; background:#f1f5f9;">شماره اقتصادی:</td>
2192 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:35%;"><span>${seller.economic_code || '—'}</span></td>
2193 + </tr>
2194 + <tr>
2195 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">شناسه ملی / شماره ثبت:</td>
2196 + <td style="border:1px solid #cbd5e1; padding:3px 6px;"><span>${sellerIds}</span></td>
2197 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">کد پستی ۱۰ رقمی:</td>
2198 + <td style="border:1px solid #cbd5e1; padding:3px 6px;"><span>${seller.postal_code || '—'}</span></td>
2199 + </tr>
2200 + <tr>
2201 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">نشانی و تلفن:</td>
2202 + <td colspan="3" style="border:1px solid #cbd5e1; padding:3px 6px;">${seller.address || '—'} ${seller.phone ? ' | تلفن: ' + seller.phone : ''}</td>
2203 + </tr>
2204 + </table>
2205
2205 - <!-- بخش ب: مشخصات خریدار -->
2206 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10.5px; margin-bottom:4px;">
2207 - <tr style="background:#f8fafc; font-weight:bold;">
2208 - <td colspan="4" style="border:1px solid #1e3a8a; padding:3px 6px; color:#1e3a8a;">ب) مشخصات خریدار</td>
2206 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10px; margin-bottom:3px;">
2207 + <tr style="background:#f8fafc; font-weight:bold;">
2208 + <td colspan="4" style="border:1px solid #1e3a8a; padding:2px 6px; color:#1e3a8a;">ب) مشخصات خریدار</td>
2209 + </tr>
2210 + <tr>
2211 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:15%; background:#f1f5f9;">نام شخص حقیقی/حقوقی:</td>
2212 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:35%;"><strong>${contact.name || inv.contactName}</strong></td>
2213 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:15%; background:#f1f5f9;">شماره اقتصادی:</td>
2214 + <td style="border:1px solid #cbd5e1; padding:3px 6px; width:35%;"><span>${contact.economic_code || '—'}</span></td>
2215 + </tr>
2216 + <tr>
2217 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">شناسه ملی / شماره ثبت:</td>
2218 + <td style="border:1px solid #cbd5e1; padding:3px 6px;"><span>${contactIds}</span></td>
2219 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">کد پستی ۱۰ رقمی:</td>
2220 + <td style="border:1px solid #cbd5e1; padding:3px 6px;"><span>${contact.postal_code || '—'}</span></td>
2221 + </tr>
2222 + <tr>
2223 + <td style="border:1px solid #cbd5e1; padding:3px 6px; background:#f1f5f9;">نشانی و تلفن:</td>
2224 + <td colspan="3" style="border:1px solid #cbd5e1; padding:3px 6px;">${contact.address || '—'} ${contact.mobile ? ' | همراه: ' + contact.mobile : ''}</td>
2225 + </tr>
2226 + </table>
2227 +
2228 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10px;">
2229 + <thead>
2230 + <tr style="background:#1e3a8a; color:#fff; font-weight:bold; text-align:center;">
2231 + <th style="border:1px solid #1e3a8a; padding:4px; width:28px;">ردیف</th>
2232 + <th style="border:1px solid #1e3a8a; padding:4px 6px; text-align:right;">شرح کالا یا خدمات</th>
2233 + <th style="border:1px solid #1e3a8a; padding:4px; width:42px;">تعداد</th>
2234 + <th style="border:1px solid #1e3a8a; padding:4px; width:42px;">واحد</th>
2235 + <th style="border:1px solid #1e3a8a; padding:4px; width:105px;">مبلغ واحد (ریال)</th>
2236 + <th style="border:1px solid #1e3a8a; padding:4px; width:110px;">مبلغ کل (ریال)</th>
2237 + <th style="border:1px solid #1e3a8a; padding:4px; width:80px;">تخفیف (ریال)</th>
2238 + <th style="border:1px solid #1e3a8a; padding:4px; width:90px;">مالیات (ریال)</th>
2239 + <th style="border:1px solid #1e3a8a; padding:4px; width:120px;">مبلغ نهایی (ریال)</th>
2240 + </tr>
2241 + </thead>
2242 + <tbody>
2243 + ${!opt.isFirstPage ? `
2244 + <tr style="background:#fef3c7; font-weight:bold; font-size:9.5px;">
2245 + <td colspan="5" style="border:1px solid #1e293b; padding:3px; text-align:left;">نقل از صفحه قبل:</td>
2246 + <td colspan="4" style="border:1px solid #1e293b; padding:3px; text-align:left;">${(opt.cumulativeBefore || 0).toLocaleString('fa-IR')} ریال</td>
2247 </tr>
2210 - <tr>
2211 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:15%; background:#f1f5f9;">نام شخص حقیقی/حقوقی:</td>
2212 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:35%;"><strong>${contact.name || inv.contactName}</strong></td>
2213 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:15%; background:#f1f5f9;">شماره اقتصادی:</td>
2214 - <td style="border:1px solid #cbd5e1; padding:4px 6px; width:35%;"><span>${contact.economic_code || '—'}</span></td>
2248 + ` : ''}
2249 + ${rowsHtml}
2250 + ${!opt.isLastPage ? `
2251 + <tr style="background:#f1f5f9; font-weight:bold; font-size:9.5px;">
2252 + <td colspan="5" style="border:1px solid #1e293b; padding:3px; text-align:left;">جمع این صفحه و نقل به صفحه بعد:</td>
2253 + <td colspan="4" style="border:1px solid #1e293b; padding:3px; text-align:left;">${cumulativeAfter.toLocaleString('fa-IR')} ریال</td>
2254 </tr>
2216 - <tr>
2217 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">شماره ثبت / شناسه ملی:</td>
2218 - <td style="border:1px solid #cbd5e1; padding:4px 6px;"><span>${contact.national_id || contact.reg_number || '—'}</span></td>
2219 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">کد پستی ۱۰ رقمی:</td>
2220 - <td style="border:1px solid #cbd5e1; padding:4px 6px;"><span>${contact.postal_code || '—'}</span></td>
2255 + ` : ''}
2256 + </tbody>
2257 + </table>
2258 +
2259 + ${opt.isLastPage ? `
2260 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10.5px; margin-top:3px;">
2261 + <tr>
2262 + <td style="width:60%; border:1px solid #1e3a8a; padding:6px; vertical-align:top; background:#f8fafc;">
2263 + <div style="margin-bottom:4px;"><strong>مبلغ کل به حروف:</strong> ${numberToPersianWords(inv.grandTotal)}</div>
2264 + <div style="margin-bottom:4px;"><strong>شرایط و نحوه پرداخت:</strong> نقدی ☑ &nbsp;&nbsp;&nbsp; غیرنقدی (نسیه/چک) ☐</div>
2265 + <div style="font-size:9.5px; color:#475569; line-height:1.5;"><strong>توضیحات:</strong> ${inv.description || seller.footer || 'تحویل کالا منوط به تسویه نهایی می‌باشد.'}</div>
2266 + </td>
2267 + <td style="width:40%; border:1px solid #1e3a8a; padding:0;">
2268 + <table style="width:100%; border-collapse:collapse; font-size:10.5px;">
2269 + <tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">جمع کل اقلام:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.subtotal.toLocaleString('fa-IR')} ریال</td></tr>
2270 + <tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">مجموع تخفیف:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.discount.toLocaleString('fa-IR')} ریال</td></tr>
2271 + <tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">مالیات و عوارض ارزش افزوده:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.vat.toLocaleString('fa-IR')} ریال</td></tr>
2272 + <tr style="background:#eff6ff; font-weight:bold; font-size:11.5px; color:#1e3a8a;">
2273 + <td style="padding:5px 8px;">مبلغ قابل پرداخت:</td>
2274 + <td style="padding:5px 8px; text-align:left;">${inv.grandTotal.toLocaleString('fa-IR')} ریال</td>
2275 + </tr>
2276 + </table>
2277 + </td>
2278 + </tr>
2279 + </table>
2280 + <table style="width:100%; border-collapse:collapse; border:1px solid #1e3a8a; margin-top:6px; text-align:center; font-size:10.5px;">
2281 + <tr>
2282 + <td style="width:50%; height:50px; vertical-align:top; border:1px solid #1e3a8a; padding:5px; background:#fff;">
2283 + <div style="font-weight:bold;">مهر و امضای فروشنده</div>
2284 + </td>
2285 + <td style="width:50%; height:50px; vertical-align:top; border:1px solid #1e3a8a; padding:5px; background:#fff;">
2286 + <div style="font-weight:bold;">مهر و امضای خریدار</div>
2287 + </td>
2288 + </tr>
2289 + </table>
2290 + ` : `
2291 + <div style="margin-top:8px; text-align:center; font-size:10.5px; color:#64748b; font-weight:bold;">(ادامه اقلام در صفحه بعد درج گردیده است...)</div>
2292 + `}
2293 + </div>
2294 + `;
2295 + }
2296 +
2297 + function buildNonFormalPage(items, seller, contact, inv, opt) {
2298 + const rowsHtml = items.map(li => `
2299 + <tr style="text-align:center;">
2300 + <td style="border:1px solid #334155; padding:4px 6px; font-size:10.5px;">${li.rowNo.toLocaleString('fa-IR')}</td>
2301 + <td style="border:1px solid #334155; padding:4px 8px; text-align:right; font-size:10.5px; font-weight:600;">${li.prodName}</td>
2302 + <td style="border:1px solid #334155; padding:4px 6px; font-size:10.5px;">${li.unit}</td>
2303 + <td style="border:1px solid #334155; padding:4px 6px; font-size:10.5px;">${li.qty.toLocaleString('fa-IR')}</td>
2304 + <td style="border:1px solid #334155; padding:4px 6px; text-align:left; font-size:10.5px;">${li.price.toLocaleString('fa-IR')}</td>
2305 + <td style="border:1px solid #334155; padding:4px 6px; text-align:left; font-size:10.5px; font-weight:bold;">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
2306 + </tr>
2307 + `).join('');
2308 +
2309 + const pageSubtotal = items.reduce((s, li) => s + li.lineSubtotal, 0);
2310 + const cumulativeAfter = (opt.cumulativeBefore || 0) + pageSubtotal;
2311 +
2312 + return `
2313 + <div class="invoice-a4-page" style="background:#fff; padding:7mm 6mm; color:#000; font-family:'Vazirmatn FD', sans-serif;">
2314 + <table style="width:100%; border-collapse:collapse; border:2px solid #334155; margin-bottom:5px;">
2315 + <tr>
2316 + <td style="width:30%; border:1px solid #334155; padding:6px; vertical-align:middle;">
2317 + <div style="font-weight:bold; font-size:13px; color:#0f172a;">${seller.name || 'فروشگاه'}</div>
2318 + <div style="font-size:10px; color:#475569; margin-top:1px;">تلفن: ${seller.phone || '—'}</div>
2319 + </td>
2320 + <td style="width:40%; border:1px solid #334155; padding:6px; text-align:center; vertical-align:middle; background:#f8fafc;">
2321 + <h1 style="font-size:15px; margin:0; font-weight:bold; color:#0f172a;">${inv.kind === 'pre_invoice' ? 'پیش‌فاکتور فروش کالا و خدمات' : 'فاکتور فروش کالا و خدمات'}</h1>
2322 + </td>
2323 + <td style="width:30%; border:1px solid #334155; padding:6px; font-size:10.5px; vertical-align:middle; text-align:left;">
2324 + <div>شماره فاکتور: <strong style="font-size:11.5px;">#${inv.number}</strong></div>
2325 + <div style="margin-top:1px;">تاریخ: <strong>${inv.date}</strong></div>
2326 + <div style="margin-top:1px; color:#64748b; font-size:9.5px;">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
2327 + </td>
2328 + </tr>
2329 + </table>
2330 +
2331 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:10.5px; margin-bottom:5px;">
2332 + <tr>
2333 + <td style="width:50%; border:1px solid #334155; padding:5px; vertical-align:top; background:#f8fafc;">
2334 + <div style="font-weight:bold; color:#334155; margin-bottom:2px;">مشخصات فروشنده:</div>
2335 + <div>نام: <strong>${seller.name || '—'}</strong></div>
2336 + <div>نشانی: ${seller.address || '—'}</div>
2337 + <div>تلفن تماس: ${seller.phone || '—'}</div>
2338 + </td>
2339 + <td style="width:50%; border:1px solid #334155; padding:5px; vertical-align:top; background:#f8fafc;">
2340 + <div style="font-weight:bold; color:#334155; margin-bottom:2px;">مشخصات خریدار:</div>
2341 + <div>نام: <strong>${contact.name || inv.contactName}</strong></div>
2342 + <div>نشانی: ${contact.address || '—'}</div>
2343 + <div>شماره همراه: ${contact.mobile || '—'}</div>
2344 + </td>
2345 + </tr>
2346 + </table>
2347 +
2348 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:10.5px;">
2349 + <thead>
2350 + <tr style="background:#334155; color:#fff; font-weight:bold; text-align:center;">
2351 + <th style="border:1px solid #334155; padding:5px 4px; width:32px;">ردیف</th>
2352 + <th style="border:1px solid #334155; padding:5px 8px; text-align:right;">شرح کالا | خدمات</th>
2353 + <th style="border:1px solid #334155; padding:5px 4px; width:60px;">واحد</th>
2354 + <th style="border:1px solid #334155; padding:5px 4px; width:60px;">مقدار</th>
2355 + <th style="border:1px solid #334155; padding:5px 4px; width:135px; text-align:left;">قیمت واحد (ریال)</th>
2356 + <th style="border:1px solid #334155; padding:5px 4px; width:150px; text-align:left;">قیمت کل (ریال)</th>
2357 + </tr>
2358 + </thead>
2359 + <tbody>
2360 + ${!opt.isFirstPage ? `
2361 + <tr style="background:#fef3c7; font-weight:bold; font-size:9.5px;">
2362 + <td colspan="5" style="border:1px solid #334155; padding:3px; text-align:left;">نقل از صفحه قبل:</td>
2363 + <td style="border:1px solid #334155; padding:3px; text-align:left;">${(opt.cumulativeBefore || 0).toLocaleString('fa-IR')} ریال</td>
2364 </tr>
2222 - <tr>
2223 - <td style="border:1px solid #cbd5e1; padding:4px 6px; background:#f1f5f9;">نشانی و تلفن:</td>
2224 - <td colspan="3" style="border:1px solid #cbd5e1; padding:4px 6px;">${contact.address || '—'} ${contact.mobile ? ' | همراه: ' + contact.mobile : ''}</td>
2365 + ` : ''}
2366 + ${rowsHtml}
2367 + ${!opt.isLastPage ? `
2368 + <tr style="background:#f1f5f9; font-weight:bold; font-size:9.5px;">
2369 + <td colspan="5" style="border:1px solid #334155; padding:3px; text-align:left;">جمع این صفحه و نقل به صفحه بعد:</td>
2370 + <td style="border:1px solid #334155; padding:3px; text-align:left;">${cumulativeAfter.toLocaleString('fa-IR')} ریال</td>
2371 </tr>
2226 - </table>
2227 -
2228 - <!-- جدول اقلام کالا و خدمات رسمی -->
2229 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:10.5px;">
2230 - <thead>
2231 - <tr style="background:#1e3a8a; color:#fff; font-weight:bold; text-align:center;">
2232 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:30px;">ردیف</th>
2233 - <th style="border:1px solid #1e3a8a; padding:5px 6px; text-align:right;">شرح کالا یا خدمات</th>
2234 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:45px;">تعداد</th>
2235 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:45px;">واحد</th>
2236 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:110px;">مبلغ واحد (ریال)</th>
2237 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:115px;">مبلغ کل (ریال)</th>
2238 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:85px;">تخفیف (ریال)</th>
2239 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:95px;">مالیات (ریال)</th>
2240 - <th style="border:1px solid #1e3a8a; padding:5px 4px; width:125px;">مبلغ نهایی (ریال)</th>
2241 - </tr>
2242 - </thead>
2243 - <tbody>
2244 - ${p > 0 ? `
2245 - <tr style="background:#fef3c7; font-weight:bold; font-size:10px;">
2246 - <td colspan="5" style="border:1px solid #1e293b; padding:4px; text-align:left;">نقل از صفحه قبل:</td>
2247 - <td colspan="4" style="border:1px solid #1e293b; padding:4px; text-align:left;">${cumulativeBefore.toLocaleString('fa-IR')} ریال</td>
2248 - </tr>
2249 - ` : ''}
2250 - ${rowsHtml}
2251 - ${!isLastPage ? `
2252 - <tr style="background:#f1f5f9; font-weight:bold; font-size:10px;">
2253 - <td colspan="5" style="border:1px solid #1e293b; padding:4px; text-align:left;">جمع این صفحه و نقل به صفحه بعد:</td>
2254 - <td colspan="4" style="border:1px solid #1e293b; padding:4px; text-align:left;">${runningSubtotal.toLocaleString('fa-IR')} ریال</td>
2372 + ` : ''}
2373 + </tbody>
2374 + </table>
2375 +
2376 + ${opt.isLastPage ? `
2377 + <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:10.5px; margin-top:5px;">
2378 + <tr>
2379 + <td style="width:60%; border:1px solid #334155; padding:6px; vertical-align:top; background:#f8fafc;">
2380 + <div style="margin-bottom:4px;"><strong>مبلغ به حروف:</strong> ${numberToPersianWords(inv.grandTotal)}</div>
2381 + <div style="font-size:9.5px; color:#475569; line-height:1.5;"><strong>توضیحات:</strong> ${inv.description || seller.footer || 'اعتبار پیش‌فاکتور به مدت ۴۸ ساعت می‌باشد.'}</div>
2382 + </td>
2383 + <td style="width:40%; border:1px solid #334155; padding:0;">
2384 + <table style="width:100%; border-collapse:collapse; font-size:10.5px;">
2385 + <tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">قیمت کل اقلام:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.subtotal.toLocaleString('fa-IR')} ریال</td></tr>
2386 + <tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">تخفیف:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.discount.toLocaleString('fa-IR')} ریال</td></tr>
2387 + ${inv.vat > 0 ? `<tr><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px;">مالیات ارزش افزوده:</td><td style="border-bottom:1px solid #cbd5e1; padding:3px 8px; text-align:left;">${inv.vat.toLocaleString('fa-IR')} ریال</td></tr>` : ''}
2388 + <tr style="background:#f1f5f9; font-weight:bold; font-size:11.5px;">
2389 + <td style="padding:5px 8px;">مبلغ کل (ریال):</td>
2390 + <td style="padding:5px 8px; text-align:left;">${inv.grandTotal.toLocaleString('fa-IR')} ریال</td>
2391 </tr>
2256 - ` : ''}
2257 - </tbody>
2258 - </table>
2392 + </table>
2393 + </td>
2394 + </tr>
2395 + </table>
2396 + <table style="width:100%; border-collapse:collapse; border:1px solid #334155; margin-top:6px; text-align:center; font-size:10.5px;">
2397 + <tr>
2398 + <td style="width:50%; height:50px; vertical-align:top; border:1px solid #334155; padding:5px; background:#fff;">
2399 + <div style="font-weight:bold;">مهر و امضای فروشنده</div>
2400 + </td>
2401 + <td style="width:50%; height:50px; vertical-align:top; border:1px solid #334155; padding:5px; background:#fff;">
2402 + <div style="font-weight:bold;">مهر و امضای خریدار</div>
2403 + </td>
2404 + </tr>
2405 + </table>
2406 + ` : `
2407 + <div style="margin-top:8px; text-align:center; font-size:10.5px; color:#64748b; font-weight:bold;">(ادامه در صفحه بعد...)</div>
2408 + `}
2409 + </div>
2410 + `;
2411 + }
2412
2260 - ${isLastPage ? `
2261 - <!-- بخش جمع‌بندی و تسویه در صفحه آخر -->
2262 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #1e3a8a; font-size:11px; margin-top:4px;">
2263 - <tr>
2264 - <td style="width:60%; border:1px solid #1e3a8a; padding:8px; vertical-align:top; background:#f8fafc;">
2265 - <div style="margin-bottom:6px;"><strong>مبلغ کل به حروف:</strong> ${numberToPersianWords(inv.grandTotal)}</div>
2266 - <div style="margin-bottom:6px;"><strong>شرایط و نحوه پرداخت:</strong> نقدی ☑ &nbsp;&nbsp;&nbsp; غیرنقدی (نسیه/چک) ☐</div>
2267 - <div style="font-size:10px; color:#475569; line-height:1.6;"><strong>توضیحات:</strong> ${inv.description || seller.footer || 'تحویل کالا منوط به تسویه نهایی می‌باشد.'}</div>
2268 - </td>
2269 - <td style="width:40%; border:1px solid #1e3a8a; padding:0;">
2270 - <table style="width:100%; border-collapse:collapse; font-size:11px;">
2271 - <tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">جمع کل اقلام:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.subtotal.toLocaleString('fa-IR')} ریال</td></tr>
2272 - <tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">مجموع تخفیف:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.discount.toLocaleString('fa-IR')} ریال</td></tr>
2273 - <tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">مالیات و عوارض ارزش افزوده:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.vat.toLocaleString('fa-IR')} ریال</td></tr>
2274 - <tr style="background:#eff6ff; font-weight:bold; font-size:12px; color:#1e3a8a;">
2275 - <td style="padding:6px 8px;">مبلغ قابل پرداخت:</td>
2276 - <td style="padding:6px 8px; text-align:left;">${inv.grandTotal.toLocaleString('fa-IR')} ریال</td>
2277 - </tr>
2278 - </table>
2279 - </td>
2280 - </tr>
2281 - </table>
2413 + function renderAndPrintDirect(invId) {
2414 + const inv = getMyInvoices().find(i => i.id === invId);
2415 + if (!inv) return;
2416
2283 - <!-- امضا و تایید رسمی -->
2284 - <table style="width:100%; border-collapse:collapse; border:1px solid #1e3a8a; margin-top:8px; text-align:center; font-size:11px;">
2285 - <tr>
2286 - <td style="width:50%; height:75px; vertical-align:top; border:1px solid #1e3a8a; padding:6px; background:#fff;">
2287 - <div style="font-weight:bold;">مهر و امضای فروشنده</div>
2288 - </td>
2289 - <td style="width:50%; height:75px; vertical-align:top; border:1px solid #1e3a8a; padding:6px; background:#fff;">
2290 - <div style="font-weight:bold;">مهر و امضای خریدار</div>
2291 - </td>
2292 - </tr>
2293 - </table>
2294 - ` : `
2295 - <div style="margin-top:14px; text-align:center; font-size:11px; color:#64748b; font-weight:bold;">(ادامه اقلام در صفحه بعد درج گردیده است...)</div>
2296 - `}
2297 - </div>
2298 - `;
2299 - } else {
2300 - containerHtml += `
2301 - <div class="invoice-a4-page" style="background:#fff; padding:8mm 6mm; color:#000; font-family:'Vazirmatn FD', sans-serif;">
2302 - <!-- سربرگ شکیل پیش‌فاکتور / غیررسمی -->
2303 - <table style="width:100%; border-collapse:collapse; border:2px solid #334155; margin-bottom:6px;">
2304 - <tr>
2305 - <td style="width:30%; border:1px solid #334155; padding:8px; vertical-align:middle;">
2306 - <div style="font-weight:bold; font-size:14px; color:#0f172a;">${seller.name || 'فروشگاه'}</div>
2307 - <div style="font-size:11px; color:#475569; margin-top:2px;">تلفن: ${seller.phone || '—'}</div>
2308 - </td>
2309 - <td style="width:40%; border:1px solid #334155; padding:8px; text-align:center; vertical-align:middle; background:#f8fafc;">
2310 - <h1 style="font-size:16px; margin:0; font-weight:bold; color:#0f172a;">${inv.kind === 'pre_invoice' ? 'پیش‌فاکتور فروش کالا و خدمات' : 'فاکتور فروش کالا و خدمات'}</h1>
2311 - </td>
2312 - <td style="width:30%; border:1px solid #334155; padding:8px; font-size:11px; vertical-align:middle; text-align:left;">
2313 - <div>شماره فاکتور: <strong style="font-size:12px;">#${inv.number}</strong></div>
2314 - <div style="margin-top:2px;">تاریخ: <strong>${inv.date}</strong></div>
2315 - <div style="margin-top:2px; color:#64748b; font-size:10px;">صفحه: ${p + 1} از ${totalPages}</div>
2316 - </td>
2317 - </tr>
2318 - </table>
2417 + const myCompanies = getMyCompanies();
2418 + const myContacts = getMyContacts();
2419 + const seller = myCompanies.find(c => c.id === inv.companyId) || myCompanies[0] || {};
2420 + const contact = myContacts.find(c => c.id === inv.contactId) || {};
2421 + const isFormal = (inv.kind === 'formal');
2422 + const builder = isFormal ? buildFormalPage : buildNonFormalPage;
2423
2320 - <!-- جعبه مشخصات طرفین -->
2321 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:11px; margin-bottom:6px;">
2322 - <tr>
2323 - <td style="width:50%; border:1px solid #334155; padding:6px; vertical-align:top; background:#f8fafc;">
2324 - <div style="font-weight:bold; color:#334155; margin-bottom:3px;">مشخصات فروشنده:</div>
2325 - <div>نام: <strong>${seller.name || '—'}</strong></div>
2326 - <div>نشانی: ${seller.address || '—'}</div>
2327 - <div>تلفن تماس: ${seller.phone || '—'}</div>
2328 - </td>
2329 - <td style="width:50%; border:1px solid #334155; padding:6px; vertical-align:top; background:#f8fafc;">
2330 - <div style="font-weight:bold; color:#334155; margin-bottom:3px;">مشخصات خریدار:</div>
2331 - <div>نام: <strong>${contact.name || inv.contactName}</strong></div>
2332 - <div>نشانی: ${contact.address || '—'}</div>
2333 - <div>شماره همراه: ${contact.mobile || '—'}</div>
2334 - </td>
2335 - </tr>
2336 - </table>
2424 + const allLines = computeInvoiceLines(inv);
2425
2338 - <!-- جدول اقلام فاکتور غیررسمی (۶ ستون) -->
2339 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:11px;">
2340 - <thead>
2341 - <tr style="background:#334155; color:#fff; font-weight:bold; text-align:center;">
2342 - <th style="border:1px solid #334155; padding:6px 4px; width:35px;">ردیف</th>
2343 - <th style="border:1px solid #334155; padding:6px 8px; text-align:right;">شرح کالا | خدمات</th>
2344 - <th style="border:1px solid #334155; padding:6px 4px; width:65px;">واحد</th>
2345 - <th style="border:1px solid #334155; padding:6px 4px; width:65px;">مقدار</th>
2346 - <th style="border:1px solid #334155; padding:6px 4px; width:145px; text-align:left;">قیمت واحد (ریال)</th>
2347 - <th style="border:1px solid #334155; padding:6px 4px; width:160px; text-align:left;">قیمت کل (ریال)</th>
2348 - </tr>
2349 - </thead>
2350 - <tbody>
2351 - ${p > 0 ? `
2352 - <tr style="background:#fef3c7; font-weight:bold; font-size:10px;">
2353 - <td colspan="5" style="border:1px solid #334155; padding:4px; text-align:left;">نقل از صفحه قبل:</td>
2354 - <td style="border:1px solid #334155; padding:4px; text-align:left;">${cumulativeBefore.toLocaleString('fa-IR')} ریال</td>
2355 - </tr>
2356 - ` : ''}
2357 - ${rowsHtml}
2358 - ${!isLastPage ? `
2359 - <tr style="background:#f1f5f9; font-weight:bold; font-size:10px;">
2360 - <td colspan="5" style="border:1px solid #334155; padding:4px; text-align:left;">جمع این صفحه و نقل به صفحه بعد:</td>
2361 - <td style="border:1px solid #334155; padding:4px; text-align:left;">${runningSubtotal.toLocaleString('fa-IR')} ریال</td>
2362 - </tr>
2363 - ` : ''}
2364 - </tbody>
2365 - </table>
2426 + // جعبه‌ی نامرئی برای اندازه‌گیری ارتفاع واقعی رندر شده، دقیقاً با همان
2427 + // عرض صفحه‌ی چاپ (190mm) تا شکستن خطوط متن هم مثل حالت چاپ واقعی باشد.
2428 + const sandbox = document.createElement('div');
2429 + sandbox.style.cssText = 'position:absolute; visibility:hidden; pointer-events:none; left:-9999px; top:0; width:190mm;';
2430 + document.body.appendChild(sandbox);
2431
2367 - ${isLastPage ? `
2368 - <!-- خلاصه فاکتور و امضاها -->
2369 - <table style="width:100%; border-collapse:collapse; border:1.5px solid #334155; font-size:11px; margin-top:6px;">
2370 - <tr>
2371 - <td style="width:60%; border:1px solid #334155; padding:8px; vertical-align:top; background:#f8fafc;">
2372 - <div style="margin-bottom:6px;"><strong>مبلغ به حروف:</strong> ${numberToPersianWords(inv.grandTotal)}</div>
2373 - <div style="font-size:10px; color:#475569; line-height:1.6;"><strong>توضیحات:</strong> ${inv.description || seller.footer || 'اعتبار پیش‌فاکتور به مدت ۴۸ ساعت می‌باشد.'}</div>
2374 - </td>
2375 - <td style="width:40%; border:1px solid #334155; padding:0;">
2376 - <table style="width:100%; border-collapse:collapse; font-size:11px;">
2377 - <tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">قیمت کل اقلام:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.subtotal.toLocaleString('fa-IR')} ریال</td></tr>
2378 - <tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">تخفیف:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.discount.toLocaleString('fa-IR')} ریال</td></tr>
2379 - ${inv.vat > 0 ? `<tr><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px;">مالیات ارزش افزوده:</td><td style="border-bottom:1px solid #cbd5e1; padding:4px 8px; text-align:left;">${inv.vat.toLocaleString('fa-IR')} ریال</td></tr>` : ''}
2380 - <tr style="background:#f1f5f9; font-weight:bold; font-size:12px;">
2381 - <td style="padding:6px 8px;">مبلغ کل (ریال):</td>
2382 - <td style="padding:6px 8px; text-align:left;">${inv.grandTotal.toLocaleString('fa-IR')} ریال</td>
2383 - </tr>
2384 - </table>
2385 - </td>
2386 - </tr>
2387 - </table>
2432 + function measureHeight(items, isFirstPage, isLastPage, cumulativeBefore) {
2433 + sandbox.innerHTML = builder(items, seller, contact, inv, { pageNum: 1, totalPages: 1, isFirstPage, isLastPage, cumulativeBefore });
2434 + return sandbox.firstElementChild.scrollHeight;
2435 + }
2436
2389 - <table style="width:100%; border-collapse:collapse; border:1px solid #334155; margin-top:8px; text-align:center; font-size:11px;">
2390 - <tr>
2391 - <td style="width:50%; height:75px; vertical-align:top; border:1px solid #334155; padding:6px; background:#fff;">
2392 - <div style="font-weight:bold;">مهر و امضای فروشنده</div>
2393 - </td>
2394 - <td style="width:50%; height:75px; vertical-align:top; border:1px solid #334155; padding:6px; background:#fff;">
2395 - <div style="font-weight:bold;">مهر و امضای خریدار</div>
2396 - </td>
2397 - </tr>
2398 - </table>
2399 - ` : `
2400 - <div style="margin-top:14px; text-align:center; font-size:11px; color:#64748b; font-weight:bold;">(ادامه در صفحه بعد...)</div>
2401 - `}
2402 - </div>
2403 - `;
2437 + const pageGroups = [];
2438 + let remaining = allLines.slice();
2439 + let cumulative = 0;
2440 + let isFirstPage = true;
2441 +
2442 + while (remaining.length > 0) {
2443 + const fitsAsLastPage = measureHeight(remaining, isFirstPage, true, cumulative) <= PAGE_USABLE_HEIGHT_PX;
2444 + if (fitsAsLastPage) {
2445 + pageGroups.push({ items: remaining, isFirstPage, isLastPage: true, cumulativeBefore: cumulative });
2446 + remaining = [];
2447 + break;
2448 + }
2449 + let count = remaining.length;
2450 + while (count > 1 && measureHeight(remaining.slice(0, count), isFirstPage, false, cumulative) > PAGE_USABLE_HEIGHT_PX) {
2451 + count--;
2452 }
2453 + const pageItems = remaining.slice(0, count);
2454 + pageGroups.push({ items: pageItems, isFirstPage, isLastPage: false, cumulativeBefore: cumulative });
2455 + cumulative += pageItems.reduce((s, li) => s + li.lineSubtotal, 0);
2456 + remaining = remaining.slice(count);
2457 + isFirstPage = false;
2458 }
2459
2460 + document.body.removeChild(sandbox);
2461 +
2462 + const totalPages = pageGroups.length;
2463 + const containerHtml = pageGroups.map((g, idx) =>
2464 + builder(g.items, seller, contact, inv, {
2465 + pageNum: idx + 1,
2466 + totalPages,
2467 + isFirstPage: g.isFirstPage,
2468 + isLastPage: g.isLastPage,
2469 + cumulativeBefore: g.cumulativeBefore
2470 + })
2471 + ).join('');
2472 +
2473 const printContainer = document.getElementById('printable-invoice');
2474 printContainer.innerHTML = containerHtml;
2475 printContainer.style.display = 'block';
@@ -2933,5 +2999,48 @@
2999 initAuthSystem();
3000 });
3001 </script>
3002 +
3003 +<style>
3004 +/* Final hardening patch */
3005 +.form-control, textarea, select { direction: rtl; }
3006 +.invoice-a4-page { max-width:190mm; margin:auto; }
3007 +@media print {
3008 + .invoice-a4-page { width:190mm!important; min-height:277mm!important; }
3009 + .invoice-row-limit { display:none!important; }
3010 +}
3011 +</style>
3012 +<script>
3013 +(function(){
3014 + const oldSave = window.commitSaveInvoice;
3015 + window.commitSaveInvoice = function(){
3016 + const rows = document.querySelectorAll('#invoice-items-table-body tr');
3017 + if(!rows.length){
3018 + alert('حداقل یک ردیف کالا یا خدمت لازم است.');
3019 + return;
3020 + }
3021 + for(const row of rows){
3022 + const name = row.querySelector('input');
3023 + if(name && !name.value.trim()){
3024 + alert('شرح کالا یا خدمت را کامل کنید.');
3025 + return;
3026 + }
3027 + }
3028 + if(typeof oldSave === 'function') return oldSave();
3029 + };
3030 +
3031 + window.EasyFactorHealth = {
3032 + version:'4.8-fixed',
3033 + storage:'localStorage',
3034 + checks(){
3035 + return {
3036 + rtl: document.documentElement.dir === 'rtl',
3037 + storage: !!window.localStorage,
3038 + printReady: !!document.getElementById('printable-invoice')
3039 + };
3040 + }
3041 + };
3042 +})();
3043 +</script>
3044 +
3045 </body>
3046 </html>