@davoodmehraban89 / Factor-easy / commits / 72d1186
Fork 0

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

test(expense): same-form expense/income amend, balanced audit, cancellation, fiscal and failed RPC guards

davoodmehraban89 committed Oct 14, 2026 at 11:48 UTC 72d1186986f9fc2519174911f902ef162d316780
1 file changed +108
scripts/accounting-document-workflow.spec.js
+108
@@ -17,6 +17,8 @@ function fakeServer(){
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 + ['accounts','A_EXP',{code:'6103',title:'هزینه عمومی',systemRole:'expense_default',postingAllowed:true,active:true}],
21 + ['accounts','A_OTHER',{code:'4201',title:'سایر درآمد',systemRole:'other_income_default',postingAllowed:true,active:true}],
22 ])records.set(collection+'|'+id,{collection,id,data:copy(data)});
23 return {records,failNext:false,failCount:0,rpcCount:0};
24 }
@@ -541,6 +543,112 @@ test('treasury amend rejection on lost RPC keeps protected original on server an
543 expect([...server.records.values()].filter(x=>x.collection==='journalVouchers').map(x=>x.id)).toEqual(vouchersBefore);
544 // After uncertain acknowledgment, do not call the amend API again blindly.
545 });
546 +function remoteExpenses(server){return [...server.records.values()].filter(x=>x.collection==='expenses').map(x=>copy(x.data));}
547 +async function createUiExpense(page,server,kind='expense',amount=1000){
548 + await page.evaluate(()=>{switchView('view-expenses');renderExpenses()});
549 + await page.locator('#trx-kind-input').selectOption(kind);
550 + await page.locator('#trx-category-input').selectOption('سایر');
551 + await page.locator('#trx-amount-input').fill(String(amount));
552 + await page.locator('#trx-desc-input').fill('ثبت اولیه آزمایشی');
553 + await page.evaluate(()=>commitSaveExpense());
554 + await expect.poll(()=>remoteExpenses(server).length,{timeout:14000}).toBe(1);
555 + await expect.poll(()=>remoteExpenses(server)[0]?.accountingStatus,{timeout:14000}).toBe('posted');
556 + return remoteExpenses(server)[0].id;
557 +}
558 +test('expense same-form correction preserves one row, posted history and project cost across reload',async({page})=>{
559 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
560 + const id=await createUiExpense(page,server);
561 + await page.waitForFunction(()=>!syncPending&&!syncRunning);
562 + const original=remoteExpenses(server)[0],oldLines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===original.journalVoucherId).map(copy),rpcBefore=server.rpcCount;
563 + await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
564 + await expect(page.locator('#finora-expense-document-management-body tr')).toHaveCount(1);
565 + await page.locator('#finora-expense-document-management-body [data-doc-action="edit"]').click();
566 + await expect(page.locator('#trx-amount-input')).toHaveValue('1000');
567 + await expect(page.locator('#trx-kind-input')).toHaveValue('expense');
568 + await expect(page.locator('#trx-desc-input')).toHaveValue('ثبت اولیه آزمایشی');
569 + await page.evaluate(()=>cancelExpenseEdit());
570 + expect(server.rpcCount).toBe(rpcBefore);
571 + expect(remoteExpenses(server)).toHaveLength(1);
572 + expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(1);
573 + await page.evaluate(x=>editExpense(x),id);
574 + await page.locator('#trx-amount-input').fill('1450');
575 + await page.locator('#trx-desc-input').fill('اصلاح در همان فرم');
576 + const rpcAtSave=server.rpcCount;
577 + await page.locator('#expense-save-btn').click();
578 + await expect.poll(()=>remoteExpenses(server).filter(x=>x.correctsExpenseId===id&&x.accountingStatus==='posted').length,{timeout:20000}).toBe(1);
579 + await page.waitForFunction(()=>!syncRunning&&!syncPending);
580 + expect(server.rpcCount-rpcAtSave).toBe(1);
581 + expect(remoteExpenses(server).find(x=>x.id===id)).toEqual(original);
582 + const newer=remoteExpenses(server).find(x=>x.correctsExpenseId===id);
583 + expect(newer.amount).toBe(1450);expect(newer.desc).toBe('اصلاح در همان فرم');
584 + const vouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
585 + expect(vouchers).toHaveLength(3);
586 + expect(vouchers.find(x=>x.id===original.journalVoucherId).data.status).toBe('reversed');
587 + expect(oldLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===original.journalVoucherId).map(copy));
588 + for(const v of vouchers){
589 + const ls=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===v.id);
590 + expect(ls.reduce((sum,l)=>sum+Number(l.data.debit||0),0)).toBe(ls.reduce((sum,l)=>sum+Number(l.data.credit||0),0));
591 + }
592 + expect(await page.evaluate(()=>expenseVisibleRecords().map(x=>x.id))).toEqual([newer.id]);
593 + expect(await page.evaluate(()=>expenseEffectiveRecords().map(x=>x.amount))).toEqual([1450]);
594 + expect(await page.evaluate(()=>getProjectFinancials('').expenses)).toBe(1450);
595 + await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
596 + await expect(page.locator('#finora-expense-document-management-body tr')).toHaveCount(1);
597 + await expect(page.locator('#finora-expense-document-management-body')).toContainText('۱٬۴۵۰');
598 + await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page);
599 + await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
600 + await expect(page.locator('#finora-expense-document-management-body tr')).toHaveCount(1);
601 + expect(await page.evaluate(()=>getProjectFinancials('').expenses)).toBe(1450);
602 +});
603 +
604 +test('income correction and cancellation preserve initial audit and eliminate active project income',async({page})=>{
605 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
606 + const id=await createUiExpense(page,server,'income',700);
607 + await page.waitForFunction(()=>!syncPending&&!syncRunning);
608 + const locked=await page.evaluate(id=>{
609 + const fy=getMyFiscalYears()[0],old=fy.status;fy.status='locked';
610 + try{expenseCorrectionEligibility(id);return 'unsafe'}catch(e){return String(e.message)}
611 + finally{fy.status=old}
612 + },id);
613 + expect(locked).toMatch(/مالی|قفل/);
614 + expect(await page.evaluate(()=>getProjectFinancials('').income)).toBe(700);
615 + await page.evaluate(id=>editExpense(id),id);
616 + await expect(page.locator('#trx-kind-input')).toHaveValue('income');
617 + await page.locator('#trx-amount-input').fill('950');
618 + await page.locator('#expense-save-btn').click();
619 + await expect.poll(()=>remoteExpenses(server).filter(x=>x.correctsExpenseId===id&&x.accountingStatus==='posted').length,{timeout:20000}).toBe(1);
620 + const replacement=remoteExpenses(server).find(x=>x.correctsExpenseId===id);
621 + expect(await page.evaluate(()=>getProjectFinancials('').income)).toBe(950);
622 + await page.evaluate(()=>{window.confirm=()=>true;window.prompt=()=> 'ابطال درآمد آزمایشی';});
623 + await page.evaluate(id=>cancelPostedExpenseUi(id),replacement.id);
624 + await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:15000}).toBe(4);
625 + expect(remoteExpenses(server)).toHaveLength(2);
626 + expect(await page.evaluate(()=>expenseEffectiveRecords().length)).toBe(0);
627 + expect(await page.evaluate(()=>getProjectFinancials('').income)).toBe(0);
628 + await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
629 + await expect(page.locator('#finora-expense-document-management-body')).toContainText('ابطال‌شده');
630 +});
631 +
632 +test('expense amendment refuses mismatched kind and failed single RPC does not partially commit posted records',async({page})=>{
633 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
634 + const id=await createUiExpense(page,server,'expense',500);
635 + await page.waitForFunction(()=>!syncRunning&&!syncPending);
636 + const original=copy(remoteExpenses(server)[0]),originalJournalIds=[...server.records.values()].filter(x=>x.collection==='journalVouchers').map(x=>x.id);
637 + const mismatch=await page.evaluate(async id=>{
638 + const e=getMyExpenses().find(x=>x.id===id);
639 + try{await expenseAmendPosted(id,{kind:'income',contactId:e.contactId||'',projectId:e.projectId||'',amount:600,category:'سایر',desc:'x'});return 'unsafe';}
640 + catch(err){return String(err.message)}
641 + },id);
642 + expect(mismatch).toMatch(/نوع سند|نوع|طرف حساب/);
643 + server.failNext=true;
644 + await page.evaluate(id=>editExpense(id),id);
645 + await page.locator('#trx-amount-input').fill('800');
646 + await page.locator('#expense-save-btn').click();
647 + await expect.poll(async()=>await page.evaluate(()=>window.__f0Alerts.some(x=>x.includes('ثبت اصلاح تأیید نشد'))),{timeout:9000}).toBe(true);
648 + expect(server.failCount).toBe(1);
649 + expect(remoteExpenses(server)).toEqual([original]);
650 + expect([...server.records.values()].filter(x=>x.collection==='journalVouchers').map(x=>x.id)).toEqual(originalJournalIds);
651 +});
652 test('cannot finalize draft if fiscal posting chart has no required account; draft remains intact',async({page})=>{
653 const server=fakeServer();server.records.delete('accounts|A_REV');await installHarness(page,server);await authenticatedMockBrowser(page);
654 await fillInvoice(page,'FIN-NO-REVENUE-ACCOUNT',2);await page.evaluate(()=>commitSaveInvoice());