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

test(pilot): exercise purchase reversal-to-draft UI, repost, inventory and payment guards

davoodmehraban89 committed Oct 14, 2026 at 08:56 UTC 583758b0759d467f607e3b5279e99a0b5059dbf8
1 file changed +86 -1
scripts/accounting-document-workflow.spec.js
+86 -1
@@ -7,7 +7,7 @@ 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:'خریدار آزمایشی'}],
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'}],
@@ -15,6 +15,8 @@ function fakeServer(){
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 }
@@ -247,6 +249,89 @@ test('pilot cancellation preserves posted invoice record while only unposted dra
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 + await page.locator('#finora-purchase-document-management-body button[data-doc-action="reopen"]').click();
276 + await expect.poll(()=>remotePurchases(server).length,{timeout:14000}).toBe(2);
277 + const corrected=remotePurchases(server).find(x=>x.correctsPurchaseId===originalId);
278 + expect(corrected.documentStatus).toBe('draft');
279 + expect(corrected.journalVoucherId).toBeUndefined();
280 + expect(corrected.number).not.toBe(first.number);
281 + expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0);
282 + expect(await page.evaluate(id=>purchaseDocumentStatus(getMyPurchases().find(x=>x.id===id)),originalId)).toBe('reversed');
283 + await expect(page.locator('#pur-edit-id')).toHaveValue(corrected.id);
284 + await page.locator('#pur-items-body .pur-qty').first().fill('3');
285 + await page.locator('#pur-items-body .pur-qty').first().dispatchEvent('input');
286 + await page.locator('#pur-save-btn').click();
287 + await expect.poll(()=>remotePurchases(server).find(x=>x.id===corrected.id)?.grandTotal,{timeout:14000}).toBe(300);
288 + expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0);
289 + await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
290 + await expect(page.locator('#finora-purchase-document-management-body button[data-doc-action="post"]')).toHaveCount(1);
291 + await page.locator('#finora-purchase-document-management-body button[data-doc-action="post"]').click();
292 + await expect.poll(()=>remotePurchases(server).find(x=>x.id===corrected.id)?.accountingStatus,{timeout:14000}).toBe('posted');
293 + expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(3);
294 + const journal=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
295 + expect(journal).toHaveLength(3);
296 + expect(journal.find(x=>x.id===voucher).data.status).toBe('reversed');
297 + expect(untouched).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===voucher).map(copy));
298 + for(const v of journal){
299 + const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===v.id);
300 + expect(lines.reduce((s,l)=>s+Number(l.data.debit||0),0)).toBe(lines.reduce((s,l)=>s+Number(l.data.credit||0),0));
301 + }
302 + const retry=await page.evaluate(async id=>{try{await returnPostedPurchaseAsDraft(id,'تکرار');return 'unsafe'}catch(e){return String(e.message)}},originalId);
303 + expect(retry).not.toBe('unsafe');
304 + await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page);
305 + expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(3);
306 +});
307 +
308 +test('pilot purchase denies paid/consumed source; cancels unallocated posting without erasing old voucher',async({page})=>{
309 + const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
310 + const purchaseId=await createPostedPurchase(page,server,'PILOT-PUR-GUARDS');
311 + // A payment allocation blocks both corrective reopen and cancellation.
312 + const blockedPayment=await page.evaluate(async id=>{
313 + datastore.payments.push({id:'QA-SETTLE',ownerUserId:currentUser.id,companyId:'QA_CO',direction:'outbound',invoiceType:'purchase',invoiceId:id,amount:10});
314 + try{await cancelPostedPurchasePilot(id,'ابطال');return 'unsafe'}catch(e){return String(e.message)}
315 + },purchaseId);
316 + expect(blockedPayment).toMatch(/پرداخت/);
317 + await page.evaluate(()=>{datastore.payments=datastore.payments.filter(x=>x.id!=='QA-SETTLE');});
318 + // Consumption means stock would become negative after purchase reversal.
319 + const blockedStock=await page.evaluate(async id=>{
320 + const sale={id:'QA-USE',ownerUserId:currentUser.id,companyId:'QA_CO',kind:'formal',documentStatus:'posted',items:[{prodId:'QA_PROD',qty:2}],grandTotal:200};
321 + datastore.invoices.push(sale);
322 + try{await returnPostedPurchaseAsDraft(id,'اصلاح');return 'unsafe'}catch(e){return String(e.message)}
323 + },purchaseId);
324 + expect(blockedStock).toMatch(/موجودی/);
325 + await page.evaluate(()=>{datastore.invoices=datastore.invoices.filter(x=>x.id!=='QA-USE');});
326 + await page.evaluate(id=>cancelPostedPurchasePilot(id,'ابطال خرید آزمایشی'),purchaseId);
327 + await expect.poll(()=>[...server.records.values()].filter(x=>x.collection==='journalVouchers').length,{timeout:14000}).toBe(2);
328 + expect(remotePurchases(server)).toHaveLength(1);
329 + const original=remotePurchases(server)[0];
330 + expect(original.id).toBe(purchaseId);
331 + expect(await page.evaluate(id=>purchaseDocumentStatus(getMyPurchases().find(x=>x.id===id)),purchaseId)).toBe('reversed');
332 + expect(await page.evaluate(()=>productMovement('QA_PROD').available)).toBe(0);
333 + expect(await page.evaluate(id=>{try{deletePurchasePilotDraft(id);return 'unsafe'}catch(e){return String(e.message)}},purchaseId)).not.toBe('unsafe');
334 +});
335 test('cannot finalize draft if fiscal posting chart has no required account; draft remains intact',async({page})=>{
336 const server=fakeServer();server.records.delete('accounts|A_REV');await installHarness(page,server);await authenticatedMockBrowser(page);
337 await fillInvoice(page,'FIN-NO-REVENUE-ACCOUNT',2);await page.evaluate(()=>commitSaveInvoice());