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

test(pilot): cover posted sale correction and manual journal return/edit/repost/cancel

davoodmehraban89 committed Oct 14, 2026 at 08:24 UTC 3e8f397d31214c81b4c9dbe27c8761226efbba18
1 file changed +85
scripts/accounting-document-workflow.spec.js
+85
@@ -133,6 +133,91 @@ test('formal invoice remains editable draft across server reload; explicit posti
133 expect(saved.grandTotal).toBe(4500);
134 expect(saved.documentVersion).toBe(2);
135 });
136 +test('posted sale can be returned to an editable corrective draft; original voucher stays immutable and balances',async({page})=>{
137 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
138 + await fillInvoice(page,'PILOT-SALE-001',2);await page.evaluate(()=>commitSaveInvoice());
139 + await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(1);
140 + const originalId=remoteInvoices(server)[0].id;
141 + await page.evaluate(id=>finalizeInvoiceDocument(id),originalId);
142 + await expect.poll(()=>remoteInvoices(server)[0]?.accountingStatus,{timeout:13000}).toBe('posted');
143 + const before=remoteInvoices(server)[0],originalVoucherId=before.journalVoucherId;
144 + const originalLines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===originalVoucherId).map(copy);
145 + const result=await page.evaluate(id=>reopenPostedInvoiceAsDraft(id,'تصحیح تعداد پایلوت'),originalId);
146 + expect(result).toBeTruthy();expect(result).not.toBe(originalId);
147 + await expect.poll(()=>remoteInvoices(server).length,{timeout:13000}).toBe(2);
148 + const after=remoteInvoices(server),original=after.find(i=>i.id===originalId),draft=after.find(i=>i.id===result);
149 + expect(original.grandTotal).toBe(3000);
150 + expect(original.journalVoucherId).toBe(originalVoucherId);
151 + expect(draft.correctsInvoiceId).toBe(originalId);
152 + expect(draft.documentStatus).toBe('draft');
153 + expect(draft.journalVoucherId).toBeUndefined();
154 + expect(draft.number).not.toBe(original.number);
155 + const allVouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
156 + expect(allVouchers).toHaveLength(2);
157 + expect(allVouchers.find(x=>x.id===originalVoucherId).data.status).toBe('reversed');
158 + const reversal=allVouchers.find(x=>x.id!==originalVoucherId);
159 + expect(reversal.data.status).toBe('posted');
160 + expect(originalLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===originalVoucherId).map(copy));
161 + expect(await page.evaluate(id=>invoiceDocumentStatus(getMyInvoices().find(x=>x.id===id)),originalId)).toBe('reversed');
162 + // The replacement source uses the normal draft editor and preserves provenance.
163 + await page.evaluate(id=>editInvoice(id),result);
164 + await expect(page.locator('#edit-invoice-id')).toHaveValue(result);
165 + await page.locator('#invoice-items-table-body .row-quantity').first().fill('3');
166 + await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input');
167 + await page.evaluate(()=>commitSaveInvoice());
168 + await expect.poll(()=>remoteInvoices(server).find(x=>x.id===result)?.grandTotal,{timeout:13000}).toBe(4500);
169 + await page.evaluate(id=>finalizeInvoiceDocument(id),result);
170 + await expect.poll(()=>remoteInvoices(server).find(x=>x.id===result)?.accountingStatus,{timeout:13000}).toBe('posted');
171 + const later=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
172 + expect(later).toHaveLength(3);
173 + expect(later.find(x=>x.id===originalVoucherId).data.status).toBe('reversed');
174 + const lines=[...server.records.values()].filter(x=>x.collection==='journalLines');
175 + for(const v of later){
176 + const subset=lines.filter(l=>l.data.voucherId===v.id);
177 + expect(subset.reduce((t,l)=>t+Number(l.data.debit||0),0)).toBe(subset.reduce((t,l)=>t+Number(l.data.credit||0),0));
178 + }
179 + // Reopening the same historical voucher again is blocked.
180 + const retry=await page.evaluate(async id=>{try{await reopenPostedInvoiceAsDraft(id,'تکرار');return 'unexpected'}catch(e){return String(e.message)}},originalId);
181 + expect(retry).not.toBe('unexpected');
182 + await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page);
183 + 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);
184 + expect(persisted.old).toBe('reversed');
185 + expect(persisted.drafts).toEqual(['posted']);
186 +});
187 +
188 +test('pilot manual journal correction permits draft edits, balanced repost and auditable cancellation',async({page})=>{
189 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
190 + const first=await page.evaluate(()=>{
191 + const v=jeCreateDraft({number:'8801',date:'1405/07/19',description:'ثبت آزمایشی',sourceType:'manual',
192 + lines:[{accountId:'A_CASH',debit:1000,credit:0},{accountId:'A_REV',debit:0,credit:1000}]});
193 + jePost(v.id);return v.id;
194 + });
195 + await expect.poll(()=>[...server.records.values()].filter(r=>r.collection==='journalVouchers').length,{timeout:13000}).toBe(1);
196 + const oldLineData=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first).map(copy);
197 + const ids=await page.evaluate(id=>{
198 + const out=jeReturnPostedToDraft(id,'اشتباه مبلغ آزمایشی','1405/07/19');
199 + return {draft:out.draft.id,reversal:out.reversal.id,old:out.original.id};
200 + },first);
201 + await page.evaluate(id=>jeUpdateDraft(id,{date:'1405/07/19',description:'اصلاح مبلغ به ۲۰۰۰',
202 + lines:[{accountId:'A_CASH',debit:2000,credit:0},{accountId:'A_REV',debit:0,credit:2000}]}),ids.draft);
203 + const details=await page.evaluate(({draft,old})=>{
204 + jePost(draft);return {old:getMyJournalVouchers().find(x=>x.id===old).status,new:getMyJournalVouchers().find(x=>x.id===draft).status,trial:jeTrialBalance()};
205 + },ids);
206 + expect(details.old).toBe('reversed');expect(details.new).toBe('posted');
207 + expect(details.trial.find(x=>x.accountId==='A_CASH').balance).toBe(2000);
208 + await page.evaluate(id=>jeCancelPostedManual(id,'ابطال بعد از تست','1405/07/19'),ids.draft);
209 + await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:13000}).toBe(4);
210 + const final=await page.evaluate(()=>jeTrialBalance());
211 + expect(final.reduce((sum,row)=>sum+row.debit-row.credit,0)).toBe(0);
212 + expect(final.find(x=>x.accountId==='A_CASH').balance).toBe(0);
213 + expect(oldLineData).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first).map(copy));
214 + const rows=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
215 + expect(rows.find(x=>x.id===first).data.status).toBe('reversed');
216 + expect(rows.find(x=>x.id===ids.draft).data.status).toBe('reversed');
217 + await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page);
218 + expect(await page.evaluate(()=>jeTrialBalance().find(x=>x.accountId==='A_CASH').balance)).toBe(0);
219 +});
220 +
221 test('cannot finalize draft if fiscal posting chart has no required account; draft remains intact',async({page})=>{
222 const server=fakeServer();server.records.delete('accounts|A_REV');await installHarness(page,server);await authenticatedMockBrowser(page);
223 await fillInvoice(page,'FIN-NO-REVENUE-ACCOUNT',2);await page.evaluate(()=>commitSaveInvoice());