سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 22:00 UTC
feat/retain-document-editor-after-save-20261014
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
| 1 | const {test,expect}=require('@playwright/test'); |
| 2 | |
| 3 | const BASE=process.env.FINORA_TEST_BASE_URL||'http://127.0.0.1:4173/index.html'; |
| 4 | const USER='FINORA_F0_FIXTURE_ONLY'; |
| 5 | const copy=value=>JSON.parse(JSON.stringify(value)); |
| 6 | function fakeServer(){ |
| 7 | const records=new Map(); |
| 8 | for(const [collection,id,data] of [ |
| 9 | ['companies','QA_CO',{name:'شرکت آزمون ایزوله',entityType:'legal'}], |
| 10 | ['contacts','QA_CONTACT',{name:'خریدار آزمایشی',role:'both'}], |
| 11 | ['products','QA_PROD',{name:'بلوک سیمانی',code:'BLK-001',unit:'عدد',sale_price:1500}], |
| 12 | ['settings','main',{invoice_kind:'formal',vat_mode:'none',vat_rate:10,default_company_id:'QA_CO',currency_mode:'rial'}], |
| 13 | ['fiscalYears','FY_QA',{name:'۱۴۰۵',startDate:'1405/01/01',endDate:'1405/12/29',status:'open'}], |
| 14 | ['accounts','A_CASH',{code:'1101',title:'صندوق',systemRole:'cash_default',postingAllowed:true,active:true}], |
| 15 | ['accounts','A_AR',{code:'1102',title:'دریافتنی',systemRole:'receivable_control',postingAllowed:true,active:true}], |
| 16 | ['accounts','A_REV',{code:'4101',title:'درآمد فروش',systemRole:'revenue_default',postingAllowed:true,active:true}], |
| 17 | ['accounts','A_VAT',{code:'2102',title:'مالیات',systemRole:'vat_payable',postingAllowed:true,active:true}], |
| 18 | ['accounts','A_BUY',{code:'5101',title:'خرید',systemRole:'purchase_default',postingAllowed:true,active:true}], |
| 19 | ['accounts','A_AP',{code:'2101',title:'پرداختنی',systemRole:'payable_control',postingAllowed:true,active:true}], |
| 20 | ])records.set(collection+'|'+id,{collection,id,data:copy(data)}); |
| 21 | return {records,failNext:false,failCount:0,rpcCount:0}; |
| 22 | } |
| 23 | function remoteInvoices(server){return [...server.records.values()].filter(r=>r.collection==='invoices').map(r=>copy(r.data));} |
| 24 | |
| 25 | async function installHarness(page,server){ |
| 26 | await page.exposeFunction('qaF0Read',async()=>[...server.records.values()].map(copy)); |
| 27 | await page.exposeFunction('qaF0Write',async args=>{ |
| 28 | server.rpcCount++; |
| 29 | if(server.failNext){server.failNext=false;server.failCount++;return {error:{message:'Network unavailable — isolated QA failure',code:'QA_NETWORK'}};} |
| 30 | for(const row of args.p_upserts||[])server.records.set(row.collection+'|'+row.id,{collection:row.collection,id:row.id,data:copy(row.data)}); |
| 31 | for(const row of args.p_deletes||[])server.records.delete(row.collection+'|'+row.id); |
| 32 | return {error:null}; |
| 33 | }); |
| 34 | await page.route(/https:\/\/[^/]*supabase\.co\//,route=>route.abort()); |
| 35 | } |
| 36 | async function authenticatedMockBrowser(page){ |
| 37 | await page.goto(BASE,{waitUntil:'domcontentloaded'}); |
| 38 | await page.waitForFunction(()=>typeof enterApp==='function'&&typeof commitSaveInvoice==='function'&&typeof pullAll==='function'); |
| 39 | await page.evaluate(async()=>{ |
| 40 | window.__FINORA_PRINT_TEST_MODE=true; |
| 41 | window.__f0Alerts=[]; |
| 42 | window.alert=m=>window.__f0Alerts.push(String(m)); |
| 43 | window.confirm=()=>false; |
| 44 | fetchCurrentUser=async()=>({id:'FINORA_F0_FIXTURE_ONLY',username:'qa-only',email:'qa@example.invalid',fullName:'QA F0',role:'admin',maxCompanies:2,subscription:{type:'lifetime',status:'active',endDate:'2099-12-31'}}); |
| 45 | maybeMigrateLegacyData=()=>{}; |
| 46 | sb.from=collection=>({ |
| 47 | select:()=>({ |
| 48 | order(){return this;}, |
| 49 | async range(from,to){const rows=(await window.qaF0Read()).filter(r=>r.collection).sort((a,b)=>a.collection.localeCompare(b.collection)||a.id.localeCompare(b.id));return {data:rows.slice(from,to+1),error:null};} |
| 50 | }) |
| 51 | }); |
| 52 | sb.rpc=async(name,args)=>{ |
| 53 | if(name!=='finora_sync_records')throw Error('Unexpected RPC '+name); |
| 54 | return await window.qaF0Write(args); |
| 55 | }; |
| 56 | await enterApp({id:'FINORA_F0_FIXTURE_ONLY'}); |
| 57 | document.querySelectorAll('.modal-backdrop.active').forEach(x=>x.classList.remove('active')); |
| 58 | switchView('view-invoices'); |
| 59 | }); |
| 60 | await expect(page.locator('#auth-screen')).toBeHidden(); |
| 61 | await expect(page.locator('#invoice-company-id option[value="QA_CO"]')).toHaveCount(1); |
| 62 | await expect(page.locator('#invoice-items-table-body .row-product-select').first().locator('option[value="QA_PROD"]')).toHaveCount(1); |
| 63 | } |
| 64 | async function fillInvoice(page,number,qty){ |
| 65 | // The native invoice-kind/contact selectors are hidden by the unified commercial combobox. |
| 66 | // Drive their real DOM change handlers, not Playwright visibility on replaced native selects. |
| 67 | await page.evaluate(()=>{ |
| 68 | for(const [id,value] of [['invoice-kind','formal'],['invoice-company-id','QA_CO'],['invoice-contact-id','QA_CONTACT']]){ |
| 69 | const el=document.getElementById(id);el.value=value;el.dispatchEvent(new Event('change',{bubbles:true})); |
| 70 | } |
| 71 | }); |
| 72 | await page.locator('#invoice-number').fill(number); |
| 73 | await page.locator('#invoice-date-input').fill('۱۴۰۵/۰۷/۱۹'); |
| 74 | await page.evaluate(()=>{ |
| 75 | const el=document.querySelector('#invoice-items-table-body .row-product-select'); |
| 76 | el.value='QA_PROD';el.dispatchEvent(new Event('change',{bubbles:true})); |
| 77 | }); |
| 78 | await page.locator('#invoice-items-table-body .row-quantity').first().fill(String(qty)); |
| 79 | await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input'); |
| 80 | } |
| 81 | |
| 82 | test('formal invoice remains editable draft across server reload; explicit posting creates exactly one balanced voucher',async({page})=>{ |
| 83 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 84 | await fillInvoice(page,'FIN-DRAFT-001',2); |
| 85 | await page.evaluate(()=>commitSaveInvoice()); |
| 86 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 87 | expect(remoteInvoices(server)[0].accountingStatus).toBe('draft'); |
| 88 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0); |
| 89 | const id=remoteInvoices(server)[0].id; |
| 90 | // A saved formal draft prints its complete invoice; the print UI must not stamp it |
| 91 | // as provisional. The underlying draft state and zero-posting invariant remain. |
| 92 | await page.evaluate(x=>renderAndPrintDirect(x),id); |
| 93 | await expect(page.locator('#printable-invoice')).toContainText('FIN-DRAFT-۰۰۱'); |
| 94 | await expect(page.locator('#printable-invoice')).toContainText('۳٬۰۰۰'); |
| 95 | await expect(page.locator('#printable-invoice')).not.toContainText('پیشنویس — غیرقطعی'); |
| 96 | await expect(page.locator('#printable-invoice [style*="rotate(-12deg)"]')).toHaveCount(0); |
| 97 | const isolatedPrint=page.frameLocator('#finora-print-frame').locator('#finora-print-document'); |
| 98 | await expect(isolatedPrint).toContainText('FIN-DRAFT-۰۰۱'); |
| 99 | await expect(isolatedPrint).not.toContainText('پیشنویس — غیرقطعی'); |
| 100 | expect(remoteInvoices(server)[0].accountingStatus).toBe('draft'); |
| 101 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0); |
| 102 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 103 | await page.evaluate(x=>editInvoice(x),id); |
| 104 | await expect(page.locator('#edit-invoice-id')).toHaveValue(id); |
| 105 | await page.locator('#invoice-items-table-body .row-quantity').first().fill('3'); |
| 106 | await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input'); |
| 107 | await page.evaluate(()=>commitSaveInvoice()); |
| 108 | await expect.poll(()=>remoteInvoices(server)[0]?.grandTotal,{timeout:13000}).toBe(4500); |
| 109 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 110 | await expect(page.locator('#finora-document-management-body')).toContainText('FIN-DRAFT-001'); |
| 111 | await expect(page.locator('#finora-document-management-body')).toContainText('پیشنویس'); |
| 112 | await page.locator('#finora-document-management-body [data-doc-action="post"]').click(); |
| 113 | // Show a confirmation requiring explicit intent; no automatic posting on Save. |
| 114 | await expect.poll(()=>remoteInvoices(server)[0].accountingStatus,{timeout:3000}).toBe('draft'); |
| 115 | await page.evaluate(()=>{window.confirm=()=>true}); |
| 116 | await page.locator('#finora-document-management-body [data-doc-action="post"]').click(); |
| 117 | await expect.poll(()=>remoteInvoices(server)[0]?.accountingStatus,{timeout:13000}).toBe('posted'); |
| 118 | const posted=remoteInvoices(server)[0]; |
| 119 | const vouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 120 | const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'); |
| 121 | expect(vouchers).toHaveLength(1); |
| 122 | expect(vouchers[0].data.status).toBe('posted'); |
| 123 | expect(lines.length).toBeGreaterThanOrEqual(2); |
| 124 | expect(lines.reduce((t,x)=>t+Number(x.data.debit||0),0)).toBe(4500); |
| 125 | expect(lines.reduce((t,x)=>t+Number(x.data.credit||0),0)).toBe(4500); |
| 126 | expect(posted.journalVoucherId).toBe(vouchers[0].id); |
| 127 | // Repeat post must be idempotent, and posted source cannot be edited or overwritten. |
| 128 | await page.evaluate(x=>finalizeInvoiceDocument(x),id); |
| 129 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(1); |
| 130 | await page.evaluate(x=>editInvoice(x),id); |
| 131 | await expect(page.locator('#edit-invoice-id')).toHaveValue(''); |
| 132 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 133 | const saved=await page.evaluate(x=>getMyInvoices().find(i=>i.id===x),id); |
| 134 | expect(saved.documentStatus).toBe('posted'); |
| 135 | expect(saved.grandTotal).toBe(4500); |
| 136 | expect(saved.documentVersion).toBe(2); |
| 137 | }); |
| 138 | test('posted sale can be returned to an editable corrective draft; original voucher stays immutable and balances',async({page})=>{ |
| 139 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 140 | await fillInvoice(page,'PILOT-SALE-001',2);await page.evaluate(()=>commitSaveInvoice()); |
| 141 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 142 | const originalId=remoteInvoices(server)[0].id; |
| 143 | await page.evaluate(id=>finalizeInvoiceDocument(id),originalId); |
| 144 | await expect.poll(()=>remoteInvoices(server)[0]?.accountingStatus,{timeout:13000}).toBe('posted'); |
| 145 | const before=remoteInvoices(server)[0],originalVoucherId=before.journalVoucherId; |
| 146 | const originalLines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===originalVoucherId).map(copy); |
| 147 | const result=await page.evaluate(id=>reopenPostedInvoiceAsDraft(id,'تصحیح تعداد پایلوت'),originalId); |
| 148 | expect(result).toBeTruthy();expect(result).not.toBe(originalId); |
| 149 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(2); |
| 150 | const after=remoteInvoices(server),original=after.find(i=>i.id===originalId),draft=after.find(i=>i.id===result); |
| 151 | expect(original.grandTotal).toBe(3000); |
| 152 | expect(original.journalVoucherId).toBe(originalVoucherId); |
| 153 | expect(draft.correctsInvoiceId).toBe(originalId); |
| 154 | expect(draft.documentStatus).toBe('draft'); |
| 155 | expect(draft.journalVoucherId).toBeUndefined(); |
| 156 | expect(draft.number).not.toBe(original.number); |
| 157 | const allVouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 158 | expect(allVouchers).toHaveLength(2); |
| 159 | expect(allVouchers.find(x=>x.id===originalVoucherId).data.status).toBe('reversed'); |
| 160 | const reversal=allVouchers.find(x=>x.id!==originalVoucherId); |
| 161 | expect(reversal.data.status).toBe('posted'); |
| 162 | expect(originalLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===originalVoucherId).map(copy)); |
| 163 | expect(await page.evaluate(id=>invoiceDocumentStatus(getMyInvoices().find(x=>x.id===id)),originalId)).toBe('reversed'); |
| 164 | // The replacement source uses the normal draft editor and preserves provenance. |
| 165 | await page.evaluate(id=>editInvoice(id),result); |
| 166 | await expect(page.locator('#edit-invoice-id')).toHaveValue(result); |
| 167 | await page.locator('#invoice-items-table-body .row-quantity').first().fill('3'); |
| 168 | await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input'); |
| 169 | await page.evaluate(()=>commitSaveInvoice()); |
| 170 | await expect.poll(()=>remoteInvoices(server).find(x=>x.id===result)?.grandTotal,{timeout:13000}).toBe(4500); |
| 171 | await page.evaluate(id=>finalizeInvoiceDocument(id),result); |
| 172 | await expect.poll(()=>remoteInvoices(server).find(x=>x.id===result)?.accountingStatus,{timeout:13000}).toBe('posted'); |
| 173 | const later=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 174 | expect(later).toHaveLength(3); |
| 175 | expect(later.find(x=>x.id===originalVoucherId).data.status).toBe('reversed'); |
| 176 | const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'); |
| 177 | for(const v of later){ |
| 178 | const subset=lines.filter(l=>l.data.voucherId===v.id); |
| 179 | expect(subset.reduce((t,l)=>t+Number(l.data.debit||0),0)).toBe(subset.reduce((t,l)=>t+Number(l.data.credit||0),0)); |
| 180 | } |
| 181 | // Reopening the same historical voucher again is blocked. |
| 182 | const retry=await page.evaluate(async id=>{try{await reopenPostedInvoiceAsDraft(id,'تکرار');return 'unexpected'}catch(e){return String(e.message)}},originalId); |
| 183 | expect(retry).not.toBe('unexpected'); |
| 184 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 185 | const persisted=await page.evaluate(id=>({old:invoiceDocumentStatus(getMyInvoices().find(x=>x.id===id)),drafts:getMyInvoices().filter(i=>i.correctsInvoiceId===id).map(i=>i.accountingStatus)}),originalId); |
| 186 | expect(persisted.old).toBe('reversed'); |
| 187 | expect(persisted.drafts).toEqual(['posted']); |
| 188 | }); |
| 189 | |
| 190 | test('pilot manual journal correction permits draft edits, balanced repost and auditable cancellation',async({page})=>{ |
| 191 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 192 | const first=await page.evaluate(()=>{ |
| 193 | const v=jeCreateDraft({number:'8801',date:'1405/07/19',description:'ثبت آزمایشی',sourceType:'manual', |
| 194 | lines:[{accountId:'A_CASH',debit:1000,credit:0},{accountId:'A_REV',debit:0,credit:1000}]}); |
| 195 | jePost(v.id);return v.id; |
| 196 | }); |
| 197 | await expect.poll(()=>[...server.records.values()].filter(r=>r.collection==='journalVouchers').length,{timeout:13000}).toBe(1); |
| 198 | const oldLineData=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first).map(copy); |
| 199 | const ids=await page.evaluate(id=>{ |
| 200 | const out=jeReturnPostedToDraft(id,'اشتباه مبلغ آزمایشی','1405/07/19'); |
| 201 | return {draft:out.draft.id,reversal:out.reversal.id,old:out.original.id}; |
| 202 | },first); |
| 203 | await page.evaluate(id=>jeUpdateDraft(id,{date:'1405/07/19',description:'اصلاح مبلغ به ۲۰۰۰', |
| 204 | lines:[{accountId:'A_CASH',debit:2000,credit:0},{accountId:'A_REV',debit:0,credit:2000}]}),ids.draft); |
| 205 | const details=await page.evaluate(({draft,old})=>{ |
| 206 | jePost(draft);return {old:getMyJournalVouchers().find(x=>x.id===old).status,new:getMyJournalVouchers().find(x=>x.id===draft).status,trial:jeTrialBalance()}; |
| 207 | },ids); |
| 208 | expect(details.old).toBe('reversed');expect(details.new).toBe('posted'); |
| 209 | expect(details.trial.find(x=>x.accountId==='A_CASH').balance).toBe(2000); |
| 210 | await page.evaluate(id=>jeCancelPostedManual(id,'ابطال بعد از تست','1405/07/19'),ids.draft); |
| 211 | await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:13000}).toBe(4); |
| 212 | const final=await page.evaluate(()=>jeTrialBalance()); |
| 213 | expect(final.reduce((sum,row)=>sum+row.debit-row.credit,0)).toBe(0); |
| 214 | expect(final.find(x=>x.accountId==='A_CASH').balance).toBe(0); |
| 215 | expect(oldLineData).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first).map(copy)); |
| 216 | const rows=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 217 | expect(rows.find(x=>x.id===first).data.status).toBe('reversed'); |
| 218 | expect(rows.find(x=>x.id===ids.draft).data.status).toBe('reversed'); |
| 219 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 220 | expect(await page.evaluate(()=>jeTrialBalance().find(x=>x.accountId==='A_CASH').balance)).toBe(0); |
| 221 | }); |
| 222 | |
| 223 | test('pilot cancellation preserves posted invoice record while only unposted drafts can be deleted',async({page})=>{ |
| 224 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 225 | await fillInvoice(page,'PILOT-CANCEL-001',2);await page.evaluate(()=>commitSaveInvoice()); |
| 226 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 227 | const invoiceId=remoteInvoices(server)[0].id; |
| 228 | await page.evaluate(id=>finalizeInvoiceDocument(id),invoiceId); |
| 229 | const original=remoteInvoices(server)[0]; |
| 230 | expect(original.accountingStatus).toBe('posted'); |
| 231 | const blocked=await page.evaluate(id=>{try{deleteInvoicePilotDraft(id);return 'unexpected'}catch(e){return String(e.message)}},invoiceId); |
| 232 | expect(blocked).not.toBe('unexpected'); |
| 233 | await page.evaluate(id=>cancelPostedInvoicePilot(id,'ابطال سند پایلوت'),invoiceId); |
| 234 | await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:13000}).toBe(2); |
| 235 | expect(remoteInvoices(server)[0].id).toBe(invoiceId); |
| 236 | expect(remoteInvoices(server)[0].grandTotal).toBe(original.grandTotal); |
| 237 | const status=await page.evaluate(id=>invoiceDocumentStatus(getMyInvoices().find(x=>x.id===id)),invoiceId); |
| 238 | expect(status).toBe('reversed'); |
| 239 | const bad=await page.evaluate(async id=>{try{await reopenPostedInvoiceAsDraft(id,'تکرار');return 'unexpected'}catch(e){return String(e.message)}},invoiceId); |
| 240 | expect(bad).not.toBe('unexpected'); |
| 241 | // A separate never-posted invoice may be deleted; posted history stays intact. |
| 242 | await fillInvoice(page,'PILOT-DELETE-DRAFT-001',1);await page.evaluate(()=>commitSaveInvoice()); |
| 243 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(2); |
| 244 | const draft=remoteInvoices(server).find(x=>x.number==='PILOT-DELETE-DRAFT-001'); |
| 245 | expect(draft).toBeTruthy(); |
| 246 | await page.evaluate(id=>deleteInvoicePilotDraft(id),draft.id); |
| 247 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 248 | expect(remoteInvoices(server)[0].id).toBe(invoiceId); |
| 249 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(2); |
| 250 | }); |
| 251 | |
| 252 | function remotePurchases(server){return [...server.records.values()].filter(x=>x.collection==='purchases').map(x=>copy(x.data));} |
| 253 | async function createPostedPurchase(page,server,number='PILOT-PUR-001',qty=2){ |
| 254 | const id=await page.evaluate(({number,qty})=>{ |
| 255 | const p={id:'PUR-QA-'+number,ownerUserId:'FINORA_F0_FIXTURE_ONLY',companyId:'QA_CO',number,date:'1405/07/19',supplierId:'QA_CONTACT',supplierName:'خریدار آزمایشی',paymentMethod:'cash',warehouseId:'',projectId:'',items:[{prodId:'QA_PROD',prodName:'بلوک سیمانی',qty,unit:'عدد',price:100,lineTotal:qty*100}],grandTotal:qty*100,vat:0,accountingVersion:1}; |
| 256 | datastore.purchases.push(p);jePostSourceRecord('purchase',p);return p.id; |
| 257 | },{number,qty}); |
| 258 | await expect.poll(()=>remotePurchases(server).find(x=>x.id===id)?.journalVoucherId,{timeout:13000}).toBeTruthy(); |
| 259 | return id; |
| 260 | } |
| 261 | test('pilot purchase: posted to corrective draft to edited balanced repost via document manager',async({page})=>{ |
| 262 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 263 | const originalId=await createPostedPurchase(page,server); |
| 264 | const first=remotePurchases(server).find(x=>x.id===originalId); |
| 265 | expect(first.accountingStatus).toBe('posted'); |
| 266 | const voucher=first.journalVoucherId; |
| 267 | const untouched=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===voucher).map(copy); |
| 268 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(2); |
| 269 | await page.evaluate(()=>{ |
| 270 | window.confirm=()=>true;window.prompt=()=> 'اصلاح مقدار خرید'; |
| 271 | navigateShell('accounting','view-journal','documents'); |
| 272 | }); |
| 273 | await expect(page.locator('#finora-purchase-document-management-body')).toContainText('PILOT-PUR-001'); |
| 274 | await expect(page.locator('#finora-purchase-document-management-body button[data-doc-action="reopen"]')).toHaveCount(1); |
| 275 | // Technical correction primitive remains available for pre-existing in-progress |
| 276 | // drafts; the new employee UI correctly DOES NOT write on open. |
| 277 | await page.locator('#finora-purchase-document-management-body button[data-doc-action="reopen"]').click(); |
| 278 | expect(remotePurchases(server)).toHaveLength(1); |
| 279 | await page.evaluate(()=>purCancelEdit()); |
| 280 | await page.evaluate(id=>returnPostedPurchaseAsDraft(id,'تطبیق پیشنویس قدیمی'),originalId); |
| 281 | await expect.poll(()=>remotePurchases(server).length,{timeout:14000}).toBe(2); |
| 282 | const corrected=remotePurchases(server).find(x=>x.correctsPurchaseId===originalId); |
| 283 | expect(corrected.documentStatus).toBe('draft'); |
| 284 | expect(corrected.journalVoucherId).toBeUndefined(); |
| 285 | expect(corrected.number).not.toBe(first.number); |
| 286 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0); |
| 287 | expect(await page.evaluate(id=>purchaseDocumentStatus(getMyPurchases().find(x=>x.id===id)),originalId)).toBe('reversed'); |
| 288 | // Internal API fixture made a technical draft without clicking the employee form. |
| 289 | await page.evaluate(id=>purEdit(id),corrected.id); |
| 290 | await expect(page.locator('#pur-edit-id')).toHaveValue(corrected.id); |
| 291 | await page.locator('#pur-items-body .pur-qty').first().fill('3'); |
| 292 | await page.locator('#pur-items-body .pur-qty').first().dispatchEvent('input'); |
| 293 | await page.locator('#pur-save-btn').click(); |
| 294 | await expect.poll(()=>remotePurchases(server).find(x=>x.id===corrected.id)?.grandTotal,{timeout:14000}).toBe(300); |
| 295 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0); |
| 296 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 297 | await expect(page.locator('#finora-purchase-document-management-body button[data-doc-action="post"]')).toHaveCount(1); |
| 298 | await page.locator('#finora-purchase-document-management-body button[data-doc-action="post"]').click(); |
| 299 | await expect.poll(()=>remotePurchases(server).find(x=>x.id===corrected.id)?.accountingStatus,{timeout:14000}).toBe('posted'); |
| 300 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(3); |
| 301 | const journal=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 302 | expect(journal).toHaveLength(3); |
| 303 | expect(journal.find(x=>x.id===voucher).data.status).toBe('reversed'); |
| 304 | expect(untouched).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===voucher).map(copy)); |
| 305 | for(const v of journal){ |
| 306 | const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===v.id); |
| 307 | expect(lines.reduce((s,l)=>s+Number(l.data.debit||0),0)).toBe(lines.reduce((s,l)=>s+Number(l.data.credit||0),0)); |
| 308 | } |
| 309 | const retry=await page.evaluate(async id=>{try{await returnPostedPurchaseAsDraft(id,'تکرار');return 'unsafe'}catch(e){return String(e.message)}},originalId); |
| 310 | expect(retry).not.toBe('unsafe'); |
| 311 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 312 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(3); |
| 313 | }); |
| 314 | |
| 315 | test('pilot purchase denies paid/consumed source; cancels unallocated posting without erasing old voucher',async({page})=>{ |
| 316 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 317 | const purchaseId=await createPostedPurchase(page,server,'PILOT-PUR-GUARDS'); |
| 318 | // A payment allocation blocks both corrective reopen and cancellation. |
| 319 | const blockedPayment=await page.evaluate(async id=>{ |
| 320 | datastore.payments.push({id:'QA-SETTLE',ownerUserId:currentUser.id,companyId:'QA_CO',direction:'outbound',invoiceType:'purchase',invoiceId:id,amount:10}); |
| 321 | try{await cancelPostedPurchasePilot(id,'ابطال');return 'unsafe'}catch(e){return String(e.message)} |
| 322 | },purchaseId); |
| 323 | expect(blockedPayment).toMatch(/پرداخت/); |
| 324 | await page.evaluate(()=>{datastore.payments=datastore.payments.filter(x=>x.id!=='QA-SETTLE');}); |
| 325 | // Consumption means stock would become negative after purchase reversal. |
| 326 | const blockedStock=await page.evaluate(async id=>{ |
| 327 | const sale={id:'QA-USE',ownerUserId:currentUser.id,companyId:'QA_CO',kind:'formal',documentStatus:'posted',items:[{prodId:'QA_PROD',qty:2}],grandTotal:200}; |
| 328 | datastore.invoices.push(sale); |
| 329 | try{await returnPostedPurchaseAsDraft(id,'اصلاح');return 'unsafe'}catch(e){return String(e.message)} |
| 330 | },purchaseId); |
| 331 | expect(blockedStock).toMatch(/موجودی/); |
| 332 | await page.evaluate(()=>{datastore.invoices=datastore.invoices.filter(x=>x.id!=='QA-USE');}); |
| 333 | await page.evaluate(id=>cancelPostedPurchasePilot(id,'ابطال خرید آزمایشی'),purchaseId); |
| 334 | await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:14000}).toBe(2); |
| 335 | expect(remotePurchases(server)).toHaveLength(1); |
| 336 | const original=remotePurchases(server)[0]; |
| 337 | expect(original.id).toBe(purchaseId); |
| 338 | expect(await page.evaluate(id=>purchaseDocumentStatus(getMyPurchases().find(x=>x.id===id)),purchaseId)).toBe('reversed'); |
| 339 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0); |
| 340 | expect(await page.evaluate(id=>{try{deletePurchasePilotDraft(id);return 'unsafe'}catch(e){return String(e.message)}},purchaseId)).not.toBe('unsafe'); |
| 341 | }); |
| 342 | test('seamless sale correction: opening and cancelling leaves ledger unchanged; saving retains visible identity, printing and audit',async({page})=>{ |
| 343 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 344 | await fillInvoice(page,'SAME-SALE-001',2);await page.evaluate(()=>commitSaveInvoice()); |
| 345 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 346 | const id=remoteInvoices(server)[0].id;await page.evaluate(x=>finalizeInvoiceDocument(x),id); |
| 347 | await expect.poll(()=>remoteInvoices(server)[0]?.accountingStatus,{timeout:13000}).toBe('posted'); |
| 348 | const original=copy(remoteInvoices(server)[0]),initialLines=[...server.records.values()].filter(x=>x.collection==='journalLines').map(copy); |
| 349 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 350 | await expect(page.locator('#finora-document-management-body tr')).toHaveCount(1); |
| 351 | await page.locator('#finora-document-management-body [data-doc-action="reopen"]').click(); |
| 352 | await expect(page.locator('#edit-invoice-id')).toHaveValue(id); |
| 353 | await expect(page.locator('#invoice-number')).toHaveValue('SAME-SALE-001'); |
| 354 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(1); |
| 355 | await page.evaluate(()=>cancelInvoiceEdit()); |
| 356 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(1); |
| 357 | expect(remoteInvoices(server)).toHaveLength(1); |
| 358 | await page.evaluate(x=>beginInvoiceAmendment(x),id); |
| 359 | await page.locator('#invoice-items-table-body .row-quantity').first().fill('3'); |
| 360 | await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input'); |
| 361 | await page.evaluate(()=>commitSaveInvoice()); |
| 362 | await expect.poll(()=>remoteInvoices(server).filter(x=>x.correctsInvoiceId===id&&x.accountingStatus==='posted').length,{timeout:20000}).toBe(1); |
| 363 | expect(remoteInvoices(server)).toHaveLength(2);expect(remoteInvoices(server).find(x=>x.id===id)).toEqual(original); |
| 364 | const current=remoteInvoices(server).find(x=>x.correctsInvoiceId===id); |
| 365 | expect(current.grandTotal).toBe(4500); |
| 366 | expect(current.number).not.toBe(original.number); |
| 367 | const visible=await page.evaluate(()=>invoiceVisibleRecords().map(x=>({id:x.id,number:invoiceVisibleNumber(x)}))); |
| 368 | expect(visible).toEqual([{id:current.id,number:'SAME-SALE-001'}]); |
| 369 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 370 | await expect(page.locator('#finora-document-management-body tr')).toHaveCount(1); |
| 371 | await expect(page.locator('#finora-document-management-body')).toContainText('SAME-SALE-001'); |
| 372 | await expect(page.locator('#finora-document-management-body')).not.toContainText('-اصلاح'); |
| 373 | await page.evaluate(x=>renderAndPrintDirect(x),id); |
| 374 | await expect(page.locator('#printable-invoice')).toContainText('SAME-SALE-۰۰۱'); |
| 375 | await expect(page.locator('#printable-invoice')).toContainText('۴٬۵۰۰'); |
| 376 | await expect(page.locator('#printable-invoice')).not.toContainText('-اصلاح'); |
| 377 | expect(initialLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===original.journalVoucherId).map(copy)); |
| 378 | const vouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 379 | expect(vouchers).toHaveLength(3);expect(vouchers.find(v=>v.id===original.journalVoucherId).data.status).toBe('reversed'); |
| 380 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 381 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 382 | await expect(page.locator('#finora-document-management-body tr')).toHaveCount(1); |
| 383 | await expect(page.locator('#finora-document-management-body')).toContainText('SAME-SALE-001'); |
| 384 | }); |
| 385 | |
| 386 | test('seamless purchase correction: same visible number, no write on opening, save and print corrected purchase',async({page})=>{ |
| 387 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 388 | const id=await createPostedPurchase(page,server,'SAME-PURCHASE-001',2); |
| 389 | const initial=copy(remotePurchases(server).find(x=>x.id===id));const before=[...server.records.values()].filter(x=>x.collection==='journalVouchers'); |
| 390 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 391 | await expect(page.locator('#finora-purchase-document-management-body tr')).toHaveCount(1); |
| 392 | await page.locator('#finora-purchase-document-management-body [data-doc-action="reopen"]').click(); |
| 393 | await expect(page.locator('#pur-edit-id')).toHaveValue(id); |
| 394 | await expect(page.locator('#pur-number')).toHaveValue('SAME-PURCHASE-001'); |
| 395 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(before.length); |
| 396 | await page.evaluate(()=>purCancelEdit()); |
| 397 | expect(remotePurchases(server)).toHaveLength(1); |
| 398 | await page.evaluate(x=>beginPurchaseAmendment(x),id); |
| 399 | await page.locator('#pur-items-body .pur-qty').first().fill('3'); |
| 400 | await page.locator('#pur-items-body .pur-qty').first().dispatchEvent('input'); |
| 401 | await page.locator('#pur-save-btn').click(); |
| 402 | await expect.poll(()=>remotePurchases(server).filter(x=>x.correctsPurchaseId===id&&x.accountingStatus==='posted').length,{timeout:20000}).toBe(1); |
| 403 | expect(remotePurchases(server).find(x=>x.id===id)).toEqual(initial); |
| 404 | const current=remotePurchases(server).find(x=>x.correctsPurchaseId===id); |
| 405 | expect(current.grandTotal).toBe(300);expect(current.number).not.toBe(initial.number); |
| 406 | const visible=await page.evaluate(()=>purchaseVisibleRecords().map(x=>({id:x.id,number:purchaseVisibleNumber(x)}))); |
| 407 | expect(visible).toEqual([{id:current.id,number:'SAME-PURCHASE-001'}]); |
| 408 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 409 | await expect(page.locator('#finora-purchase-document-management-body tr')).toHaveCount(1); |
| 410 | await expect(page.locator('#finora-purchase-document-management-body')).toContainText('SAME-PURCHASE-001'); |
| 411 | await expect(page.locator('#finora-purchase-document-management-body')).not.toContainText('-اصلاح'); |
| 412 | await page.evaluate(x=>purPrint(x),id); |
| 413 | await expect(page.locator('#finora-purchase-document-management-body')).toContainText('SAME-PURCHASE-001'); |
| 414 | expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(3); |
| 415 | await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page); |
| 416 | await page.evaluate(()=>navigateShell('accounting','view-journal','documents')); |
| 417 | await expect(page.locator('#finora-purchase-document-management-body tr')).toHaveCount(1); |
| 418 | }); |
| 419 | test('cannot finalize draft if fiscal posting chart has no required account; draft remains intact',async({page})=>{ |
| 420 | const server=fakeServer();server.records.delete('accounts|A_REV');await installHarness(page,server);await authenticatedMockBrowser(page); |
| 421 | await fillInvoice(page,'FIN-NO-REVENUE-ACCOUNT',2);await page.evaluate(()=>commitSaveInvoice()); |
| 422 | await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1); |
| 423 | const id=remoteInvoices(server)[0].id; |
| 424 | const result=await page.evaluate(async x=>{try{await finalizeInvoiceDocument(x);return 'unexpected success'}catch(e){return String(e.message)}},id); |
| 425 | expect(result).toMatch(/حساب|کدینگ/); |
| 426 | expect(remoteInvoices(server)[0].accountingStatus).toBe('draft'); |
| 427 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0); |
| 428 | }); |
| 429 | |
| 430 | |
| 431 | test('F2 validates formal invoice then saves a draft without posting any financial journal',async({page})=>{ |
| 432 | const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page); |
| 433 | await page.keyboard.press('F2'); |
| 434 | expect(remoteInvoices(server)).toHaveLength(0); |
| 435 | expect(await page.evaluate(()=>window.__f0Alerts.at(-1))).toMatch(/حداقل یک کالا|خدمت معتبر/); |
| 436 | await fillInvoice(page,'F2-SAFE-DRAFT-001',2); |
| 437 | await page.keyboard.press('F2'); |
| 438 | await expect.poll(()=>remoteInvoices(server).length,{timeout:14000}).toBe(1); |
| 439 | const saved=remoteInvoices(server)[0]; |
| 440 | expect(saved.number).toBe('F2-SAFE-DRAFT-001'); |
| 441 | expect(saved.accountingStatus).toBe('draft'); |
| 442 | expect(saved.documentStatus).toBe('draft'); |
| 443 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0); |
| 444 | await page.keyboard.press('F2'); |
| 445 | expect(remoteInvoices(server)).toHaveLength(1); |
| 446 | expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0); |
| 447 | }); |