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

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

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

Synced Dec 3, 20:45 UTC

feat/draft-first-attachment-lifecycle-216
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 575 lines 41 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 // Print-only display normalization: stored serials, API identifiers and numeric values stay unchanged.
21 function localizePrintTextNodes(root){
22 if(!root)return;
23 const walker=(root.ownerDocument||document).createTreeWalker(root,NodeFilter.SHOW_TEXT);
24 const latin='0123456789',arabic='٠١٢٣٤٥٦٧٨٩',persian='۰۱۲۳۴۵۶۷۸۹';
25 let node;
26 while((node=walker.nextNode())){
27 if(node.parentElement&&/^(SCRIPT|STYLE|NOSCRIPT)$/i.test(node.parentElement.tagName))continue;
28 node.nodeValue=node.nodeValue.replace(/[0-9٠-٩]/g,ch=>{
29 const n=latin.includes(ch)?latin.indexOf(ch):arabic.indexOf(ch);
30 return n<0?ch:persian[n];
31 });
32 }
33 }
34 function localizePrintHtmlDigits(html){
35 const template=document.createElement('template');
36 template.innerHTML=String(html??'');
37 localizePrintTextNodes(template.content);
38 return template.innerHTML;
39 }
40 function buildIsolatedPrintDocument(content,spec){
41 content=localizePrintHtmlDigits(content);
42 const usableWidth=Math.max(1,spec.widthMm-(spec.marginMm*2));
43 const usableHeight=Math.max(1,spec.heightMm-(spec.marginMm*2));
44 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>'
45 +'@page{size:'+spec.paper+' '+spec.orientation+';margin:'+spec.marginMm+'mm}'
46 +'*{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}'
47 +'#finora-print-document{margin:0!important;padding:0!important;width:100%!important}'
48 +'.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}'
49 +'.invoice-a4-page:last-child{page-break-after:auto!important;break-after:auto!important}'
50 +'.invoice-a4-page table{max-width:100%!important}'
51 +'@media print{html,body,#finora-print-document{width:100%!important}.invoice-a4-page{box-shadow:none!important}}'
52 +'</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>';
53 }
54 function removeIsolatedPrintFrame(){
55 const old=document.getElementById('finora-print-frame');if(old)old.remove();
56 }
57 function printIsolatedDocument(content,spec){
58 removeIsolatedPrintFrame();
59 const frame=document.createElement('iframe');
60 frame.id='finora-print-frame';
61 frame.title='Finora isolated print document';
62 frame.setAttribute('aria-hidden','true');
63 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';
64 document.body.appendChild(frame);
65 const doc=frame.contentDocument;
66 doc.open();doc.write(buildIsolatedPrintDocument(content,spec));doc.close();
67 if(window.__FINORA_PRINT_TEST_MODE)return frame;
68 const cleanup=()=>{setTimeout(removeIsolatedPrintFrame,0);};
69 frame.contentWindow.addEventListener('afterprint',cleanup,{once:true});
70 setTimeout(async()=>{try{
71 await Promise.all([...doc.images].map(img=>img.decode()));
72 if(doc.fonts)await doc.fonts.ready;
73 if(!frame.isConnected)return;
74 frame.contentWindow.focus();frame.contentWindow.print();setTimeout(cleanup,3000);
75 }catch(err){cleanup();alert('تصویر یا منابع چاپ بارگذاری نشد. لطفاً دوباره تلاش کنید.');}},80);
76 return frame;
77 }
78 function stageInvoicePrint(content,spec){
79 const localized=localizePrintHtmlDigits(content);
80 const preview=document.getElementById('printable-invoice');
81 if(preview){preview.innerHTML=localized;preview.style.display='none';preview.dataset.paper=spec.paper;preview.dataset.orientation=spec.orientation;}
82 let orientationStyle=document.getElementById('dynamic-print-orientation');
83 if(!orientationStyle){orientationStyle=document.createElement('style');orientationStyle.id='dynamic-print-orientation';document.head.appendChild(orientationStyle);}
84 orientationStyle.innerHTML='@media print { @page { size: '+spec.paper+' '+spec.orientation+'; margin: '+spec.marginMm+'mm; } }';
85 return printIsolatedDocument(localized,spec);
86 }
87
88 function buildElectronicDocPage(inv,edoc,seller,contact){
89 const cur='ریال';
90 const items=inv.items;
91 const vatRate=(inv.vatRate!==undefined&&inv.vatType!=='fixed')?inv.vatRate:10;
92
93 const itemsRows=items.map((it,idx)=>{
94 const line=it.lineTotal;
95 const rowVatRate=(inv.vat>0)?vatRate:0;
96 const vat=Math.round(line*rowVatRate/100);
97 const total=line+vat;
98 const edItem=(edoc.items&&edoc.items[idx])?edoc.items[idx]:{};
99 return `<tr>
100 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(idx+1)}</td>
101 <td style=\"border:1px solid #000;padding:4px\">${esc(it.prodName||'')}</td>
102 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(it.qty)}</td>
103 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(it.unit||'')}</td>
104 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${it.price.toLocaleString('fa-IR')}</td>
105 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${line.toLocaleString('fa-IR')}</td>
106 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${toPersianDigits(rowVatRate)}٪</td>
107 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${vat.toLocaleString('fa-IR')}</td>
108 <td style=\"border:1px solid #000;padding:4px;text-align:left\">${total.toLocaleString('fa-IR')}</td>
109 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(edItem.internalId||'')}</td>
110 <td style=\"border:1px solid #000;padding:4px;text-align:center\">${esc(edItem.officialId||'')}</td>
111 </tr>`;
112 }).join('');
113
114 let contractText='';
115 if(edoc.pattern==='پیمانکاری'&&edoc.contractId){
116 contractText=items.map((it,idx)=>
117 `+ شناسه یکتای ثبت قرارداد حق‌العمل‌کاری برای ردیف ${toPersianDigits(idx+1)}: ${esc(edoc.contractId)} است`
118 ).join('<br>');
119 }
120
121 const grandTotal=inv.grandTotal||0;
122 const vatTotal=inv.vat||0;
123 const baseTotal=grandTotal-vatTotal;
124
125 return `<div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:6mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif;position:relative\">
126 <table style=\"width:100%;border-collapse:collapse;margin-bottom:4px\">
127 <tr>
128 <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>
129 <td style=\"width:60%;text-align:center;vertical-align:middle;padding:6px\">
130 <div style=\"font-size:14px;font-weight:bold;color:#1e3a8a\">صورتحساب الکترونیکی فروش کالا / خدمات</div>
131 <div style=\"font-size:9.5px;color:#475569;margin-top:2px\">(مطابق استاندارد سامانه مودیان)</div>
132 </td>
133 <td style=\"width:20%;vertical-align:top;padding:4px;font-size:9.5px;text-align:left\">
134 <div>${esc(seller.name||'')}</div>
135 <div style=\"color:#475569\">${esc(seller.phone||'')}</div>
136 </td>
137 </tr>
138 </table>
139
140 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
141 <tr>
142 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9;width:30%\"><strong>شماره منحصر به فرد صورتحساب الکترونیکی مالیاتی</strong></td>
143 <td style=\"border:1px solid #000;padding:4px;width:70%\">${esc(edoc.uniqueNumber||'—')}</td>
144 </tr>
145 <tr>
146 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>شماره مالیاتی مندرج در برگه تشخیص</strong></td>
147 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.taxNumber||'—')}</td>
148 </tr>
149 <tr>
150 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>تاریخ و زمان صدور صورتحساب الکترونیکی</strong></td>
151 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.issueDateTime||'—')}</td>
152 </tr>
153 <tr>
154 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>تاریخ صورتحساب الکترونیکی اصلی</strong></td>
155 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.originalDate||'—')}</td>
156 </tr>
157 <tr>
158 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>شماره صورتحساب الکترونیکی اصلی</strong></td>
159 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.originalNumber||'—')}</td>
160 </tr>
161 <tr>
162 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>نوع صورتحساب</strong></td>
163 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.kind||'—')}</td>
164 </tr>
165 <tr>
166 <td style=\"border:1px solid #000;padding:4px;background:#f1f5f9\"><strong>موضوع صورتحساب</strong></td>
167 <td style=\"border:1px solid #000;padding:4px\">${esc(edoc.pattern||'—')}</td>
168 </tr>
169 </table>
170
171 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
172 <tr><td colspan=\"4\" style=\"border:1px solid #000;padding:4px;background:#f1f5f9;font-weight:bold\">مشخصات فروشنده</td></tr>
173 <tr>
174 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">نام شخص</td>
175 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(seller.name||'—')}</td>
176 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">شماره اقتصادی فروشنده</td>
177 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(seller.economic_code||'0000')}</td>
178 </tr>
179 <tr>
180 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">شماره ثبت / شناسه ملی</td>
181 <td style=\"border:1px solid #000;padding:4px\">${esc(seller.national_id||'—')}</td>
182 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">کد پستی</td>
183 <td style=\"border:1px solid #000;padding:4px\">${esc(seller.postal_code||'—')}</td>
184 </tr>
185 </table>
186
187 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-bottom:4px\">
188 <tr><td colspan=\"4\" style=\"border:1px solid #000;padding:4px;background:#f1f5f9;font-weight:bold\">مشخصات خریدار</td></tr>
189 <tr>
190 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">نام شخص</td>
191 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(contact.name||inv.contactName)}</td>
192 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:15%\">شماره اقتصادی خریدار</td>
193 <td style=\"border:1px solid #000;padding:4px;width:35%\">${esc(contact.economic_code||'0000')}</td>
194 </tr>
195 <tr>
196 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">شماره ثبت / شناسه ملی</td>
197 <td style=\"border:1px solid #000;padding:4px\">${esc(contact.national_id||'—')}</td>
198 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">کد پستی</td>
199 <td style=\"border:1px solid #000;padding:4px\">${esc(contact.postal_code||'—')}</td>
200 </tr>
201 </table>
202
203 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:9.5px\">
204 <thead>
205 <tr style=\"background:#f1f5f9;font-weight:bold;text-align:center\">
206 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">ردیف</th>
207 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شرح کالا/خدمت</th>
208 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">تعداد/مقدار</th>
209 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">واحد اندازه‌گیری</th>
210 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">مبلغ واحد (ریال)</th>
211 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">مبلغ کل (ریال)</th>
212 <th style=\"border:1px solid #000;padding:4px\" colspan=\"3\">مالیات بر ارزش افزوده</th>
213 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شناسه کالا/خدمت داخلی</th>
214 <th style=\"border:1px solid #000;padding:4px\" rowspan=\"2\">شناسه کالا/خدمت</th>
215 </tr>
216 <tr style=\"background:#f1f5f9;font-weight:bold;text-align:center\">
217 <th style=\"border:1px solid #000;padding:4px\">نرخ</th>
218 <th style=\"border:1px solid #000;padding:4px\">مبلغ</th>
219 <th style=\"border:1px solid #000;padding:4px\">مبلغ کل کالا/خدمت</th>
220 </tr>
221 </thead>
222 <tbody>${itemsRows}</tbody>
223 </table>
224
225 <table style=\"width:100%;border-collapse:collapse;border:1px solid #000;font-size:10px;margin-top:6px\">
226 <tr>
227 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:25%\">جمع کل مبلغ کالا/خدمت</td>
228 <td style=\"border:1px solid #000;padding:4px;width:25%;text-align:left\">${baseTotal.toLocaleString('fa-IR')} ${cur}</td>
229 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc;width:25%\">جمع کل مالیات بر ارزش افزوده</td>
230 <td style=\"border:1px solid #000;padding:4px;width:25%;text-align:left\">${vatTotal.toLocaleString('fa-IR')} ${cur}</td>
231 </tr>
232 <tr>
233 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مبلغ کل صورتحساب</td>
234 <td style=\"border:1px solid #000;padding:4px;text-align:left;font-weight:bold\" colspan=\"3\">${grandTotal.toLocaleString('fa-IR')} ${cur}</td>
235 </tr>
236 <tr>
237 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مصرف‌کننده نهایی</td>
238 <td style=\"border:1px solid #000;padding:4px\">خیر</td>
239 <td style=\"border:1px solid #000;padding:4px;background:#f8fafc\">مالیات موضوع ماده ۱۷</td>
240 <td style=\"border:1px solid #000;padding:4px;text-align:left\">۰ ${cur}</td>
241 </tr>
242 </table>
243
244 ${contractText?`<div style=\"border:1px solid #000;padding:6px;margin-top:6px;font-size:10.5px;line-height:1.7;background:#fffbeb\">${contractText}</div>`:''}
245
246 <table style=\"width:100%;border-collapse:collapse;margin-top:8px;font-size:10.5px\">
247 <tr>
248 <td style=\"width:50%;border:1px solid #000;padding:8px;height:70px;vertical-align:top\">
249 <div style=\"font-weight:bold;margin-bottom:4px\">مهر و امضای فروشنده</div>
250 <div style=\"font-size:9.5px;color:#475569\">${esc(seller.name||'')}</div>
251 </td>
252 <td style=\"width:50%;border:1px solid #000;padding:8px;height:70px;vertical-align:top\">
253 <div style=\"font-weight:bold;margin-bottom:4px\">مهر و امضای خریدار</div>
254 <div style=\"font-size:9.5px;color:#475569\">${esc(contact.name||inv.contactName)}</div>
255 </td>
256 </tr>
257 </table>
258 </div>`;
259 }
260
261 function printElectronicDoc(invId){
262 const inv=getMyInvoices().find(i=>i.id===invId);
263 if(inv&&typeof invoiceDocumentStatus==='function'&&invoiceDocumentStatus(inv)==='draft'){alert('سند رسمی مؤدیان فقط پس از قطعی‌سازی قابل چاپ است.');return;}
264 if(!inv||!inv.electronicDoc){
265 alert('سند الکترونیکی برای این فاکتور ثبت نشده است.');
266 return;
267 }
268 const myCompanies=getMyCompanies();
269 const myContacts=getMyContacts();
270 const seller=myCompanies.find(c=>c.id===inv.companyId)||myCompanies[0]||{};
271 const contact=myContacts.find(c=>c.id===inv.contactId)||{};
272 const html=buildElectronicDocPage(inv,inv.electronicDoc,seller,contact);
273 stageInvoicePrint(html,{paper:'A4',orientation:'landscape',widthMm:297,heightMm:210,marginMm:7});
274 }
275
276 // ============ فاکتورهای معمول ============
277 const FORMAL_ROWS_PER_PAGE=12;
278 const NONFORMAL_ROWS_PER_PAGE=10;
279 function computeInvoiceLines(inv){
280 const n=inv.items.length;
281 const afterDiscountTotal=Math.max(0,(inv.subtotal||0)-(inv.discount||0));
282 let usedDiscount=0,usedVat=0;
283 return inv.items.map((it,idx)=>{
284 const lineSubtotal=it.lineTotal;
285 const isLastItem=(idx===n-1);
286 let lineDiscount,lineVat;
287 if(isLastItem){
288 lineDiscount=Math.max(0,(inv.discount||0)-usedDiscount);
289 lineVat=Math.max(0,(inv.vat||0)-usedVat);
290 }else{
291 lineDiscount=(inv.subtotal>0)?Math.round((inv.discount||0)*(lineSubtotal/inv.subtotal)):0;
292 const lineAfterDiscount=lineSubtotal-lineDiscount;
293 lineVat=(afterDiscountTotal>0&&inv.vat>0)?Math.round((inv.vat||0)*(lineAfterDiscount/afterDiscountTotal)):0;
294 }
295 usedDiscount+=lineDiscount;usedVat+=lineVat;
296 const lineNet=lineSubtotal-lineDiscount+lineVat;
297 return {rowNo:idx+1,prodName:it.prodName,qty:it.qty,unit:it.unit||'عدد',price:it.price,lineSubtotal,lineDiscount,lineVat,lineNet};
298 });
299 }
300 function buildFormalPage(items,seller,contact,inv,opt){
301 const cur=inv.currencyLabel||'ریال';
302 const isLast=opt.isLastPage;
303 const rowsHtml=items.map(li=>`
304 <tr style=\"text-align:center\">
305 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${li.rowNo.toLocaleString('fa-IR')}</td>
306 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:right;font-size:10.5px;font-weight:600\">${esc(li.prodName)}</td>
307 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${li.qty.toLocaleString('fa-IR')}</td>
308 <td style=\"border:1px solid #1e293b;padding:3px 6px;font-size:10.5px\">${esc(li.unit)}</td>
309 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.price.toLocaleString('fa-IR')}</td>
310 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
311 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineDiscount.toLocaleString('fa-IR')}</td>
312 <td style=\"border:1px solid #1e293b;padding:3px 6px;text-align:left;font-size:10.5px\">${li.lineVat.toLocaleString('fa-IR')}</td>
313 <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>
314 </tr>`).join('');
315 const sellerIds=`${esc(seller.national_id||'—')} / ${esc(seller.reg_number||'—')}`;
316 const contactIds=`${esc(contact.national_id||'—')} / ${esc(contact.reg_number||'—')}`;
317 const cumSub=opt.cumulativeSubtotal||0;
318 const cumDisc=opt.cumulativeDiscount||0;
319 const cumVat=opt.cumulativeVat||0;
320 const cumNet=opt.cumulativeNet||0;
321 const subtotalLabel=isLast?'جمع کل اقلام:':'جمع اقلام تا این صفحه:';
322 const discountLabel=isLast?'مجموع تخفیف:':'مجموع تخفیف تا این صفحه:';
323 const vatLabel=isLast?'مالیات و عوارض ارزش افزوده:':'مالیات تا این صفحه:';
324 const netLabel=isLast?'مبلغ قابل پرداخت:':'جمع تا این صفحه:';
325 const wordsLabel=isLast?'مبلغ کل به حروف:':'مبلغ تا این صفحه به حروف:';
326 const summaryBox=`
327 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10.5px;margin-top:3px\">
328 <tr>
329 <td style=\"width:60%;border:1px solid #1e3a8a;padding:6px;vertical-align:top;background:#f8fafc\">
330 <div style=\"margin-bottom:4px\"><strong>${wordsLabel}</strong> ${numberToPersianWords(cumNet,cur)}</div>
331 ${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>`}
332 </td>
333 <td style=\"width:40%;border:1px solid #1e3a8a;padding:0\">
334 <table style=\"width:100%;border-collapse:collapse;font-size:10.5px\">
335 <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>
336 <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>
337 <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>
338 <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>
339 </table>
340 </td>
341 </tr>
342 </table>`;
343 const signatures=isLast?`
344 <table style=\"width:100%;border-collapse:collapse;border:1px solid #1e3a8a;margin-top:6px;text-align:center;font-size:10.5px\">
345 <tr>
346 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #1e3a8a;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای فروشنده</div></td>
347 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #1e3a8a;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای خریدار</div></td>
348 </tr>
349 </table>`:'';
350 return `
351 <div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:6mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif\">
352 <table style=\"width:100%;border-collapse:collapse;margin-bottom:3px\">
353 <tr>
354 <td style=\"width:28%;padding:5px;vertical-align:middle\"></td>
355 <td style=\"width:44%;padding:6px;text-align:center;vertical-align:middle;background:#eff6ff\">
356 <h1 style=\"font-size:15px;margin:0;font-weight:bold;color:#1e3a8a\">صورتحساب فروش کالا و خدمات</h1>
357 </td>
358 <td style=\"width:28%;padding:5px;font-size:10.5px;vertical-align:middle;text-align:left\">
359 <div>شماره سریال: <strong style=\"color:#b91c1c\">${esc(inv.number)}</strong></div>
360 <div style=\"margin-top:1px\">تاریخ صدور: <strong>${esc(inv.date)}</strong></div>
361 <div style=\"margin-top:1px;color:#64748b;font-size:9.5px\">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
362 </td>
363 </tr>
364 </table>
365 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px;margin-bottom:3px\">
366 <tr style=\"background:#f8fafc;font-weight:bold\"><td colspan=\"4\" style=\"border:1px solid #1e3a8a;padding:2px 6px;color:#1e3a8a\">مشخصات فروشنده</td></tr>
367 <tr>
368 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">نام شخص حقیقی/حقوقی:</td>
369 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\"><strong>${esc(seller.name||'—')}</strong></td>
370 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">شماره اقتصادی:</td>
371 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\">${esc(seller.economic_code||'—')}</td>
372 </tr>
373 <tr>
374 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">شناسه ملی / شماره ثبت:</td>
375 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${sellerIds}</td>
376 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">کد پستی ۱۰ رقمی:</td>
377 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(seller.postal_code||'—')}</td>
378 </tr>
379 <tr>
380 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">نشانی و تلفن:</td>
381 <td colspan=\"3\" style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(seller.address||'—')} ${esc(seller.phone?' | تلفن: '+seller.phone:'')}</td>
382 </tr>
383 </table>
384 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px;margin-bottom:3px\">
385 <tr style=\"background:#f8fafc;font-weight:bold\"><td colspan=\"4\" style=\"border:1px solid #1e3a8a;padding:2px 6px;color:#1e3a8a\">مشخصات خریدار</td></tr>
386 <tr>
387 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">نام شخص حقیقی/حقوقی:</td>
388 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\"><strong>${esc(contact.name||inv.contactName)}</strong></td>
389 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:15%;background:#f1f5f9\">شماره اقتصادی:</td>
390 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;width:35%\">${esc(contact.economic_code||'—')}</td>
391 </tr>
392 <tr>
393 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">شناسه ملی / شماره ثبت:</td>
394 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${contactIds}</td>
395 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">کد پستی ۱۰ رقمی:</td>
396 <td style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(contact.postal_code||'—')}</td>
397 </tr>
398 <tr>
399 <td style=\"border:1px solid #cbd5e1;padding:3px 6px;background:#f1f5f9\">نشانی و تلفن:</td>
400 <td colspan=\"3\" style=\"border:1px solid #cbd5e1;padding:3px 6px\">${esc(contact.address||'—')} ${esc(contact.mobile?' | تلفن: '+contact.mobile:'')}</td>
401 </tr>
402 </table>
403 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #1e3a8a;font-size:10px\">
404 <thead>
405 <tr style=\"background:#1e3a8a;color:#fff;font-weight:bold;text-align:center\">
406 <th style=\"border:1px solid #1e3a8a;padding:4px;width:28px\">ردیف</th>
407 <th style=\"border:1px solid #1e3a8a;padding:4px 6px;text-align:right\">شرح کالا یا خدمات</th>
408 <th style=\"border:1px solid #1e3a8a;padding:4px;width:42px\">تعداد</th>
409 <th style=\"border:1px solid #1e3a8a;padding:4px;width:42px\">واحد</th>
410 <th style=\"border:1px solid #1e3a8a;padding:4px;width:105px\">مبلغ واحد (${cur})</th>
411 <th style=\"border:1px solid #1e3a8a;padding:4px;width:110px\">مبلغ کل (${cur})</th>
412 <th style=\"border:1px solid #1e3a8a;padding:4px;width:80px\">تخفیف (${cur})</th>
413 <th style=\"border:1px solid #1e3a8a;padding:4px;width:90px\">مالیات (${cur})</th>
414 <th style=\"border:1px solid #1e3a8a;padding:4px;width:120px\">مبلغ نهایی (${cur})</th>
415 </tr>
416 </thead>
417 <tbody>
418 ${!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>`:''}
419 ${rowsHtml}
420 </tbody>
421 </table>
422 ${summaryBox}
423 ${signatures}
424 </div>`;
425 }
426 function buildNonFormalPage(items,seller,contact,inv,opt){
427 const cur=inv.currencyLabel||'ریال';
428 const isPre=inv.kind==='pre_invoice';
429 const isLast=opt.isLastPage;
430 const hasVat=isPre&&inv.vat>0;
431 const title=isPre?'پیش‌فاکتور فروش کالا و خدمات':'فاکتور فروش کالا و خدمات';
432 const rowsHtml=items.map(li=>hasVat?`
433 <tr style=\"text-align:center\">
434 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${li.rowNo.toLocaleString('fa-IR')}</td>
435 <td style=\"border:1px solid #334155;padding:3px 6px;text-align:right;font-size:10px;font-weight:600\">${esc(li.prodName)}</td>
436 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${esc(li.unit)}</td>
437 <td style=\"border:1px solid #334155;padding:3px 5px;font-size:10px\">${li.qty.toLocaleString('fa-IR')}</td>
438 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.price.toLocaleString('fa-IR')}</td>
439 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.lineSubtotal.toLocaleString('fa-IR')}</td>
440 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px\">${li.lineVat.toLocaleString('fa-IR')}</td>
441 <td style=\"border:1px solid #334155;padding:3px 5px;text-align:left;font-size:10px;font-weight:bold\">${li.lineNet.toLocaleString('fa-IR')}</td>
442 </tr>`:`
443 <tr style=\"text-align:center\">
444 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${li.rowNo.toLocaleString('fa-IR')}</td>
445 <td style=\"border:1px solid #334155;padding:4px 8px;text-align:right;font-size:10.5px;font-weight:600\">${esc(li.prodName)}</td>
446 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${esc(li.unit)}</td>
447 <td style=\"border:1px solid #334155;padding:4px 5px;font-size:10.5px\">${li.qty.toLocaleString('fa-IR')}</td>
448 <td style=\"border:1px solid #334155;padding:4px 5px;text-align:left;font-size:10.5px\">${li.price.toLocaleString('fa-IR')}</td>
449 <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>
450 </tr>`).join('');
451 const cumSub=opt.cumulativeSubtotal||0;
452 const cumDisc=opt.cumulativeDiscount||0;
453 const cumVat=opt.cumulativeVat||0;
454 const cumNet=opt.cumulativeNet||0;
455 const subtotalLabel=isLast?'قیمت کل اقلام:':'جمع اقلام تا این صفحه:';
456 const discountLabel=isLast?'تخفیف:':'تخفیف تا این صفحه:';
457 const vatLabel=isLast?'مالیات ارزش افزوده:':'مالیات تا این صفحه:';
458 const netLabel=isLast?`مبلغ کل (${cur}):`:'جمع تا این صفحه:';
459 const wordsLabel=isLast?'مبلغ به حروف:':'مبلغ تا این صفحه به حروف:';
460 const summaryBox=`
461 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px;margin-top:5px\">
462 <tr>
463 <td style=\"width:60%;border:1px solid #334155;padding:6px;vertical-align:top;background:#f8fafc\">
464 <div style=\"margin-bottom:4px\"><strong>${wordsLabel}</strong> ${numberToPersianWords(cumNet,cur)}</div>
465 ${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>`}
466 </td>
467 <td style=\"width:40%;border:1px solid #334155;padding:0\">
468 <table style=\"width:100%;border-collapse:collapse;font-size:10.5px\">
469 <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>
470 ${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>`:''}
471 ${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>`:''}
472 <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>
473 </table>
474 </td>
475 </tr>
476 </table>`;
477 const signatures=isLast?`
478 <table style=\"width:100%;border-collapse:collapse;border:1px solid #334155;margin-top:6px;text-align:center;font-size:10.5px\">
479 <tr>
480 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #334155;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای فروشنده</div></td>
481 <td style=\"width:50%;height:50px;vertical-align:top;border:1px solid #334155;padding:5px;background:#fff\"><div style=\"font-weight:bold\">مهر و امضای خریدار</div></td>
482 </tr>
483 </table>`:'';
484 return `
485 <div class=\"invoice-a4-page landscape\" style=\"background:#fff;padding:7mm 8mm;color:#000;font-family:'Vazirmatn FD',sans-serif\">
486 <table style=\"width:100%;border-collapse:collapse;margin-bottom:5px\">
487 <tr>
488 <td style=\"width:30%;padding:6px;vertical-align:middle\"></td>
489 <td style=\"width:40%;padding:6px;text-align:center;vertical-align:middle;background:#f8fafc\">
490 <h1 style=\"font-size:15px;margin:0;font-weight:bold;color:#0f172a\">${title}</h1>
491 </td>
492 <td style=\"width:30%;padding:6px;font-size:10.5px;vertical-align:middle;text-align:left\">
493 <div>شماره: <strong style=\"font-size:11.5px\">#${esc(inv.number)}</strong></div>
494 <div style=\"margin-top:1px\">تاریخ: <strong>${esc(inv.date)}</strong></div>
495 <div style=\"margin-top:1px;color:#64748b;font-size:9.5px\">صفحه: ${opt.pageNum} از ${opt.totalPages}</div>
496 </td>
497 </tr>
498 </table>
499 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px;margin-bottom:5px\">
500 <tr>
501 <td style=\"width:50%;border:1px solid #334155;padding:5px;vertical-align:top;background:#f8fafc\">
502 <div style=\"font-weight:bold;color:#334155;margin-bottom:2px\">مشخصات فروشنده:</div>
503 <div>نام: <strong>${esc(seller.name||'—')}</strong></div>
504 <div>نشانی: ${esc(seller.address||'—')}</div>
505 <div>تلفن تماس: ${esc(seller.phone||'—')}</div>
506 </td>
507 <td style=\"width:50%;border:1px solid #334155;padding:5px;vertical-align:top;background:#f8fafc\">
508 <div style=\"font-weight:bold;color:#334155;margin-bottom:2px\">مشخصات خریدار:</div>
509 <div>نام: <strong>${esc(contact.name||inv.contactName)}</strong></div>
510 <div>نشانی: ${esc(contact.address||'—')}</div>
511 <div>تلفن: ${esc(contact.mobile||'—')}</div>
512 </td>
513 </tr>
514 </table>
515 <table style=\"width:100%;border-collapse:collapse;border:1.5px solid #334155;font-size:10.5px\">
516 <thead>
517 <tr style=\"background:#334155;color:#fff;font-weight:bold;text-align:center\">
518 <th style=\"border:1px solid #334155;padding:5px 4px;width:32px\">ردیف</th>
519 <th style=\"border:1px solid #334155;padding:5px 8px;text-align:right\">شرح کالا | خدمات</th>
520 <th style=\"border:1px solid #334155;padding:5px 4px;width:60px\">واحد</th>
521 <th style=\"border:1px solid #334155;padding:5px 4px;width:60px\">مقدار</th>
522 <th style=\"border:1px solid #334155;padding:5px 4px;width:${hasVat?'110':'150'}px;text-align:left\">قیمت واحد (${cur})</th>
523 <th style=\"border:1px solid #334155;padding:5px 4px;width:${hasVat?'120':'170'}px;text-align:left\">قیمت کل (${cur})</th>
524 ${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>`:''}
525 </tr>
526 </thead>
527 <tbody>
528 ${!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>`:''}
529 ${rowsHtml}
530 </tbody>
531 </table>
532 ${summaryBox}
533 ${signatures}
534 </div>`;
535 }
536 function renderAndPrintDirect(invId){
537 const record=getMyInvoices().find(i=>i.id===invId);
538 if(!record||!afCompanyId()||record.companyId!==afCompanyId()){
539 alert('برای چاپ، ابتدا شرکت مربوط به فاکتور را در تنظیمات فعال کنید.');return;
540 }
541 const inv=typeof invoicePrintView==='function'?invoicePrintView(record):record;
542 if(!inv)return;
543 const myCompanies=getMyCompanies();
544 const myContacts=getMyContacts();
545 const seller=myCompanies.find(c=>c.id===inv.companyId)||myCompanies[0]||{};
546 const contact=myContacts.find(c=>c.id===inv.contactId)||{};
547 const isFormal=(inv.kind==='formal');
548 const isPreInvoice=(inv.kind==='pre_invoice');
549 const builder=isFormal?buildFormalPage:buildNonFormalPage;
550 const rowsPerPage=isFormal?FORMAL_ROWS_PER_PAGE:NONFORMAL_ROWS_PER_PAGE;
551 const allLines=computeInvoiceLines(inv);
552 if(!allLines.length){alert('این سند ردیف قابل چاپ ندارد.');return;}
553 const pageGroups=[];
554 let cumulativeSub=0,cumulativeDisc=0,cumulativeVat=0;
555 for(let start=0;start<allLines.length;start+=rowsPerPage){
556 const pageItems=allLines.slice(start,start+rowsPerPage);
557 const isFirstPage=(start===0);
558 const isLastPage=(start+rowsPerPage>=allLines.length);
559 const pageSub=pageItems.reduce((s,li)=>s+li.lineSubtotal,0);
560 const pageDisc=pageItems.reduce((s,li)=>s+li.lineDiscount,0);
561 const pageVat=pageItems.reduce((s,li)=>s+li.lineVat,0);
562 const cumSubAfter=cumulativeSub+pageSub;
563 const cumDiscAfter=cumulativeDisc+pageDisc;
564 const cumVatAfter=cumulativeVat+pageVat;
565 const cumNetAfter=cumSubAfter-cumDiscAfter+cumVatAfter;
566 pageGroups.push({items:pageItems,isFirstPage,isLastPage,cumulativeBefore:cumulativeSub,cumulativeSubtotal:cumSubAfter,cumulativeDiscount:cumDiscAfter,cumulativeVat:cumVatAfter,cumulativeNet:cumNetAfter});
567 cumulativeSub=cumSubAfter;cumulativeDisc=cumDiscAfter;cumulativeVat=cumVatAfter;
568 }
569 const totalPages=pageGroups.length;
570 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('');
571 const spec=getInvoicePrintSpec(inv);
572 // Print the complete invoice layout without a draft-status watermark.
573 // Document state and explicit accounting finalization remain unchanged.
574 stageInvoicePrint(containerHtml,spec);
575 }