@davoodmehraban89 / Factor-easy-2 Mirror · read-only
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 17:00 UTC

fix/auth-network-diagnostics
audit-remediation-wave3-4-20261002 audit-remediation-wave3-verify-20261002 audit-remediation-wave4-20261002 audit-remediation-wave4b-20261002 audit-remediation-wave5a-identity-20261002 audit-remediation-wave5b-offboarding-20261002 audit-remediation-wave5c-authorization-20261002 audit-security-intent-review-20261002 audit/finora-financial-core-20261115 audit/finora-master-prompt-phase0-20261016 audit/finora-stage3-legacy-source-readonly-20261023 audit/full-controls-20261030 audit/owner-live-state-reconcile-20261012 audit/owner-review-20261007 audit/phase1-live-permissions-readonly-20261013 audit/reconcile-live-state-20261006 audit/reconcile-state-20261002 audit/wp0-hardening-baseline-20261102 branding-login-favicon-20261002 c8-workflow-task1 c8-workflow-task2 c8-workflow-task3 chore/wave0-pages-gate-closeout claude-code-plugins-setup design/finora-c01-authoritative-source-draft-20261124 docs-wave4b-verified-20261002 docs/active-company-invoice-release-evidence-20261014 docs/agent-operating-model-20261114 docs/chart-taxpayer-release-evidence-20261004 docs/cloud-only-agent-orchestration-20261115 docs/confirm-finora-product-charter-20261115 docs/continuation-20261030 docs/d4a-verified docs/external-work-review-20261121 docs/finora-audit-continuity-20261014 docs/finora-backup-merge-evidence-20261014 docs/finora-ci-reconciliation-20261122 docs/finora-codex-master-prompt docs/finora-eight-phase-plan-20261011 docs/finora-f002-live-buyer-filter-verified-20261015 docs/finora-license-race-release-evidence-20261014 docs/finora-license-rollout-acceptance-20261015 docs/finora-phase0-repeat-and-pages-nav-verified-20261015 docs/finora-phase0-repeat-evidence-20261015 docs/finora-selectors-release-and-fixture-qa-20261015 docs/finora-stagea-pr240-ci-triage-20261123 docs/finora-test-license-live-pass-20261015 docs/finora-v2-m01-coverage-20261124 docs/handoff-charter-v2-20261124 docs/invoice-product-search-release-20261011 docs/invoice-workspace-release-20261007 docs/phase-status-20261031 docs/phase0-acceptance-release-evidence-20261012 docs/phase0-admin-provision-release-evidence-20261012 docs/phase0-core-bundle-owner-login-open-20261013 docs/phase0-duplicate-js-release-20261016 docs/phase0-f001-auth-rest-shared-timeout-20261013 docs/phase0-f001-owner-login-observed-20261013 docs/phase0-f001-second-device-fail-40s-20261013 docs/phase0-f003-release-verification-20261011 docs/phase0-f004-print-release-evidence-20261012 docs/phase0-f004-watermark-release-evidence-20261012 docs/phase0-f005-large-selector-release-20261012 docs/phase0-f005-release-evidence-20261012 docs/phase0-f006-dark-release-evidence-20261012 docs/phase0-f008-final-pr-closure-evidence-20261012 docs/phase0-f008-reconcile-stale-prs-20261012 docs/phase0-f107-production-evidence-20261012 docs/phase0-kavenegar-otp-preflight-release-20261012 docs/phase0-login-release-evidence-20261012 docs/phase0-login-rest-bootstrap-stage-release-20261012 docs/phase0-owner-app-assets-bundle-release-20261013 docs/phase0-owner-login-csp-release-20261012 docs/phase0-reconcile-postmerge-evidence-20261012 docs/phase0-site-probe-release-evidence-20261012 docs/phase0-startup-release-evidence-20261012 docs/phase3-treasury-release-evidence-20261014 docs/pilot-posted-correction-release-evidence-20261014 docs/pilot-purchase-correction-release-evidence-20261014 docs/print-persian-release-verification-20261011 docs/qa-ci-triage-218-20261122 docs/real-account-qa-20261030 docs/release-state-20261007 docs/seamless-amendment-release-20261014 docs/seamless-document-correction-release-20261014 docs/settings-login-release-20261025 docs/stripe-qa-deployment-evidence-20261011 docs/ux-f2-release-evidence-20261011 docs/ux-f2-release-verification-20261011 docs/wave0-closeout docs/wave2-closeout exec-phase1-architecture exec-phase1-implementation exec-phase1-qa exec-phase1-security-release exec-phase1-ux feat/accounting-document-draft-approval-20261011 feat/accounting-document-release-candidate-20261011 feat/active-company-strict-isolation-20261014 feat/admin-provisioning-reconciled-20261011 feat/admin-secure-user-provisioning-20261011 feat/admin-user-provisioning-merged-20261011 feat/d4a-request-work-queue feat/document-lifecycle-atf-20261118 feat/document-lifecycle-temp-permanent-atef feat/draft-first-attachment-lifecycle-216 feat/finora-offline-encrypted-backup-20261014 feat/finora-phase-a-navigation-20261023 feat/finora-stripe-sandbox-edge-gate feat/immutable-attachment-temporary-vouchers feat/iran-accounting-design-audit feat/journal-document-lifecycle-20261118 feat/journal-rpc-repository-adapter-20261117 feat/office-c7-fulltext-ocr-provenance feat/password-recovery-admin-reset-20261025 feat/pilot-expense-income-inplace-amendment-20261014 feat/pilot-journal-invoice-correction-20261014 feat/pilot-purchase-correction-20261014 feat/pilot-treasury-unallocated-correction-20261014 feat/retain-document-editor-after-save-20261014 feat/seamless-financial-document-correction-20261014 feat/slice-b-tenant-rbac feat/stripe-sandbox-invoice-probe feature/accounting-core-p0 feature/accounting-product-guidance feature/admin-identity-phone-auth feature/commercial-rnd feature/finora-phase1a-supabase-cli-20261016 feature/finora-stage2-manual-canary-20261017 feature/finora-stage3-commercial-payload-20261023 feature/finora-stage3-enforce-invoice-rpc-20261023 feature/finora-stage3-enforce-purchase-red-20261023 feature/invoice-workspace-cleanup feature/phase0-kavenegar-otp-preflight-20261012 feature/ux-commerce-settings-r2 fix-shell-scroll-live-cache-20261002 fix-shell-scrollbar-explicit-ownership-20261002 fix-shell-scrollbar-ownership-20261002 fix-shell-scrollbar-right-edge-20261002 fix/about-bug-report-20261114 fix/active-company-invoice-scope-20261014 fix/active-company-isolation-reconcile-20261014 fix/admin-central-password-reset-20261028 fix/auth-network-diagnostics fix/chart-taxpayer-reference-20261003 fix/combobox-enter-reliability fix/combobox-live-search fix/combobox-production-cache fix/combobox-release-asset-v2 fix/company-edit-action-20261028 fix/company-edit-pilot-delete-20261014 fix/company-form-action-state-20261014 fix/company-form-action-state-rebased-20261014 fix/contact-operational-balance-20261111 fix/dark-theme-contrast fix/financial-lifecycle-recovery-20261124 fix/finora-accounting-test-organization-readiness-20261015 fix/finora-b01-safe-org-switch-20261124 fix/finora-b02-sync-recovery-20261124 fix/finora-b03-conflict-review-20261124 fix/finora-org-license-bootstrap-race-20261014 fix/finora-stage3-posted-source-immutable-20261023 fix/finora-v2-b03-r2-qa-20261124 fix/finora-v2-b03-r3-readonly-20261124 fix/g01-asof-financial-position-20261128 fix/invoice-product-search-rehydrate-20261011 fix/invoice-search-cache-bust fix/issue229-linked-draft-status-20261122 fix/login-assets-retry-20261014 fix/login-inert-startup-20261110 fix/login-member-access-20261101 fix/login-resilience-20261017 fix/login-short-viewport-20261028 fix/login-startup-readiness-20261024 fix/navigation-utilities-placement-20261105 fix/office-c7-source-ref-binding fix/office-c7-source-ref-binding-v2 fix/owner-login-network-20261026 fix/password-recovery-invalid-cancel-20261026 fix/payment-amount-format-20261111 fix/phase0-admin-provision-seat-bootstrap-20261012 fix/phase0-auth-bundled-assets-for-unstable-isp-20261013 fix/phase0-auth-handler-binding-stalled-session-20261012 fix/phase0-login-context-stage-visibility-20261012 fix/phase0-login-single-local-bundle-20261013 fix/phase0-login-site-probe-clarity-20261012 fix/phase0-login-ui-ready-20261012 fix/phase0-modal-dashboard-drill-20261011 fix/phase0-pages-blocking-core-bundles-20261013 fix/phase0-print-no-draft-watermark-20261012 fix/phase0-startup-parallel-asset-load-20261012 fix/phase3-behavioral-verification fix/preserve-pending-edits-20261121 fix/print-persian-digit-consistency-20261011 fix/recovery-login-startup-20261027 fix/report-opening-balance-release-20261130 fix/settings-checkbox-persistence-20261024 fix/taxpayer-arithmetic-20261004 fix/ux-keyboard-search-f2-20261011 fix/wave2-request-input-integrity hardening/c01-source-draft-20261129 hardening/global-draft-conversion-20261113 hardening/wp1-ledger-integrity-20261102 integration/finora-execution-20261124 integration/issue229-ui-qa-20261122 integration/wp1-ledger-core-20261115 integration/wp2-manual-journal-rpc-20261116 main ops-heartbeat-v2-20261002 ops-progress-heartbeat-rule-20261002 ops/wave4-production-20261003 plan/financial-core-phase1-approval-20261016 qa/attachment-allocator-two-session-20261122 qa/draft-first-save-red-evidence-20261122 qa/finora-autopost-retry-identity-red-20261122 qa/finora-identity-rpc-security-tripwire-20261122 qa/finora-issue223-manual-kardex-browser-red-20261123 qa/finora-issue223-stock-real-jwt-20261123 qa/finora-issue238-inventory-count-browser-red-20261123 qa/finora-issue238-save-browser-red-20261122 qa/finora-login-cors-header-preflight-20261025 qa/finora-phase0-saved-session-repeat-20261015 qa/finora-phase5-posted-insert-jwt-red-20261123 qa/finora-stage3-ui-jwt-autopost-20261023 qa/finora-v2-g01-report-reconciliation-20261125 qa/free-supabase-local-wp2-20261116 qa/issue229-approval-boundary-jwt-20261122 qa/issue229-sale-save-linked-draft-red-20261122 qa/phase0-acceptance-start-20261012 qa/phase0-f005-account-project-contract-stress-20261012 qa/phase0-f005-selector-stress-20261012 qa/phase0-f006-dark-contrast-20261012 qa/post-wave4-verification-20261003 qa/posted-source-immutable-jwt-20261122 qa/pr219-wp1-disposable-dburl-proof-20261123 qa/purchase-source-jwt-20261122 qa/real-settings-acceptance-20261025 qa/recovery-mail-acceptance-20261026 qa/source-autopost-jwt-atomicity-20261122 qa/wave0-negative-gate-proof qa/wp2-auth-tenant-e2e-20261117 qa/wp2-authorized-journal-write-20261122 qa/wp2-financial-paths-reconciliation-20261122 qa/wp3-financial-tenant-isolation-20261117 remediation/wave0-liveqa-wave1 remediation/wave2-request-integrity staging/finora-stage22-free-20261022 state-scroll-verified-20261002 state-wave4b-production-20261002 state-wave5a-production-20261002 test/finora-accounting-boot-readiness-20261015 test/finora-f0-02-production-selectors-20261015 test/finora-pages-commerce-event-wiring-20261015 test/finora-pages-invoice-selectors-readonly-20261015 test/finora-stage3-jwt-source-post-20261023 test/phase0-invoice-fixture-bootstrap-20261016 test/phase0-invoice-persistence-20261011 test/phase0-pages-delayed-dispatcher-20261016 ux-scroll-ownership-20261002 work/finora-stage3-enforce-purchase-green-20261023 work/finora-stage3-enforce-sale-green-20261023 work/finora-stage3-payload-green-20261023
js 546 lines 39.5 KB
Raw
1 function buildTaxLogo(){
2 return `<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\" style=\"width:80px;height:80px\">
3 <circle cx=\"50\" cy=\"50\" r=\"46\" fill=\"none\" stroke=\"#1e3a8a\" stroke-width=\"1.5\"/>
4 <circle cx=\"50\" cy=\"50\" r=\"38\" fill=\"none\" stroke=\"#1e3a8a\" stroke-width=\"1\"/>
5 <rect x=\"30\" y=\"32\" width=\"4\" height=\"36\" fill=\"#1e3a8a\"/>
6 <rect x=\"40\" y=\"26\" width=\"4\" height=\"42\" fill=\"#1e3a8a\"/>
7 <rect x=\"50\" y=\"22\" width=\"4\" height=\"46\" fill=\"#1e3a8a\"/>
8 <rect x=\"60\" y=\"26\" width=\"4\" height=\"42\" fill=\"#1e3a8a\"/>
9 <rect x=\"70\" y=\"32\" width=\"4\" height=\"36\" fill=\"#1e3a8a\"/>
10 <rect x=\"26\" y=\"20\" width=\"52\" height=\"5\" fill=\"#1e3a8a\" rx=\"1\"/>
11 <rect x=\"28\" y=\"66\" width=\"48\" height=\"4\" fill=\"#1e3a8a\" rx=\"1\"/>
12 <rect x=\"24\" y=\"72\" width=\"56\" height=\"4\" fill=\"#1e3a8a\" rx=\"1\"/>
13 </svg>`;
14 }
15 function getInvoicePrintSpec(inv){
16 const kind=String(inv?.kind||'').toLowerCase();
17 if(kind==='non_formal'||kind==='informal')return {paper:'A5',orientation:'landscape',widthMm:210,heightMm:148,marginMm:5};
18 return {paper:'A4',orientation:'landscape',widthMm:297,heightMm:210,marginMm:5};
19 }
20 function buildIsolatedPrintDocument(content,spec){
21 const usableWidth=Math.max(1,spec.widthMm-(spec.marginMm*2));
22 const usableHeight=Math.max(1,spec.heightMm-(spec.marginMm*2));
23 return '<!doctype html><html lang="fa" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Finora Print</title><style>'
24 +'@page{size:'+spec.paper+' '+spec.orientation+';margin:'+spec.marginMm+'mm}'
25 +'*{box-sizing:border-box}html,body{margin:0!important;padding:0!important;background:#fff!important;color:#000!important;width:100%!important;direction:rtl;font-family:Tahoma,Arial,sans-serif;-webkit-print-color-adjust:exact;print-color-adjust:exact}'
26 +'#finora-print-document{margin:0!important;padding:0!important;width:100%!important}'
27 +'.invoice-a4-page{width:'+usableWidth+'mm!important;max-width:'+usableWidth+'mm!important;min-height:'+usableHeight+'mm!important;margin:0 auto!important;box-sizing:border-box!important;page-break-after:always!important;break-after:page!important;overflow:hidden!important}'
28 +'.invoice-a4-page:last-child{page-break-after:auto!important;break-after:auto!important}'
29 +'.invoice-a4-page table{max-width:100%!important}'
30 +'@media print{html,body,#finora-print-document{width:100%!important}.invoice-a4-page{box-shadow:none!important}}'
31 +'</style></head><body><main id="finora-print-document" data-paper="'+spec.paper+'" data-orientation="'+spec.orientation+'">'+content+'</main><script>(function(){var m={0:"۰",1:"۱",2:"۲",3:"۳",4:"۴",5:"۵",6:"۶",7:"۷",8:"۸",9:"۹"},w=document.createTreeWalker(document.getElementById("finora-print-document"),NodeFilter.SHOW_TEXT),n;while(n=w.nextNode()){if(n.parentNode&&/^(SCRIPT|STYLE)$/i.test(n.parentNode.nodeName))continue;n.nodeValue=n.nodeValue.replace(/[0-9]/g,function(d){return m[d]})}})();<\/script></body></html>';
32 }
33 function removeIsolatedPrintFrame(){
34 const old=document.getElementById('finora-print-frame');if(old)old.remove();
35 }
36 function printIsolatedDocument(content,spec){
37 removeIsolatedPrintFrame();
38 const frame=document.createElement('iframe');
39 frame.id='finora-print-frame';
40 frame.title='Finora isolated print document';
41 frame.setAttribute('aria-hidden','true');
42 frame.style.position='fixed';frame.style.left='0';frame.style.bottom='0';frame.style.width='1px';frame.style.height='1px';frame.style.border='0';frame.style.opacity='0';frame.style.pointerEvents='none';
43 document.body.appendChild(frame);
44 const doc=frame.contentDocument;
45 doc.open();doc.write(buildIsolatedPrintDocument(content,spec));doc.close();
46 if(window.__FINORA_PRINT_TEST_MODE)return frame;
47 const cleanup=()=>{setTimeout(removeIsolatedPrintFrame,0);};
48 frame.contentWindow.addEventListener('afterprint',cleanup,{once:true});
49 setTimeout(async()=>{try{
50 await Promise.all([...doc.images].map(img=>img.decode()));
51 if(doc.fonts)await doc.fonts.ready;
52 if(!frame.isConnected)return;
53 frame.contentWindow.focus();frame.contentWindow.print();setTimeout(cleanup,3000);
54 }catch(err){cleanup();alert('تصویر یا منابع چاپ بارگذاری نشد. لطفاً دوباره تلاش کنید.');}},80);
55 return frame;
56 }
57 function stageInvoicePrint(content,spec){
58 const preview=document.getElementById('printable-invoice');
59 if(preview){preview.innerHTML=content;preview.style.display='none';preview.dataset.paper=spec.paper;preview.dataset.orientation=spec.orientation;}
60 let orientationStyle=document.getElementById('dynamic-print-orientation');
61 if(!orientationStyle){orientationStyle=document.createElement('style');orientationStyle.id='dynamic-print-orientation';document.head.appendChild(orientationStyle);}
62 orientationStyle.innerHTML='@media print { @page { size: '+spec.paper+' '+spec.orientation+'; margin: '+spec.marginMm+'mm; } }';
63 return printIsolatedDocument(content,spec);
64 }
65
66 function buildElectronicDocPage(inv,edoc,seller,contact){
67 const cur='ریال';
68 const items=inv.items;
69 const vatRate=(inv.vatRate!==undefined&&inv.vatType!=='fixed')?inv.vatRate:10;
70
71 const itemsRows=items.map((it,idx)=>{
72 const line=it.lineTotal;
73 const rowVatRate=(inv.vat>0)?vatRate:0;
74 const vat=Math.round(line*rowVatRate/100);
75 const total=line+vat;
76 const edItem=(edoc.items&&edoc.items[idx])?edoc.items[idx]:{};
77 return `<tr>
78 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(idx+1)}</td>
79 <td style=\"border:1px solid #000;padding:4px\">${esc(it.prodName||'')}</td>
80 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(it.qty)}</td>
81 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(it.unit||'')}</td>
82 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${it.price.toLocaleString('fa-IR')}</td>
83 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${line.toLocaleString('fa-IR')}</td>
84 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(rowVatRate)}٪</td>
85 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${vat.toLocaleString('fa-IR')}</td>
86 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${total.toLocaleString('fa-IR')}</td>
87 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(edItem.internalId||'')}</td>
88 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(edItem.officialId||'')}</td>
89 </tr>`;
90 }).join('');
91
92 let contractText='';
93 if(edoc.pattern==='پیمانکاری'&&edoc.contractId){
94 contractText=items.map((it,idx)=>
95 `+ شناسه یکتای ثبت قرارداد حق‌العمل‌کاری برای ردیف ${toPersianDigits(idx+1)}: ${esc(edoc.contractId)} است`
96 ).join('<br>');
97 }
98
99 const grandTotal=inv.grandTotal||0;
100 const vatTotal=inv.vat||0;
101 const baseTotal=grandTotal-vatTotal;
102
103 return `<div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:6mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif;position:relative\">
104 <table style=\"width:100%;border-collapse:collapse;margin-bottom:4px\">
105 <tr>
106 <td style=\"width:20%;vertical-align:top;text-align:center;padding:4px\">${buildTaxLogo()}<div style=\"font-size:9px;color:#1e3a8a;margin-top:2px\">سازمان امور مالیاتی</div></td>
107 <td style=\"width:60%;text-align:center;vertical-align:middle;padding:6px\">
108 <div style=\"font-size:14px;font-weight:bold;color:#1e3a8a\">صورتحساب الکترونیکی فروش کالا / خدمات</div>
109 <div style=\"font-size:9.5px;color:#475569;margin-top:2px\">(مطابق استاندارد سامانه مودیان)</div>
110 </td>
111 <td style=\"width:20%;vertical-align:top;padding:4px;font-size:9.5px;text-align:left\">
112 <div>${esc(seller.name||'')}</div>
113 <div style=\"color:#475569\">${esc(seller.phone||'')}</div>
114 </td>
115 </tr>
116 </table>
117
118 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
119 <tr>
120 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9;width:30%\"><strong>شماره منحصر به فرد صورتحساب الکترونیکی مالیاتی</strong></td>
121 <td style=\"border:1px solid #000;padding:4px;width:70%\">${esc(edoc.uniqueNumber||'—')}</td>
122 </tr>
123 <tr>
124 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>شماره مالیاتی مندرج در برگه تشخیص</strong></td>
125 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.taxNumber||'—')}</td>
126 </tr>
127 <tr>
128 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>تاریخ و زمان صدور صورتحساب الکترونیکی</strong></td>
129 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.issueDateTime||'—')}</td>
130 </tr>
131 <tr>
132 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>تاریخ صورتحساب الکترونیکی اصلی</strong></td>
133 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.originalDate||'—')}</td>
134 </tr>
135 <tr>
136 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>شماره صورتحساب الکترونیکی اصلی</strong></td>
137 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.originalNumber||'—')}</td>
138 </tr>
139 <tr>
140 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>نوع صورتحساب</strong></td>
141 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.kind||'—')}</td>
142 </tr>
143 <tr>
144 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>موضوع صورتحساب</strong></td>
145 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.pattern||'—')}</td>
146 </tr>
147 </table>
148
149 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
150 <tr><td colspan=\"4\" style=\"border:1px solid #000;padding:4px;background:#f1f5f9;font-weight:bold\">مشخصات فروشنده</td></tr>
151 <tr>
152 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">نام شخص</td>
153 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(seller.name||'—')}</td>
154 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">شماره اقتصادی فروشنده</td>
155 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(seller.economic_code||'0000')}</td>
156 </tr>
157 <tr>
158 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">شماره ثبت / شناسه ملی</td>
159 <td style=\"border:1px solid #000;padding:4px\">${esc(seller.national_id||'—')}</td>
160 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">کد پستی</td>
161 <td style=\"border:1px solid #000;padding:4px\">${esc(seller.postal_code||'—')}</td>
162 </tr>
163 </table>
164
165 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
166 <tr><td colspan=\"4\" style=\"border:1px solid #000;padding:4px;background:#f1f5f9;font-weight:bold\">مشخصات خریدار</td></tr>
167 <tr>
168 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">نام شخص</td>
169 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(contact.name||inv.contactName)}</td>
170 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">شماره اقتصادی خریدار</td>
171 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(contact.economic_code||'0000')}</td>
172 </tr>
173 <tr>
174 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">شماره ثبت / شناسه ملی</td>
175 <td style=\"border:1px solid #000;padding:4px\">${esc(contact.national_id||'—')}</td>
176 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">کد پستی</td>
177 <td style=\"border:1px solid #000;padding:4px\">${esc(contact.postal_code||'—')}</td>
178 </tr>
179 </table>
180
181 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:9.5px\">
182 <thead>
183 <tr style=\"background:#f1f5f9;font-weight:bold;text-align:center\">
184 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">ردیف</th>
185 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شرح کالا/خدمت</th>
186 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">تعداد/مقدار</th>
187 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">واحد اندازه‌گیری</th>
188 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">مبلغ واحد (ریال)</th>
189 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">مبلغ کل (ریال)</th>
190 <th style=\"border:1px solid #000;padding:4px\" colspan=\"3\">مالیات بر ارزش افزوده</th>
191 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شناسه کالا/خدمت داخلی</th>
192 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شناسه کالا/خدمت</th>
193 </tr>
194 <tr style=\"background:#f1f5f9;font-weight:bold;text-align:center\">
195 <th style=\"border:1px solid #000;padding:4px\">نرخ</th>
196 <th style=\"border:1px solid #000;padding:4px\">مبلغ</th>
197 <th style=\"border:1px solid #000;padding:4px\">مبلغ کل کالا/خدمت</th>
198 </tr>
199 </thead>
200 <tbody>${itemsRows}</tbody>
201 </table>
202
203 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-top:6px\">
204 <tr>
205 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:25%\">جمع کل مبلغ کالا/خدمت</td>
206 <td style=\"border:1px solid #000;padding:4px;width:25%;text-align:left\">${baseTotal.toLocaleString('fa-IR')} ${cur}</td>
207 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:25%\">جمع کل مالیات بر ارزش افزوده</td>
208 <td style=\"border:1px solid #000;padding:4px;width:25%;text-align:left\">${vatTotal.toLocaleString('fa-IR')} ${cur}</td>
209 </tr>
210 <tr>
211 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مبلغ کل صورتحساب</td>
212 <td style=\"border:1px solid #000;padding:4px;text-align:left;font-weight:bold\" colspan=\"3\">${grandTotal.toLocaleString('fa-IR')} ${cur}</td>
213 </tr>
214 <tr>
215 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مصرف‌کننده نهایی</td>
216 <td style=\"border:1px solid #000;padding:4px\">خیر</td>
217 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مالیات موضوع ماده ۱۷</td>
218 <td style=\"border:1px solid #000;padding:4px;text-align:left\">۰ ${cur}</td>
219 </tr>
220 </table>
221
222 ${contractText?`<div style=\"border:1px solid #000;padding:6px;margin-top:6px;font-size:10.5px;line-height:1.7;background:#fffbeb\">${contractText}</div>`:''}
223
224 <table style=\"width:100%;border-collapse:collapse;margin-top:8px;font-size:10.5px\">
225 <tr>
226 <td style=\"width:50%;border:1px solid #000;padding:8px;height:70px;vertical-align:top\">
227 <div style=\"font-weight:bold;margin-bottom:4px\">مهر و امضای فروشنده</div>
228 <div style=\"font-size:9.5px;color:#475569\">${esc(seller.name||'')}</div>
229 </td>
230 <td style=\"width:50%;border:1px solid #000;padding:8px;height:70px;vertical-align:top\">
231 <div style=\"font-weight:bold;margin-bottom:4px\">مهر و امضای خریدار</div>
232 <div style=\"font-size:9.5px;color:#475569\">${esc(contact.name||inv.contactName)}</div>
233 </td>
234 </tr>
235 </table>
236 </div>`;
237 }
238
239 function printElectronicDoc(invId){
240 const inv=getMyInvoices().find(i=>i.id===invId);
241 if(!inv||!inv.electronicDoc){
242 alert('سند الکترونیکی برای این فاکتور ثبت نشده است.');
243 return;
244 }
245 const myCompanies=getMyCompanies();
246 const myContacts=getMyContacts();
247 const seller=myCompanies.find(c=>c.id===inv.companyId)||myCompanies[0]||{};
248 const contact=myContacts.find(c=>c.id===inv.contactId)||{};
249 const html=buildElectronicDocPage(inv,inv.electronicDoc,seller,contact);
250 stageInvoicePrint(html,{paper:'A4',orientation:'landscape',widthMm:297,heightMm:210,marginMm:7});
251 }
252
253 // ============ فاکتورهای معمول ============
254 const FORMAL_ROWS_PER_PAGE=12;
255 const NONFORMAL_ROWS_PER_PAGE=10;
256 function computeInvoiceLines(inv){
257 const n=inv.items.length;
258 const afterDiscountTotal=Math.max(0,(inv.subtotal||0)-(inv.discount||0));
259 let usedDiscount=0,usedVat=0;
260 return inv.items.map((it,idx)=>{
261 const lineSubtotal=it.lineTotal;
262 const isLastItem=(idx===n-1);
263 let lineDiscount,lineVat;
264 if(isLastItem){
265 lineDiscount=Math.max(0,(inv.discount||0)-usedDiscount);
266 lineVat=Math.max(0,(inv.vat||0)-usedVat);
267 }else{
268 lineDiscount=(inv.subtotal>0)?Math.round((inv.discount||0)*(lineSubtotal/inv.subtotal)):0;
269 const lineAfterDiscount=lineSubtotal-lineDiscount;
270 lineVat=(afterDiscountTotal>0&&inv.vat>0)?Math.round((inv.vat||0)*(lineAfterDiscount/afterDiscountTotal)):0;
271 }
272 usedDiscount+=lineDiscount;usedVat+=lineVat;
273 const lineNet=lineSubtotal-lineDiscount+lineVat;
274 return {rowNo:idx+1,prodName:it.prodName,qty:it.qty,unit:it.unit||'عدد',price:it.price,lineSubtotal,lineDiscount,lineVat,lineNet};
275 });
276 }
277 function buildFormalPage(items,seller,contact,inv,opt){
278 const cur=inv.currencyLabel||'ریال';
279 const isLast=opt.isLastPage;
280 const rowsHtml=items.map(li=>`
281 <tr style=\"text-align:center\">
282 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${li.rowNo.toLocaleString('fa-IR')}</td>
283 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:right;font-size:10.5px;font-weight:600\">${esc(li.prodName)}</td>
284 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${li.qty.toLocaleString('fa-IR')}</td>
285 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${esc(li.unit)}</td>
286 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.price.toLocaleString('fa-IR')}</td>
287 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
288 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineDiscount.toLocaleString('fa-IR')}</td>
289 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineVat.toLocaleString('fa-IR')}</td>
290 <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>
291 </tr>`).join('');
292 const sellerIds=`${esc(seller.national_id||'—')} / ${esc(seller.reg_number||'—')}`;
293 const contactIds=`${esc(contact.national_id||'—')} / ${esc(contact.reg_number||'—')}`;
294 const cumSub=opt.cumulativeSubtotal||0;
295 const cumDisc=opt.cumulativeDiscount||0;
296 const cumVat=opt.cumulativeVat||0;
297 const cumNet=opt.cumulativeNet||0;
298 const subtotalLabel=isLast?'جمع کل اقلام:':'جمع اقلام تا این صفحه:';
299 const discountLabel=isLast?'مجموع تخفیف:':'مجموع تخفیف تا این صفحه:';
300 const vatLabel=isLast?'مالیات و عوارض ارزش افزوده:':'مالیات تا این صفحه:';
301 const netLabel=isLast?'مبلغ قابل پرداخت:':'جمع تا این صفحه:';
302 const wordsLabel=isLast?'مبلغ کل به حروف:':'مبلغ تا این صفحه به حروف:';
303 const summaryBox=`
304 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10.5px;margin-top:3px\">
305 <tr>
306 <td style=\"width:60%;border:1px solid #1e3a8a;padding:6px;vertical-align:top;background:#f8fafc\">
307 <div style=\"margin-bottom:4px\"><strong>${wordsLabel}</strong> ${numberToPersianWords(cumNet,cur)}</div>
308 ${isLast?`<div style=\"margin-bottom:4px\"><strong>شرایط و نحوه پرداخت:</strong> ${inv.paymentMethod==='credit'?'نقدی ☐ &nbsp;&nbsp;&nbsp; غیرنقدی (نسیه/چک) ☑':'نقدی ☑ &nbsp;&nbsp;&nbsp; غیرنقدی (نسیه/چک) ☐'}</div><div style=\"font-size:9.5px;color:#475569;line-height:1.5\"><strong>توضیحات:</strong> ${esc(inv.description||seller.footer||'تحویل کالا منوط به تسویه نهایی می‌باشد.')}</div>`:`<div style=\"font-size:9.5px;color:#64748b;margin-top:4px\">(ادامه اقلام در صفحه بعد درج گردیده است...)</div>`}
309 </td>
310 <td style=\"width:40%;border:1px solid #1e3a8a;padding:0\">
311 <table style=\"width:100%;border-collapse:collapse;font-size:10.5px\">
312 <tr><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\">${subtotalLabel}</td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\">${cumSub.toLocaleString('fa-IR')} ${cur}</td></tr>
313 <tr><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\">${discountLabel}</td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\">${cumDisc.toLocaleString('fa-IR')} ${cur}</td></tr>
314 <tr><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\">${vatLabel}</td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\">${cumVat.toLocaleString('fa-IR')} ${cur}</td></tr>
315 <tr style=\"background:#eff6ff;font-weight:bold;font-size:11.5px;color:#1e3a8a\"><td style=\"padding:5px 8px\">${netLabel}</td><td style=\"padding:5px 8px;text-align:left\">${cumNet.toLocaleString('fa-IR')} ${cur}</td></tr>
316 </table>
317 </td>
318 </tr>
319 </table>`;
320 const signatures=isLast?`
321 <table style=\"width:100%;border-collapse:collapse;border:1px solid #1e3a8a;margin-top:6px;text-align:center;font-size:10.5px\">
322 <tr>
323 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #1e3a8a;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای فروشنده</div></td>
324 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #1e3a8a;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای خریدار</div></td>
325 </tr>
326 </table>`:'';
327 return `
328 <div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:6mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif\">
329 <table style=\"width:100%;border-collapse:collapse;margin-bottom:3px\">
330 <tr>
331 <td style=\"width:28%;padding:5px;vertical-align:middle\"></td>
332 <td style=\"width:44%;padding:6px;text-align:center;vertical-align:middle;background:#eff6ff\">
333 <h1 style=\"font-size:15px;margin:0;font-weight:bold;color:#1e3a8a\">صورتحساب فروش کالا و خدمات</h1>
334 </td>
335 <td style=\"width:28%;padding:5px;font-size:10.5px;vertical-align:middle;text-align:left\">
336 <div>شماره سریال: <strong style=\"color:#b91c1c\">${esc(inv.number)}</strong></div>
337 <div style=\"margin-top:1px\">تاریخ صدور: <strong>${esc(inv.date)}</strong></div>
338 <div style=\"margin-top:1px;color:#64748b;font-size:9.5px\">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
339 </td>
340 </tr>
341 </table>
342 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px;margin-bottom:3px\">
343 <tr style=\"background:#f8fafc;font-weight:bold\"><td colspan=\"4\" style=\"border:1px solid #1e3a8a;padding:2px 6px;color:#1e3a8a\">مشخصات فروشنده</td></tr>
344 <tr>
345 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">نام شخص حقیقی/حقوقی:</td>
346 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\"><strong>${esc(seller.name||'—')}</strong></td>
347 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">شماره اقتصادی:</td>
348 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\">${esc(seller.economic_code||'—')}</td>
349 </tr>
350 <tr>
351 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">شناسه ملی / شماره ثبت:</td>
352 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${sellerIds}</td>
353 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">کد پستی ۱۰ رقمی:</td>
354 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(seller.postal_code||'—')}</td>
355 </tr>
356 <tr>
357 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">نشانی و تلفن:</td>
358 <td colspan=\"3\" style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(seller.address||'—')} ${esc(seller.phone?' | تلفن: '+seller.phone:'')}</td>
359 </tr>
360 </table>
361 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px;margin-bottom:3px\">
362 <tr style=\"background:#f8fafc;font-weight:bold\"><td colspan=\"4\" style=\"border:1px solid #1e3a8a;padding:2px 6px;color:#1e3a8a\">مشخصات خریدار</td></tr>
363 <tr>
364 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">نام شخص حقیقی/حقوقی:</td>
365 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\"><strong>${esc(contact.name||inv.contactName)}</strong></td>
366 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">شماره اقتصادی:</td>
367 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\">${esc(contact.economic_code||'—')}</td>
368 </tr>
369 <tr>
370 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">شناسه ملی / شماره ثبت:</td>
371 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${contactIds}</td>
372 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">کد پستی ۱۰ رقمی:</td>
373 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(contact.postal_code||'—')}</td>
374 </tr>
375 <tr>
376 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">نشانی و تلفن:</td>
377 <td colspan=\"3\" style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(contact.address||'—')} ${esc(contact.mobile?' | تلفن: '+contact.mobile:'')}</td>
378 </tr>
379 </table>
380 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px\">
381 <thead>
382 <tr style=\"background:#1e3a8a;color:#fff;font-weight:bold;text-align:center\">
383 <th style=\"border:1px solid #1e3a8a;padding:4px;width:28px\">ردیف</th>
384 <th style=\"border:1px solid #1e3a8a;padding:4px 6px;text-align:right\">شرح کالا یا خدمات</th>
385 <th style=\"border:1px solid #1e3a8a;padding:4px;width:42px\">تعداد</th>
386 <th style=\"border:1px solid #1e3a8a;padding:4px;width:42px\">واحد</th>
387 <th style=\"border:1px solid #1e3a8a;padding:4px;width:105px\">مبلغ واحد (${cur})</th>
388 <th style=\"border:1px solid #1e3a8a;padding:4px;width:110px\">مبلغ کل (${cur})</th>
389 <th style=\"border:1px solid #1e3a8a;padding:4px;width:80px\">تخفیف (${cur})</th>
390 <th style=\"border:1px solid #1e3a8a;padding:4px;width:90px\">مالیات (${cur})</th>
391 <th style=\"border:1px solid #1e3a8a;padding:4px;width:120px\">مبلغ نهایی (${cur})</th>
392 </tr>
393 </thead>
394 <tbody>
395 ${!opt.isFirstPage?`<tr style=\"background:#fef3c7;font-weight:bold;font-size:9.5px\"><td colspan=\"5\" style=\"border:1px solid #1e293b;padding:3px;text-align:left\">نقل از صفحه قبل:</td><td colspan=\"4\" style=\"border:1px solid #1e293b;padding:3px;text-align:left\">${(opt.cumulativeBefore||0).toLocaleString('fa-IR')} ${cur}</td></tr>`:''}
396 ${rowsHtml}
397 </tbody>
398 </table>
399 ${summaryBox}
400 ${signatures}
401 </div>`;
402 }
403 function buildNonFormalPage(items,seller,contact,inv,opt){
404 const cur=inv.currencyLabel||'ریال';
405 const isPre=inv.kind==='pre_invoice';
406 const isLast=opt.isLastPage;
407 const hasVat=isPre&&inv.vat>0;
408 const title=isPre?'پیش‌فاکتور فروش کالا و خدمات':'فاکتور فروش کالا و خدمات';
409 const rowsHtml=items.map(li=>hasVat?`
410 <tr style=\"text-align:center\">
411 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${li.rowNo.toLocaleString('fa-IR')}</td>
412 <td style=\"border:1px solid #334155;padding:3px 6px;text-align:right;font-size:10px;font-weight:600\">${esc(li.prodName)}</td>
413 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${esc(li.unit)}</td>
414 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${li.qty.toLocaleString('fa-IR')}</td>
415 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.price.toLocaleString('fa-IR')}</td>
416 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
417 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.lineVat.toLocaleString('fa-IR')}</td>
418 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px;font-weight:bold\">${li.lineNet.toLocaleString('fa-IR')}</td>
419 </tr>`:`
420 <tr style=\"text-align:center\">
421 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${li.rowNo.toLocaleString('fa-IR')}</td>
422 <td style=\"border:1px solid #334155;padding:4px 8px;text-align:right;font-size:10.5px;font-weight:600\">${esc(li.prodName)}</td>
423 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${esc(li.unit)}</td>
424 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${li.qty.toLocaleString('fa-IR')}</td>
425 <td style=\"border:1px solid #334155;padding:4px 5px;text-align:left;font-size:10.5px\">${li.price.toLocaleString('fa-IR')}</td>
426 <td style=\"border:1px solid #334155;padding:4px 5px;text-align:left;font-size:10.5px;font-weight:bold\">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
427 </tr>`).join('');
428 const cumSub=opt.cumulativeSubtotal||0;
429 const cumDisc=opt.cumulativeDiscount||0;
430 const cumVat=opt.cumulativeVat||0;
431 const cumNet=opt.cumulativeNet||0;
432 const subtotalLabel=isLast?'قیمت کل اقلام:':'جمع اقلام تا این صفحه:';
433 const discountLabel=isLast?'تخفیف:':'تخفیف تا این صفحه:';
434 const vatLabel=isLast?'مالیات ارزش افزوده:':'مالیات تا این صفحه:';
435 const netLabel=isLast?`مبلغ کل (${cur}):`:'جمع تا این صفحه:';
436 const wordsLabel=isLast?'مبلغ به حروف:':'مبلغ تا این صفحه به حروف:';
437 const summaryBox=`
438 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px;margin-top:5px\">
439 <tr>
440 <td style=\"width:60%;border:1px solid #334155;padding:6px;vertical-align:top;background:#f8fafc\">
441 <div style=\"margin-bottom:4px\"><strong>${wordsLabel}</strong> ${numberToPersianWords(cumNet,cur)}</div>
442 ${isLast?`<div style=\"font-size:9.5px;color:#475569;line-height:1.5\"><strong>توضیحات:</strong> ${esc(inv.description||seller.footer||(isPre?'اعتبار پیش‌فاکتور به مدت ۴۸ ساعت می‌باشد.':'از خرید شما سپاسگزاریم.'))}</div>`:`<div style=\"font-size:9.5px;color:#64748b\">(ادامه در صفحه بعد...)</div>`}
443 </td>
444 <td style=\"width:40%;border:1px solid #334155;padding:0\">
445 <table style=\"width:100%;border-collapse:collapse;font-size:10.5px\">
446 <tr><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\">${subtotalLabel}</td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\">${cumSub.toLocaleString('fa-IR')} ${cur}</td></tr>
447 ${cumDisc>0||inv.discount>0?`<tr><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\">${discountLabel}</td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\">${cumDisc.toLocaleString('fa-IR')} ${cur}</td></tr>`:''}
448 ${hasVat?`<tr style=\"background:#fffbeb\"><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px\"><strong>${vatLabel}</strong></td><td style=\"border-bottom:1px solid #cbd5e1;padding:3px 8px;text-align:left\"><strong>${cumVat.toLocaleString('fa-IR')} ${cur}</strong></td></tr>`:''}
449 <tr style=\"background:#f1f5f9;font-weight:bold;font-size:11.5px\"><td style=\"padding:5px 8px\">${netLabel}</td><td style=\"padding:5px 8px;text-align:left\">${cumNet.toLocaleString('fa-IR')} ${cur}</td></tr>
450 </table>
451 </td>
452 </tr>
453 </table>`;
454 const signatures=isLast?`
455 <table style=\"width:100%;border-collapse:collapse;border:1px solid #334155;margin-top:6px;text-align:center;font-size:10.5px\">
456 <tr>
457 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #334155;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای فروشنده</div></td>
458 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #334155;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای خریدار</div></td>
459 </tr>
460 </table>`:'';
461 return `
462 <div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:7mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif\">
463 <table style=\"width:100%;border-collapse:collapse;margin-bottom:5px\">
464 <tr>
465 <td style=\"width:30%;padding:6px;vertical-align:middle\"></td>
466 <td style=\"width:40%;padding:6px;text-align:center;vertical-align:middle;background:#f8fafc\">
467 <h1 style=\"font-size:15px;margin:0;font-weight:bold;color:#0f172a\">${title}</h1>
468 </td>
469 <td style=\"width:30%;padding:6px;font-size:10.5px;vertical-align:middle;text-align:left\">
470 <div>شماره: <strong style=\"font-size:11.5px\">#${esc(inv.number)}</strong></div>
471 <div style=\"margin-top:1px\">تاریخ: <strong>${esc(inv.date)}</strong></div>
472 <div style=\"margin-top:1px;color:#64748b;font-size:9.5px\">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
473 </td>
474 </tr>
475 </table>
476 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px;margin-bottom:5px\">
477 <tr>
478 <td style=\"width:50%;border:1px solid #334155;padding:5px;vertical-align:top;background:#f8fafc\">
479 <div style=\"font-weight:bold;color:#334155;margin-bottom:2px\">مشخصات فروشنده:</div>
480 <div>نام: <strong>${esc(seller.name||'—')}</strong></div>
481 <div>نشانی: ${esc(seller.address||'—')}</div>
482 <div>تلفن تماس: ${esc(seller.phone||'—')}</div>
483 </td>
484 <td style=\"width:50%;border:1px solid #334155;padding:5px;vertical-align:top;background:#f8fafc\">
485 <div style=\"font-weight:bold;color:#334155;margin-bottom:2px\">مشخصات خریدار:</div>
486 <div>نام: <strong>${esc(contact.name||inv.contactName)}</strong></div>
487 <div>نشانی: ${esc(contact.address||'—')}</div>
488 <div>تلفن: ${esc(contact.mobile||'—')}</div>
489 </td>
490 </tr>
491 </table>
492 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px\">
493 <thead>
494 <tr style=\"background:#334155;color:#fff;font-weight:bold;text-align:center\">
495 <th style=\"border:1px solid #334155;padding:5px 4px;width:32px\">ردیف</th>
496 <th style=\"border:1px solid #334155;padding:5px 8px;text-align:right\">شرح کالا | خدمات</th>
497 <th style=\"border:1px solid #334155;padding:5px 4px;width:60px\">واحد</th>
498 <th style=\"border:1px solid #334155;padding:5px 4px;width:60px\">مقدار</th>
499 <th style=\"border:1px solid #334155;padding:5px 4px;width:${hasVat?'110':'150'}px;text-align:left\">قیمت واحد (${cur})</th>
500 <th style=\"border:1px solid #334155;padding:5px 4px;width:${hasVat?'120':'170'}px;text-align:left\">قیمت کل (${cur})</th>
501 ${hasVat?`<th style=\"border:1px solid #334155;padding:5px 4px;width:110px;text-align:left\">مالیات (${cur})</th><th style=\"border:1px solid #334155;padding:5px 4px;width:130px;text-align:left\">مبلغ نهایی (${cur})</th>`:''}
502 </tr>
503 </thead>
504 <tbody>
505 ${!opt.isFirstPage?`<tr style=\"background:#fef3c7;font-weight:bold;font-size:9.5px\"><td colspan=\"${hasVat?6:5}\" style=\"border:1px solid #334155;padding:3px;text-align:left\">نقل از صفحه قبل:</td><td colspan=\"${hasVat?2:1}\" style=\"border:1px solid #334155;padding:3px;text-align:left\">${(opt.cumulativeBefore||0).toLocaleString('fa-IR')} ${cur}</td></tr>`:''}
506 ${rowsHtml}
507 </tbody>
508 </table>
509 ${summaryBox}
510 ${signatures}
511 </div>`;
512 }
513 function renderAndPrintDirect(invId){
514 const inv=getMyInvoices().find(i=>i.id===invId);
515 if(!inv)return;
516 const myCompanies=getMyCompanies();
517 const myContacts=getMyContacts();
518 const seller=myCompanies.find(c=>c.id===inv.companyId)||myCompanies[0]||{};
519 const contact=myContacts.find(c=>c.id===inv.contactId)||{};
520 const isFormal=(inv.kind==='formal');
521 const isPreInvoice=(inv.kind==='pre_invoice');
522 const builder=isFormal?buildFormalPage:buildNonFormalPage;
523 const rowsPerPage=isFormal?FORMAL_ROWS_PER_PAGE:NONFORMAL_ROWS_PER_PAGE;
524 const allLines=computeInvoiceLines(inv);
525 if(!allLines.length){alert('این سند ردیف قابل چاپ ندارد.');return;}
526 const pageGroups=[];
527 let cumulativeSub=0,cumulativeDisc=0,cumulativeVat=0;
528 for(let start=0;start<allLines.length;start+=rowsPerPage){
529 const pageItems=allLines.slice(start,start+rowsPerPage);
530 const isFirstPage=(start===0);
531 const isLastPage=(start+rowsPerPage>=allLines.length);
532 const pageSub=pageItems.reduce((s,li)=>s+li.lineSubtotal,0);
533 const pageDisc=pageItems.reduce((s,li)=>s+li.lineDiscount,0);
534 const pageVat=pageItems.reduce((s,li)=>s+li.lineVat,0);
535 const cumSubAfter=cumulativeSub+pageSub;
536 const cumDiscAfter=cumulativeDisc+pageDisc;
537 const cumVatAfter=cumulativeVat+pageVat;
538 const cumNetAfter=cumSubAfter-cumDiscAfter+cumVatAfter;
539 pageGroups.push({items:pageItems,isFirstPage,isLastPage,cumulativeBefore:cumulativeSub,cumulativeSubtotal:cumSubAfter,cumulativeDiscount:cumDiscAfter,cumulativeVat:cumVatAfter,cumulativeNet:cumNetAfter});
540 cumulativeSub=cumSubAfter;cumulativeDisc=cumDiscAfter;cumulativeVat=cumVatAfter;
541 }
542 const totalPages=pageGroups.length;
543 const containerHtml=pageGroups.map((g,idx)=>builder(g.items,seller,contact,inv,{pageNum:idx+1,totalPages,isFirstPage:g.isFirstPage,isLastPage:g.isLastPage,cumulativeBefore:g.cumulativeBefore,cumulativeSubtotal:g.cumulativeSubtotal,cumulativeDiscount:g.cumulativeDiscount,cumulativeVat:g.cumulativeVat,cumulativeNet:g.cumulativeNet})).join('');
544 const spec=getInvoicePrintSpec(inv);
545 stageInvoicePrint(containerHtml,spec);
546 }