سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 06:45 UTC
test(qa): reproduce purchase amendment Save auto-finalize as P0 expected RED
davoodmehraban89 committed
Nov 22, 2026 at 18:32 UTC
3de78a9b04e4196d4b61ab51b6f6834c4e359385
1 file changed
+61
scripts/accounting-document-workflow.spec.js
+61
@@ -1058,3 +1058,64 @@ test('P0 #229 expected RED: formal sale Save must persist linked journal draft w
1058
postedVouchers:0,automaticPostCalls:0
1059
});
1060
});
1061
+
1062
+
1063
+test('P0 #229 expected RED: posted purchase correction Save must remain linked accounting draft',async({page})=>{
1064
+ const server=fakeServer();
1065
+ await installHarness(page,server);
1066
+ await authenticatedMockBrowser(page);
1067
+ const id=await createPostedPurchase(page,server,'QA-ADR216-CORRECTION-SAVE',2);
1068
+ const original=remotePurchases(server).find(p=>p.id===id);
1069
+ const originalVoucher=[...server.records.values()].find(r=>r.collection==='journalVouchers'&&r.id===original.journalVoucherId);
1070
+ if(original?.accountingStatus!=='posted'||originalVoucher?.data.status!=='posted')
1071
+ throw Error('Original posted purchase fixture invalid; NOT the expected correction Save RED');
1072
+ const originalSourceFingerprint=copy(original);
1073
+ const originalVoucherFingerprint=copy(originalVoucher);
1074
+ const originalLines=[...server.records.values()].filter(r=>r.collection==='journalLines'&&
1075
+ r.data.voucherId===original.journalVoucherId).map(copy);
1076
+ await page.evaluate(x=>beginPurchaseAmendment(x),id);
1077
+ await expect(page.locator('#pur-edit-id')).toHaveValue(id);
1078
+ await page.locator('#pur-items-body .pur-qty').first().fill('3');
1079
+ await page.locator('#pur-items-body .pur-qty').first().dispatchEvent('input');
1080
+ await page.locator('#pur-save-btn').click();
1081
+ // Await the actual completed Save path, not its intermediate locally inserted draft.
1082
+ await expect(page.locator('#pur-edit-id')).toHaveValue('',{timeout:22000});
1083
+ await expect.poll(()=>remotePurchases(server).filter(p=>p.correctsPurchaseId===id).length,
1084
+ {timeout:15000}).toBe(1);
1085
+ await page.waitForFunction(()=>!syncPending&&!syncRunning);
1086
+ const corrected=remotePurchases(server).find(p=>p.correctsPurchaseId===id);
1087
+ if(corrected?.grandTotal!==300||corrected?.companyId!=='QA_CO'||
1088
+ corrected?.correctionOriginalNumber!==original.number)
1089
+ throw Error('Correction Save fixture invalid; NOT the expected product RED: '+JSON.stringify(corrected));
1090
+ // A correction must never rewrite or destroy the original posted source/journal.
1091
+ expect(remotePurchases(server).find(p=>p.id===id)).toEqual(originalSourceFingerprint);
1092
+ expect([...server.records.values()].find(r=>r.collection==='journalVouchers'&&
1093
+ r.id===original.journalVoucherId)).toEqual(originalVoucherFingerprint);
1094
+ expect([...server.records.values()].filter(r=>r.collection==='journalLines'&&
1095
+ r.data.voucherId===original.journalVoucherId)).toEqual(originalLines);
1096
+ const vouchers=[...server.records.values()].filter(r=>r.collection==='journalVouchers');
1097
+ const linked=vouchers.find(r=>r.id===corrected.journalVoucherId);
1098
+ const lines=[...server.records.values()].filter(r=>r.collection==='journalLines'&&
1099
+ r.data.voucherId===corrected.journalVoucherId);
1100
+ const debit=lines.reduce((sum,r)=>sum+Number(r.data.debit||0),0);
1101
+ const credit=lines.reduce((sum,r)=>sum+Number(r.data.credit||0),0);
1102
+ const observed={
1103
+ documentStatus:corrected.documentStatus,
1104
+ accountingStatus:corrected.accountingStatus,
1105
+ hasSourceLinkedJournalDraft:!!linked&&linked.data.status==='draft',
1106
+ journalPointsToCorrection:!!linked&&linked.data.sourceType==='purchase'&&
1107
+ linked.data.sourceId===corrected.id,
1108
+ balancedDraftLines:lines.length>=2&&debit===credit&&debit===300,
1109
+ correctionAlreadyPosted:vouchers.some(r=>r.data.status==='posted'&&
1110
+ r.data.sourceType==='purchase'&&r.data.sourceId===corrected.id),
1111
+ originalReversalCount:server.reverseCount
1112
+ };
1113
+ // CURRENT: purchaseSaveSeamless directly calls finalizePurchasePilotDraft(newId)
1114
+ // on Save, bypassing independent accounting approval for the correction.
1115
+ test.fail(true,'#229 / ADR216: correcting posted purchase Save auto-finalizes the new source');
1116
+ expect(observed).toEqual({
1117
+ documentStatus:'draft',accountingStatus:'draft',
1118
+ hasSourceLinkedJournalDraft:true,journalPointsToCorrection:true,
1119
+ balancedDraftLines:true,correctionAlreadyPosted:false,originalReversalCount:1
1120
+ });
1121
+});